copy/move: detect file size change during copy/move - fixes #1250

This commit is contained in:
ishuah 2018-01-31 23:18:31 +03:00 committed by Nick Craig-Wood
parent 1018e9bb27
commit 4c1ffc7f54
2 changed files with 50 additions and 0 deletions

View file

@ -651,10 +651,17 @@ type localOpenFile struct {
o *Object // object that is open
in io.ReadCloser // handle we are wrapping
hash *hash.MultiHasher // currently accumulating hashes
fd *os.File // file object reference
}
// Read bytes from the object - see io.Reader
func (file *localOpenFile) Read(p []byte) (n int, err error) {
// Check if file has the same size and modTime
fi, err := file.fd.Stat()
if file.o.size != fi.Size() || file.o.modTime != fi.ModTime() {
return 0, errors.New("can't copy - source file is being updated")
}
n, err = file.in.Read(p)
if n > 0 {
// Hash routines never return an error
@ -713,6 +720,7 @@ func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
o: o,
in: wrappedFd,
hash: hash,
fd: fd,
}
return in, nil
}