hnakamur / go-scp

[Unmaintained] A scp client library written in Go
MIT License
41 stars 23 forks source link

SendDir with AcceptFunc may fail #7

Closed yamamoto-febc closed 7 years ago

yamamoto-febc commented 7 years ago

I want to process only the files under the directory (except directories).
so I wrote following code.

scpClient := scp.NewSCP(conn)
localPath := "/path/to/local/dir"
remotePath := "/path/to/remote/dir"

// use AcceptFunc to ignore directories under localPath
err := scpClient.SendDir(localPath, remotePath, func(parentDir string, info os.FileInfo) (bool, error) {
    current := filepath.Join(parentDir, info.Name())
    return localPath == current || (localPath == parentDir && !info.IsDir()), nil
})

It look like good. but, in some cases, the following error occurs.

scp: /root/example-2: Is a directory

It seems to be caused by the order of files and directories.
In the case of the following, when local path is


$ tree 
.
├── example-1
│   ├── 01_file
│   ├── 02_file
│   └── 03_dir
│       ├── 01_file
│       └── 02_file
└── example-2
    ├── 01_dir
    │   ├── 01_file
    │   └── 02_file
    ├── 02_file
    └── 03_file

Is there any solution to fix this problem?

hnakamur commented 7 years ago

Thanks for you report. Before I start to investigate the problem, I added two test cases to make sure what you mean. https://github.com/hnakamur/go-scp/commit/7e30af2bdde7adcc00e37ae05955d5538a200c37

Test results on my environment go version go1.8 linux/amd64

    --- PASS: TestSendDir/send_only_files_case_#1 (0.20s)
    --- FAIL: TestSendDir/send_only_files_case_#2 (0.15s)
        source_test.go:443: unmatch entry count. got:2, want:0

Could you confirm these two test cases are representing your problem? Thanks!

yamamoto-febc commented 7 years ago

Hi @hnakamur

Thanks for your response!!
I confirmed that two test cases are added, and I think that there are representing enough our problems.

FYI: It is not included in the test case, but our problems also occurs on Windows.
My go env is go version go1.8 windows/amd64

yamamoto-febc commented 7 years ago

Oh, sorry, I confirmed test cases again, there are not enough.
This problem occurs only when destination directory is not exists.

I will add test cases to cover there soon.

yamamoto-febc commented 7 years ago

I added test cases for when destination directory is not exists.
ee5fda3879c15cd99ee903605096eb71aee3ecbf

And test results on my machine is:

    --- PASS: TestSendDir/send_only_files_case_#1_dest_dir_exists (0.20s)
    --- FAIL: TestSendDir/send_only_files_case_#2_dest_dir_exists (0.26s)
        source_test.go:532: unmatch entry count. got:2, want:0
    --- PASS: TestSendDir/send_only_files_case_#3_dest_dir_not_exists (0.25s)
    --- FAIL: TestSendDir/send_only_files_case_#4_dest_dir_not_exists (0.16s)
        source_test.go:382: fail to SendDir; failed to write scp file body: err=EOF
        source_test.go:532: unmatch entry count. got:2, want:0
hnakamur commented 7 years ago

@yamamoto-febc Fixed. Thanks for your bug report and a test case!

yamamoto-febc commented 7 years ago

You did awesome!!!!
Thank you 👍