file.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. package sftpfs
  14. import (
  15. "os"
  16. "github.com/pkg/sftp"
  17. )
  18. type File struct {
  19. fd *sftp.File
  20. }
  21. func FileOpen(s *sftp.Client, name string) (*File, error) {
  22. fd, err := s.Open(name)
  23. if err != nil {
  24. return &File{}, err
  25. }
  26. return &File{fd: fd}, nil
  27. }
  28. func FileCreate(s *sftp.Client, name string) (*File, error) {
  29. fd, err := s.Create(name)
  30. if err != nil {
  31. return &File{}, err
  32. }
  33. return &File{fd: fd}, nil
  34. }
  35. func (f *File) Close() error {
  36. return f.fd.Close()
  37. }
  38. func (f *File) Name() string {
  39. return f.fd.Name()
  40. }
  41. func (f *File) Stat() (os.FileInfo, error) {
  42. return f.fd.Stat()
  43. }
  44. func (f *File) Sync() error {
  45. return nil
  46. }
  47. func (f *File) Truncate(size int64) error {
  48. return f.fd.Truncate(size)
  49. }
  50. func (f *File) Read(b []byte) (n int, err error) {
  51. return f.fd.Read(b)
  52. }
  53. // TODO
  54. func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
  55. return 0,nil
  56. }
  57. // TODO
  58. func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
  59. return nil,nil
  60. }
  61. // TODO
  62. func (f *File) Readdirnames(n int) (names []string, err error) {
  63. return nil,nil
  64. }
  65. func (f *File) Seek(offset int64, whence int) (int64, error) {
  66. return f.fd.Seek(offset, whence)
  67. }
  68. func (f *File) Write(b []byte) (n int, err error) {
  69. return f.fd.Write(b)
  70. }
  71. // TODO
  72. func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
  73. return 0,nil
  74. }
  75. func (f *File) WriteString(s string) (ret int, err error) {
  76. return f.fd.Write([]byte(s))
  77. }