aboutsummaryrefslogtreecommitdiff
path: root/fileop
diff options
context:
space:
mode:
Diffstat (limited to 'fileop')
-rw-r--r--fileop/fileop.go46
-rw-r--r--fileop/fileop_test.go84
2 files changed, 55 insertions, 75 deletions
diff --git a/fileop/fileop.go b/fileop/fileop.go
index d08cb82..1be3f2e 100644
--- a/fileop/fileop.go
+++ b/fileop/fileop.go
@@ -8,7 +8,15 @@ import (
"path/filepath"
)
-func WalkFolder(folder string) <-chan string {
+type WalkMode int
+
+const (
+ Folder WalkMode = iota
+ File
+ FileFolder
+)
+
+func WalkFolder(folder string, walkMode WalkMode) <-chan string {
c := make(chan string)
go func(folder string, c chan string) {
@@ -16,7 +24,17 @@ func WalkFolder(folder string) <-chan string {
file, _ := os.Open(path)
defer file.Close()
fileInfo, _ := file.Stat()
- if !fileInfo.IsDir() {
+
+ switch walkMode {
+ case Folder:
+ if fileInfo.IsDir() {
+ c <- path
+ }
+ case File:
+ if !fileInfo.IsDir() {
+ c <- path
+ }
+ case FileFolder:
c <- path
}
return nil
@@ -28,15 +46,9 @@ func WalkFolder(folder string) <-chan string {
return c
}
-func CalculateSHA256(file string) (string, error) {
- f, err := os.Open(file)
- if err != nil {
- return "", err
- }
- defer f.Close()
-
+func CalculateSHA256(r io.Reader) (string, error) {
h := sha256.New()
- if _, err := io.Copy(h, f); err != nil {
+ if _, err := io.Copy(h, r); err != nil {
return "", err
}
@@ -64,3 +76,17 @@ func Move() chan<- *MoveCommand {
return c
}
+
+func IsEmpty(name string) (bool, error) {
+ f, err := os.Open(name)
+ if err != nil {
+ return false, err
+ }
+ defer f.Close()
+
+ _, err = f.Readdirnames(1)
+ if err == io.EOF {
+ return true, nil
+ }
+ return false, err
+}
diff --git a/fileop/fileop_test.go b/fileop/fileop_test.go
index f2ab864..134090d 100644
--- a/fileop/fileop_test.go
+++ b/fileop/fileop_test.go
@@ -2,57 +2,14 @@ package fileop
import (
"fmt"
- "math/rand"
"os"
+ "porg/testutil"
"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))
+ src := testutil.CreateTmpFile()
+ dest := fmt.Sprintf("/tmp/%s", testutil.RandomString(10))
c := Move()
@@ -65,33 +22,30 @@ func TestMove(t *testing.T) {
}
-func TestWalk(t *testing.T) {
- fileCount := 1000
- folder := createFolder()
- files := map[string]struct{}{}
- walkedFiles := map[string]struct{}{}
+func TestIsEmpty(t *testing.T) {
+ folder := testutil.CreateFolder()
- for i := 0; i < fileCount; i++ {
- files[appendEmptyFile(folder)] = struct{}{}
- }
+ empty, err := IsEmpty(folder)
- c := WalkFolder(folder)
- for file := range c {
- walkedFiles[file] = struct{}{}
+ if err != nil {
+ t.Fatalf("Error reading the folder %s", err.Error())
}
- for k := range files {
- _, ok := walkedFiles[k]
- if !ok {
- t.Errorf("File %s was not walked", k)
- }
+ if !empty {
+ t.Errorf("Folder is not empty %s", folder)
}
+
}
func TestCalculateSHA256(t *testing.T) {
- sh256, _ := CalculateSHA256("test_file.txt")
- if sh256 != "027cc886f9b8c866f932ef8b8da9a32f0857ef8e16ec98dd2797021b34623b88" {
- t.Errorf("Wrong sh256 hash, given %s", sh256)
+ if f, err := os.Open("test_file.txt"); err != nil {
+ t.Error(err)
+ } else {
+ sh256, _ := CalculateSHA256(f)
+ if sh256 != "027cc886f9b8c866f932ef8b8da9a32f0857ef8e16ec98dd2797021b34623b88" {
+ t.Errorf("Wrong sh256 hash, given %s", sh256)
+ }
}
+
}