aboutsummaryrefslogtreecommitdiff
path: root/storage
diff options
context:
space:
mode:
Diffstat (limited to 'storage')
-rw-r--r--storage/storage.go30
-rw-r--r--storage/storage_fs.go40
-rw-r--r--storage/storage_fs_test.go29
3 files changed, 99 insertions, 0 deletions
diff --git a/storage/storage.go b/storage/storage.go
new file mode 100644
index 0000000..b788efb
--- /dev/null
+++ b/storage/storage.go
@@ -0,0 +1,30 @@
+package storage
+
+import (
+ "crypto/sha256"
+ "fmt"
+ "io"
+)
+
+type WalkMode int
+
+const (
+ Folder WalkMode = iota
+ File
+ FileFolder
+)
+
+type Storage interface {
+ Walk(path string, walkMode WalkMode) <-chan string
+ Get(path string) (io.Reader, error)
+}
+
+func CalculateSHA256(r io.Reader) (string, error) {
+ h := sha256.New()
+ if _, err := io.Copy(h, r); err != nil {
+ return "", err
+ }
+
+ return fmt.Sprintf("%x", h.Sum(nil)), nil
+
+}
diff --git a/storage/storage_fs.go b/storage/storage_fs.go
new file mode 100644
index 0000000..35ce58b
--- /dev/null
+++ b/storage/storage_fs.go
@@ -0,0 +1,40 @@
+package storage
+
+import (
+ "os"
+ "path/filepath"
+)
+
+type FileSystem struct {
+ root string
+}
+
+func WalkFolder(folder string, walkMode WalkMode) <-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()
+
+ switch walkMode {
+ case Folder:
+ if fileInfo.IsDir() {
+ c <- path
+ }
+ case File:
+ if !fileInfo.IsDir() {
+ c <- path
+ }
+ case FileFolder:
+ c <- path
+ }
+ return nil
+ })
+ close(c)
+
+ }(folder, c)
+
+ return c
+}
diff --git a/storage/storage_fs_test.go b/storage/storage_fs_test.go
new file mode 100644
index 0000000..b746c7e
--- /dev/null
+++ b/storage/storage_fs_test.go
@@ -0,0 +1,29 @@
+package storage
+
+import (
+ "porg/testutil"
+ "testing"
+)
+
+func TestWalk(t *testing.T) {
+ fileCount := 1000
+ folder := testutil.CreateFolder()
+ files := map[string]struct{}{}
+ walkedFiles := map[string]struct{}{}
+
+ for i := 0; i < fileCount; i++ {
+ files[testutil.AppendEmptyFile(folder)] = struct{}{}
+ }
+
+ c := WalkFolder(folder, File)
+ for file := range c {
+ walkedFiles[file] = struct{}{}
+ }
+
+ for k := range files {
+ _, ok := walkedFiles[k]
+ if !ok {
+ t.Errorf("File %s was not walked", k)
+ }
+ }
+}