forked from pkg/sftp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add serverside StatVFS function, implemented for darwin and linux (pk…
- Loading branch information
1 parent
8ceba57
commit 9ff4de5
Showing
8 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sftp | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func statvfsFromStatfst(stat *syscall.Statfs_t) (*StatVFS, error) { | ||
return &StatVFS{ | ||
Bsize: uint64(stat.Bsize), | ||
Frsize: uint64(stat.Bsize), // fragment size is a linux thing; use block size here | ||
Blocks: stat.Blocks, | ||
Bfree: stat.Bfree, | ||
Bavail: stat.Bavail, | ||
Files: stat.Files, | ||
Ffree: stat.Ffree, | ||
Favail: stat.Ffree, // not sure how to calculate Favail | ||
Fsid: uint64(uint64(stat.Fsid.Val[1])<<32 | uint64(stat.Fsid.Val[0])), // endianness? | ||
Flag: uint64(stat.Flags), // assuming POSIX? | ||
Namemax: 1024, // man 2 statfs shows: #define MAXPATHLEN 1024 | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// +build darwin linux | ||
|
||
// fill in statvfs structure with OS specific values | ||
// Statfs_t is different per-kernel, and only exists on some unixes (not Solaris for instance) | ||
|
||
package sftp | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func (p sshFxpExtendedPacketStatVFS) respond(svr *Server) error { | ||
stat := &syscall.Statfs_t{} | ||
if err := syscall.Statfs(p.Path, stat); err != nil { | ||
return svr.sendPacket(statusFromError(p.ID, err)) | ||
} | ||
|
||
retPkt, err := statvfsFromStatfst(stat) | ||
if err != nil { | ||
return svr.sendPacket(statusFromError(p.ID, err)) | ||
} | ||
retPkt.ID = p.ID | ||
|
||
return svr.sendPacket(retPkt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sftp | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func statvfsFromStatfst(stat *syscall.Statfs_t) (*StatVFS, error) { | ||
return &StatVFS{ | ||
Bsize: uint64(stat.Bsize), | ||
Frsize: uint64(stat.Frsize), | ||
Blocks: stat.Blocks, | ||
Bfree: stat.Bfree, | ||
Bavail: stat.Bavail, | ||
Files: stat.Files, | ||
Ffree: stat.Ffree, | ||
Favail: stat.Ffree, // not sure how to calculate Favail | ||
Fsid: uint64(uint64(stat.Fsid.X__val[1])<<32 | uint64(stat.Fsid.X__val[0])), // endianness? | ||
Flag: uint64(stat.Flags), // assuming POSIX? | ||
Namemax: uint64(stat.Namelen), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build !darwin,!linux | ||
|
||
package sftp | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func (p sshFxpExtendedPacketStatVFS) respond(svr *Server) error { | ||
return syscall.ENOTSUP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package sftp | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
var errClientRecvFinished = errors.New("client recv finished") | ||
|
||
func clientServerPair(t *testing.T) (*Client, *Server) { | ||
c, s := netPipe(t) | ||
server, err := NewServer(s) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
go server.Serve() | ||
client, err := NewClientPipe(c, c) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return client, server | ||
} | ||
|
||
type sshFxpTestBadExtendedPacket struct { | ||
ID uint32 | ||
Extension string | ||
Data string | ||
} | ||
|
||
func (p sshFxpTestBadExtendedPacket) id() uint32 { return p.ID } | ||
|
||
func (p sshFxpTestBadExtendedPacket) MarshalBinary() ([]byte, error) { | ||
l := 1 + 4 + 4 + // type(byte) + uint32 + uint32 | ||
len(p.Extension) + | ||
len(p.Data) | ||
|
||
b := make([]byte, 0, l) | ||
b = append(b, ssh_FXP_EXTENDED) | ||
b = marshalUint32(b, p.ID) | ||
b = marshalString(b, p.Extension) | ||
b = marshalString(b, p.Data) | ||
return b, nil | ||
} | ||
|
||
// test that errors are sent back when we request an invalid extended packet operation | ||
func TestInvalidExtendedPacket(t *testing.T) { | ||
client, _ := clientServerPair(t) | ||
defer client.Close() | ||
badPacket := sshFxpTestBadExtendedPacket{client.nextID(), "thisDoesn'tExist", "foobar"} | ||
_, _, err := client.sendRequest(badPacket) | ||
if err != nil { | ||
t.Log(err) | ||
} else { | ||
t.Fatal("expected error from bad packet") | ||
} | ||
|
||
// try to stat a file; the client should have shut down. | ||
filePath := "/etc/passwd" | ||
_, err = client.Stat(filePath) | ||
} |