Skip to content

Commit

Permalink
Fix windows style file paths in fs cp command (#1118)
Browse files Browse the repository at this point in the history
## Changes
Copying a local file in windows to remote directory in DBFS would fail
if the path was specified as a windows style path (compared to a UNIX
style path). This PR fixes that.

Note, UNIX style paths will continue to work because `filepath.Base`
respects both `/` and `\` as file separators. See: `IsPathSeparator` in
https://go.dev/src/os/path_windows.go.

Fixes issue: #1109.

## Tests
Integration test and manually
```
C:\Users\shreyas.goenka>Desktop\cli.exe fs cp .\Desktop\foo.txt dbfs:/Users/[email protected]
.\Desktop\foo.txt -> dbfs:/Users/[email protected]/foo.txt

C:\Users\shreyas.goenka>Desktop\cli.exe fs cat  dbfs:/Users/[email protected]/foo.txt
hello, world
````
  • Loading branch information
shreyas-goenka authored Jan 11, 2024
1 parent 7dcdadd commit 2c0d067
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/fs/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *copy) cpDirToDir(sourceDir, targetDir string) error {
}

func (c *copy) cpFileToDir(sourcePath, targetDir string) error {
fileName := path.Base(sourcePath)
fileName := filepath.Base(sourcePath)
targetPath := path.Join(targetDir, fileName)

return c.cpFileToFile(sourcePath, targetPath)
Expand Down
17 changes: 17 additions & 0 deletions internal/fs_cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"path"
"path/filepath"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -140,6 +141,22 @@ func TestAccFsCpFileToDir(t *testing.T) {
}
}

func TestAccFsCpFileToDirForWindowsPaths(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping test on non-windows OS")
}

ctx := context.Background()
sourceFiler, sourceDir := setupLocalFiler(t)
targetFiler, targetDir := setupDbfsFiler(t)
setupSourceFile(t, ctx, sourceFiler)

windowsPath := filepath.Join(filepath.FromSlash(sourceDir), "foo.txt")

RequireSuccessfulRun(t, "fs", "cp", windowsPath, targetDir)
assertTargetFile(t, ctx, targetFiler, "foo.txt")
}

func TestAccFsCpDirToDirFileNotOverwritten(t *testing.T) {
ctx := context.Background()
table := setupTable()
Expand Down

0 comments on commit 2c0d067

Please sign in to comment.