mirror of https://github.com/spf13/afero.git
Adding chmod and chtimes support.
This commit is contained in:
parent
0bc61700a3
commit
0c3b992fe6
7
fs.go
7
fs.go
|
@ -27,6 +27,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// File represents a file in the filesystem.
|
// File represents a file in the filesystem.
|
||||||
|
@ -85,6 +86,12 @@ type Fs interface {
|
||||||
|
|
||||||
// The name of this FileSystem
|
// The name of this FileSystem
|
||||||
Name() string
|
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 (
|
var (
|
||||||
|
|
21
memfile.go
21
memfile.go
|
@ -32,16 +32,19 @@ type MemDir interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type InMemoryFile struct {
|
type InMemoryFile struct {
|
||||||
at int64
|
at int64
|
||||||
name string
|
name string
|
||||||
data []byte
|
data []byte
|
||||||
memDir MemDir
|
memDir MemDir
|
||||||
dir bool
|
dir bool
|
||||||
closed bool
|
closed bool
|
||||||
|
mode os.FileMode
|
||||||
|
modtime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func MemFileCreate(name string) *InMemoryFile {
|
func MemFileCreate(name string) *InMemoryFile {
|
||||||
return &InMemoryFile{name: name}
|
return &InMemoryFile{name: name, mode: os.ModeTemporary, modtime: time.Now()}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *InMemoryFile) Close() error {
|
func (f *InMemoryFile) Close() error {
|
||||||
|
@ -184,7 +187,7 @@ type InMemoryFileInfo struct {
|
||||||
// Implements os.FileInfo
|
// Implements os.FileInfo
|
||||||
func (s *InMemoryFileInfo) Name() string { return s.file.Name() }
|
func (s *InMemoryFileInfo) Name() string { return s.file.Name() }
|
||||||
func (s *InMemoryFileInfo) Size() int64 { return int64(len(s.file.data)) }
|
func (s *InMemoryFileInfo) Size() int64 { return int64(len(s.file.data)) }
|
||||||
func (s *InMemoryFileInfo) Mode() os.FileMode { return os.ModeTemporary }
|
func (s *InMemoryFileInfo) Mode() os.FileMode { return s.file.mode }
|
||||||
func (s *InMemoryFileInfo) ModTime() time.Time { return time.Time{} }
|
func (s *InMemoryFileInfo) ModTime() time.Time { return s.file.modtime }
|
||||||
func (s *InMemoryFileInfo) IsDir() bool { return s.file.dir }
|
func (s *InMemoryFileInfo) IsDir() bool { return s.file.dir }
|
||||||
func (s *InMemoryFileInfo) Sys() interface{} { return nil }
|
func (s *InMemoryFileInfo) Sys() interface{} { return nil }
|
||||||
|
|
36
memmap.go
36
memmap.go
|
@ -14,11 +14,13 @@
|
||||||
package afero
|
package afero
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mux = &sync.Mutex{}
|
var mux = &sync.Mutex{}
|
||||||
|
@ -217,3 +219,37 @@ func (m *MemMapFs) Stat(name string) (os.FileInfo, error) {
|
||||||
}
|
}
|
||||||
return &InMemoryFileInfo{file: f.(*InMemoryFile)}, nil
|
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
13
os.go
|
@ -14,7 +14,10 @@
|
||||||
|
|
||||||
package afero
|
package afero
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// OsFs is a Fs implementation that uses functions provided by the os package.
|
// 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) {
|
func (OsFs) Stat(name string) (os.FileInfo, error) {
|
||||||
return os.Stat(name)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue