Call `OpenFile` instead of `Open` in `CacheOnReadFs` (fixes #193)

This commit is contained in:
Felix Pojtinger 2021-12-25 21:19:03 +01:00
parent d70f944720
commit 039e386d52
2 changed files with 26 additions and 8 deletions

View File

@ -75,6 +75,10 @@ func (u *CacheOnReadFs) copyToLayer(name string) error {
return copyToLayer(u.base, u.layer, name) return copyToLayer(u.base, u.layer, name)
} }
func (u *CacheOnReadFs) copyFileToLayer(name string, flag int, perm os.FileMode) error {
return copyFileToLayer(u.base, u.layer, name, flag, perm)
}
func (u *CacheOnReadFs) Chtimes(name string, atime, mtime time.Time) error { func (u *CacheOnReadFs) Chtimes(name string, atime, mtime time.Time) error {
st, _, err := u.cacheStatus(name) st, _, err := u.cacheStatus(name)
if err != nil { if err != nil {
@ -212,7 +216,7 @@ func (u *CacheOnReadFs) OpenFile(name string, flag int, perm os.FileMode) (File,
switch st { switch st {
case cacheLocal, cacheHit: case cacheLocal, cacheHit:
default: default:
if err := u.copyToLayer(name); err != nil { if err := u.copyFileToLayer(name, flag, perm); err != nil {
return nil, err return nil, err
} }
} }

View File

@ -268,13 +268,7 @@ func (f *UnionFile) WriteString(s string) (n int, err error) {
return 0, BADFD return 0, BADFD
} }
func copyToLayer(base Fs, layer Fs, name string) error { func copyFile(base Fs, layer Fs, name string, bfh File) error {
bfh, err := base.Open(name)
if err != nil {
return err
}
defer bfh.Close()
// First make sure the directory exists // First make sure the directory exists
exists, err := Exists(layer, filepath.Dir(name)) exists, err := Exists(layer, filepath.Dir(name))
if err != nil { if err != nil {
@ -315,3 +309,23 @@ func copyToLayer(base Fs, layer Fs, name string) error {
} }
return layer.Chtimes(name, bfi.ModTime(), bfi.ModTime()) return layer.Chtimes(name, bfi.ModTime(), bfi.ModTime())
} }
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)
}