Skip to content

Commit c903adf

Browse files
authored
Add Symlink, Lstat and ReadLink function for the Driver interface (#229)
1 parent d446cdf commit c903adf

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

driver/driver.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type Driver interface {
1717
MkdirAll(path string) error
1818
// Create creates the named file
1919
Create(path string) (err error)
20+
// Symlink create a symbolic link
21+
Symlink(oldname, newname string) error
2022
// Remove removes the specified file or directory
2123
Remove(path string) error
2224
// Rename renames a file
@@ -27,10 +29,14 @@ type Driver interface {
2729
WalkDir(root string, fn fs.WalkDirFunc) error
2830
// Open opens the named file for reading
2931
Open(path string) (f http.File, err error)
30-
// Stat returns the os.FileInfo describing the named file
32+
// Stat returns the os.FileInfo describing the named file, if path is a symbolic link, read the real file
3133
Stat(path string) (fi os.FileInfo, err error)
34+
// Lstat returns the os.FileInfo describing the named file, if path is a symbolic link, read the symbolic link info
35+
Lstat(path string) (fi os.FileInfo, err error)
3236
// GetFileTime get the creation time, last access time, last modify time of the path
3337
GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error)
3438
// Write write src file to dest file
3539
Write(src string, dest string) error
40+
// ReadLink returns the destination of the named symbolic link
41+
ReadLink(path string) (string, error)
3642
}

driver/minio/minio.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/minio/minio-go/v7/pkg/credentials"
1919
"github.com/minio/minio-go/v7/pkg/s3utils"
2020
"github.com/no-src/gofs/driver"
21+
nsfs "github.com/no-src/gofs/fs"
2122
"github.com/no-src/gofs/internal/rate"
2223
"github.com/no-src/gofs/retry"
2324
"github.com/no-src/log"
@@ -144,6 +145,18 @@ func (c *minIODriver) Create(path string) (err error) {
144145
return err
145146
}
146147

148+
func (c *minIODriver) Symlink(oldname, newname string) (err error) {
149+
if err = c.Remove(newname); err != nil {
150+
return err
151+
}
152+
err = c.reconnectIfLost(func() error {
153+
content := nsfs.SymlinkText(oldname)
154+
_, err = c.client.PutObject(c.ctx, c.bucketName, newname, bytes.NewReader([]byte(content)), int64(len(content)), minio.PutObjectOptions{})
155+
return err
156+
})
157+
return err
158+
}
159+
147160
func (c *minIODriver) Remove(path string) (err error) {
148161
return c.reconnectIfLost(func() error {
149162
infoChan := c.client.ListObjects(c.ctx, c.bucketName, minio.ListObjectsOptions{
@@ -250,6 +263,10 @@ func (c *minIODriver) Stat(path string) (fi os.FileInfo, err error) {
250263
return fi, err
251264
}
252265

266+
func (c *minIODriver) Lstat(path string) (fi os.FileInfo, err error) {
267+
return c.Stat(path)
268+
}
269+
253270
func (c *minIODriver) GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error) {
254271
err = c.reconnectIfLost(func() error {
255272
var info minio.ObjectInfo
@@ -288,6 +305,10 @@ func (c *minIODriver) Client() *minio.Client {
288305
return c.client
289306
}
290307

308+
func (c *minIODriver) ReadLink(path string) (string, error) {
309+
return path, nil
310+
}
311+
291312
// fPutObject - Create an object in a bucket, with contents from file at filePath. Allows request cancellation.
292313
// Keep up to date with the minio.Client.FPutObject.
293314
func (c *minIODriver) fPutObject(ctx context.Context, bucketName, objectName, filePath string, opts minio.PutObjectOptions) (info minio.UploadInfo, err error) {

driver/sftp/sftp.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,18 @@ func (sc *sftpDriver) Create(path string) (err error) {
172172
return err
173173
}
174174

175+
func (sc *sftpDriver) Symlink(oldname, newname string) error {
176+
if err := sc.Remove(newname); err != nil {
177+
return err
178+
}
179+
return sc.reconnectIfLost(func() error {
180+
return sc.client.Symlink(oldname, newname)
181+
})
182+
}
183+
175184
func (sc *sftpDriver) Remove(path string) error {
176185
return sc.reconnectIfLost(func() error {
177-
f, err := sc.client.Stat(path)
186+
f, err := sc.client.Lstat(path)
178187
if os.IsNotExist(err) {
179188
return nil
180189
}
@@ -260,10 +269,18 @@ func (sc *sftpDriver) Stat(path string) (fi os.FileInfo, err error) {
260269
return fi, err
261270
}
262271

272+
func (sc *sftpDriver) Lstat(path string) (fi os.FileInfo, err error) {
273+
err = sc.reconnectIfLost(func() error {
274+
fi, err = sc.client.Lstat(path)
275+
return err
276+
})
277+
return fi, err
278+
}
279+
263280
func (sc *sftpDriver) GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error) {
264281
err = sc.reconnectIfLost(func() error {
265282
var fi os.FileInfo
266-
fi, err = sc.client.Stat(path)
283+
fi, err = sc.client.Lstat(path)
267284
if err != nil {
268285
return err
269286
}
@@ -315,6 +332,14 @@ func (sc *sftpDriver) Write(src string, dest string) (err error) {
315332
return err
316333
}
317334

335+
func (sc *sftpDriver) ReadLink(path string) (realPath string, err error) {
336+
err = sc.reconnectIfLost(func() error {
337+
realPath, err = sc.client.ReadLink(path)
338+
return err
339+
})
340+
return realPath, err
341+
}
342+
318343
type statDirEntry struct {
319344
info fs.FileInfo
320345
}

0 commit comments

Comments
 (0)