2016-01-02 09:52:40 +03:00
|
|
|
package afero
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The UnionFile implements the afero.File interface and will be returned
|
|
|
|
// when reading a directory present at least in the overlay or opening a file
|
|
|
|
// for writing.
|
|
|
|
//
|
|
|
|
// The calls to
|
|
|
|
// Readdir() and Readdirnames() merge the file os.FileInfo / names from the
|
|
|
|
// base and the overlay - for files present in both layers, only those
|
|
|
|
// from the overlay will be used.
|
|
|
|
//
|
|
|
|
// When opening files for writing (Create() / OpenFile() with the right flags)
|
|
|
|
// the operations will be done in both layers, starting with the overlay. A
|
|
|
|
// successful read in the overlay will move the cursor position in the base layer
|
|
|
|
// by the number of bytes read.
|
|
|
|
type UnionFile struct {
|
2018-03-28 18:26:24 +03:00
|
|
|
Base File
|
|
|
|
Layer File
|
|
|
|
Merger DirsMerger
|
|
|
|
off int
|
|
|
|
files []os.FileInfo
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Close() error {
|
|
|
|
// first close base, so we have a newer timestamp in the overlay. If we'd close
|
|
|
|
// the overlay first, we'd get a cacheStale the next time we access this file
|
|
|
|
// -> cache would be useless ;-)
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
f.Base.Close()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
return f.Layer.Close()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Read(s []byte) (int, error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
n, err := f.Layer.Read(s)
|
|
|
|
if (err == nil || err == io.EOF) && f.Base != nil {
|
2016-01-02 09:52:40 +03:00
|
|
|
// advance the file position also in the base file, the next
|
|
|
|
// call may be a write at this position (or a seek with SEEK_CUR)
|
2023-03-06 12:18:15 +03:00
|
|
|
if _, seekErr := f.Base.Seek(int64(n), io.SeekCurrent); seekErr != nil {
|
2016-01-02 09:52:40 +03:00
|
|
|
// only overwrite err in case the seek fails: we need to
|
|
|
|
// report an eventual io.EOF to the caller
|
|
|
|
err = seekErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.Read(s)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return 0, BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) ReadAt(s []byte, o int64) (int, error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
n, err := f.Layer.ReadAt(s, o)
|
|
|
|
if (err == nil || err == io.EOF) && f.Base != nil {
|
2022-07-14 14:06:04 +03:00
|
|
|
_, err = f.Base.Seek(o+int64(n), io.SeekStart)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.ReadAt(s, o)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return 0, BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Seek(o int64, w int) (pos int64, err error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
pos, err = f.Layer.Seek(o, w)
|
|
|
|
if (err == nil || err == io.EOF) && f.Base != nil {
|
|
|
|
_, err = f.Base.Seek(o, w)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
return pos, err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.Seek(o, w)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return 0, BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Write(s []byte) (n int, err error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
n, err = f.Layer.Write(s)
|
|
|
|
if err == nil && f.Base != nil { // hmm, do we have fixed size files where a write may hit the EOF mark?
|
|
|
|
_, err = f.Base.Write(s)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.Write(s)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return 0, BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) WriteAt(s []byte, o int64) (n int, err error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
n, err = f.Layer.WriteAt(s, o)
|
|
|
|
if err == nil && f.Base != nil {
|
|
|
|
_, err = f.Base.WriteAt(s, o)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.WriteAt(s, o)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return 0, BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Name() string {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
return f.Layer.Name()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
return f.Base.Name()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
2018-03-28 18:26:24 +03:00
|
|
|
// DirsMerger is how UnionFile weaves two directories together.
|
|
|
|
// It takes the FileInfo slices from the layer and the base and returns a
|
|
|
|
// single view.
|
|
|
|
type DirsMerger func(lofi, bofi []os.FileInfo) ([]os.FileInfo, error)
|
|
|
|
|
|
|
|
var defaultUnionMergeDirsFn = func(lofi, bofi []os.FileInfo) ([]os.FileInfo, error) {
|
2023-02-23 12:14:33 +03:00
|
|
|
files := make(map[string]os.FileInfo)
|
2018-03-28 18:26:24 +03:00
|
|
|
|
|
|
|
for _, fi := range lofi {
|
|
|
|
files[fi.Name()] = fi
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range bofi {
|
|
|
|
if _, exists := files[fi.Name()]; !exists {
|
|
|
|
files[fi.Name()] = fi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rfi := make([]os.FileInfo, len(files))
|
2018-03-22 19:32:10 +03:00
|
|
|
|
2018-03-28 18:26:24 +03:00
|
|
|
i := 0
|
|
|
|
for _, fi := range files {
|
|
|
|
rfi[i] = fi
|
|
|
|
i++
|
2018-03-22 19:32:10 +03:00
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
|
|
|
|
return rfi, nil
|
2018-03-22 19:32:10 +03:00
|
|
|
}
|
|
|
|
|
2016-01-14 00:37:11 +03:00
|
|
|
// Readdir will weave the two directories together and
|
2019-03-02 05:18:57 +03:00
|
|
|
// return a single view of the overlayed directories.
|
|
|
|
// At the end of the directory view, the error is io.EOF if c > 0.
|
2016-01-02 09:52:40 +03:00
|
|
|
func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
var merge DirsMerger = f.Merger
|
|
|
|
if merge == nil {
|
|
|
|
merge = defaultUnionMergeDirsFn
|
|
|
|
}
|
|
|
|
|
2016-01-02 09:52:40 +03:00
|
|
|
if f.off == 0 {
|
2018-03-28 18:26:24 +03:00
|
|
|
var lfi []os.FileInfo
|
|
|
|
if f.Layer != nil {
|
|
|
|
lfi, err = f.Layer.Readdir(-1)
|
2016-01-02 09:52:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 00:37:11 +03:00
|
|
|
|
2018-03-28 18:26:24 +03:00
|
|
|
var bfi []os.FileInfo
|
|
|
|
if f.Base != nil {
|
|
|
|
bfi, err = f.Base.Readdir(-1)
|
2016-01-02 09:52:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
merged, err := merge(lfi, bfi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
f.files = append(f.files, merged...)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2020-08-20 02:36:22 +03:00
|
|
|
files := f.files[f.off:]
|
2019-01-30 00:50:16 +03:00
|
|
|
|
2020-08-20 02:36:22 +03:00
|
|
|
if c <= 0 {
|
|
|
|
return files, nil
|
2019-03-02 05:18:57 +03:00
|
|
|
}
|
|
|
|
|
2020-08-20 02:36:22 +03:00
|
|
|
if len(files) == 0 {
|
2019-01-30 00:50:16 +03:00
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
2020-08-20 02:36:22 +03:00
|
|
|
if c > len(files) {
|
|
|
|
c = len(files)
|
2019-01-30 00:50:16 +03:00
|
|
|
}
|
|
|
|
|
2016-01-02 09:52:40 +03:00
|
|
|
defer func() { f.off += c }()
|
2020-08-20 02:36:22 +03:00
|
|
|
return files[:c], nil
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Readdirnames(c int) ([]string, error) {
|
|
|
|
rfi, err := f.Readdir(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var names []string
|
|
|
|
for _, fi := range rfi {
|
|
|
|
names = append(names, fi.Name())
|
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Stat() (os.FileInfo, error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
return f.Layer.Stat()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.Stat()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return nil, BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Sync() (err error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
err = f.Layer.Sync()
|
|
|
|
if err == nil && f.Base != nil {
|
|
|
|
err = f.Base.Sync()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.Sync()
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) Truncate(s int64) (err error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
err = f.Layer.Truncate(s)
|
|
|
|
if err == nil && f.Base != nil {
|
|
|
|
err = f.Base.Truncate(s)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.Truncate(s)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *UnionFile) WriteString(s string) (n int, err error) {
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Layer != nil {
|
|
|
|
n, err = f.Layer.WriteString(s)
|
|
|
|
if err == nil && f.Base != nil {
|
|
|
|
_, err = f.Base.WriteString(s)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2018-03-28 18:26:24 +03:00
|
|
|
if f.Base != nil {
|
|
|
|
return f.Base.WriteString(s)
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-11 22:28:57 +03:00
|
|
|
return 0, BADFD
|
2016-01-02 09:52:40 +03:00
|
|
|
}
|
2016-01-12 00:07:40 +03:00
|
|
|
|
2021-12-25 23:19:03 +03:00
|
|
|
func copyFile(base Fs, layer Fs, name string, bfh File) error {
|
2016-01-12 04:27:36 +03:00
|
|
|
// First make sure the directory exists
|
2016-01-12 00:07:40 +03:00
|
|
|
exists, err := Exists(layer, filepath.Dir(name))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
2023-02-23 12:14:33 +03:00
|
|
|
err = layer.MkdirAll(filepath.Dir(name), 0o777) // FIXME?
|
2016-01-12 00:07:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:27:36 +03:00
|
|
|
// Create the file on the overlay
|
2016-01-12 00:07:40 +03:00
|
|
|
lfh, err := layer.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err := io.Copy(lfh, bfh)
|
|
|
|
if err != nil {
|
2016-01-12 04:27:36 +03:00
|
|
|
// If anything fails, clean up the file
|
2016-01-12 00:07:40 +03:00
|
|
|
layer.Remove(name)
|
|
|
|
lfh.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bfi, err := bfh.Stat()
|
|
|
|
if err != nil || bfi.Size() != n {
|
|
|
|
layer.Remove(name)
|
|
|
|
lfh.Close()
|
|
|
|
return syscall.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lfh.Close()
|
|
|
|
if err != nil {
|
|
|
|
layer.Remove(name)
|
|
|
|
lfh.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return layer.Chtimes(name, bfi.ModTime(), bfi.ModTime())
|
|
|
|
}
|
2021-12-25 23:19:03 +03:00
|
|
|
|
|
|
|
func copyToLayer(base Fs, layer Fs, name string) error {
|
|
|
|
bfh, err := base.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer bfh.Close()
|
|
|
|
|
|
|
|
return copyFile(base, layer, name, bfh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFileToLayer(base Fs, layer Fs, name string, flag int, perm os.FileMode) error {
|
|
|
|
bfh, err := base.OpenFile(name, flag, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer bfh.Close()
|
|
|
|
|
|
|
|
return copyFile(base, layer, name, bfh)
|
|
|
|
}
|