Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export RemoveFile method #548

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions client.go
Expand Up @@ -728,7 +728,7 @@ func (c *Client) Join(elem ...string) string { return path.Join(elem...) }
// file or directory with the specified path exists, or if the specified directory
// is not empty.
func (c *Client) Remove(path string) error {
err := c.removeFile(path)
err := c.RemoveFile(path)
// some servers, *cough* osx *cough*, return EPERM, not ENODIR.
// serv-u returns ssh_FX_FILE_IS_A_DIRECTORY
// EPERM is converted to os.ErrPermission so it is not a StatusError
Expand All @@ -744,7 +744,7 @@ func (c *Client) Remove(path string) error {
return err
}

func (c *Client) removeFile(path string) error {
func (c *Client) RemoveFile(path string) error {
id := c.nextID()
typ, data, err := c.sendPacket(nil, &sshFxpRemovePacket{
ID: id,
Expand Down
39 changes: 39 additions & 0 deletions client_integration_test.go
Expand Up @@ -689,6 +689,45 @@ func TestClientRemoveFailed(t *testing.T) {
}
}

func TestClientRemoveFile(t *testing.T) {
sftp, cmd := testClient(t, READWRITE, NODELAY)
defer cmd.Wait()
defer sftp.Close()

f, err := ioutil.TempFile("", "sftptest-remove")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
f.Close()

if err := sftp.RemoveFile(f.Name()); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(f.Name()); !os.IsNotExist(err) {
t.Fatal(err)
}
}

func TestClientRemoveFileFailed(t *testing.T) {
sftp, cmd := testClient(t, READONLY, NODELAY)
defer cmd.Wait()
defer sftp.Close()

f, err := ioutil.TempFile("", "sftptest-removefailed")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())

if err := sftp.RemoveFile(f.Name()); err == nil {
t.Fatalf("Remove(%v): want: permission denied, got %v", f.Name(), err)
}
if _, err := os.Lstat(f.Name()); err != nil {
t.Fatal(err)
}
}

func TestClientRename(t *testing.T) {
sftp, cmd := testClient(t, READWRITE, NODELAY)
defer cmd.Wait()
Expand Down