From 0c3b992fe65a59969fa6c79c3a28041877eb7d64 Mon Sep 17 00:00:00 2001 From: spf13 Date: Fri, 31 Oct 2014 23:36:45 -0400 Subject: [PATCH] Adding chmod and chtimes support. --- fs.go | 7 +++++++ memfile.go | 21 ++++++++++++--------- memmap.go | 36 ++++++++++++++++++++++++++++++++++++ os.go | 13 ++++++++++++- 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/fs.go b/fs.go index e9b9cbf..593ca23 100644 --- a/fs.go +++ b/fs.go @@ -27,6 +27,7 @@ import ( "io" "os" "sort" + "time" ) // File represents a file in the filesystem. @@ -85,6 +86,12 @@ type Fs interface { // The name of this FileSystem Name() string + + //Chmod changes the mode of the named file to mode. + Chmod(name string, mode os.FileMode) error + + //Chtimes changes the access and modification times of the named file + Chtimes(name string, atime time.Time, mtime time.Time) error } var ( diff --git a/memfile.go b/memfile.go index ef6fdaf..ceaf528 100644 --- a/memfile.go +++ b/memfile.go @@ -32,16 +32,19 @@ type MemDir interface { } type InMemoryFile struct { - at int64 - name string - data []byte - memDir MemDir - dir bool - closed bool + at int64 + name string + data []byte + memDir MemDir + dir bool + closed bool + mode os.FileMode + modtime time.Time } func MemFileCreate(name string) *InMemoryFile { - return &InMemoryFile{name: name} + return &InMemoryFile{name: name, mode: os.ModeTemporary, modtime: time.Now()} +} } func (f *InMemoryFile) Close() error { @@ -184,7 +187,7 @@ type InMemoryFileInfo struct { // Implements os.FileInfo func (s *InMemoryFileInfo) Name() string { return s.file.Name() } func (s *InMemoryFileInfo) Size() int64 { return int64(len(s.file.data)) } -func (s *InMemoryFileInfo) Mode() os.FileMode { return os.ModeTemporary } -func (s *InMemoryFileInfo) ModTime() time.Time { return time.Time{} } +func (s *InMemoryFileInfo) Mode() os.FileMode { return s.file.mode } +func (s *InMemoryFileInfo) ModTime() time.Time { return s.file.modtime } func (s *InMemoryFileInfo) IsDir() bool { return s.file.dir } func (s *InMemoryFileInfo) Sys() interface{} { return nil } diff --git a/memmap.go b/memmap.go index 9b4d0a0..af3c653 100644 --- a/memmap.go +++ b/memmap.go @@ -14,11 +14,13 @@ package afero import ( + "errors" "os" "path" "path/filepath" "strings" "sync" + "time" ) var mux = &sync.Mutex{} @@ -217,3 +219,37 @@ func (m *MemMapFs) Stat(name string) (os.FileInfo, error) { } return &InMemoryFileInfo{file: f.(*InMemoryFile)}, nil } + +func (m *MemMapFs) Chmod(name string, mode os.FileMode) error { + f, ok := m.getData()[name] + if !ok { + return &os.PathError{"chmod", name, ErrFileNotFound} + } + + ff, ok := f.(*InMemoryFile) + if ok { + m.lock() + ff.mode = mode + m.unlock() + } else { + return errors.New("Unable to Chmod Memory File") + } + return nil +} + +func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error { + f, ok := m.getData()[name] + if !ok { + return &os.PathError{"chtimes", name, ErrFileNotFound} + } + + ff, ok := f.(*InMemoryFile) + if ok { + m.lock() + ff.modtime = mtime + m.unlock() + } else { + return errors.New("Unable to Chtime Memory File") + } + return nil +} diff --git a/os.go b/os.go index 4f472fc..30f23ca 100644 --- a/os.go +++ b/os.go @@ -14,7 +14,10 @@ package afero -import "os" +import ( + "os" + "time" +) // OsFs is a Fs implementation that uses functions provided by the os package. // @@ -59,3 +62,11 @@ func (OsFs) Rename(oldname, newname string) error { func (OsFs) Stat(name string) (os.FileInfo, error) { return os.Stat(name) } + +func (OsFs) Chmod(name string, mode os.FileMode) error { + return os.Chmod(name, mode) +} + +func (OsFs) Chtimes(name string, atime time.Time, mtime time.Time) error { + return os.Chtimes(name, atime, mtime) +}