未验证 提交 c903adf2 编写于 作者: M mstmdev 提交者: GitHub

Add Symlink, Lstat and ReadLink function for the Driver interface (#229)

上级 d446cdf9
...@@ -17,6 +17,8 @@ type Driver interface { ...@@ -17,6 +17,8 @@ type Driver interface {
MkdirAll(path string) error MkdirAll(path string) error
// Create creates the named file // Create creates the named file
Create(path string) (err error) Create(path string) (err error)
// Symlink create a symbolic link
Symlink(oldname, newname string) error
// Remove removes the specified file or directory // Remove removes the specified file or directory
Remove(path string) error Remove(path string) error
// Rename renames a file // Rename renames a file
...@@ -27,10 +29,14 @@ type Driver interface { ...@@ -27,10 +29,14 @@ type Driver interface {
WalkDir(root string, fn fs.WalkDirFunc) error WalkDir(root string, fn fs.WalkDirFunc) error
// Open opens the named file for reading // Open opens the named file for reading
Open(path string) (f http.File, err error) Open(path string) (f http.File, err error)
// Stat returns the os.FileInfo describing the named file // Stat returns the os.FileInfo describing the named file, if path is a symbolic link, read the real file
Stat(path string) (fi os.FileInfo, err error) Stat(path string) (fi os.FileInfo, err error)
// Lstat returns the os.FileInfo describing the named file, if path is a symbolic link, read the symbolic link info
Lstat(path string) (fi os.FileInfo, err error)
// GetFileTime get the creation time, last access time, last modify time of the path // GetFileTime get the creation time, last access time, last modify time of the path
GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error) GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error)
// Write write src file to dest file // Write write src file to dest file
Write(src string, dest string) error Write(src string, dest string) error
// ReadLink returns the destination of the named symbolic link
ReadLink(path string) (string, error)
} }
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/s3utils" "github.com/minio/minio-go/v7/pkg/s3utils"
"github.com/no-src/gofs/driver" "github.com/no-src/gofs/driver"
nsfs "github.com/no-src/gofs/fs"
"github.com/no-src/gofs/internal/rate" "github.com/no-src/gofs/internal/rate"
"github.com/no-src/gofs/retry" "github.com/no-src/gofs/retry"
"github.com/no-src/log" "github.com/no-src/log"
...@@ -144,6 +145,18 @@ func (c *minIODriver) Create(path string) (err error) { ...@@ -144,6 +145,18 @@ func (c *minIODriver) Create(path string) (err error) {
return err return err
} }
func (c *minIODriver) Symlink(oldname, newname string) (err error) {
if err = c.Remove(newname); err != nil {
return err
}
err = c.reconnectIfLost(func() error {
content := nsfs.SymlinkText(oldname)
_, err = c.client.PutObject(c.ctx, c.bucketName, newname, bytes.NewReader([]byte(content)), int64(len(content)), minio.PutObjectOptions{})
return err
})
return err
}
func (c *minIODriver) Remove(path string) (err error) { func (c *minIODriver) Remove(path string) (err error) {
return c.reconnectIfLost(func() error { return c.reconnectIfLost(func() error {
infoChan := c.client.ListObjects(c.ctx, c.bucketName, minio.ListObjectsOptions{ 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) { ...@@ -250,6 +263,10 @@ func (c *minIODriver) Stat(path string) (fi os.FileInfo, err error) {
return fi, err return fi, err
} }
func (c *minIODriver) Lstat(path string) (fi os.FileInfo, err error) {
return c.Stat(path)
}
func (c *minIODriver) GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error) { func (c *minIODriver) GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error) {
err = c.reconnectIfLost(func() error { err = c.reconnectIfLost(func() error {
var info minio.ObjectInfo var info minio.ObjectInfo
...@@ -288,6 +305,10 @@ func (c *minIODriver) Client() *minio.Client { ...@@ -288,6 +305,10 @@ func (c *minIODriver) Client() *minio.Client {
return c.client return c.client
} }
func (c *minIODriver) ReadLink(path string) (string, error) {
return path, nil
}
// fPutObject - Create an object in a bucket, with contents from file at filePath. Allows request cancellation. // fPutObject - Create an object in a bucket, with contents from file at filePath. Allows request cancellation.
// Keep up to date with the minio.Client.FPutObject. // Keep up to date with the minio.Client.FPutObject.
func (c *minIODriver) fPutObject(ctx context.Context, bucketName, objectName, filePath string, opts minio.PutObjectOptions) (info minio.UploadInfo, err error) { func (c *minIODriver) fPutObject(ctx context.Context, bucketName, objectName, filePath string, opts minio.PutObjectOptions) (info minio.UploadInfo, err error) {
......
...@@ -172,9 +172,18 @@ func (sc *sftpDriver) Create(path string) (err error) { ...@@ -172,9 +172,18 @@ func (sc *sftpDriver) Create(path string) (err error) {
return err return err
} }
func (sc *sftpDriver) Symlink(oldname, newname string) error {
if err := sc.Remove(newname); err != nil {
return err
}
return sc.reconnectIfLost(func() error {
return sc.client.Symlink(oldname, newname)
})
}
func (sc *sftpDriver) Remove(path string) error { func (sc *sftpDriver) Remove(path string) error {
return sc.reconnectIfLost(func() error { return sc.reconnectIfLost(func() error {
f, err := sc.client.Stat(path) f, err := sc.client.Lstat(path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
} }
...@@ -260,10 +269,18 @@ func (sc *sftpDriver) Stat(path string) (fi os.FileInfo, err error) { ...@@ -260,10 +269,18 @@ func (sc *sftpDriver) Stat(path string) (fi os.FileInfo, err error) {
return fi, err return fi, err
} }
func (sc *sftpDriver) Lstat(path string) (fi os.FileInfo, err error) {
err = sc.reconnectIfLost(func() error {
fi, err = sc.client.Lstat(path)
return err
})
return fi, err
}
func (sc *sftpDriver) GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error) { func (sc *sftpDriver) GetFileTime(path string) (cTime time.Time, aTime time.Time, mTime time.Time, err error) {
err = sc.reconnectIfLost(func() error { err = sc.reconnectIfLost(func() error {
var fi os.FileInfo var fi os.FileInfo
fi, err = sc.client.Stat(path) fi, err = sc.client.Lstat(path)
if err != nil { if err != nil {
return err return err
} }
...@@ -315,6 +332,14 @@ func (sc *sftpDriver) Write(src string, dest string) (err error) { ...@@ -315,6 +332,14 @@ func (sc *sftpDriver) Write(src string, dest string) (err error) {
return err return err
} }
func (sc *sftpDriver) ReadLink(path string) (realPath string, err error) {
err = sc.reconnectIfLost(func() error {
realPath, err = sc.client.ReadLink(path)
return err
})
return realPath, err
}
type statDirEntry struct { type statDirEntry struct {
info fs.FileInfo info fs.FileInfo
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册