2021-04-06 20:19:11 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
package ulfs
|
2021-04-06 20:19:11 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-10 19:31:20 +01:00
|
|
|
"io"
|
2021-04-06 20:19:11 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-06-25 02:55:13 +01:00
|
|
|
"sort"
|
2023-02-03 16:02:14 +00:00
|
|
|
"strings"
|
2021-04-06 20:19:11 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2021-05-06 17:56:57 +01:00
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulloc"
|
2021-04-06 20:19:11 +01:00
|
|
|
)
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
// LocalBackendFile represents a file in the filesystem.
|
|
|
|
type LocalBackendFile interface {
|
|
|
|
io.Closer
|
|
|
|
io.ReaderAt
|
|
|
|
io.WriterAt
|
|
|
|
Name() string
|
|
|
|
Stat() (os.FileInfo, error)
|
|
|
|
Readdir(int) ([]os.FileInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalBackend abstracts what the Local filesystem interacts with.
|
|
|
|
type LocalBackend interface {
|
|
|
|
Create(name string) (LocalBackendFile, error)
|
|
|
|
MkdirAll(path string, perm os.FileMode) error
|
|
|
|
Open(name string) (LocalBackendFile, error)
|
|
|
|
Remove(name string) error
|
|
|
|
Rename(oldname, newname string) error
|
|
|
|
Stat(name string) (os.FileInfo, error)
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// Local implements something close to a filesystem but backed by the local disk.
|
2022-05-10 19:31:20 +01:00
|
|
|
type Local struct {
|
|
|
|
fs LocalBackend
|
|
|
|
}
|
2021-05-06 17:56:57 +01:00
|
|
|
|
|
|
|
// NewLocal constructs a Local filesystem.
|
2022-05-10 19:31:20 +01:00
|
|
|
func NewLocal(fs LocalBackend) *Local {
|
|
|
|
return &Local{
|
|
|
|
fs: fs,
|
|
|
|
}
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
2021-04-06 20:19:11 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// Open returns a read ReadHandle for the given local path.
|
2021-12-09 18:36:11 +00:00
|
|
|
func (l *Local) Open(ctx context.Context, path string) (MultiReadHandle, error) {
|
2022-05-10 19:31:20 +01:00
|
|
|
fh, err := l.fs.Open(path)
|
2021-04-06 20:19:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
2021-12-09 18:36:11 +00:00
|
|
|
return newOSMultiReadHandle(fh)
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// Create makes any directories necessary to create a file at path and returns a WriteHandle.
|
2021-12-09 19:03:42 +00:00
|
|
|
func (l *Local) Create(ctx context.Context, path string) (MultiWriteHandle, error) {
|
2022-05-10 19:31:20 +01:00
|
|
|
fi, err := l.fs.Stat(path)
|
2021-04-06 20:19:11 +01:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
} else if err == nil && fi.IsDir() {
|
|
|
|
return nil, errs.New("path exists as a directory already")
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
if err := l.fs.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
2021-04-06 20:19:11 +01:00
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: atomic rename
|
2022-05-10 19:31:20 +01:00
|
|
|
fh, err := l.fs.Create(path)
|
2021-04-06 20:19:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
2022-05-10 19:31:20 +01:00
|
|
|
return newOSMultiWriteHandle(l.fs, fh), nil
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:48:13 +01:00
|
|
|
// Move moves file to provided path.
|
|
|
|
func (l *Local) Move(ctx context.Context, oldpath, newpath string) error {
|
2022-05-10 19:31:20 +01:00
|
|
|
return l.fs.Rename(oldpath, newpath)
|
2021-10-05 17:48:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 10:07:19 +00:00
|
|
|
// Copy copies file to provided path.
|
|
|
|
func (l *Local) Copy(ctx context.Context, oldpath, newpath string) error {
|
|
|
|
return errs.New("not supported")
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:20:21 +01:00
|
|
|
// Remove unlinks the file at the path. It is not an error if the file does not exist.
|
2021-10-02 00:47:53 +01:00
|
|
|
func (l *Local) Remove(ctx context.Context, path string, opts *RemoveOptions) error {
|
|
|
|
if opts.isPending() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
if err := l.fs.Remove(path); os.IsNotExist(err) {
|
2021-05-14 20:20:21 +01:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
// List returns an ObjectIterator listing files and directories that have string prefix
|
2021-05-06 17:56:57 +01:00
|
|
|
// with the provided path.
|
2021-10-02 00:47:53 +01:00
|
|
|
func (l *Local) List(ctx context.Context, path string, opts *ListOptions) (ObjectIterator, error) {
|
|
|
|
if opts.isPending() {
|
|
|
|
return emptyObjectIterator{}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:02:14 +00:00
|
|
|
prefix := filepath.Clean(path)
|
|
|
|
if !strings.HasSuffix(prefix, string(filepath.Separator)) {
|
|
|
|
prefix += string(filepath.Separator)
|
|
|
|
}
|
2021-06-25 02:55:13 +01:00
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
var err error
|
2021-04-06 20:19:11 +01:00
|
|
|
var files []os.FileInfo
|
2022-05-10 19:31:20 +01:00
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
if opts.isRecursive() {
|
2022-05-10 19:31:20 +01:00
|
|
|
err = walk(l.fs, filepath.Clean(path), func(path string, info os.FileInfo, err error) error {
|
2021-04-06 20:19:11 +01:00
|
|
|
if err == nil && !info.IsDir() {
|
2021-06-22 23:41:22 +01:00
|
|
|
rel, err := filepath.Rel(prefix, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 20:19:11 +01:00
|
|
|
files = append(files, &namedFileInfo{
|
|
|
|
FileInfo: info,
|
2021-06-22 23:41:22 +01:00
|
|
|
name: rel,
|
2021-04-06 20:19:11 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
} else {
|
2022-05-10 19:31:20 +01:00
|
|
|
files, err = readdir(l.fs, prefix, -1)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return emptyObjectIterator{}, nil
|
|
|
|
}
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-05-10 19:31:20 +01:00
|
|
|
return nil, errs.Wrap(err)
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
sort.Slice(files, func(i, j int) bool {
|
|
|
|
if files[i].IsDir() && files[j].IsDir() {
|
|
|
|
return files[i].Name() < files[j].Name()
|
|
|
|
} else if files[i].IsDir() {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
var trim ulloc.Location
|
2021-10-02 00:47:53 +01:00
|
|
|
if !opts.isRecursive() {
|
2021-06-25 02:55:13 +01:00
|
|
|
trim = ulloc.NewLocal(prefix)
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &filteredObjectIterator{
|
|
|
|
trim: trim,
|
2021-06-25 02:55:13 +01:00
|
|
|
filter: ulloc.NewLocal(prefix),
|
2021-04-06 20:19:11 +01:00
|
|
|
iter: &fileinfoObjectIterator{
|
|
|
|
base: prefix,
|
|
|
|
files: files,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// IsLocalDir returns true if the path is a directory.
|
|
|
|
func (l *Local) IsLocalDir(ctx context.Context, path string) bool {
|
2022-05-10 19:31:20 +01:00
|
|
|
fi, err := l.fs.Stat(path)
|
2021-04-06 20:19:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return fi.IsDir()
|
|
|
|
}
|
|
|
|
|
2021-10-26 11:49:03 +01:00
|
|
|
// Stat returns an ObjectInfo describing the provided path.
|
|
|
|
func (l *Local) Stat(ctx context.Context, path string) (*ObjectInfo, error) {
|
2022-05-10 19:31:20 +01:00
|
|
|
fi, err := l.fs.Stat(path)
|
2021-10-26 11:49:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ObjectInfo{
|
|
|
|
Loc: ulloc.NewLocal(path),
|
|
|
|
Created: fi.ModTime(),
|
|
|
|
ContentLength: fi.Size(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:19:11 +01:00
|
|
|
type namedFileInfo struct {
|
|
|
|
os.FileInfo
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *namedFileInfo) Name() string { return n.name }
|
|
|
|
|
|
|
|
type fileinfoObjectIterator struct {
|
|
|
|
base string
|
|
|
|
files []os.FileInfo
|
|
|
|
current os.FileInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fi *fileinfoObjectIterator) Next() bool {
|
|
|
|
if len(fi.files) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fi.current, fi.files = fi.files[0], fi.files[1:]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fi *fileinfoObjectIterator) Err() error { return nil }
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func (fi *fileinfoObjectIterator) Item() ObjectInfo {
|
2021-04-06 20:19:11 +01:00
|
|
|
name := filepath.Join(fi.base, fi.current.Name())
|
|
|
|
isDir := fi.current.IsDir()
|
|
|
|
if isDir {
|
|
|
|
name += string(filepath.Separator)
|
|
|
|
}
|
2021-05-06 17:56:57 +01:00
|
|
|
return ObjectInfo{
|
|
|
|
Loc: ulloc.NewLocal(name),
|
2021-04-06 20:19:11 +01:00
|
|
|
IsPrefix: isDir,
|
|
|
|
Created: fi.current.ModTime(), // TODO: use real crtime
|
|
|
|
ContentLength: fi.current.Size(),
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 19:31:20 +01:00
|
|
|
|
|
|
|
func walk(fs LocalBackend, path string, cb func(path string, info os.FileInfo, err error) error) error {
|
|
|
|
info, err := fs.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return cb(path, nil, err)
|
|
|
|
}
|
|
|
|
return walkHelper(fs, path, info, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func walkHelper(fs LocalBackend, path string, info os.FileInfo, cb func(path string, info os.FileInfo, err error) error) error {
|
|
|
|
if !info.IsDir() {
|
|
|
|
return cb(path, info, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
infos, err := readdir(fs, path, -1)
|
|
|
|
err1 := cb(path, info, err)
|
|
|
|
if err != nil || err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
filename := filepath.Join(path, info.Name())
|
|
|
|
fileInfo, err := fs.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
if err := cb(filename, fileInfo, err); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := walkHelper(fs, filename, fileInfo, cb)
|
|
|
|
if err != nil && !fileInfo.IsDir() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readdir(fs LocalBackend, path string, n int) ([]os.FileInfo, error) {
|
|
|
|
f, err := fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list, err := f.Readdir(n)
|
|
|
|
_ = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Slice(list, func(i, j int) bool {
|
|
|
|
return list[i].Name() < list[j].Name()
|
|
|
|
})
|
|
|
|
return list, nil
|
|
|
|
}
|