Skip to content

Commit

Permalink
Fix and improve outputDir handling and adjust tests accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
70sh1 committed Dec 1, 2023
1 parent 0983705 commit a3c6f90
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
25 changes: 13 additions & 12 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,27 @@ func CleanAndCheckPaths(paths []string, outputDir string) ([]string, string, err
return nil, "", errors.New("empty path sequence")
}

// Clean all paths and outputDir
// Clean paths
for i := 0; i < len(paths); i++ {
paths[i] = filepath.Clean(paths[i])
}
outputDir = filepath.Clean(outputDir)

// Check if outputDir is actually a directory
fileInfo, err := os.Stat(outputDir)
if err != nil {
return nil, "", err
}
if !fileInfo.IsDir() {
return nil, "", fmt.Errorf("'%v' is not a directory", filepath.Base(outputDir))
}

if hasDuplicates(paths) {
return nil, "", errors.New("duplicate paths are not allowed")
}

if len(outputDir) > 0 && outputDir != "." {
if outputDir != "" {
outputDir = filepath.Clean(outputDir)

// Check if outputDir is actually a directory
fileInfo, err := os.Stat(outputDir)
if err != nil {
return nil, "", err
}
if !fileInfo.IsDir() {
return nil, "", fmt.Errorf("'%s' is not a directory", filepath.Base(outputDir))
}

if hasDuplicateFilenames(paths) {
return nil, "", errors.New("duplicate filenames are not allowed with output (-o) flag")
}
Expand Down
8 changes: 4 additions & 4 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ func TestCleanAndCheckPaths(t *testing.T) {
[]string{"C:/t./11est/something.txt", "D:/path/something.txt", "C:////\\/"},
[]string{filepath.Clean("C:/t./11est/something.txt"), filepath.Clean("D:/path/something.txt"), filepath.Clean("C:////\\/")},
"",
".",
"",
},
{
[]string{"C:\\something.txt", "H:/qq", "a", "/home/\\//user//test/some"},
[]string{filepath.Clean("C:\\something.txt"), filepath.Clean("H:/qq"), filepath.Clean("a"), filepath.Clean("/home/\\//user//test/some")},
"",
".",
"",
},
{
[]string{"file1", "./file2", "/////somewhere\\file3"},
Expand All @@ -291,7 +291,7 @@ func TestCleanAndCheckPaths(t *testing.T) {
{
[]string{"file1"},
[]string{filepath.Clean("file1")},
"",
".",
".",
},
{
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestCleanAndCheckPathsError(t *testing.T) {
"", // Expecting OS specific err
},
{
[]string{"path/to/something", "path\\////to//\\/something/", "file1"},
[]string{"path/to/something", "path\\////to//\\/something2/", "file1"},
nil,
filepath.Join(dir, "small.txt"),
"",
Expand Down
2 changes: 1 addition & 1 deletion core/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func DecryptFiles(paths []string, outputDir, password string, overwrite bool) er
go func() {
defer wg.Done()
fileOut := strings.TrimSuffix(fileIn, ".eddy")
if len(outputDir) > 0 {
if outputDir != "" {
fileOut = filepath.Join(outputDir, filepath.Base(fileOut))
}
if _, err := os.Stat(fileOut); !errors.Is(err, os.ErrNotExist) && !overwrite {
Expand Down
2 changes: 1 addition & 1 deletion core/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func EncryptFiles(paths []string, outputDir, password string, overwrite bool) (i
go func() {
defer wg.Done()
fileOut := fileIn + ".eddy"
if len(outputDir) > 0 {
if outputDir != "" {
fileOut = filepath.Join(outputDir, filepath.Base(fileOut))
}
if _, err := os.Stat(fileOut); !errors.Is(err, os.ErrNotExist) && !overwrite {
Expand Down

0 comments on commit a3c6f90

Please sign in to comment.