aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--repo.go24
-rw-r--r--repo_test.go18
3 files changed, 19 insertions, 24 deletions
diff --git a/.gitignore b/.gitignore
index 41d0470..3a0ec3b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
## Test generated
test/result
+test/repo
diff --git a/repo.go b/repo.go
index 22c323f..9cde6fa 100644
--- a/repo.go
+++ b/repo.go
@@ -36,9 +36,7 @@ import (
"strconv"
)
-const (
- chunkSize = 8 << 10
-)
+var chunkSize = 8 << 10
type File struct {
Path string
@@ -52,13 +50,13 @@ func Commit(source string, repo string) {
newChunkPath := path.Join(newPath, "chunks")
os.Mkdir(newPath, 0775)
os.Mkdir(newChunkPath, 0775)
- files := make(chan File)
- newChunks := make(chan []byte)
- oldChunks := make(chan []byte)
+ newChunks := make(chan []byte, 16)
+ oldChunks := make(chan []byte, 16)
+ files := ListFiles(source)
go LoadChunks(repo, oldChunks)
- go ListFiles(source, files)
go ReadFiles(files, newChunks)
StoreChunks(newChunkPath, newChunks)
+ fmt.Println(files)
}
func GetLastVersion(repo string) int {
@@ -83,7 +81,8 @@ func GetLastVersion(repo string) int {
return v
}
-func ListFiles(path string, files chan<- File) {
+func ListFiles(path string) []File {
+ var files []File
err := filepath.Walk(path,
func(p string, i fs.FileInfo, err error) error {
if err != nil {
@@ -93,21 +92,20 @@ func ListFiles(path string, files chan<- File) {
if i.IsDir() {
return nil
}
- var file = File{p, i.Size()}
- files <- file
+ files = append(files, File{p, i.Size()})
return nil
})
if err != nil {
log.Println(err)
}
- close(files)
+ return files
}
-func ReadFiles(files <-chan File, chunks chan<- []byte) {
+func ReadFiles(files []File, chunks chan<- []byte) {
var buff []byte
var prev, read = chunkSize, 0
- for f := range files {
+ for _, f := range files {
file, err := os.Open(f.Path)
if err != nil {
log.Println(err)
diff --git a/repo_test.go b/repo_test.go
index 956bd84..7c6316e 100644
--- a/repo_test.go
+++ b/repo_test.go
@@ -15,9 +15,8 @@ func prepareResult() {
func chunkCompare(t *testing.T, dataDir string, testFiles []string, chunkCount int) {
- files := make(chan File)
chunks := make(chan []byte)
- go ListFiles(dataDir, files)
+ files := ListFiles(dataDir)
go ReadFiles(files, chunks)
offset := 0
@@ -81,15 +80,12 @@ func TestLoadChunks(t *testing.T) {
prepareResult()
dataDir := path.Join("test", "data")
resultDir := path.Join("test", "result")
- files1 := make(chan File)
- files2 := make(chan File)
- chunks1 := make(chan []byte)
- chunks2 := make(chan []byte)
- chunks3 := make(chan []byte)
- go ListFiles(dataDir, files1)
- go ListFiles(dataDir, files2)
- go ReadFiles(files1, chunks1)
- go ReadFiles(files2, chunks2)
+ chunks1 := make(chan []byte, 16)
+ chunks2 := make(chan []byte, 16)
+ chunks3 := make(chan []byte, 16)
+ files := ListFiles(dataDir)
+ go ReadFiles(files, chunks1)
+ go ReadFiles(files, chunks2)
StoreChunks(resultDir, chunks1)
go LoadChunks(resultDir, chunks3)