| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/pkg/errors" |
| 10 |
) |
| 11 |
|
| 12 |
// NewLocalFS initializes a new instance of a local filesystem that |
| 13 |
// can be used for tar/untar operations. |
| 14 |
func NewLocalFS(root string, opts LocalFSOptions) *LocalFS { |
| 15 |
return &LocalFS{ |
| 16 |
Root: root, |
| 17 |
opts: opts, |
| 18 |
entries: make(chan walkEntry), |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// prepareDirWrite does nothing on Windows. Permission attributes are ignored |
| 23 |
// here, and chmod'ing a directory would only clear the read-only attribute |
| 24 |
// that SetDirPermissions never restores. |
| 25 |
func (fs *LocalFS) prepareDirWrite(r *os.Root, n NodeDirectory, existing os.FileMode, created bool) { |
| 26 |
} |
| 27 |
|
| 28 |
func (fs *LocalFS) SetDirPermissions(n NodeDirectory) error { |
| 29 |
// Permission attributes are ignored on Windows |
| 30 |
return nil |
| 31 |
} |
| 32 |
|
| 33 |
func (fs *LocalFS) SetFilePermissions(n NodeFile) error { |
| 34 |
// Permission attributes are ignored on Windows |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
func (fs *LocalFS) SetSymlinkPermissions(n NodeSymlink) error { |
| 39 |
// Permission attributes are ignored on Windows |
| 40 |
return nil |
| 41 |
} |
| 42 |
|
| 43 |
func (fs *LocalFS) CreateDevice(n NodeDevice) error { |
| 44 |
return errors.New("Device nodes not supported on this platform") |
| 45 |
} |
| 46 |
|
| 47 |
// Next returns the next filesystem entry or io.EOF when done. The caller is responsible |
| 48 |
// for closing the returned File object. |
| 49 |
func (fs *LocalFS) Next() (*File, error) { |
| 50 |
fs.once.Do(func() { |
| 51 |
fs.startSerializer() |
| 52 |
}) |
| 53 |
|
| 54 |
entry, ok := <-fs.entries |
| 55 |
if !ok { |
| 56 |
return nil, fs.sErr |
| 57 |
} |
| 58 |
if entry.err != nil { |
| 59 |
return nil, entry.err |
| 60 |
} |
| 61 |
|
| 62 |
// If it's a file, open it and return a ReadCloser |
| 63 |
var r io.ReadCloser |
| 64 |
if entry.info.Mode().IsRegular() { |
| 65 |
data, err := os.Open(entry.path) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
r = data |
| 70 |
} |
| 71 |
|
| 72 |
// If this is a symlink we need to get the link target |
| 73 |
var ( |
| 74 |
linkTarget string |
| 75 |
err error |
| 76 |
) |
| 77 |
if entry.info.Mode()&os.ModeSymlink != 0 { |
| 78 |
linkTarget, err = os.Readlink(entry.path) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
mtime := entry.info.ModTime() |
| 85 |
if fs.opts.NoTime { |
| 86 |
mtime = time.Unix(0, 0) |
| 87 |
} |
| 88 |
|
| 89 |
f := &File{ |
| 90 |
Name: entry.info.Name(), |
| 91 |
Path: filepath.ToSlash(filepath.Clean(entry.path)), |
| 92 |
Mode: entry.info.Mode(), |
| 93 |
ModTime: mtime, |
| 94 |
Size: uint64(entry.info.Size()), |
| 95 |
LinkTarget: filepath.ToSlash(linkTarget), |
| 96 |
Data: r, |
| 97 |
} |
| 98 |
|
| 99 |
return f, nil |
| 100 |
} |
| 101 |
|
| 102 |
func (fs *LocalFS) startSerializer() { |
| 103 |
go func() { |
| 104 |
err := filepath.Walk(fs.Root, func(path string, info os.FileInfo, err error) error { |
| 105 |
fs.entries <- walkEntry{path, info, err} |
| 106 |
return nil |
| 107 |
}) |
| 108 |
fs.sErr = err |
| 109 |
if err == nil { |
| 110 |
fs.sErr = io.EOF |
| 111 |
} |
| 112 |
close(fs.entries) |
| 113 |
}() |
| 114 |
} |