Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
074b201
Reference ScriptRock ssh library, not current google one
marksheahan Jan 9, 2015
1165da5
Updated to use github.com/Scriptrock/crypto/ssh
marksheahan Feb 13, 2015
5b6348f
version, lstat
marksheahan Jul 25, 2015
91d0c9e
mkdir
marksheahan Jul 26, 2015
058e1be
open & close
marksheahan Jul 26, 2015
2888b4a
implement read
marksheahan Jul 30, 2015
c9cee8a
implement write
marksheahan Jul 30, 2015
bf6b5bc
fstat
marksheahan Jul 30, 2015
7ab0966
readonly (only for open right now)
marksheahan Jul 31, 2015
435f753
readdir, rename, remove
marksheahan Jul 31, 2015
0f2bc1a
server is passing the client integration tests now. I don't understan…
marksheahan Aug 1, 2015
f4c4138
update client integration tests for more coverage
marksheahan Aug 5, 2015
615df61
Merge branch 'master' of https://github.com/pkg/sftp
marksheahan Aug 5, 2015
2f3ef8b
Merge branch 'master' into server
marksheahan Aug 5, 2015
348ee1a
truncate + darwin VFS test
marksheahan Aug 5, 2015
82ef508
server integration test scaffolding
marksheahan Aug 5, 2015
4325c36
fix format of 'name' packets (shortname, longname, attrs), add Stat
marksheahan Aug 5, 2015
f9e831b
proper ssh closing sequence
marksheahan Aug 6, 2015
1502f6c
compare golang sftp subsystem to openssh
marksheahan Aug 7, 2015
0d8e136
removing sftp server to client layer; using straight os.* calls
marksheahan Sep 7, 2015
5d19ff2
copy os/user/* to here
marksheahan Sep 7, 2015
8d4ed82
apply patch set 2 from https://codereview.appspot.com/13454043
marksheahan Sep 7, 2015
45ad5b7
neuter OSX 'staff' group complaint, it makes no sense to me
marksheahan Sep 7, 2015
c7b2d97
move group to user such that import matches
marksheahan Sep 7, 2015
c36e806
runLs output matching openssh for some stuff
marksheahan Sep 7, 2015
d9371ac
skipping integration tests for server unless specified
marksheahan Sep 7, 2015
0aec5ce
use merge-to target branches, not ScriptRock ones
marksheahan Sep 7, 2015
421e891
remove unused flags
marksheahan Sep 7, 2015
ec909a2
gofmt
marksheahan Sep 7, 2015
af012f1
fix import path for server_standalone
marksheahan Sep 7, 2015
7bb2083
Address review comments; about to change decodePacket()
marksheahan Sep 7, 2015
d80ae36
rmdir and symlink packet handling
marksheahan Sep 7, 2015
90f1d88
add test cleanup back in
marksheahan Sep 7, 2015
cbd08ae
implement setstat & fsetstat
marksheahan Sep 7, 2015
20391fc
skip server tests if the openssh sftp client binary is unavailable
marksheahan Sep 7, 2015
c69ab31
all tests invoking external binaries considered integration tests to …
marksheahan Sep 7, 2015
2eabfa3
remove user/ directory; uid -> username & gid -> groupname lookup rem…
marksheahan Sep 8, 2015
a94c674
missed a file...
marksheahan Sep 8, 2015
bce43f2
Address review comments; alter server test to allow the user / group …
marksheahan Sep 8, 2015
fe6bfd7
remove testdata/ from .gitignore
marksheahan Sep 8, 2015
a6fc4b8
Add comments for *Client.Stat and *Client.Lstat
marksheahan Sep 8, 2015
d730996
add more tests; bug setting S_IFREG caused openssh sftp to refuse to …
marksheahan Sep 9, 2015
e4daa2d
added server integration test for recursive put and recursive get
marksheahan Sep 9, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.*.swo
.*.swp

server_standalone/server_standalone

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown

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?

Copy link
Copy Markdown
Contributor Author

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...

Copy link
Copy Markdown
Member

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:

In .gitignore #46 (comment):

@@ -0,0 +1,5 @@
+..swo
+.
.swp
+
+server_standalone/server_standalone

Yes, I did my integration tests that way. I probably could have used go
install but it's just habit...

—
Reply to this email directly or view it on GitHub
https://github.com/pkg/sftp/pull/46/files#r39007718.


91 changes: 91 additions & 0 deletions attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,56 @@ func unmarshalAttrs(b []byte) (*FileStat, []byte) {
return &fs, b
}

func marshalFileInfo(b []byte, fi os.FileInfo) []byte {
// attributes variable struct, and also variable per protocol version
// spec version 3 attributes:
// uint32 flags
// uint64 size present only if flag SSH_FILEXFER_ATTR_SIZE
// uint32 uid present only if flag SSH_FILEXFER_ATTR_UIDGID
// uint32 gid present only if flag SSH_FILEXFER_ATTR_UIDGID
// uint32 permissions present only if flag SSH_FILEXFER_ATTR_PERMISSIONS
// uint32 atime present only if flag SSH_FILEXFER_ACMODTIME
// uint32 mtime present only if flag SSH_FILEXFER_ACMODTIME
// uint32 extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED
// string extended_type
// string extended_data
// ... more extended data (extended_type - extended_data pairs),
// so that number of pairs equals extended_count

uid := uint32(0)
gid := uint32(0)
mtime := uint32(fi.ModTime().Unix())
atime := mtime

var flags uint32 = ssh_FILEXFER_ATTR_SIZE |
ssh_FILEXFER_ATTR_PERMISSIONS |
ssh_FILEXFER_ATTR_ACMODTIME

if statt, ok := fi.Sys().(*syscall.Stat_t); ok {
flags |= ssh_FILEXFER_ATTR_UIDGID
uid = statt.Uid
gid = statt.Gid
}

b = marshalUint32(b, flags) // flags
if flags&ssh_FILEXFER_ATTR_SIZE != 0 {
b = marshalUint64(b, uint64(fi.Size())) // size
}
if flags&ssh_FILEXFER_ATTR_UIDGID != 0 {
b = marshalUint32(b, uid)
b = marshalUint32(b, gid)
}
if flags&ssh_FILEXFER_ATTR_PERMISSIONS != 0 {
b = marshalUint32(b, fromFileMode(fi.Mode())) // permissions
}
if flags&ssh_FILEXFER_ATTR_ACMODTIME != 0 {
b = marshalUint32(b, atime)
b = marshalUint32(b, mtime)
}

return b
}

// toFileMode converts sftp filemode bits to the os.FileMode specification
func toFileMode(mode uint32) os.FileMode {
var fm = os.FileMode(mode & 0777)
Expand Down Expand Up @@ -136,3 +186,44 @@ func toFileMode(mode uint32) os.FileMode {
}
return fm
}

// fromFileMode converts from the os.FileMode specification to sftp filemode bits
func fromFileMode(mode os.FileMode) uint32 {
ret := uint32(0)

if mode&os.ModeDevice != 0 {
if mode&os.ModeCharDevice != 0 {
ret |= syscall.S_IFCHR
} else {
ret |= syscall.S_IFBLK
}
}
if mode&os.ModeDir != 0 {
ret |= syscall.S_IFDIR
}
if mode&os.ModeSymlink != 0 {
ret |= syscall.S_IFLNK
}
if mode&os.ModeNamedPipe != 0 {
ret |= syscall.S_IFIFO
}
if mode&os.ModeSetgid != 0 {
ret |= syscall.S_ISGID
}
if mode&os.ModeSetuid != 0 {
ret |= syscall.S_ISUID
}
if mode&os.ModeSticky != 0 {
ret |= syscall.S_ISVTX
}
if mode&os.ModeSocket != 0 {
ret |= syscall.S_IFSOCK
}

if mode&os.ModeType == 0 {
ret |= syscall.S_IFREG
}
ret |= uint32(mode & os.ModePerm)

return ret
}
54 changes: 54 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -312,6 +340,25 @@ func (c *Client) ReadLink(p string) (string, error) {
}
}

// Symlink creates a symbolic link at 'newname', pointing at target 'oldname'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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:
https://golang.org/pkg/os/#Symlink

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies, this was my mistake

On 8 Sep 2015, at 14:11, Mark Sheahan [email protected] wrote:

In client.go:

@@ -312,6 +336,25 @@ func (c *Client) ReadLink(p string) (string, error) {
}
}

+// Symlink creates a symbolic link at 'newname', pointing at target 'oldname'
os.Link() creates a hard link, not a soft link. This name was chosen for consistency with os.Symlink:
https://golang.org/pkg/os/#Symlink

—
Reply to this email directly or view it on GitHub.

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()
Expand Down Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions client_integration_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sftp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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")
}
}
Loading