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

Use path instead of filepath in sftp #237

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions sftp/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"io"
"os"
"path/filepath"
"path"
"strings"

"github.com/graymeta/stow"
Expand All @@ -28,8 +28,8 @@ func (c *container) Name() string {
// Item returns a stow.Item instance of a container based on the name of the
// container and the file.
func (c *container) Item(id string) (stow.Item, error) {
path := filepath.Join(c.location.config.basePath, c.name, filepath.FromSlash(id))
info, err := c.location.sftpClient.Stat(path)
p := path.Join(c.location.config.basePath, c.name, id)
info, err := c.location.sftpClient.Stat(p)
if err != nil {
if os.IsNotExist(err) {
return nil, stow.ErrNotFound
Expand All @@ -54,7 +54,7 @@ func (c *container) Item(id string) (stow.Item, error) {
// the prefix argument. The 'cursor' variable facilitates pagination.
func (c *container) Items(prefix, cursor string, count int) ([]stow.Item, string, error) {
var entries []entry
entries, cursor, err := c.getFolderItems([]entry{}, prefix, "", filepath.Join(c.location.config.basePath, c.name), cursor, count, false)
entries, cursor, err := c.getFolderItems([]entry{}, prefix, "", path.Join(c.location.config.basePath, c.name), cursor, count, false)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (c *container) getFolderItems(entries []entry, prefix, relPath, id, cursor
}

for i, file := range files {
fileRelPath := filepath.Join(relPath, file.Name())
fileRelPath := path.Join(relPath, file.Name())
if !start && cursorPieces[0] != "" && (file.Name() >= cursorPieces[0] || fileRelPath >= cursor) {
start = true
if file.Name() == cursorPieces[0] && !file.IsDir() {
Expand All @@ -110,7 +110,7 @@ func (c *container) getFolderItems(entries []entry, prefix, relPath, id, cursor
if file.IsDir() {
var err error
var retCursor string
entries, retCursor, err = c.getFolderItems(entries, prefix, fileRelPath, filepath.Join(id, file.Name()), cursor, limit, true)
entries, retCursor, err = c.getFolderItems(entries, prefix, fileRelPath, path.Join(id, file.Name()), cursor, limit, true)
if err != nil {
return nil, "", err
}
Expand All @@ -127,7 +127,7 @@ func (c *container) getFolderItems(entries []entry, prefix, relPath, id, cursor

// TODO: prefix could be optimized to not look down paths that don't match,
// but this is a quick/cheap first implementation.
filePath := strings.TrimPrefix(filepath.Join(id, file.Name()), filepath.Join(c.location.config.basePath, c.name)+"/")
filePath := strings.TrimPrefix(path.Join(id, file.Name()), path.Join(c.location.config.basePath, c.name)+"/")
if !strings.HasPrefix(filePath, prefix) {
continue
}
Expand All @@ -136,7 +136,7 @@ func (c *container) getFolderItems(entries []entry, prefix, relPath, id, cursor
entries,
entry{
Name: file.Name(),
ID: filepath.Join(id, file.Name()),
ID: path.Join(id, file.Name()),
RelPath: fileRelPath,
item: &item{
container: c,
Expand All @@ -161,7 +161,7 @@ func (c *container) getFolderItems(entries []entry, prefix, relPath, id, cursor

// RemoveItem removes a file from the remote server.
func (c *container) RemoveItem(id string) error {
return c.location.sftpClient.Remove(filepath.Join(c.location.config.basePath, c.name, filepath.FromSlash(id)))
return c.location.sftpClient.Remove(path.Join(c.location.config.basePath, c.name, id))
}

// Put sends a request to upload content to the container.
Expand All @@ -170,17 +170,17 @@ func (c *container) Put(name string, r io.Reader, size int64, metadata map[strin
return nil, stow.NotSupported("metadata")
}

path := filepath.Join(c.location.config.basePath, c.name, filepath.FromSlash(name))
p := path.Join(c.location.config.basePath, c.name, name)
item := &item{
container: c,
path: name,
size: size,
}
err := c.location.sftpClient.MkdirAll(filepath.Dir(path))
err := c.location.sftpClient.MkdirAll(path.Dir(p))
if err != nil {
return nil, err
}
f, err := c.location.sftpClient.Create(path)
f, err := c.location.sftpClient.Create(p)
if err != nil {
return nil, err
}
Expand All @@ -193,7 +193,7 @@ func (c *container) Put(name string, r io.Reader, size int64, metadata map[strin
return nil, errors.New("bad size")
}

info, err := c.location.sftpClient.Stat(path)
info, err := c.location.sftpClient.Stat(p)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions sftp/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"net/url"
"os"
"path/filepath"
"path"
"time"

"github.com/graymeta/stow/local"
Expand Down Expand Up @@ -51,7 +51,7 @@ func (i *item) URL() *url.URL {
// resource which is returned along with an error.
func (i *item) Open() (io.ReadCloser, error) {
return i.container.location.sftpClient.Open(
filepath.Join(
path.Join(
i.container.location.config.basePath,
i.container.Name(),
i.Name(),
Expand Down
16 changes: 8 additions & 8 deletions sftp/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sftp
import (
"net/url"
"os"
"path/filepath"
"path"
"sort"
"strings"

Expand All @@ -24,7 +24,7 @@ type location struct {

// CreateContainer creates a new container, in this case a directory on the remote server.
func (l *location) CreateContainer(containerName string) (stow.Container, error) {
if err := l.sftpClient.Mkdir(filepath.Join(l.config.basePath, containerName)); err != nil {
if err := l.sftpClient.Mkdir(path.Join(l.config.basePath, containerName)); err != nil {
return nil, err
}

Expand Down Expand Up @@ -99,7 +99,7 @@ func (l *location) Close() error {

// Container retrieves a stow.Container based on its name which must be exact.
func (l *location) Container(id string) (stow.Container, error) {
fi, err := l.sftpClient.Stat(filepath.Join(l.config.basePath, id))
fi, err := l.sftpClient.Stat(path.Join(l.config.basePath, id))
if err != nil {
if os.IsNotExist(err) {
return nil, stow.ErrNotFound
Expand All @@ -117,12 +117,12 @@ func (l *location) Container(id string) (stow.Container, error) {

// RemoveContainer removes a container by name.
func (l *location) RemoveContainer(id string) error {
return recurseRemove(l.sftpClient, filepath.Join(l.config.basePath, id))
return recurseRemove(l.sftpClient, path.Join(l.config.basePath, id))
}

// recurseRemove recursively purges content from a path.
func recurseRemove(client *sftp.Client, path string) error {
infos, err := client.ReadDir(path)
func recurseRemove(client *sftp.Client, p string) error {
infos, err := client.ReadDir(p)
if err != nil {
return err
}
Expand All @@ -131,12 +131,12 @@ func recurseRemove(client *sftp.Client, path string) error {
if !v.IsDir() {
return errors.Errorf("directory not empty - %q", v.Name())
}
if err := recurseRemove(client, filepath.Join(path, v.Name())); err != nil {
if err := recurseRemove(client, path.Join(p, v.Name())); err != nil {
return err
}
}

return client.RemoveDirectory(path)
return client.RemoveDirectory(p)
}

// ItemByURL retrieves a stow.Item by parsing the URL.
Expand Down