From 73c0db612a117d9b30ffb3f51e3c5e09a6d709d1 Mon Sep 17 00:00:00 2001 From: Lenni vH Date: Sat, 27 Mar 2021 12:59:38 +0100 Subject: [PATCH] magic numbers, cuddle --- .golangci.yml | 2 +- copy.go | 39 +++++++++++++++++++++++++++++++-------- flags.go | 4 ++-- tui.go | 27 +++++++++++++-------------- ui.go | 13 ++++++------- util.go | 2 ++ 6 files changed, 55 insertions(+), 32 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 545a8bd..426479f 100755 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,7 +26,7 @@ linters-settings: settings: mnd: checks: argument, case, condition, operation, return, assign - ignored-numbers: 0,1,2,10,16,100 + ignored-numbers: 0,1,2,10,16,100,1000 lll: line-length: 120 misspell: diff --git a/copy.go b/copy.go index 393c981..48c28c8 100644 --- a/copy.go +++ b/copy.go @@ -9,9 +9,9 @@ import ( "strconv" ) -var BUFFERSIZE uint = 32768 +var buffersize uint = 32768 -var buf []byte = make([]byte, BUFFERSIZE) +var buf []byte = make([]byte, buffersize) /** * Loops checking / waiting for any left work. @@ -21,27 +21,37 @@ func copyLoop() { for !done { filesLock.Lock() if len(folders) > 0 { - var localFolders []string = folders + + // Create all folders: + localFolders := folders folders = nil + filesLock.Unlock() createFolders(localFolders) } else if len(pendingConflicts) > 0 { + + // Work resolved conflicts: var id int = pendingConflicts[0] pendingConflicts = pendingConflicts[1:] var sourcePath string = fileOrder[id] destPath := filepath.Join(targets[sourcePath], filepath.Base(sourcePath)) + filesLock.Unlock() copyFilePath(sourcePath, destPath) } else if i < len(fileOrder) { + + // Copy normal files: var sourcePath string = fileOrder[i] var destPath string + if createFoldersInTarget { destPath = filepath.Join(targets[sourcePath], filepath.Base(sourcePath)) } else { destPath = targets[sourcePath] } + filesLock.Unlock() // check if file already exists and we even care about that: @@ -73,6 +83,7 @@ func copyLoop() { filesLock.RLock() if len(folders) == 0 && len(fileOrder) == i && len(piledConflicts) == 0 && len(pendingConflicts) == 0 { + // 1. all folders have been created // 2. we've tried to copy all files so far // 3. all conflicts we had to ask the user are resolved @@ -81,6 +92,7 @@ func copyLoop() { syncdel(&fileOrder) syncdel(&sources) } + done = true } filesLock.RUnlock() @@ -109,9 +121,11 @@ func copyFilePath(sourcePath string, destPath string) { } } } - var stat os.FileInfo - stat, err = os.Lstat(sourcePath) - if stat.Mode().IsRegular() { + + stat, err := os.Lstat(sourcePath) + if err != nil { + errCopying(sourcePath, destPath, err) + } else if stat.Mode().IsRegular() { currentTaskType = 1 currentFile = sourcePath var source, dest *os.File @@ -166,17 +180,21 @@ func copyFilePath(sourcePath string, destPath string) { */ func createFolders(folders []string) { var folder string + for _, folder = range folders { currentTaskType = 3 currentFile = folder + if progressLSColors { currentFile = "\033[" + lscolors.FormatType("di") + "m" + currentFile + "\033[" + lscolors.FormatType("rs") + "m" } + var err error = os.MkdirAll(folder, 0o755) if err != nil { errCreatingFile(err, folder) } + doneSize += uint64(folderSize) } } @@ -184,26 +202,31 @@ func createFolders(folders []string) { func copyFile(source *os.File, dest *os.File, progressStorage *uint64) error { var readAmount, writtenAmount int var err error + for { readAmount, err = source.Read(buf) - if err != nil && err != io.EOF { + + if err != nil && !errors.Is(err, io.EOF) { errCopying(source.Name(), dest.Name(), err) } if readAmount == 0 { // when the file is fully read break } + writtenAmount, err = dest.Write(buf[:readAmount]) if err != nil { return err } + if readAmount != writtenAmount { return errors.New("couldn't write all the data: " + strconv.Itoa(readAmount) + " read, " + strconv.Itoa(writtenAmount) + "written") } + *progressStorage += uint64(writtenAmount) } - // verbCopyFinished(source.Name(), dest.Name()); + return nil } diff --git a/flags.go b/flags.go index cc41c08..32699e7 100644 --- a/flags.go +++ b/flags.go @@ -104,8 +104,8 @@ func parseKeyValue(key string, value string) { var err error val, err = strconv.Atoi(value) if err == nil { - BUFFERSIZE = uint(val) - buf = make([]byte, BUFFERSIZE) + buffersize = uint(val) + buf = make([]byte, buffersize) } default: warnBadConfigKey(key) diff --git a/tui.go b/tui.go index b36f748..dca5dff 100644 --- a/tui.go +++ b/tui.go @@ -70,8 +70,7 @@ func drawLoop() { fmt.Print(" ") } fmt.Print("] ") - var unit int - unit = sizeAutoUnit(float64(fullSize)) + unit := sizeAutoUnit(float64(fullSize)) fmt.Print(formatSize(float64(doneSize), unit)) fmt.Print(" / ") fmt.Print(formatSize(float64(fullSize), unit)) @@ -95,7 +94,7 @@ func drawLoop() { // remaining time: fmt.Print(", ") - var secondsLeft float32 = float32(fullSize-doneSize) / sizePerSecond + secondsLeft := float32(fullSize-doneSize) / sizePerSecond fmt.Print(formatSeconds(float64(secondsLeft))) fmt.Println(" remaining") lines++ @@ -123,7 +122,8 @@ func drawLoop() { fmt.Print("[I]nfo | [R]ename target | [Q]uit") fmt.Println(color.Text.Reset) lines++ - var in rune = getChoice("soavidreq") + in := getChoice("soavidreq") + switch in { case 's': filesLock.Lock() @@ -173,31 +173,30 @@ func skipFile(path string) { doneSize += uint64(symlinkSize) } - doneAmount += 1 + doneAmount++ } func printSummary() { - var elapsed time.Duration - elapsed = time.Now().Sub(start) - var i int - for i = 0; i < lines; i++ { + elapsed := time.Since(start) + for i := 0; i < lines; i++ { fmt.Print("\033[1A\033[2K") } if verbose > VerbQuiet { fmt.Print(" [") - for i = 0; i < barWidth; i++ { + + for i := 0; i < barWidth; i++ { fmt.Print("=") } + fmt.Println("]") fmt.Print(" Copied " + strconv.FormatUint(fullAmount, 9)) fmt.Print(" files in ") fmt.Print(formatSeconds(elapsed.Seconds())) fmt.Print(" (") - var full_speed float64 - full_speed = float64(fullSize) / float64(elapsed.Seconds()) - fmt.Print(formatSize(float64(full_speed), - sizeAutoUnit(float64(full_speed)))) + fullSpeed := float64(fullSize) / float64(elapsed.Seconds()) + fmt.Print(formatSize(float64(fullSpeed), + sizeAutoUnit(float64(fullSpeed)))) fmt.Print("/s).") fmt.Println() } diff --git a/ui.go b/ui.go index 328e960..2f4fef0 100644 --- a/ui.go +++ b/ui.go @@ -1,6 +1,7 @@ package main import ( + "errors" "easy-copy/color" "fmt" "os" @@ -80,7 +81,7 @@ func printVersion() { func printCopying() { _, err := runPager(infoCopying()) - if err == errNoPager { + if errors.Is(err, errNoPager) { fmt.Println(infoCopying()) } else if err != nil { fmt.Println(err) @@ -90,7 +91,7 @@ func printCopying() { func printWarranty() { _, err := runPager(infoWarranty()) - if err == errNoPager { + if errors.Is(err, errNoPager) { fmt.Println(infoCopying()) } else if err != nil { fmt.Println(err) @@ -145,13 +146,11 @@ func verbTargets() { fmt.Println("-------------------------") fmt.Println("these tasks will be done:") filesLock.RLock() - var v string - for _, v = range folders { + for _, v := range folders { fmt.Println("need to create folder", v) } - for _, v = range fileOrder { - var target string = targets[v] - fmt.Println(v, "will be copied to", target+"/") + for _, v := range fileOrder { + fmt.Println(v, "will be copied to", targets[v]+"/") } filesLock.RUnlock() fmt.Println("-------------------------") diff --git a/util.go b/util.go index cd3a717..819c94e 100644 --- a/util.go +++ b/util.go @@ -65,11 +65,13 @@ func runPager(text string) (bool, error) { writer.Flush() out.Close() cmd.Wait() + return true, nil } func getChoice(choices string) rune { var in rune + for { in = unicode.ToLower(input.Getch()) if strings.ContainsRune(choices, in) {