Skip to content

Commit

Permalink
Merge branch 'main' into feat/format-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
andriygm committed Oct 11, 2023
2 parents 305a438 + 8a924b5 commit c4fd120
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 22 deletions.
18 changes: 13 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# go
# Go workspace file
go.work

# build
# Binaries
dvs

# testing
.dvs.yaml
testenv/
# Devious entry
/testing/266-536x354.jpg

# Devious entry
/testing/10G

# Devious entry
/testing/1G

# Devious entry
/testing/571-536x354.jpg
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "dvs status",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["status"]
}
]
}
6 changes: 3 additions & 3 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {
FileSize uint64 `json:"fileSize"`
FileHash string `json:"fileHash"`
Timestamp string `json:"timestamp"`
User string `json:"user"`
SavedBy string `json:"savedBy"`
Message string `json:"message"`
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {
log.ColorFile(relPath), "",
log.ColorFaint(humanize.Bytes(metadata.FileSize)), "",
log.ColorFaint(timestamp), "",
log.ColorFaint(metadata.User), "",
log.ColorFaint(metadata.SavedBy), "",
log.ColorFaint(metadata.Message),
)
jsonLogger.Files = append(jsonLogger.Files, jsonFileResult{
Expand All @@ -142,7 +142,7 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {
FileSize: metadata.FileSize,
FileHash: metadata.FileHash,
Timestamp: timestamp,
User: metadata.User,
SavedBy: metadata.SavedBy,
Message: metadata.Message,
})
}
Expand Down
1 change: 1 addition & 0 deletions dvs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
storage-dir: /data/dvs/devious
2 changes: 1 addition & 1 deletion internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Config struct {
StorageDir string `yaml:"storage-dir"`
}

var ConfigFileName = ".dvs.yaml"
var ConfigFileName = "dvs.yaml"

func Read(rootDir string) (Config, error) {
// Read the config file
Expand Down
2 changes: 1 addition & 1 deletion internal/meta/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Metadata struct {
FileSize uint64 `json:"file_size_bytes"`
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
User string `json:"user"`
SavedBy string `json:"saved_by"`
}

// Creates a metadata file
Expand Down
2 changes: 1 addition & 1 deletion internal/meta/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions internal/storage/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"dvs/internal/utils"
"os"
"os/user"
"path/filepath"
"time"
)

Expand All @@ -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
Expand Down Expand Up @@ -87,7 +87,7 @@ func Add(localPath string, storageDir string, gitDir string, message string, dry
FileHash: fileHash,
FileSize: fileSize,
Timestamp: time.Now(),
User: userName,
SavedBy: userName,
Message: message,
}
err = meta.Save(metadata, localPath, dry)
Expand Down
3 changes: 1 addition & 2 deletions internal/storage/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
)

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestGetAgainAfterLocalMod(t *testing.T) {
}

// Wait a bit to ensure modification time is different
time.Sleep(time.Millisecond)
time.Sleep(2 * time.Millisecond)

// Modify the file locally
err = os.WriteFile(filepath.Join(tempDir, "test.txt"), []byte("test2"), 0644)
Expand Down
12 changes: 10 additions & 2 deletions internal/storage/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions internal/storage/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"dvs/internal/log"
"dvs/internal/meta"
"os"
"path/filepath"
)

// Remove a file from storage
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions testing/10G.dvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "28960eef7d587ab6d1627b7efe30c7a07ce2dce4871d339fdfb607cb0776e064",
"file_size_bytes": 10737418240,
"timestamp": "2023-10-11T12:49:39.475255693-04:00",
"message": "",
"saved_by": "andriygm"
}
7 changes: 7 additions & 0 deletions testing/10G.dvsmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "28960eef7d587ab6d1627b7efe30c7a07ce2dce4871d339fdfb607cb0776e064",
"file_size_bytes": 10737418240,
"timestamp": "2023-10-11T11:47:21.735488265-04:00",
"message": "",
"saved_by": "andriygm"
}
7 changes: 7 additions & 0 deletions testing/1G.dvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "94b4ec39d8d42ebda685fbb5429e8ab0086e65245e750142c1eea36a26abc24d",
"file_size_bytes": 1073741824,
"timestamp": "2023-10-11T12:49:20.077594173-04:00",
"message": "",
"saved_by": "andriygm"
}
7 changes: 7 additions & 0 deletions testing/1G.dvsmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "94b4ec39d8d42ebda685fbb5429e8ab0086e65245e750142c1eea36a26abc24d",
"file_size_bytes": 1073741824,
"timestamp": "2023-10-11T11:47:21.736802714-04:00",
"message": "",
"saved_by": "andriygm"
}
7 changes: 7 additions & 0 deletions testing/266-536x354.jpg.dvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "8ea93e919458fe8db0c7079c775f975af6d5a654daf9dcf3651e60a5a94879d8",
"file_size_bytes": 22600,
"timestamp": "2023-10-11T12:48:34.89499103-04:00",
"message": "",
"saved_by": "andriygm"
}
7 changes: 7 additions & 0 deletions testing/266-536x354.jpg.dvsmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "8ea93e919458fe8db0c7079c775f975af6d5a654daf9dcf3651e60a5a94879d8",
"file_size_bytes": 22600,
"timestamp": "2023-10-11T11:47:21.737105368-04:00",
"message": "",
"saved_by": "andriygm"
}
7 changes: 7 additions & 0 deletions testing/571-536x354.jpg.dvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "800fe626b5b4a066de815d9486893fe549e2af1742558ed4110c6ea424425c42",
"file_size_bytes": 23091,
"timestamp": "2023-10-11T12:49:10.692239925-04:00",
"message": "",
"saved_by": "andriygm"
}
7 changes: 7 additions & 0 deletions testing/571-536x354.jpg.dvsmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blake3_checksum": "800fe626b5b4a066de815d9486893fe549e2af1742558ed4110c6ea424425c42",
"file_size_bytes": 23091,
"timestamp": "2023-10-11T11:47:21.737655146-04:00",
"message": "",
"saved_by": "andriygm"
}

0 comments on commit c4fd120

Please sign in to comment.