diff --git a/core/core.go b/core/core.go index 7203f96..14cda1d 100644 --- a/core/core.go +++ b/core/core.go @@ -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") } diff --git a/core/core_test.go b/core/core_test.go index 4d2ea89..5f46e5a 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -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"}, @@ -291,7 +291,7 @@ func TestCleanAndCheckPaths(t *testing.T) { { []string{"file1"}, []string{filepath.Clean("file1")}, - "", + ".", ".", }, { @@ -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"), "", diff --git a/core/decrypt.go b/core/decrypt.go index 5a50ef4..13d1bd0 100644 --- a/core/decrypt.go +++ b/core/decrypt.go @@ -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 { diff --git a/core/encrypt.go b/core/encrypt.go index ce9c678..d20b08d 100644 --- a/core/encrypt.go +++ b/core/encrypt.go @@ -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 {