-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
nfs_sftpfs.go
181 lines (147 loc) · 3.67 KB
/
nfs_sftpfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2024 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package sshlib
import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/helper/chroot"
"github.com/go-git/go-billy/v5/helper/temporal"
"github.com/pkg/sftp"
)
// sftpFile is a wrapper around sftp.File that implements the billy.File interface
type sftpFile struct {
*sftp.File
mx sync.Mutex
}
func (f *sftpFile) Lock() error {
f.mx.Lock()
return nil
}
func (f *sftpFile) Unlock() error {
f.mx.Unlock()
return nil
}
func NewChangeSFTPFS(client *sftp.Client, base string) billy.Filesystem {
return temporal.New(
chroot.New(&SFTPFS{Client: client}, base),
"",
)
}
type SFTPFS struct {
billy.Filesystem
Client *sftp.Client
}
// Create
func (fs *SFTPFS) Create(filename string) (billy.File, error) {
return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
}
// OpenFile
func (fs *SFTPFS) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
return fs.openFile(filename, flag, perm, fs.createDir)
}
func (fs *SFTPFS) openFile(fn string, flag int, perm os.FileMode, createDir func(string) error) (billy.File, error) {
if flag&os.O_CREATE != 0 {
if createDir == nil {
return nil, fmt.Errorf("createDir func cannot be nil if file needs to be opened in create mode")
}
if err := createDir(fn); err != nil {
return nil, err
}
}
f, err := fs.Client.OpenFile(fn, flag)
if err != nil {
return nil, err
}
return &sftpFile{File: f}, nil
}
func (fs *SFTPFS) createDir(fullpath string) error {
dir := filepath.Dir(fullpath)
if dir != "." {
if err := fs.Client.MkdirAll(dir); err != nil {
return err
}
}
return nil
}
// ReadDir
func (fs *SFTPFS) ReadDir(path string) ([]os.FileInfo, error) {
l, err := fs.Client.ReadDir(path)
if err != nil {
return nil, err
}
var s = make([]os.FileInfo, len(l))
for i, f := range l {
s[i] = f
}
return s, nil
}
// Rename
func (fs *SFTPFS) Rename(from, to string) error {
return fs.Client.Rename(from, to)
}
// MkdirAll
func (fs *SFTPFS) MkdirAll(filename string, perm os.FileMode) error {
err := fs.Client.MkdirAll(filename)
if err != nil {
return err
}
return nil
}
// Open
func (fs *SFTPFS) Open(filename string) (billy.File, error) {
f, err := fs.Client.Open(filename)
if err != nil {
return nil, err
}
return &sftpFile{File: f}, nil
}
// Stat
func (fs *SFTPFS) Stat(filename string) (os.FileInfo, error) {
return fs.Client.Stat(filepath.Clean(filename))
}
// Remove
func (fs *SFTPFS) Remove(filename string) error {
return fs.Client.Remove(filename)
}
// TempFile
func (fs *SFTPFS) TempFile(dir, prefix string) (billy.File, error) {
if err := fs.createDir(dir + string(os.PathSeparator)); err != nil {
return nil, err
}
tempFileName := prefix + strconv.FormatInt(time.Now().UnixNano(), 10)
tempFilePath := filepath.Join(dir, tempFileName)
f, err := fs.Create(tempFilePath)
if err != nil {
return nil, err
}
return f, nil
}
func (fs *SFTPFS) Join(elem ...string) string {
return filepath.Join(elem...)
}
// RemoveAll
func (fs *SFTPFS) RemoveAll(filename string) error {
return fs.Client.RemoveAll(filename)
}
// Lstat
func (fs *SFTPFS) Lstat(filename string) (os.FileInfo, error) {
return fs.Client.Lstat(filepath.Clean(filename))
}
// Symlink
func (fs *SFTPFS) Symlink(target, link string) error {
return fs.Client.Symlink(target, link)
}
// Readlink
func (fs *SFTPFS) Readlink(link string) (string, error) {
return fs.Client.ReadLink(link)
}
// Capabilities
func (fs *SFTPFS) Capabilities() billy.Capability {
return billy.DefaultCapabilities
}