1 // Copyright © 2015 Jerry Jacobs <jerry.jacobs@xor-gate.org>.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
21 "github.com/spf13/afero"
24 // Fs is a afero.Fs implementation that uses functions provided by the sftp package.
26 // For details in any method, check the documentation of the sftp package
27 // (github.com/pkg/sftp).
32 func New(client *sftp.Client) afero.Fs {
33 return &Fs{client: client}
36 func (s Fs) Name() string { return "sftpfs" }
38 func (s Fs) Create(name string) (afero.File, error) {
39 return FileCreate(s.client, name)
42 func (s Fs) Mkdir(name string, perm os.FileMode) error {
43 err := s.client.Mkdir(name)
47 return s.client.Chmod(name, perm)
50 func (s Fs) MkdirAll(path string, perm os.FileMode) error {
51 // Fast path: if we can tell whether path is a directory or file, stop with success or error.
52 dir, err := s.Stat(path)
60 // Slow path: make sure parent exists and then call Mkdir for path.
62 for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
67 for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
73 err = s.MkdirAll(path[0:j-1], perm)
79 // Parent now exists; invoke Mkdir and use its result.
80 err = s.Mkdir(path, perm)
82 // Handle arguments like "foo/." by
83 // double-checking that directory doesn't exist.
84 dir, err1 := s.Lstat(path)
85 if err1 == nil && dir.IsDir() {
93 func (s Fs) Open(name string) (afero.File, error) {
94 return FileOpen(s.client, name)
97 func (s Fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
101 func (s Fs) Remove(name string) error {
102 return s.client.Remove(name)
105 func (s Fs) RemoveAll(path string) error {
106 // TODO have a look at os.RemoveAll
107 // https://github.com/golang/go/blob/master/src/os/path.go#L66
111 func (s Fs) Rename(oldname, newname string) error {
112 return s.client.Rename(oldname, newname)
115 func (s Fs) Stat(name string) (os.FileInfo, error) {
116 return s.client.Stat(name)
119 func (s Fs) Lstat(p string) (os.FileInfo, error) {
120 return s.client.Lstat(p)
123 func (s Fs) Chmod(name string, mode os.FileMode) error {
124 return s.client.Chmod(name, mode)
127 func (s Fs) Chtimes(name string, atime time.Time, mtime time.Time) error {
128 return s.client.Chtimes(name, atime, mtime)