Skip to content

Commit

Permalink
magic numbers, cuddle
Browse files Browse the repository at this point in the history
  • Loading branch information
scrouthtv committed Mar 27, 2021
1 parent b77e9b0 commit 73c0db6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 31 additions & 8 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -81,6 +92,7 @@ func copyLoop() {
syncdel(&fileOrder)
syncdel(&sources)
}

done = true
}
filesLock.RUnlock()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -166,44 +180,53 @@ 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)
}
}

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
}
4 changes: 2 additions & 2 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 13 additions & 14 deletions tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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++
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
Expand Down
13 changes: 6 additions & 7 deletions ui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"easy-copy/color"
"fmt"
"os"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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("-------------------------")
Expand Down
2 changes: 2 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 73c0db6

Please sign in to comment.