2017-05-15 17:13:34 -04:00
|
|
|
package fsutil
|
|
|
|
|
|
|
|
import (
|
2018-06-08 21:07:42 -04:00
|
|
|
"context"
|
2017-05-15 17:13:34 -04:00
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-05-15 17:13:34 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-10-05 05:05:42 -04:00
|
|
|
"github.com/tonistiigi/fsutil/types"
|
2017-05-15 17:13:34 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WriteToFunc func(context.Context, string, io.WriteCloser) error
|
|
|
|
|
|
|
|
type DiskWriterOpt struct {
|
2017-09-29 06:32:26 -04:00
|
|
|
AsyncDataCb WriteToFunc
|
|
|
|
SyncDataCb WriteToFunc
|
|
|
|
NotifyCb func(ChangeKind, string, os.FileInfo, error) error
|
|
|
|
ContentHasher ContentHasher
|
|
|
|
Filter FilterFunc
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
2018-10-05 05:05:42 -04:00
|
|
|
type FilterFunc func(*types.Stat) bool
|
2017-09-29 06:32:26 -04:00
|
|
|
|
2017-05-15 17:13:34 -04:00
|
|
|
type DiskWriter struct {
|
|
|
|
opt DiskWriterOpt
|
|
|
|
dest string
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
eg *errgroup.Group
|
2017-09-29 06:32:26 -04:00
|
|
|
filter FilterFunc
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDiskWriter(ctx context.Context, dest string, opt DiskWriterOpt) (*DiskWriter, error) {
|
|
|
|
if opt.SyncDataCb == nil && opt.AsyncDataCb == nil {
|
|
|
|
return nil, errors.New("no data callback specified")
|
|
|
|
}
|
|
|
|
if opt.SyncDataCb != nil && opt.AsyncDataCb != nil {
|
|
|
|
return nil, errors.New("can't specify both sync and async data callbacks")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
return &DiskWriter{
|
|
|
|
opt: opt,
|
|
|
|
dest: dest,
|
|
|
|
eg: eg,
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2018-06-08 05:26:10 -04:00
|
|
|
filter: opt.Filter,
|
2017-05-15 17:13:34 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dw *DiskWriter) Wait(ctx context.Context) error {
|
|
|
|
return dw.eg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, err error) (retErr error) {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-dw.ctx.Done():
|
|
|
|
return dw.ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
|
|
|
dw.cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
destPath := filepath.Join(dw.dest, filepath.FromSlash(p))
|
2017-05-15 17:13:34 -04:00
|
|
|
|
|
|
|
if kind == ChangeKindDelete {
|
|
|
|
// todo: no need to validate if diff is trusted but is it always?
|
|
|
|
if err := os.RemoveAll(destPath); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove: %s", destPath)
|
|
|
|
}
|
|
|
|
if dw.opt.NotifyCb != nil {
|
|
|
|
if err := dw.opt.NotifyCb(kind, p, nil, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:05:42 -04:00
|
|
|
stat, ok := fi.Sys().(*types.Stat)
|
2017-05-15 17:13:34 -04:00
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("%s invalid change without stat information", p)
|
|
|
|
}
|
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
statCopy := *stat
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
if dw.filter != nil {
|
2018-06-08 21:07:42 -04:00
|
|
|
if ok := dw.filter(&statCopy); !ok {
|
2017-09-29 06:32:26 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:13:34 -04:00
|
|
|
rename := true
|
|
|
|
oldFi, err := os.Lstat(destPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if kind != ChangeKindAdd {
|
|
|
|
return errors.Wrapf(err, "invalid addition: %s", destPath)
|
|
|
|
}
|
|
|
|
rename = false
|
|
|
|
} else {
|
|
|
|
return errors.Wrapf(err, "failed to stat %s", destPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldFi != nil && fi.IsDir() && oldFi.IsDir() {
|
2018-06-08 21:07:42 -04:00
|
|
|
if err := rewriteMetadata(destPath, &statCopy); err != nil {
|
2017-05-15 17:13:34 -04:00
|
|
|
return errors.Wrapf(err, "error setting dir metadata for %s", destPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newPath := destPath
|
|
|
|
if rename {
|
|
|
|
newPath = filepath.Join(filepath.Dir(destPath), ".tmp."+nextSuffix())
|
|
|
|
}
|
|
|
|
|
|
|
|
isRegularFile := false
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case fi.IsDir():
|
|
|
|
if err := os.Mkdir(newPath, fi.Mode()); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create dir %s", newPath)
|
|
|
|
}
|
|
|
|
case fi.Mode()&os.ModeDevice != 0 || fi.Mode()&os.ModeNamedPipe != 0:
|
2018-06-08 21:07:42 -04:00
|
|
|
if err := handleTarTypeBlockCharFifo(newPath, &statCopy); err != nil {
|
2017-05-15 17:13:34 -04:00
|
|
|
return errors.Wrapf(err, "failed to create device %s", newPath)
|
|
|
|
}
|
|
|
|
case fi.Mode()&os.ModeSymlink != 0:
|
2018-06-08 21:07:42 -04:00
|
|
|
if err := os.Symlink(statCopy.Linkname, newPath); err != nil {
|
2017-05-15 17:13:34 -04:00
|
|
|
return errors.Wrapf(err, "failed to symlink %s", newPath)
|
|
|
|
}
|
2018-06-08 21:07:42 -04:00
|
|
|
case statCopy.Linkname != "":
|
|
|
|
if err := os.Link(filepath.Join(dw.dest, statCopy.Linkname), newPath); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to link %s to %s", newPath, statCopy.Linkname)
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
isRegularFile = true
|
|
|
|
file, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY, fi.Mode()) //todo: windows
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create %s", newPath)
|
|
|
|
}
|
|
|
|
if dw.opt.SyncDataCb != nil {
|
|
|
|
if err := dw.processChange(ChangeKindAdd, p, fi, file); err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to close %s", newPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
if err := rewriteMetadata(newPath, &statCopy); err != nil {
|
2017-05-15 17:13:34 -04:00
|
|
|
return errors.Wrapf(err, "error setting metadata for %s", newPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rename {
|
2018-08-11 15:04:13 -04:00
|
|
|
if oldFi.IsDir() != fi.IsDir() {
|
|
|
|
if err := os.RemoveAll(destPath); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove %s", destPath)
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 17:13:34 -04:00
|
|
|
if err := os.Rename(newPath, destPath); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to rename %s to %s", newPath, destPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isRegularFile {
|
|
|
|
if dw.opt.AsyncDataCb != nil {
|
|
|
|
dw.requestAsyncFileData(p, destPath, fi)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return dw.processChange(kind, p, fi, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dw *DiskWriter) requestAsyncFileData(p, dest string, fi os.FileInfo) {
|
|
|
|
// todo: limit worker threads
|
|
|
|
dw.eg.Go(func() error {
|
|
|
|
if err := dw.processChange(ChangeKindAdd, p, fi, &lazyFileWriter{
|
|
|
|
dest: dest,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return chtimes(dest, fi.ModTime().UnixNano()) // TODO: parent dirs
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dw *DiskWriter) processChange(kind ChangeKind, p string, fi os.FileInfo, w io.WriteCloser) error {
|
|
|
|
origw := w
|
|
|
|
var hw *hashedWriter
|
|
|
|
if dw.opt.NotifyCb != nil {
|
|
|
|
var err error
|
2017-09-29 06:32:26 -04:00
|
|
|
if hw, err = newHashWriter(dw.opt.ContentHasher, fi, w); err != nil {
|
2017-05-15 17:13:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w = hw
|
|
|
|
}
|
|
|
|
if origw != nil {
|
|
|
|
fn := dw.opt.SyncDataCb
|
|
|
|
if fn == nil && dw.opt.AsyncDataCb != nil {
|
|
|
|
fn = dw.opt.AsyncDataCb
|
|
|
|
}
|
|
|
|
if err := fn(dw.ctx, p, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hw != nil {
|
|
|
|
hw.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hw != nil {
|
|
|
|
return dw.opt.NotifyCb(kind, p, hw, nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type hashedWriter struct {
|
|
|
|
os.FileInfo
|
|
|
|
io.Writer
|
2017-09-29 06:32:26 -04:00
|
|
|
h hash.Hash
|
|
|
|
w io.WriteCloser
|
|
|
|
dgst digest.Digest
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
func newHashWriter(ch ContentHasher, fi os.FileInfo, w io.WriteCloser) (*hashedWriter, error) {
|
2018-10-05 05:05:42 -04:00
|
|
|
stat, ok := fi.Sys().(*types.Stat)
|
2017-09-29 06:32:26 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid change without stat information")
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := ch(stat)
|
2017-05-15 17:13:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hw := &hashedWriter{
|
|
|
|
FileInfo: fi,
|
|
|
|
Writer: io.MultiWriter(w, h),
|
|
|
|
h: h,
|
|
|
|
w: w,
|
|
|
|
}
|
|
|
|
return hw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hw *hashedWriter) Close() error {
|
2017-09-29 06:32:26 -04:00
|
|
|
hw.dgst = digest.NewDigest(digest.SHA256, hw.h)
|
2017-05-15 17:13:34 -04:00
|
|
|
if hw.w != nil {
|
|
|
|
return hw.w.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
func (hw *hashedWriter) Digest() digest.Digest {
|
|
|
|
return hw.dgst
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type lazyFileWriter struct {
|
2018-06-08 21:07:42 -04:00
|
|
|
dest string
|
|
|
|
ctx context.Context
|
|
|
|
f *os.File
|
|
|
|
fileMode *os.FileMode
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lfw *lazyFileWriter) Write(dt []byte) (int, error) {
|
|
|
|
if lfw.f == nil {
|
|
|
|
file, err := os.OpenFile(lfw.dest, os.O_WRONLY, 0) //todo: windows
|
2018-06-08 21:07:42 -04:00
|
|
|
if os.IsPermission(err) {
|
|
|
|
// retry after chmod
|
|
|
|
fi, er := os.Stat(lfw.dest)
|
|
|
|
if er == nil {
|
|
|
|
mode := fi.Mode()
|
|
|
|
lfw.fileMode = &mode
|
|
|
|
er = os.Chmod(lfw.dest, mode|0222)
|
|
|
|
if er == nil {
|
|
|
|
file, err = os.OpenFile(lfw.dest, os.O_WRONLY, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 17:13:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrapf(err, "failed to open %s", lfw.dest)
|
|
|
|
}
|
|
|
|
lfw.f = file
|
|
|
|
}
|
|
|
|
return lfw.f.Write(dt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lfw *lazyFileWriter) Close() error {
|
2018-06-08 21:07:42 -04:00
|
|
|
var err error
|
2017-05-15 17:13:34 -04:00
|
|
|
if lfw.f != nil {
|
2018-06-08 21:07:42 -04:00
|
|
|
err = lfw.f.Close()
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
2018-06-08 21:07:42 -04:00
|
|
|
if err == nil && lfw.fileMode != nil {
|
|
|
|
err = os.Chmod(lfw.dest, *lfw.fileMode)
|
|
|
|
}
|
|
|
|
return err
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func mkdev(major int64, minor int64) uint32 {
|
|
|
|
return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random number state.
|
|
|
|
// We generate random temporary file names so that there's a good
|
|
|
|
// chance the file doesn't exist yet - keeps the number of tries in
|
|
|
|
// TempFile to a minimum.
|
|
|
|
var rand uint32
|
|
|
|
var randmu sync.Mutex
|
|
|
|
|
|
|
|
func reseed() uint32 {
|
|
|
|
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextSuffix() string {
|
|
|
|
randmu.Lock()
|
|
|
|
r := rand
|
|
|
|
if r == 0 {
|
|
|
|
r = reseed()
|
|
|
|
}
|
|
|
|
r = r*1664525 + 1013904223 // constants from Numerical Recipes
|
|
|
|
rand = r
|
|
|
|
randmu.Unlock()
|
|
|
|
return strconv.Itoa(int(1e9 + r%1e9))[1:]
|
|
|
|
}
|