Skip to content

Replace deprecated ioutil #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions duplicacy/duplicacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (

"github.com/gilbertchen/cli"

"io/ioutil"

"github.com/gilbertchen/duplicacy/src"
duplicacy "github.com/gilbertchen/duplicacy/src"
)

const (
Expand Down Expand Up @@ -316,7 +314,7 @@ func configRepository(context *cli.Context, init bool) {
// write real path into .duplicacy file inside repository
duplicacyFileName := path.Join(repository, duplicacy.DUPLICACY_FILE)
d1 := []byte(preferencePath)
err = ioutil.WriteFile(duplicacyFileName, d1, 0644)
err = os.WriteFile(duplicacyFileName, d1, 0644)
if err != nil {
duplicacy.LOG_ERROR("REPOSITORY_PATH", "Failed to write %s file inside repository %v", duplicacyFileName, err)
return
Expand Down Expand Up @@ -705,7 +703,7 @@ func changePassword(context *cli.Context) {
}

configPath := path.Join(duplicacy.GetDuplicacyPreferencePath(), "config")
err = ioutil.WriteFile(configPath, description, 0600)
err = os.WriteFile(configPath, description, 0600)
if err != nil {
duplicacy.LOG_ERROR("CONFIG_SAVE", "Failed to save the old config to %s: %v", configPath, err)
return
Expand Down Expand Up @@ -1043,7 +1041,6 @@ func printFile(context *cli.Context) {
snapshotID = context.String("id")
}


backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, "", "", false)
duplicacy.SavePassword(*preference, "password", password)

Expand Down Expand Up @@ -1290,7 +1287,7 @@ func copySnapshots(context *cli.Context) {
destinationStorage.SetRateLimits(0, context.Int("upload-limit-rate"))

destinationManager := duplicacy.CreateBackupManager(destination.SnapshotID, destinationStorage, repository,
destinationPassword, "", "", false)
destinationPassword, "", "", false)
duplicacy.SavePassword(*destination, "password", destinationPassword)
destinationManager.SetupSnapshotCache(destination.Name)

Expand Down Expand Up @@ -1415,7 +1412,7 @@ func benchmark(context *cli.Context) {
if storage == nil {
return
}
duplicacy.Benchmark(repository, storage, int64(fileSize) * 1024 * 1024, chunkSize * 1024 * 1024, chunkCount, uploadThreads, downloadThreads)
duplicacy.Benchmark(repository, storage, int64(fileSize)*1024*1024, chunkSize*1024*1024, chunkCount, uploadThreads, downloadThreads)
}

func main() {
Expand Down Expand Up @@ -1454,8 +1451,8 @@ func main() {
Argument: "<level>",
},
cli.BoolFlag{
Name: "zstd",
Usage: "short for -zstd default",
Name: "zstd",
Usage: "short for -zstd default",
},
cli.IntFlag{
Name: "iterations",
Expand Down Expand Up @@ -1530,8 +1527,8 @@ func main() {
Argument: "<level>",
},
cli.BoolFlag{
Name: "zstd",
Usage: "short for -zstd default",
Name: "zstd",
Usage: "short for -zstd default",
},
cli.BoolFlag{
Name: "vss",
Expand Down Expand Up @@ -1564,7 +1561,6 @@ func main() {
Usage: "the maximum number of entries kept in memory (defaults to 1M)",
Argument: "<number>",
},

},
Usage: "Save a snapshot of the repository to the storage",
ArgsUsage: " ",
Expand Down Expand Up @@ -1624,7 +1620,7 @@ func main() {
cli.BoolFlag{
Name: "persist",
Usage: "continue processing despite chunk errors or existing files (without -overwrite), reporting any affected files",
},
},
cli.StringFlag{
Name: "key-passphrase",
Usage: "the passphrase to decrypt the RSA private key",
Expand Down Expand Up @@ -1982,8 +1978,8 @@ func main() {
Argument: "<level>",
},
cli.BoolFlag{
Name: "zstd",
Usage: "short for -zstd default",
Name: "zstd",
Usage: "short for -zstd default",
},
cli.IntFlag{
Name: "iterations",
Expand Down Expand Up @@ -2248,8 +2244,8 @@ func main() {
Usage: "add a comment to identify the process",
},
cli.StringSliceFlag{
Name: "suppress, s",
Usage: "suppress logs with the specified id",
Name: "suppress, s",
Usage: "suppress logs with the specified id",
Argument: "<id>",
},
cli.BoolFlag{
Expand Down
6 changes: 3 additions & 3 deletions src/duplicacy_acdclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"mime/multipart"
"net/http"
"os"
"sync"
"time"

Expand Down Expand Up @@ -45,7 +45,7 @@ type ACDClient struct {

func NewACDClient(tokenFile string) (*ACDClient, error) {

description, err := ioutil.ReadFile(tokenFile)
description, err := os.ReadFile(tokenFile)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func (client *ACDClient) RefreshToken() (err error) {
return err
}

err = ioutil.WriteFile(client.TokenFile, description, 0644)
err = os.WriteFile(client.TokenFile, description, 0644)
if err != nil {
return err
}
Expand Down
62 changes: 30 additions & 32 deletions src/duplicacy_b2client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
package duplicacy

import (
"io"
"os"
"fmt"
"bytes"
"time"
"sync"
"strconv"
"strings"
"net/url"
"net/http"
"math/rand"
"io/ioutil"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/base64"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
)

type B2Error struct {
Expand All @@ -41,27 +40,27 @@ type B2UploadArgument struct {
var B2AuthorizationURL = "https://api.backblazeb2.com/b2api/v1/b2_authorize_account"

type B2Client struct {
HTTPClient *http.Client
HTTPClient *http.Client

AccountID string
ApplicationKeyID string
ApplicationKey string
BucketName string
BucketID string
StorageDir string
AccountID string
ApplicationKeyID string
ApplicationKey string
BucketName string
BucketID string
StorageDir string

Lock sync.Mutex
AuthorizationToken string
APIURL string
DownloadURL string
IsAuthorized bool

UploadURLs []string
UploadTokens []string
UploadURLs []string
UploadTokens []string

Threads int
MaximumRetries int
TestMode bool
Threads int
MaximumRetries int
TestMode bool

LastAuthorizationTime int64
}
Expand All @@ -81,7 +80,7 @@ func NewB2Client(applicationKeyID string, applicationKey string, downloadURL str
storageDir = storageDir[1:]
}

if storageDir != "" && storageDir[len(storageDir) - 1] != '/' {
if storageDir != "" && storageDir[len(storageDir)-1] != '/' {
storageDir += "/"
}

Expand Down Expand Up @@ -128,7 +127,7 @@ func (client *B2Client) retry(retries int, response *http.Response) int {
}
}

if retries >= client.MaximumRetries + 1 {
if retries >= client.MaximumRetries+1 {
return 0
}
retries++
Expand All @@ -143,7 +142,7 @@ func (client *B2Client) retry(retries int, response *http.Response) int {
}

func (client *B2Client) call(threadIndex int, requestURL string, method string, requestHeaders map[string]string, input interface{}) (
io.ReadCloser, http.Header, int64, error) {
io.ReadCloser, http.Header, int64, error) {

var response *http.Response

Expand Down Expand Up @@ -171,7 +170,6 @@ func (client *B2Client) call(threadIndex int, requestURL string, method string,
inputReader = rateLimitedReader
}


if isUpload {
if client.UploadURLs[threadIndex] == "" || client.UploadTokens[threadIndex] == "" {
err := client.getUploadURL(threadIndex)
Expand Down Expand Up @@ -303,7 +301,7 @@ func (client *B2Client) AuthorizeAccount(threadIndex int) (err error, allowed bo
defer client.Lock.Unlock()

// Don't authorize if the previous one was done less than 30 seconds ago
if client.LastAuthorizationTime != 0 && client.LastAuthorizationTime > time.Now().Unix() - 30 {
if client.LastAuthorizationTime != 0 && client.LastAuthorizationTime > time.Now().Unix()-30 {
return nil, false
}

Expand Down Expand Up @@ -426,7 +424,7 @@ func (client *B2Client) ListFileNames(threadIndex int, startFileName string, sin
apiURL = client.getAPIURL() + "/b2api/v1/b2_list_file_versions"
} else if singleFile {
// handle a single file with no versions as a special case to download the last byte of the file
apiURL = client.getDownloadURL() + "/file/" + client.BucketName + "/" + B2Escape(client.StorageDir + startFileName)
apiURL = client.getDownloadURL() + "/file/" + client.BucketName + "/" + B2Escape(client.StorageDir+startFileName)
// requesting byte -1 works for empty files where 0-0 fails with a 416 error
requestHeaders["Range"] = "bytes=-1"
// HEAD request
Expand Down Expand Up @@ -500,7 +498,7 @@ func (client *B2Client) ListFileNames(threadIndex int, startFileName string, sin
return nil, err
}

ioutil.ReadAll(readCloser)
io.ReadAll(readCloser)

for _, file := range output.Files {
file.FileName = file.FileName[len(client.StorageDir):]
Expand Down Expand Up @@ -585,7 +583,7 @@ func (client *B2Client) HideFile(threadIndex int, fileName string) (fileID strin
func (client *B2Client) DownloadFile(threadIndex int, filePath string) (io.ReadCloser, int64, error) {

if !strings.HasSuffix(filePath, ".fsl") {
url := client.getDownloadURL() + "/file/" + client.BucketName + "/" + B2Escape(client.StorageDir + filePath)
url := client.getDownloadURL() + "/file/" + client.BucketName + "/" + B2Escape(client.StorageDir+filePath)

readCloser, _, len, err := client.call(threadIndex, url, http.MethodGet, make(map[string]string), 0)
return readCloser, len, err
Expand Down
3 changes: 1 addition & 2 deletions src/duplicacy_benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -113,7 +112,7 @@ func Benchmark(localDirectory string, storage Storage, fileSize int64, chunkSize

startTime := float64(time.Now().UnixNano()) / 1e9
LOG_INFO("BENCHMARK_WRITE", "Writing random data to local disk")
err = ioutil.WriteFile(filename, data, 0600)
err = io.WriteFile(filename, data, 0600)
if err != nil {
LOG_ERROR("BENCHMARK_WRITE", "Failed to write the random data: %v", err)
return false
Expand Down
Loading