github.com/bpfs/defs@v0.0.15/afero/sftpfs/file.go (about) 1 // Copyright © 2015 Jerry Jacobs <jerry.jacobs@xor-gate.org>. 2 // 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 7 // 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. 13 14 package sftpfs 15 16 import ( 17 "os" 18 19 "github.com/pkg/sftp" 20 ) 21 22 type File struct { 23 client *sftp.Client 24 fd *sftp.File 25 } 26 27 func FileOpen(s *sftp.Client, name string) (*File, error) { 28 fd, err := s.Open(name) 29 if err != nil { 30 return &File{}, err 31 } 32 return &File{fd: fd, client: s}, nil 33 } 34 35 func FileCreate(s *sftp.Client, name string) (*File, error) { 36 fd, err := s.Create(name) 37 if err != nil { 38 return &File{}, err 39 } 40 return &File{fd: fd, client: s}, nil 41 } 42 43 func (f *File) Close() error { 44 return f.fd.Close() 45 } 46 47 func (f *File) Name() string { 48 return f.fd.Name() 49 } 50 51 func (f *File) Stat() (os.FileInfo, error) { 52 return f.fd.Stat() 53 } 54 55 func (f *File) Sync() error { 56 return nil 57 } 58 59 func (f *File) Truncate(size int64) error { 60 return f.fd.Truncate(size) 61 } 62 63 func (f *File) Read(b []byte) (n int, err error) { 64 return f.fd.Read(b) 65 } 66 67 func (f *File) ReadAt(b []byte, off int64) (n int, err error) { 68 return f.fd.ReadAt(b, off) 69 } 70 71 func (f *File) Readdir(count int) (res []os.FileInfo, err error) { 72 res, err = f.client.ReadDir(f.Name()) 73 if err != nil { 74 return 75 } 76 if count > 0 { 77 if len(res) > count { 78 res = res[:count] 79 } 80 } 81 return 82 } 83 84 func (f *File) Readdirnames(n int) (names []string, err error) { 85 data, err := f.Readdir(n) 86 if err != nil { 87 return nil, err 88 } 89 for _, v := range data { 90 names = append(names, v.Name()) 91 } 92 return 93 } 94 95 func (f *File) Seek(offset int64, whence int) (int64, error) { 96 return f.fd.Seek(offset, whence) 97 } 98 99 func (f *File) Write(b []byte) (n int, err error) { 100 return f.fd.Write(b) 101 } 102 103 // TODO 104 func (f *File) WriteAt(b []byte, off int64) (n int, err error) { 105 return 0, nil 106 } 107 108 func (f *File) WriteString(s string) (ret int, err error) { 109 return f.fd.Write([]byte(s)) 110 }