Adding chmod and chtimes support.

This commit is contained in:
spf13 2014-10-31 23:36:45 -04:00
parent 0bc61700a3
commit 0c3b992fe6
4 changed files with 67 additions and 10 deletions

7
fs.go
View File

@ -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 (

View File

@ -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 }

View File

@ -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
}

13
os.go
View File

@ -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)
}