Skip to content

Commit

Permalink
user pointer to FileInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Nov 9, 2023
1 parent 982a64e commit 79a9c50
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 78 deletions.
8 changes: 4 additions & 4 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func IdenticalDirContents(ctx context.Context, dirA, dirB File, recursive bool)
return true, nil
}

fileInfosA := make(map[string]FileInfo)
err = dirA.ListDirInfoContext(ctx, func(info FileInfo) error {
fileInfosA := make(map[string]*FileInfo)
err = dirA.ListDirInfoContext(ctx, func(info *FileInfo) error {
if !info.IsDir || recursive {
fileInfosA[info.Name] = info
}
Expand All @@ -96,9 +96,9 @@ func IdenticalDirContents(ctx context.Context, dirA, dirB File, recursive bool)
return false, fmt.Errorf("IdenticalDirContents: error listing dirA %q: %w", dirA, err)
}

fileInfosB := make(map[string]FileInfo, len(fileInfosA))
fileInfosB := make(map[string]*FileInfo, len(fileInfosA))
hasDiff := errors.New("hasDiff")
err = dirB.ListDirInfoContext(ctx, func(info FileInfo) error {
err = dirB.ListDirInfoContext(ctx, func(info *FileInfo) error {
if !info.IsDir || recursive {
infoA, found := fileInfosA[info.Name]
if !found || info.Size != infoA.Size || info.IsDir != infoA.IsDir {
Expand Down
4 changes: 2 additions & 2 deletions compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func writeEmptyFile(file File) error {

func deleteRandomFileInDir(dir File) error {
var files []File
err := dir.ListDirInfo(func(info FileInfo) error {
err := dir.ListDirInfo(func(info *FileInfo) error {
if !info.IsDir {
files = append(files, info.File)
}
Expand All @@ -49,7 +49,7 @@ func deleteRandomFileInDir(dir File) error {

func deleteRandomSubDir(dir File) error {
var dirs []File
err := dir.ListDirInfo(func(info FileInfo) error {
err := dir.ListDirInfo(func(info *FileInfo) error {
if info.IsDir {
dirs = append(dirs, info.File)
}
Expand Down
38 changes: 21 additions & 17 deletions dropboxfs/dropboxfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ func (dbfs *DropboxFileSystem) AbsPath(filePath string) string {
return path.Clean(filePath)
}

func metadataToFileInfo(meta *dropbox.Metadata) (info fs.FileInfo) {
func metadataToFileInfo(meta *dropbox.Metadata) *fs.FileInfo {
var info fs.FileInfo
info.Name = meta.Name
info.Exists = true
info.IsRegular = true
Expand All @@ -156,23 +157,26 @@ func metadataToFileInfo(meta *dropbox.Metadata) (info fs.FileInfo) {
info.Permissions = DefaultPermissions
}
// info.ContentHash = meta.ContentHash
return info
return &info
}

// info returns FileInfo
func (dbfs *DropboxFileSystem) info(filePath string) (info fs.FileInfo) {
func (dbfs *DropboxFileSystem) info(filePath string) *fs.FileInfo {

// The root folder is unsupported by the API
if filePath == "/" {
// info.Name = ""
info.Exists = true
info.IsRegular = true
info.IsDir = true
info.Permissions = DefaultDirPermissions
return info

return &fs.FileInfo{
Name: "",
Exists: true,
IsRegular: true,
IsDir: true,
Permissions: DefaultDirPermissions,
}
}

if cachedInfo, ok := dbfs.fileInfoCache.Get(filePath); ok {
return *cachedInfo
return cachedInfo
}

meta, err := dbfs.client.Files.GetMetadata(
Expand All @@ -183,11 +187,11 @@ func (dbfs *DropboxFileSystem) info(filePath string) (info fs.FileInfo) {
if err != nil {
dbfs.fileInfoCache.Delete(filePath)
// fmt.Println(meta, err)
return fs.FileInfo{}
return new(fs.FileInfo)
}
info = metadataToFileInfo(&meta.Metadata)
info := metadataToFileInfo(&meta.Metadata)
if dbfs.fileInfoCache != nil {
dbfs.fileInfoCache.Put(filePath, &info)
dbfs.fileInfoCache.Put(filePath, info)
}
return info
}
Expand All @@ -213,7 +217,7 @@ func (dbfs *DropboxFileSystem) IsSymbolicLink(filePath string) bool {
return false
}

func (dbfs *DropboxFileSystem) listDirInfo(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string, recursive bool) (err error) {
func (dbfs *DropboxFileSystem) listDirInfo(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string, recursive bool) (err error) {
if ctx.Err() != nil {
return ctx.Err()
}
Expand Down Expand Up @@ -261,7 +265,7 @@ func (dbfs *DropboxFileSystem) listDirInfo(ctx context.Context, dirPath string,
if match {
info := metadataToFileInfo(entry)
if dbfs.fileInfoCache != nil {
dbfs.fileInfoCache.Put(entry.PathDisplay, &info)
dbfs.fileInfoCache.Put(entry.PathDisplay, info)
}
// file := dbfs.File(entry.PathDisplay)
err = callback(info)
Expand All @@ -278,11 +282,11 @@ func (dbfs *DropboxFileSystem) listDirInfo(ctx context.Context, dirPath string,
return nil
}

func (dbfs *DropboxFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) (err error) {
func (dbfs *DropboxFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) (err error) {
return dbfs.listDirInfo(ctx, dirPath, callback, patterns, true)
}

func (dbfs *DropboxFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) (err error) {
func (dbfs *DropboxFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) (err error) {
return dbfs.listDirInfo(ctx, dirPath, callback, patterns, true)
}

Expand Down
4 changes: 2 additions & 2 deletions dropboxfs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ replace github.com/ungerik/go-fs => ../
require github.com/tj/go-dropbox v0.0.0-20171107035848-42dd2be3662d

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/segmentio/go-env v1.1.0 // indirect
github.com/ungerik/go-dry v0.0.0-20230805093253-df9da4cd3437 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)
7 changes: 2 additions & 5 deletions dropboxfs/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/go-env v1.1.0 h1:AGJ7OnCx9M5NWpkYPGYELS6III/pFSnAs1GvKWStiEo=
Expand All @@ -12,8 +11,6 @@ github.com/tj/go-dropbox v0.0.0-20171107035848-42dd2be3662d h1:kc+jLVc4Ivy9I77bY
github.com/tj/go-dropbox v0.0.0-20171107035848-42dd2be3662d/go.mod h1:+zP9ykDCb5wHDCWHCuLZ2YhDAiy42yV+HAmI2BIocBI=
github.com/ungerik/go-dry v0.0.0-20230805093253-df9da4cd3437 h1:dQYWi16lkE/DZeZxJzyfQlgLzXJQ2cChrty8VZjmoIc=
github.com/ungerik/go-dry v0.0.0-20230805093253-df9da4cd3437/go.mod h1:g61b/Pvp64yQ4oYVbcdA7qqzn1RcQIHZQuhWOVG1VHk=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 5 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (file File) Stat() (iofs.FileInfo, error) {
// Info returns FileInfo.
//
// Use File.Stat to get a standard library io/fs.FileInfo.
func (file File) Info() FileInfo {
func (file File) Info() *FileInfo {
fileSystem, path := file.ParseRawURI()
info, err := fileSystem.Stat(path)
if err != nil {
Expand Down Expand Up @@ -422,7 +422,7 @@ func (file File) ListDirContext(ctx context.Context, callback func(File) error,
// ListDirInfo calls the passed callback function for every file and directory in dirPath.
// If any patterns are passed, then only files with a name that matches
// at least one of the patterns are returned.
func (file File) ListDirInfo(callback func(FileInfo) error, patterns ...string) error {
func (file File) ListDirInfo(callback func(*FileInfo) error, patterns ...string) error {
if file == "" {
return ErrEmptyPath
}
Expand All @@ -433,7 +433,7 @@ func (file File) ListDirInfo(callback func(FileInfo) error, patterns ...string)
// ListDirInfoContext calls the passed callback function for every file and directory in dirPath.
// If any patterns are passed, then only files with a name that matches
// at least one of the patterns are returned.
func (file File) ListDirInfoContext(ctx context.Context, callback func(FileInfo) error, patterns ...string) error {
func (file File) ListDirInfoContext(ctx context.Context, callback func(*FileInfo) error, patterns ...string) error {
if file == "" {
return ErrEmptyPath
}
Expand Down Expand Up @@ -465,7 +465,7 @@ func (file File) ListDirRecursiveContext(ctx context.Context, callback func(File
// recursing into all sub-directories.
// If any patterns are passed, then only files (not directories) with a name that matches
// at least one of the patterns are returned.
func (file File) ListDirInfoRecursive(callback func(FileInfo) error, patterns ...string) error {
func (file File) ListDirInfoRecursive(callback func(*FileInfo) error, patterns ...string) error {
if file == "" {
return ErrEmptyPath
}
Expand All @@ -477,7 +477,7 @@ func (file File) ListDirInfoRecursive(callback func(FileInfo) error, patterns ..
// recursing into all sub-directories.
// If any patterns are passed, then only files (not directories) with a name that matches
// at least one of the patterns are returned.
func (file File) ListDirInfoRecursiveContext(ctx context.Context, callback func(FileInfo) error, patterns ...string) error {
func (file File) ListDirInfoRecursiveContext(ctx context.Context, callback func(*FileInfo) error, patterns ...string) error {
if file == "" {
return ErrEmptyPath
}
Expand Down
20 changes: 16 additions & 4 deletions fileinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fs

import (
"errors"
iofs "io/fs"
"os"
"time"
Expand All @@ -21,14 +22,25 @@ type FileInfo struct {
Permissions Permissions
}

// Validate returns an error if the FileInfo is invalid.
func (i *FileInfo) Validate() error {
if i == nil {
return errors.New("<nil> FileInfo")
}
if i.File == "" || i.Name == "" {
return ErrEmptyPath
}
return nil
}

// NewFileInfo returns a FileInfo using the
// data from an io/fs.FileInfo as snapshot
// of an existing file.
// Use NewNonExistingFileInfo to get
// a FileInfo for non existing file.
func NewFileInfo(file File, info iofs.FileInfo, hidden bool) FileInfo {
func NewFileInfo(file File, info iofs.FileInfo, hidden bool) *FileInfo {
mode := info.Mode()
return FileInfo{
return &FileInfo{
File: file,
Name: info.Name(),
Exists: true,
Expand All @@ -46,9 +58,9 @@ func NewFileInfo(file File, info iofs.FileInfo, hidden bool) FileInfo {
// FileInfo.Exists will be false, but the
// file may exist at any point of time.
// IsHidden will be true if the name starts with a dot.
func NewNonExistingFileInfo(file File) FileInfo {
func NewNonExistingFileInfo(file File) *FileInfo {
name := file.Name()
return FileInfo{
return &FileInfo{
File: file,
Name: name,
Exists: false,
Expand Down
4 changes: 2 additions & 2 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ type FileSystem interface {
// ListDirInfo calls the passed callback function for every file and directory in dirPath.
// If any patterns are passed, then only files or directores with a name that matches
// at least one of the patterns are returned.
ListDirInfo(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) error
ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error

// TODO
// ListDirInfoRecursive calls the passed callback function for every file (not directory) in dirPath
// recursing into all sub-directories.
// If any patterns are passed, then only files (not directories) with a name that matches
// at least one of the patterns are returned.
ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) error
ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error

// TODO
// ListDirMax returns at most max files and directories in dirPath.
Expand Down
8 changes: 4 additions & 4 deletions ftpfs/ftpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ func (i fileInfo) ModTime() time.Time { return i.entry.Time }
func (i fileInfo) IsDir() bool { return i.entry.Type == ftp.EntryTypeFolder }
func (i fileInfo) Sys() any { return nil }

func entryToFileInfo(entry *ftp.Entry, file fs.File) fs.FileInfo {
return fs.FileInfo{
func entryToFileInfo(entry *ftp.Entry, file fs.File) *fs.FileInfo {
return &fs.FileInfo{
File: file,
Name: entry.Name,
Exists: true,
Expand Down Expand Up @@ -233,7 +233,7 @@ func (f *FTPFileSystem) IsSymbolicLink(filePath string) bool {
return entry.Type == ftp.EntryTypeLink
}

func (f *FTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) error {
func (f *FTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
conn, dirPath, release, err := f.getConn(dirPath)
if err != nil {
return err
Expand All @@ -253,7 +253,7 @@ func (f *FTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callbac
return nil
}

func (f *FTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) error {
func (f *FTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
return fmt.Errorf("FTPFileSystem.ListDirInfoRecursive: %w", errors.ErrUnsupported)
}

Expand Down
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ go 1.21.0
use (
.
./dropboxfs
./sftpfs
./ftpfs
./s3fs
./sftpfs
)
4 changes: 2 additions & 2 deletions httpfs/httpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func (f *HTTPFileSystem) Exists(filePath string) bool {
func (f *HTTPFileSystem) IsHidden(filePath string) bool { return false }
func (f *HTTPFileSystem) IsSymbolicLink(filePath string) bool { return false }

func (f *HTTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) error {
func (f *HTTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
return fs.NewErrUnsupported(f, "ListDirInfo")
}

func (f *HTTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) error {
func (f *HTTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
return fs.NewErrUnsupported(f, "ListDirInfoRecursive")
}

Expand Down
4 changes: 2 additions & 2 deletions invalidfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ func (InvalidFileSystem) IsSymbolicLink(filePath string) bool {
return false
}

func (InvalidFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) error {
func (InvalidFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error {
return ErrInvalidFileSystem
}

func (InvalidFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) error {
func (InvalidFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error {
return ErrInvalidFileSystem
}

Expand Down
4 changes: 2 additions & 2 deletions listdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func ListDirMaxImpl(ctx context.Context, max int, listDir func(ctx context.Conte
// ListDirInfoRecursiveImpl can be used by FileSystem implementations to
// implement FileSystem.ListDirRecursive if it doesn't have an internal
// optimzed implementation for it.
func ListDirInfoRecursiveImpl(ctx context.Context, fs FileSystem, dirPath string, callback func(FileInfo) error, patterns []string) error {
func ListDirInfoRecursiveImpl(ctx context.Context, fs FileSystem, dirPath string, callback func(*FileInfo) error, patterns []string) error {
return fs.ListDirInfo(
ctx,
dirPath,
func(info FileInfo) error {
func(info *FileInfo) error {
if info.IsDir {
err := info.File.ListDirInfoRecursiveContext(ctx, callback, patterns...)
// Don't mind files that have been deleted while iterating
Expand Down
4 changes: 2 additions & 2 deletions localfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (local *LocalFileSystem) ReadSymbolicLink(file File) (linked File, err erro
return File(linkedPath), nil
}

func (local *LocalFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) (err error) {
func (local *LocalFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) (err error) {
if ctx.Err() != nil {
return ctx.Err()
}
Expand Down Expand Up @@ -321,7 +321,7 @@ func (local *LocalFileSystem) ListDirInfo(ctx context.Context, dirPath string, c
return nil
}

func (local *LocalFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) error {
func (local *LocalFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error {
if dirPath == "" {
return ErrEmptyPath
}
Expand Down
4 changes: 2 additions & 2 deletions memfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ func (*MemFileSystem) IsSymbolicLink(filePath string) bool {
return false
}

func (*MemFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) error {
func (*MemFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error {
return nil
}

func (*MemFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(FileInfo) error, patterns []string) error {
func (*MemFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error {
return nil
}

Expand Down
Loading

0 comments on commit 79a9c50

Please sign in to comment.