From 10a896a8f8567f42abf73d2ce2b3e3dd64519f85 Mon Sep 17 00:00:00 2001 From: Andriy Massimilla Date: Wed, 11 Oct 2023 12:52:08 -0400 Subject: [PATCH] Rename .dvsmeta to .dvs, segment storage files into subdirs --- internal/meta/parse.go | 2 +- internal/storage/add.go | 4 ++-- internal/storage/get.go | 3 +-- internal/storage/get_test.go | 2 +- internal/storage/main.go | 12 ++++++++++-- internal/storage/remove.go | 3 +-- testing/10G.dvs | 7 +++++++ testing/1G.dvs | 7 +++++++ testing/266-536x354.jpg.dvs | 7 +++++++ testing/571-536x354.jpg.dvs | 7 +++++++ 10 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 testing/10G.dvs create mode 100644 testing/1G.dvs create mode 100644 testing/266-536x354.jpg.dvs create mode 100644 testing/571-536x354.jpg.dvs diff --git a/internal/meta/parse.go b/internal/meta/parse.go index 3cc8f02..b6913e0 100644 --- a/internal/meta/parse.go +++ b/internal/meta/parse.go @@ -10,7 +10,7 @@ import ( "golang.org/x/exp/slices" ) -var FileExtension = ".dvsmeta" +var FileExtension = ".dvs" // Gets a list of all meta file paths in the directory recursively func GetAllMetaFiles(dir string) (metaFiles []string, err error) { diff --git a/internal/storage/add.go b/internal/storage/add.go index 6a82f0c..a7112cf 100644 --- a/internal/storage/add.go +++ b/internal/storage/add.go @@ -8,7 +8,6 @@ import ( "dvs/internal/utils" "os" "os/user" - "path/filepath" "time" ) @@ -32,7 +31,8 @@ func Add(localPath string, storageDir string, gitDir string, message string, dry Path: localPath, }) - dstPath := filepath.Join(storageDir, fileHash) + FileExtension + // Get storage path + dstPath := getStoragePath(storageDir, fileHash) // Copy the file to the storage directory // if the destination already exists, skip copying diff --git a/internal/storage/get.go b/internal/storage/get.go index 673769d..bceb0e9 100644 --- a/internal/storage/get.go +++ b/internal/storage/get.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "os" - "path/filepath" "time" ) @@ -23,7 +22,7 @@ func Get(localPath string, storageDir string, gitDir string, dry bool) error { } // Get storage path - storagePath := filepath.Join(storageDir, metadata.FileHash) + FileExtension + storagePath := getStoragePath(storageDir, metadata.FileHash) // Check if file is already present locally _, err = os.Stat(localPath) diff --git a/internal/storage/get_test.go b/internal/storage/get_test.go index d23be72..aabad0d 100644 --- a/internal/storage/get_test.go +++ b/internal/storage/get_test.go @@ -45,7 +45,7 @@ func TestGetNoLongerInStorage(t *testing.T) { } // Remove file from storage manually - err = os.Remove(filepath.Join(tempDir, hash) + FileExtension) + err = os.Remove(getStoragePath(tempDir, hash)) if err != nil { t.Fatal(err) } diff --git a/internal/storage/main.go b/internal/storage/main.go index e8e3f67..e83e3dc 100644 --- a/internal/storage/main.go +++ b/internal/storage/main.go @@ -1,9 +1,17 @@ package storage -import "io/fs" +import ( + "io/fs" + "path/filepath" +) var ( - FileExtension = ".dvsfile" storageDirPermissions = fs.FileMode(0777) storageFilePermissions = fs.FileMode(0666) ) + +func getStoragePath(storageDir string, fileHash string) string { + firstHashSegment := fileHash[:2] + secondHashSegment := fileHash[2:] + return filepath.Join(storageDir, firstHashSegment, secondHashSegment) +} diff --git a/internal/storage/remove.go b/internal/storage/remove.go index cdae9bd..bc4b9cc 100644 --- a/internal/storage/remove.go +++ b/internal/storage/remove.go @@ -6,7 +6,6 @@ import ( "dvs/internal/log" "dvs/internal/meta" "os" - "path/filepath" ) // Remove a file from storage @@ -18,7 +17,7 @@ func Remove(path string, conf config.Config, gitDir string, dry bool) error { } // Get storage path - storagePath := filepath.Join(conf.StorageDir, metadata.FileHash) + FileExtension + storagePath := getStoragePath(conf.StorageDir, metadata.FileHash) // Remove file from storage if !dry { diff --git a/testing/10G.dvs b/testing/10G.dvs new file mode 100644 index 0000000..c0054c9 --- /dev/null +++ b/testing/10G.dvs @@ -0,0 +1,7 @@ +{ + "blake3_checksum": "28960eef7d587ab6d1627b7efe30c7a07ce2dce4871d339fdfb607cb0776e064", + "file_size_bytes": 10737418240, + "timestamp": "2023-10-11T12:49:39.475255693-04:00", + "message": "", + "saved_by": "andriygm" +} \ No newline at end of file diff --git a/testing/1G.dvs b/testing/1G.dvs new file mode 100644 index 0000000..c1f49fb --- /dev/null +++ b/testing/1G.dvs @@ -0,0 +1,7 @@ +{ + "blake3_checksum": "94b4ec39d8d42ebda685fbb5429e8ab0086e65245e750142c1eea36a26abc24d", + "file_size_bytes": 1073741824, + "timestamp": "2023-10-11T12:49:20.077594173-04:00", + "message": "", + "saved_by": "andriygm" +} \ No newline at end of file diff --git a/testing/266-536x354.jpg.dvs b/testing/266-536x354.jpg.dvs new file mode 100644 index 0000000..ca3d6bf --- /dev/null +++ b/testing/266-536x354.jpg.dvs @@ -0,0 +1,7 @@ +{ + "blake3_checksum": "8ea93e919458fe8db0c7079c775f975af6d5a654daf9dcf3651e60a5a94879d8", + "file_size_bytes": 22600, + "timestamp": "2023-10-11T12:48:34.89499103-04:00", + "message": "", + "saved_by": "andriygm" +} \ No newline at end of file diff --git a/testing/571-536x354.jpg.dvs b/testing/571-536x354.jpg.dvs new file mode 100644 index 0000000..fd73f50 --- /dev/null +++ b/testing/571-536x354.jpg.dvs @@ -0,0 +1,7 @@ +{ + "blake3_checksum": "800fe626b5b4a066de815d9486893fe549e2af1742558ed4110c6ea424425c42", + "file_size_bytes": 23091, + "timestamp": "2023-10-11T12:49:10.692239925-04:00", + "message": "", + "saved_by": "andriygm" +} \ No newline at end of file