diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-08-28 17:03:38 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-08-28 17:07:40 +0200 |
commit | 544bbeeaf836436305cbed87ae1019511de62535 (patch) | |
tree | 8c7b020bba75d4d850d4a1d538e7bc7db2f01c33 /fileop | |
download | porg-544bbeeaf836436305cbed87ae1019511de62535.tar.gz porg-544bbeeaf836436305cbed87ae1019511de62535.tar.bz2 porg-544bbeeaf836436305cbed87ae1019511de62535.zip |
feat: Add init source code
For now only `pipe` is feature complete. The order module needs to be
changed a lot.
Diffstat (limited to 'fileop')
-rw-r--r-- | fileop/fileop.go | 66 | ||||
-rw-r--r-- | fileop/fileop_test.go | 97 | ||||
-rw-r--r-- | fileop/test_file.txt | 8 |
3 files changed, 171 insertions, 0 deletions
diff --git a/fileop/fileop.go b/fileop/fileop.go new file mode 100644 index 0000000..d08cb82 --- /dev/null +++ b/fileop/fileop.go @@ -0,0 +1,66 @@ +package fileop + +import ( + "crypto/sha256" + "fmt" + "io" + "os" + "path/filepath" +) + +func WalkFolder(folder string) <-chan string { + c := make(chan string) + + go func(folder string, c chan string) { + filepath.Walk(folder, func(path string, info os.FileInfo, err error) error { + file, _ := os.Open(path) + defer file.Close() + fileInfo, _ := file.Stat() + if !fileInfo.IsDir() { + c <- path + } + return nil + }) + close(c) + + }(folder, c) + + return c +} + +func CalculateSHA256(file string) (string, error) { + f, err := os.Open(file) + if err != nil { + return "", err + } + defer f.Close() + + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + return "", err + } + + shaString := fmt.Sprintf("%x", h.Sum(nil)) + + return shaString, nil +} + +type MoveCommand struct { + Source string + Destination string +} + +func Move() chan<- *MoveCommand { + c := make(chan *MoveCommand) + go func(chan *MoveCommand) { + for cmd := range c { + // TODO: add error handling + err := os.Rename(cmd.Source, cmd.Destination) + if err != nil { + fmt.Println(err.Error()) + } + } + }(c) + + return c +} diff --git a/fileop/fileop_test.go b/fileop/fileop_test.go new file mode 100644 index 0000000..f2ab864 --- /dev/null +++ b/fileop/fileop_test.go @@ -0,0 +1,97 @@ +package fileop + +import ( + "fmt" + "math/rand" + "os" + "testing" + "time" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func RandomString(n int) string { + var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + s := make([]rune, n) + for i := range s { + s[i] = letters[rand.Intn(len(letters))] + } + return string(s) +} + +func createFolder() (tmp string) { + tmp = fmt.Sprintf("/tmp/%s", RandomString(10)) + + err := os.Mkdir(tmp, 0755) + + if err != nil { + fmt.Println(err.Error()) + } + + return +} + +func appendEmptyFile(path string) (fullPath string) { + fullPath = fmt.Sprintf("%s/%s", path, RandomString(10)) + os.OpenFile(fullPath, os.O_RDONLY|os.O_CREATE, 0666) + + return +} + +func createEmptyFile(path string) { + os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0666) +} + +func createTmpFile() (fullPath string) { + path := createFolder() + fullPath = appendEmptyFile(path) + return +} + +func TestMove(t *testing.T) { + src := createTmpFile() + dest := fmt.Sprintf("/tmp/%s", RandomString(10)) + + c := Move() + + c <- &MoveCommand{Source: src, Destination: dest} + close(c) + + if _, err := os.Stat(dest); err != nil { + t.Errorf("File %s not moved to %s", src, dest) + } + +} + +func TestWalk(t *testing.T) { + fileCount := 1000 + folder := createFolder() + files := map[string]struct{}{} + walkedFiles := map[string]struct{}{} + + for i := 0; i < fileCount; i++ { + files[appendEmptyFile(folder)] = struct{}{} + } + + c := WalkFolder(folder) + for file := range c { + walkedFiles[file] = struct{}{} + } + + for k := range files { + _, ok := walkedFiles[k] + if !ok { + t.Errorf("File %s was not walked", k) + } + } +} + +func TestCalculateSHA256(t *testing.T) { + sh256, _ := CalculateSHA256("test_file.txt") + + if sh256 != "027cc886f9b8c866f932ef8b8da9a32f0857ef8e16ec98dd2797021b34623b88" { + t.Errorf("Wrong sh256 hash, given %s", sh256) + } +} diff --git a/fileop/test_file.txt b/fileop/test_file.txt new file mode 100644 index 0000000..db88e33 --- /dev/null +++ b/fileop/test_file.txt @@ -0,0 +1,8 @@ +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod +tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At +vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd +gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum +dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor +invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero +eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no +sea takimata sanctus est Lorem ipsum dolor sit amet. |