Repository navigation
sftp server implementation #46
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
Changes from all commits
074b201
1165da5
5b6348f
91d0c9e
058e1be
2888b4a
c9cee8a
bf6b5bc
7ab0966
435f753
0f2bc1a
f4c4138
615df61
2f3ef8b
348ee1a
82ef508
4325c36
f9e831b
1502f6c
0d8e136
5d19ff2
8d4ed82
45ad5b7
c7b2d97
c36e806
d9371ac
0aec5ce
421e891
ec909a2
af012f1
7bb2083
d80ae36
90f1d88
cbd08ae
20391fc
c69ab31
2eabfa3
a94c674
bce43f2
fe6bfd7
a6fc4b8
d730996
e4daa2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .*.swo | ||
| .*.swp | ||
|
|
||
| server_standalone/server_standalone | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,6 +259,34 @@ func (c *Client) opendir(path string) (string, error) { | |
| } | ||
| } | ||
|
|
||
| // Stat returns a FileInfo structure describing the file specified by path 'p'. | ||
| // If 'p' is a symbolic link, the returned FileInfo structure describes the referent file. | ||
| func (c *Client) Stat(p string) (os.FileInfo, error) { | ||
| id := c.nextId() | ||
| typ, data, err := c.sendRequest(sshFxpStatPacket{ | ||
| Id: id, | ||
| Path: p, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| switch typ { | ||
| case ssh_FXP_ATTRS: | ||
| sid, data := unmarshalUint32(data) | ||
| if sid != id { | ||
| return nil, &unexpectedIdErr{id, sid} | ||
| } | ||
| attr, _ := unmarshalAttrs(data) | ||
| return fileInfoFromStat(attr, path.Base(p)), nil | ||
| case ssh_FXP_STATUS: | ||
| return nil, unmarshalStatus(id, data) | ||
| default: | ||
| return nil, unimplementedPacketErr(typ) | ||
| } | ||
| } | ||
|
|
||
| // Lstat returns a FileInfo structure describing the file specified by path 'p'. | ||
| // If 'p' is a symbolic link, the returned FileInfo structure describes the symbolic link. | ||
| func (c *Client) Lstat(p string) (os.FileInfo, error) { | ||
| id := c.nextId() | ||
| typ, data, err := c.sendRequest(sshFxpLstatPacket{ | ||
|
|
@@ -312,6 +340,25 @@ func (c *Client) ReadLink(p string) (string, error) { | |
| } | ||
| } | ||
|
|
||
| // Symlink creates a symbolic link at 'newname', pointing at target 'oldname' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be called Link, for compatibility with the os package, http://godoc.org/os#Link You can do this in a followup if you like.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. os.Link() creates a hard link, not a soft link. This name was chosen for consistency with os.Symlink:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My apologies, this was my mistake
|
||
| func (c *Client) Symlink(oldname, newname string) error { | ||
| id := c.nextId() | ||
| typ, data, err := c.sendRequest(sshFxpSymlinkPacket{ | ||
| Id: id, | ||
| Linkpath: newname, | ||
| Targetpath: oldname, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| switch typ { | ||
| case ssh_FXP_STATUS: | ||
| return okOrErr(unmarshalStatus(id, data)) | ||
| default: | ||
| return unimplementedPacketErr(typ) | ||
| } | ||
| } | ||
|
|
||
| // setstat is a convience wrapper to allow for changing of various parts of the file descriptor. | ||
| func (c *Client) setstat(path string, flags uint32, attrs interface{}) error { | ||
| id := c.nextId() | ||
|
|
@@ -1071,6 +1118,13 @@ func unmarshalStatus(id uint32, data []byte) error { | |
| } | ||
| } | ||
|
|
||
| func marshalStatus(b []byte, err StatusError) []byte { | ||
| b = marshalUint32(b, err.Code) | ||
| b = marshalString(b, err.msg) | ||
| b = marshalString(b, err.lang) | ||
| return b | ||
| } | ||
|
|
||
| // flags converts the flags passed to OpenFile into ssh flags. | ||
| // Unsupported flags are ignored. | ||
| func flags(f int) uint32 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package sftp | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does this test only run on darwin ? can ou rename it to client_integration_test.go and use // +build !windows to rule out that os ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found that the field names available in syscall.Statfs_t were adequately different between kernels (darwin vs linux) that I could not re-use client_integration_linux_test.go when testing on darwin. |
||
|
|
||
| import ( | ||
| "syscall" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestClientStatVFS(t *testing.T) { | ||
| sftp, cmd := testClient(t, READWRITE, NO_DELAY) | ||
| defer cmd.Wait() | ||
| defer sftp.Close() | ||
|
|
||
| vfs, err := sftp.StatVFS("/") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // get system stats | ||
| s := syscall.Statfs_t{} | ||
| err = syscall.Statfs("/", &s) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // check some stats | ||
| if vfs.Files != uint64(s.Files) { | ||
| t.Fatal("fr_size does not match") | ||
| } | ||
|
|
||
| if vfs.Bfree != uint64(s.Bfree) { | ||
| t.Fatal("f_bsize does not match") | ||
| } | ||
|
|
||
| if vfs.Favail != uint64(s.Ffree) { | ||
| t.Fatal("f_namemax does not match") | ||
| } | ||
|
|
||
| if vfs.Bavail != s.Bavail { | ||
| t.Fatal("f_bavail does not match") | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can probably delete this as well, this would only happen if you did go build in the server_standalone directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that .gitignore files need to be filled with thousands of lines of junk. Assumedly including 'server_standalone/server_standalone' is useful during development?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I did my integration tests that way. I probably could have used go install but it's just habit...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's your call, doesn't really matter much each way, i'm just finding
things to nit pick while you land your change.
On Wed, Sep 9, 2015 at 3:06 PM, Mark Sheahan [email protected]
wrote: