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"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-06-25 02:55:13 +01:00
|
|
|
"sort"
|
2021-04-06 20:19:11 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// Local implements something close to a filesystem but backed by the local disk.
|
|
|
|
type Local struct{}
|
|
|
|
|
|
|
|
// NewLocal constructs a Local filesystem.
|
|
|
|
func NewLocal() *Local {
|
|
|
|
return &Local{}
|
|
|
|
}
|
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) {
|
2021-04-06 20:19:11 +01:00
|
|
|
fh, err := os.Open(path)
|
|
|
|
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) {
|
2021-04-06 20:19:11 +01:00
|
|
|
fi, err := os.Stat(path)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: atomic rename
|
|
|
|
fh, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
2021-12-09 19:03:42 +00:00
|
|
|
return newOSMultiWriteHandle(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 {
|
|
|
|
return os.Rename(oldpath, newpath)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:20:21 +01:00
|
|
|
if err := os.Remove(path); os.IsNotExist(err) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:19:11 +01:00
|
|
|
prefix := path
|
2021-06-25 02:55:13 +01:00
|
|
|
if idx := strings.LastIndex(path, "/"); idx >= 0 {
|
2021-04-06 20:19:11 +01:00
|
|
|
prefix = path[:idx+1]
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
prefix, err := filepath.Abs(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
prefix += string(filepath.Separator)
|
|
|
|
|
2021-04-06 20:19:11 +01:00
|
|
|
var files []os.FileInfo
|
2021-10-02 00:47:53 +01:00
|
|
|
if opts.isRecursive() {
|
2021-04-06 20:19:11 +01:00
|
|
|
err = filepath.Walk(prefix, func(path string, info os.FileInfo, err error) error {
|
|
|
|
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 {
|
|
|
|
files, err = ioutil.ReadDir(prefix)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-04-06 20:19:11 +01:00
|
|
|
fi, err := os.Stat(path)
|
|
|
|
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) {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|