Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 37 additions & 15 deletions lib/sshutils/sftp/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"io/fs"
"os"
"path/filepath"
"time"

"github.com/gravitational/trace"
Expand All @@ -36,9 +37,22 @@ func (l localFS) Type() string {
return "local"
}

func (l *localFS) Glob(ctx context.Context, pattern string) ([]string, error) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: Can you add godocs on all exported names?

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 can, but I thought it was unnecessary due to the fact that localFS's methods are never called directly, only as a FileSystem interface which has all of its methods documented. For future reference, in this case is it still necessary to add comments to every method of an implementation of an interface that is (should) never be used directly?

if err := ctx.Err(); err != nil {
return nil, trace.Wrap(err)
}

matches, err := filepath.Glob(pattern)
if err != nil {
return nil, trace.Wrap(err)
}

return matches, nil
}

func (l localFS) Stat(ctx context.Context, path string) (os.FileInfo, error) {
if err := ctx.Err(); err != nil {
return nil, err
return nil, trace.Wrap(err)
}

fi, err := os.Stat(path)
Expand All @@ -51,28 +65,36 @@ func (l localFS) Stat(ctx context.Context, path string) (os.FileInfo, error) {

func (l localFS) ReadDir(ctx context.Context, path string) ([]os.FileInfo, error) {
if err := ctx.Err(); err != nil {
return nil, err
}

// normally os.ReadDir would be used as it's potentially more efficient,
// but because we want os.FileInfos of every file this is easier
f, err := os.Open(path)
if err != nil {
return nil, trace.Wrap(err)
}
defer f.Close()

fileInfos, err := f.Readdir(-1)
entries, err := os.ReadDir(path)
if err != nil {
return nil, trace.Wrap(err)
}
fileInfos := make([]fs.FileInfo, len(entries))
for i, entry := range entries {
info, err := entry.Info()
if err != nil {
return nil, trace.Wrap(err)
}
// if the file is a symlink, return the info of the linked file
if info.Mode().Type()&os.ModeSymlink != 0 {
info, err = os.Stat(filepath.Join(path, info.Name()))
if err != nil {
return nil, trace.Wrap(err)
}
}

fileInfos[i] = info
}

return fileInfos, nil
}

func (l localFS) Open(ctx context.Context, path string) (fs.File, error) {
if err := ctx.Err(); err != nil {
return nil, err
return nil, trace.Wrap(err)
}

f, err := os.Open(path)
Expand All @@ -85,7 +107,7 @@ func (l localFS) Open(ctx context.Context, path string) (fs.File, error) {

func (l localFS) Create(ctx context.Context, path string) (io.WriteCloser, error) {
if err := ctx.Err(); err != nil {
return nil, err
return nil, trace.Wrap(err)
}

f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, defaults.FilePermissions)
Expand All @@ -98,7 +120,7 @@ func (l localFS) Create(ctx context.Context, path string) (io.WriteCloser, error

func (l localFS) Mkdir(ctx context.Context, path string) error {
if err := ctx.Err(); err != nil {
return err
return trace.Wrap(err)
}

err := os.MkdirAll(path, defaults.DirectoryPermissions)
Expand All @@ -111,15 +133,15 @@ func (l localFS) Mkdir(ctx context.Context, path string) error {

func (l localFS) Chmod(ctx context.Context, path string, mode os.FileMode) error {
if err := ctx.Err(); err != nil {
return err
return trace.Wrap(err)
}

return trace.Wrap(os.Chmod(path, mode))
}

func (l localFS) Chtimes(ctx context.Context, path string, atime, mtime time.Time) error {
if err := ctx.Err(); err != nil {
return err
return trace.Wrap(err)
}

return trace.ConvertSystemError(os.Chtimes(path, atime, mtime))
Expand Down
37 changes: 30 additions & 7 deletions lib/sshutils/sftp/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"io/fs"
"os"
portablepath "path"
"time"

"github.com/gravitational/trace"
Expand All @@ -37,9 +38,22 @@ func (r *remoteFS) Type() string {
return "remote"
}

func (r *remoteFS) Glob(ctx context.Context, pattern string) ([]string, error) {
if err := ctx.Err(); err != nil {
return nil, trace.Wrap(err)
}

matches, err := r.c.Glob(pattern)
if err != nil {
return nil, trace.Wrap(err)
}

return matches, nil
}

func (r *remoteFS) Stat(ctx context.Context, path string) (os.FileInfo, error) {
if err := ctx.Err(); err != nil {
return nil, err
return nil, trace.Wrap(err)
}

fi, err := r.c.Stat(path)
Expand All @@ -52,20 +66,29 @@ func (r *remoteFS) Stat(ctx context.Context, path string) (os.FileInfo, error) {

func (r *remoteFS) ReadDir(ctx context.Context, path string) ([]os.FileInfo, error) {
if err := ctx.Err(); err != nil {
return nil, err
return nil, trace.Wrap(err)
}

fileInfos, err := r.c.ReadDir(path)
if err != nil {
return nil, trace.Wrap(err)
}
for i := range fileInfos {
// if the file is a symlink, return the info of the linked file
if fileInfos[i].Mode().Type()&os.ModeSymlink != 0 {
fileInfos[i], err = r.c.Stat(portablepath.Join(path, fileInfos[i].Name()))
if err != nil {
return nil, trace.Wrap(err)
}
}
}

return fileInfos, nil
}

func (r *remoteFS) Open(ctx context.Context, path string) (fs.File, error) {
if err := ctx.Err(); err != nil {
return nil, err
return nil, trace.Wrap(err)
}

f, err := r.c.Open(path)
Expand All @@ -78,7 +101,7 @@ func (r *remoteFS) Open(ctx context.Context, path string) (fs.File, error) {

func (r *remoteFS) Create(ctx context.Context, path string) (io.WriteCloser, error) {
if err := ctx.Err(); err != nil {
return nil, err
return nil, trace.Wrap(err)
}

f, err := r.c.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
Expand All @@ -91,7 +114,7 @@ func (r *remoteFS) Create(ctx context.Context, path string) (io.WriteCloser, err

func (r *remoteFS) Mkdir(ctx context.Context, path string) error {
if err := ctx.Err(); err != nil {
return err
return trace.Wrap(err)
}

err := r.c.MkdirAll(path)
Expand All @@ -104,15 +127,15 @@ func (r *remoteFS) Mkdir(ctx context.Context, path string) error {

func (r *remoteFS) Chmod(ctx context.Context, path string, mode os.FileMode) error {
if err := ctx.Err(); err != nil {
return err
return trace.Wrap(err)
}

return trace.Wrap(r.c.Chmod(path, mode))
}

func (r *remoteFS) Chtimes(ctx context.Context, path string, atime, mtime time.Time) error {
if err := ctx.Err(); err != nil {
return err
return trace.Wrap(err)
}

return trace.Wrap(r.c.Chtimes(path, atime, mtime))
Expand Down
Loading