2012-12-26 20:23:58 +08:00
|
|
|
// Local filesystem interface
|
2013-06-28 03:13:07 +08:00
|
|
|
package local
|
2012-12-26 20:23:58 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-01-19 07:21:02 +08:00
|
|
|
"io/ioutil"
|
2012-12-26 20:23:58 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-01-19 07:21:02 +08:00
|
|
|
"sync"
|
2012-12-26 20:23:58 +08:00
|
|
|
"time"
|
2014-03-16 00:06:11 +08:00
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2012-12-26 20:23:58 +08:00
|
|
|
)
|
|
|
|
|
2013-06-28 03:13:07 +08:00
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2014-03-16 00:06:11 +08:00
|
|
|
fs.Register(&fs.FsInfo{
|
|
|
|
Name: "local",
|
|
|
|
NewFs: NewFs,
|
|
|
|
})
|
2013-06-28 03:13:07 +08:00
|
|
|
}
|
|
|
|
|
2012-12-26 20:23:58 +08:00
|
|
|
// FsLocal represents a local filesystem rooted at root
|
|
|
|
type FsLocal struct {
|
2013-01-19 07:21:02 +08:00
|
|
|
root string // The root directory
|
|
|
|
precisionOk sync.Once // Whether we need to read the precision
|
|
|
|
precision time.Duration // precision of local filesystem
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FsObjectLocal represents a local filesystem object
|
|
|
|
type FsObjectLocal struct {
|
|
|
|
remote string // The remote path
|
|
|
|
path string // The local path
|
|
|
|
info os.FileInfo // Interface for file info
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
2012-12-29 19:35:41 +08:00
|
|
|
|
2013-06-28 03:13:07 +08:00
|
|
|
// NewFs contstructs an FsLocal from the path
|
2014-03-16 00:06:11 +08:00
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
2012-12-29 19:35:41 +08:00
|
|
|
root = path.Clean(root)
|
|
|
|
f := &FsLocal{root: root}
|
|
|
|
return f, nil
|
|
|
|
}
|
2012-12-26 20:23:58 +08:00
|
|
|
|
2013-01-01 00:40:34 +08:00
|
|
|
// String converts this FsLocal to a string
|
|
|
|
func (f *FsLocal) String() string {
|
|
|
|
return fmt.Sprintf("Local file system at %s", f.root)
|
|
|
|
}
|
|
|
|
|
2012-12-26 20:23:58 +08:00
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2013-06-28 15:57:32 +08:00
|
|
|
func (f *FsLocal) NewFsObjectWithInfo(remote string, info os.FileInfo) fs.Object {
|
2012-12-26 20:23:58 +08:00
|
|
|
path := filepath.Join(f.root, remote)
|
2013-06-28 03:13:07 +08:00
|
|
|
o := &FsObjectLocal{remote: remote, path: path}
|
2012-12-26 20:23:58 +08:00
|
|
|
if info != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
o.info = info
|
2012-12-26 20:23:58 +08:00
|
|
|
} else {
|
2013-06-28 03:13:07 +08:00
|
|
|
err := o.lstat()
|
2012-12-26 20:23:58 +08:00
|
|
|
if err != nil {
|
2013-06-28 15:57:32 +08:00
|
|
|
fs.Debug(o, "Failed to stat %s: %s", path, err)
|
2012-12-26 20:23:58 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 03:13:07 +08:00
|
|
|
return o
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2013-06-28 15:57:32 +08:00
|
|
|
func (f *FsLocal) NewFsObject(remote string) fs.Object {
|
2012-12-26 20:23:58 +08:00
|
|
|
return f.NewFsObjectWithInfo(remote, nil)
|
|
|
|
}
|
|
|
|
|
2012-12-29 00:38:51 +08:00
|
|
|
// List the path returning a channel of FsObjects
|
2012-12-26 20:23:58 +08:00
|
|
|
//
|
2012-12-29 00:38:51 +08:00
|
|
|
// Ignores everything which isn't Storable, eg links etc
|
2013-06-28 15:57:32 +08:00
|
|
|
func (f *FsLocal) List() fs.ObjectsChan {
|
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
2012-12-26 20:23:58 +08:00
|
|
|
go func() {
|
|
|
|
err := filepath.Walk(f.root, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2012-12-26 20:23:58 +08:00
|
|
|
log.Printf("Failed to open directory: %s: %s", path, err)
|
|
|
|
} else {
|
|
|
|
remote, err := filepath.Rel(f.root, path)
|
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2012-12-26 20:23:58 +08:00
|
|
|
log.Printf("Failed to get relative path %s: %s", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if remote == "." {
|
|
|
|
return nil
|
|
|
|
// remote = ""
|
|
|
|
}
|
|
|
|
if fs := f.NewFsObjectWithInfo(remote, fi); fs != nil {
|
|
|
|
if fs.Storable() {
|
|
|
|
out <- fs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2012-12-26 20:23:58 +08:00
|
|
|
log.Printf("Failed to open directory: %s: %s", f.root, err)
|
|
|
|
}
|
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-01-24 06:43:20 +08:00
|
|
|
// Walk the path returning a channel of FsObjects
|
2013-06-28 15:57:32 +08:00
|
|
|
func (f *FsLocal) ListDir() fs.DirChan {
|
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
2013-01-24 06:43:20 +08:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
items, err := ioutil.ReadDir(f.root)
|
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2013-01-24 06:43:20 +08:00
|
|
|
log.Printf("Couldn't find read directory: %s", err)
|
|
|
|
} else {
|
|
|
|
for _, item := range items {
|
|
|
|
if item.IsDir() {
|
2013-06-28 15:57:32 +08:00
|
|
|
dir := &fs.Dir{
|
2013-01-24 06:43:20 +08:00
|
|
|
Name: item.Name(),
|
|
|
|
When: item.ModTime(),
|
|
|
|
Bytes: 0,
|
|
|
|
Count: 0,
|
|
|
|
}
|
|
|
|
// Go down the tree to count the files and directories
|
|
|
|
dirpath := path.Join(f.root, item.Name())
|
|
|
|
err := filepath.Walk(dirpath, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2013-01-24 06:43:20 +08:00
|
|
|
log.Printf("Failed to open directory: %s: %s", path, err)
|
|
|
|
} else {
|
|
|
|
dir.Count += 1
|
|
|
|
dir.Bytes += fi.Size()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2013-01-24 06:43:20 +08:00
|
|
|
log.Printf("Failed to open directory: %s: %s", dirpath, err)
|
|
|
|
}
|
|
|
|
out <- dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// err := f.findRoot(false)
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2012-12-26 20:23:58 +08:00
|
|
|
// Puts the FsObject to the local filesystem
|
2013-06-28 15:57:32 +08:00
|
|
|
func (f *FsLocal) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
2013-01-11 05:58:46 +08:00
|
|
|
dstPath := filepath.Join(f.root, remote)
|
2012-12-26 20:23:58 +08:00
|
|
|
// Temporary FsObject under construction
|
2013-01-11 05:58:46 +08:00
|
|
|
fs := &FsObjectLocal{remote: remote, path: dstPath}
|
2012-12-26 20:23:58 +08:00
|
|
|
|
|
|
|
dir := path.Dir(dstPath)
|
|
|
|
err := os.MkdirAll(dir, 0770)
|
|
|
|
if err != nil {
|
2013-01-11 05:58:46 +08:00
|
|
|
return fs, err
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(dstPath)
|
|
|
|
if err != nil {
|
2013-01-11 05:58:46 +08:00
|
|
|
return fs, err
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(out, in)
|
2013-01-11 05:58:46 +08:00
|
|
|
outErr := out.Close()
|
2012-12-26 20:23:58 +08:00
|
|
|
if err != nil {
|
2013-01-11 05:58:46 +08:00
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
if outErr != nil {
|
|
|
|
return fs, outErr
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the mtime
|
2013-01-11 05:58:46 +08:00
|
|
|
fs.SetModTime(modTime)
|
|
|
|
return fs, err
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the directory if it doesn't exist
|
|
|
|
func (f *FsLocal) Mkdir() error {
|
|
|
|
return os.MkdirAll(f.root, 0770)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir removes the directory
|
|
|
|
//
|
|
|
|
// If it isn't empty it will return an error
|
|
|
|
func (f *FsLocal) Rmdir() error {
|
|
|
|
return os.Remove(f.root)
|
|
|
|
}
|
|
|
|
|
2013-01-19 07:21:02 +08:00
|
|
|
// Return the precision
|
|
|
|
func (f *FsLocal) Precision() (precision time.Duration) {
|
|
|
|
f.precisionOk.Do(func() {
|
|
|
|
f.precision = f.readPrecision()
|
|
|
|
})
|
|
|
|
return f.precision
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the precision
|
|
|
|
func (f *FsLocal) readPrecision() (precision time.Duration) {
|
|
|
|
// Default precision of 1s
|
|
|
|
precision = time.Second
|
|
|
|
|
|
|
|
// Create temporary file and test it
|
2013-06-28 03:00:01 +08:00
|
|
|
fd, err := ioutil.TempFile("", "rclone")
|
2013-01-19 07:21:02 +08:00
|
|
|
if err != nil {
|
|
|
|
// If failed return 1s
|
|
|
|
// fmt.Println("Failed to create temp file", err)
|
|
|
|
return time.Second
|
|
|
|
}
|
|
|
|
path := fd.Name()
|
|
|
|
// fmt.Println("Created temp file", path)
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
// Delete it on return
|
|
|
|
defer func() {
|
|
|
|
// fmt.Println("Remove temp file")
|
|
|
|
os.Remove(path)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Find the minimum duration we can detect
|
|
|
|
for duration := time.Duration(1); duration < time.Second; duration *= 10 {
|
|
|
|
// Current time with delta
|
|
|
|
t := time.Unix(time.Now().Unix(), int64(duration))
|
2013-06-28 03:13:07 +08:00
|
|
|
err := os.Chtimes(path, t, t)
|
2013-01-19 07:21:02 +08:00
|
|
|
if err != nil {
|
|
|
|
// fmt.Println("Failed to Chtimes", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the actual time back
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
// fmt.Println("Failed to Stat", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it matches - have found the precision
|
|
|
|
// fmt.Println("compare", fi.ModTime(), t)
|
|
|
|
if fi.ModTime() == t {
|
|
|
|
// fmt.Println("Precision detected as", duration)
|
|
|
|
return duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-26 20:23:58 +08:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Return the remote path
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) Remote() string {
|
|
|
|
return o.remote
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Md5sum calculates the Md5sum of a file returning a lowercase hex string
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) Md5sum() (string, error) {
|
|
|
|
in, err := os.Open(o.path)
|
2012-12-26 20:23:58 +08:00
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2013-06-28 15:57:32 +08:00
|
|
|
fs.Log(o, "Failed to open: %s", err)
|
2012-12-26 20:23:58 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer in.Close() // FIXME ignoring error
|
|
|
|
hash := md5.New()
|
|
|
|
_, err = io.Copy(hash, in)
|
|
|
|
if err != nil {
|
2013-06-28 03:13:07 +08:00
|
|
|
fs.Stats.Error()
|
2013-06-28 15:57:32 +08:00
|
|
|
fs.Log(o, "Failed to read: %s", err)
|
2012-12-26 20:23:58 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) Size() int64 {
|
|
|
|
return o.info.Size()
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) ModTime() time.Time {
|
|
|
|
return o.info.ModTime()
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the modification time of the local fs object
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) SetModTime(modTime time.Time) {
|
|
|
|
err := os.Chtimes(o.path, modTime, modTime)
|
2012-12-26 20:23:58 +08:00
|
|
|
if err != nil {
|
2013-06-28 15:57:32 +08:00
|
|
|
fs.Debug(o, "Failed to set mtime on file: %s", err)
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this object storable
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) Storable() bool {
|
|
|
|
mode := o.info.Mode()
|
2012-12-26 20:23:58 +08:00
|
|
|
if mode&(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) != 0 {
|
2013-06-28 15:57:32 +08:00
|
|
|
fs.Debug(o, "Can't transfer non file/directory")
|
2012-12-26 20:23:58 +08:00
|
|
|
return false
|
|
|
|
} else if mode&os.ModeDir != 0 {
|
2013-06-28 15:57:32 +08:00
|
|
|
fs.Debug(o, "FIXME Skipping directory")
|
2012-12-26 20:23:58 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) Open() (in io.ReadCloser, err error) {
|
|
|
|
in, err = os.Open(o.path)
|
2012-12-26 20:23:58 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat a FsObject into info
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) lstat() error {
|
|
|
|
info, err := os.Lstat(o.path)
|
|
|
|
o.info = info
|
2012-12-26 20:23:58 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
2013-06-28 03:13:07 +08:00
|
|
|
func (o *FsObjectLocal) Remove() error {
|
|
|
|
return os.Remove(o.path)
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2013-06-28 03:13:07 +08:00
|
|
|
var _ fs.Fs = &FsLocal{}
|
2013-06-28 15:57:32 +08:00
|
|
|
var _ fs.Object = &FsObjectLocal{}
|