From 30c3c32863c4b870f55170d0437ef82a9d75f3fe Mon Sep 17 00:00:00 2001 From: Bohdan Lisovskyi Date: Wed, 22 Jan 2020 16:25:31 +0200 Subject: [PATCH 1/3] add chown method --- afero.go | 3 +++ basepath.go | 7 +++++++ cacheOnReadFs.go | 21 +++++++++++++++++++++ copyOnWriteFs.go | 13 +++++++++++++ mem/file.go | 14 ++++++++++++++ memmap.go | 15 +++++++++++++++ os.go | 4 ++++ readonlyfs.go | 4 ++++ regexpfs.go | 7 +++++++ sftpfs/sftp.go | 4 ++++ 10 files changed, 92 insertions(+) diff --git a/afero.go b/afero.go index f5b5e12..5702d89 100644 --- a/afero.go +++ b/afero.go @@ -96,6 +96,9 @@ type Fs interface { //Chtimes changes the access and modification times of the named file Chtimes(name string, atime time.Time, mtime time.Time) error + + // Chown changes uid and gid of the named file. + Chown(name string, uid, gid int) error } var ( diff --git a/basepath.go b/basepath.go index 616ff8f..404188e 100644 --- a/basepath.go +++ b/basepath.go @@ -177,4 +177,11 @@ func (b *BasePathFs) LstatIfPossible(name string) (os.FileInfo, bool, error) { return fi, false, err } + +func (b *BasePathFs) Chown(name string, uid, gid int) (err error) { + if name, err = b.RealPath(name); err != nil { + return &os.PathError{Op: "chown", Path: name, Err: err} + } + return b.source.Chown(name, uid, gid) +} // vim: ts=4 sw=4 noexpandtab nolist syn=go diff --git a/cacheOnReadFs.go b/cacheOnReadFs.go index 29a26c6..d0db6c5 100644 --- a/cacheOnReadFs.go +++ b/cacheOnReadFs.go @@ -288,3 +288,24 @@ func (u *CacheOnReadFs) Create(name string) (File, error) { } return &UnionFile{Base: bfh, Layer: lfh}, nil } + +func (u *CacheOnReadFs) Chown(name string, uid, gid int) error { + st, _, err := u.cacheStatus(name) + if err != nil { + return err + } + switch st { + case cacheLocal: + case cacheHit: + err = u.base.Chown(name, uid, gid) + case cacheStale, cacheMiss: + if err := u.copyToLayer(name); err != nil { + return err + } + err = u.base.Chown(name, uid, gid) + } + if err != nil { + return err + } + return u.layer.Chown(name, uid, gid) +} diff --git a/copyOnWriteFs.go b/copyOnWriteFs.go index e8108a8..3b76268 100644 --- a/copyOnWriteFs.go +++ b/copyOnWriteFs.go @@ -291,3 +291,16 @@ func (u *CopyOnWriteFs) MkdirAll(name string, perm os.FileMode) error { func (u *CopyOnWriteFs) Create(name string) (File, error) { return u.OpenFile(name, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) } + +func (u *CopyOnWriteFs) Chown(name string, uid, gid int) error { + b, err := u.isBaseFile(name) + if err != nil { + return err + } + if b { + if err := u.copyToLayer(name); err != nil { + return err + } + } + return u.layer.Chown(name, uid, gid) +} diff --git a/mem/file.go b/mem/file.go index 7af2fb5..c0acbd7 100644 --- a/mem/file.go +++ b/mem/file.go @@ -57,6 +57,8 @@ type FileData struct { dir bool mode os.FileMode modtime time.Time + uid int + gid int } func (d *FileData) Name() string { @@ -95,6 +97,18 @@ func setModTime(f *FileData, mtime time.Time) { f.modtime = mtime } +func SetUID(f *FileData, uid int) { + f.Lock() + f.uid = uid + f.Unlock() +} + +func SetGID(f *FileData, gid int) { + f.Lock() + f.uid = gid + f.Unlock() +} + func GetFileInfo(f *FileData) *FileInfo { return &FileInfo{f} } diff --git a/memmap.go b/memmap.go index 09498e7..7f57436 100644 --- a/memmap.go +++ b/memmap.go @@ -358,6 +358,21 @@ func (m *MemMapFs) List() { } } +func (m *MemMapFs) Chown(name string, uid, gid int) error { + name = normalizePath(name) + + m.mu.RLock() + f, ok := m.getData()[name] + m.mu.RUnlock() + if !ok { + return &os.PathError{Op: "chown", Path: name, Err: ErrFileNotFound} + } + + mem.SetUID(f, uid) + mem.SetGID(f, gid) + + return nil +} // func debugMemMapList(fs Fs) { // if x, ok := fs.(*MemMapFs); ok { // x.List() diff --git a/os.go b/os.go index 13cc1b8..916ad38 100644 --- a/os.go +++ b/os.go @@ -99,3 +99,7 @@ func (OsFs) LstatIfPossible(name string) (os.FileInfo, bool, error) { fi, err := os.Lstat(name) return fi, true, err } + +func (OsFs) Chown(name string, uid, gid int) error { + return os.Chown(name, uid, gid) +} diff --git a/readonlyfs.go b/readonlyfs.go index c6376ec..7783386 100644 --- a/readonlyfs.go +++ b/readonlyfs.go @@ -78,3 +78,7 @@ func (r *ReadOnlyFs) MkdirAll(n string, p os.FileMode) error { func (r *ReadOnlyFs) Create(n string) (File, error) { return nil, syscall.EPERM } + +func (r *ReadOnlyFs) Chown(n string, uid, gid int) error { + return syscall.EPERM +} diff --git a/regexpfs.go b/regexpfs.go index 9d92dbc..44e50c5 100644 --- a/regexpfs.go +++ b/regexpfs.go @@ -212,3 +212,10 @@ func (f *RegexpFile) Truncate(s int64) error { func (f *RegexpFile) WriteString(s string) (int, error) { return f.f.WriteString(s) } + +func (r *RegexpFs) Chown(name string, uid, gid int) error { + if err := r.dirOrMatches(name); err != nil { + return err + } + return r.source.Chown(name, uid, gid) +} diff --git a/sftpfs/sftp.go b/sftpfs/sftp.go index 28721da..6ad0287 100644 --- a/sftpfs/sftp.go +++ b/sftpfs/sftp.go @@ -127,3 +127,7 @@ func (s Fs) Chmod(name string, mode os.FileMode) error { func (s Fs) Chtimes(name string, atime time.Time, mtime time.Time) error { return s.client.Chtimes(name, atime, mtime) } + +func (s Fs) Chown(name string, uid, gid int) error { + return s.client.Chown(name, uid, gid) +} From 5207eb96b775bbd12dce445db3d0c841c05f48ef Mon Sep 17 00:00:00 2001 From: Mike Futerko Date: Mon, 7 Dec 2020 17:10:55 +0200 Subject: [PATCH 2/3] Fix chown pull request errors --- README.md | 1 + httpFs.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/README.md b/README.md index c3e807a..1400bc4 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ AppFs.Open('/tmp/foo') File System Methods Available: ```go Chmod(name string, mode os.FileMode) : error +Chown(name string, uid, gid int) : error Chtimes(name string, atime time.Time, mtime time.Time) : error Create(name string) : File, error Mkdir(name string, perm os.FileMode) : error diff --git a/httpFs.go b/httpFs.go index c421936..2b86e30 100644 --- a/httpFs.go +++ b/httpFs.go @@ -67,6 +67,10 @@ func (h HttpFs) Chmod(name string, mode os.FileMode) error { return h.source.Chmod(name, mode) } +func (h HttpFs) Chown(name string, uid, gid int) error { + return h.source.Chown(name, uid, gid) +} + func (h HttpFs) Chtimes(name string, atime time.Time, mtime time.Time) error { return h.source.Chtimes(name, atime, mtime) } From cdee88cb2d1ac0abea8338f9e206d62e5059c08e Mon Sep 17 00:00:00 2001 From: Mike Futerko Date: Mon, 7 Dec 2020 19:36:56 +0200 Subject: [PATCH 3/3] Add go1.15 to the build matrix --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 45bc131..8156c4e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ arch: go: - "1.13" - "1.14" + - "1.15" - tip os: