diff options
author | Santiago M. Mola <santi@mola.io> | 2016-10-31 16:11:07 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-10-31 15:11:07 +0000 |
commit | 5078f52a9f2217027b0f475d5a91e677b3228588 (patch) | |
tree | ad42e162e131456052938d77977c1030281126b9 /utils/fs/os/os.go | |
parent | 659386309f36c482ddc0bb9e854ebda3da216491 (diff) | |
download | go-git-5078f52a9f2217027b0f475d5a91e677b3228588.tar.gz |
utils/fs: add OpenFile method to filesystem interface. (#104)
* utils/fs: add OpenFile method to filesystem interface.
* added OpenFile to fs.Filesystem interface.
* added OpenFile implementation to 'os' filesystem.
* bring back BaseFile.
* utils/fs/os: do not use wildcard import.
* utils/fs/os: implement Open and Create using OpenFile.
Diffstat (limited to 'utils/fs/os/os.go')
-rw-r--r-- | utils/fs/os/os.go | 68 |
1 files changed, 29 insertions, 39 deletions
diff --git a/utils/fs/os/os.go b/utils/fs/os/os.go index 800bd41..02395d6 100644 --- a/utils/fs/os/os.go +++ b/utils/fs/os/os.go @@ -6,7 +6,7 @@ import ( "path" "path/filepath" - . "gopkg.in/src-d/go-git.v4/utils/fs" + "gopkg.in/src-d/go-git.v4/utils/fs" ) // OS a filesystem base on the os filesystem @@ -21,15 +21,24 @@ func NewOS(baseDir string) *OS { } } -// Create creates a new GlusterFSFile -func (fs *OS) Create(filename string) (File, error) { +// Create creates a file and opens it with standard permissions +// and modes O_RDWR, O_CREATE and O_TRUNC. +func (fs *OS) Create(filename string) (fs.File, error) { + return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) +} + +// OpenFile is equivalent to standard os.OpenFile. +// If flag os.O_CREATE is set, all parent directories will be created. +func (fs *OS) OpenFile(filename string, flag int, perm os.FileMode) (fs.File, error) { fullpath := path.Join(fs.base, filename) - if err := fs.createDir(fullpath); err != nil { - return nil, err + if flag|os.O_CREATE != 0 { + if err := fs.createDir(fullpath); err != nil { + return nil, err + } } - f, err := os.Create(fullpath) + f, err := os.OpenFile(fullpath, flag, perm) if err != nil { return nil, err } @@ -55,15 +64,15 @@ func (fs *OS) createDir(fullpath string) error { // ReadDir returns the filesystem info for all the archives under the specified // path. -func (fs *OS) ReadDir(path string) ([]FileInfo, error) { - fullpath := fs.Join(fs.base, path) +func (ofs *OS) ReadDir(path string) ([]fs.FileInfo, error) { + fullpath := ofs.Join(ofs.base, path) l, err := ioutil.ReadDir(fullpath) if err != nil { return nil, err } - var s = make([]FileInfo, len(l)) + var s = make([]fs.FileInfo, len(l)) for i, f := range l { s[i] = f } @@ -82,20 +91,13 @@ func (fs *OS) Rename(from, to string) error { return os.Rename(from, to) } -// Open opens the named file for reading. If successful, methods on the returned -// file can be used for reading only. -func (fs *OS) Open(filename string) (File, error) { - fullpath := fs.Join(fs.base, filename) - f, err := os.Open(fullpath) - if err != nil { - return nil, err - } - - return newOSFile(filename, f), nil +// Open opens a file in read-only mode. +func (fs *OS) Open(filename string) (fs.File, error) { + return fs.OpenFile(filename, os.O_RDONLY, 0) } // Stat returns the FileInfo structure describing file. -func (fs *OS) Stat(filename string) (FileInfo, error) { +func (fs *OS) Stat(filename string) (fs.FileInfo, error) { fullpath := fs.Join(fs.base, filename) return os.Stat(fullpath) } @@ -105,7 +107,7 @@ func (fs *OS) Remove(filename string) error { return os.Remove(fullpath) } -func (fs *OS) TempFile(dir, prefix string) (File, error) { +func (fs *OS) TempFile(dir, prefix string) (fs.File, error) { fullpath := fs.Join(fs.base, dir) if err := fs.createDir(fullpath + string(os.PathSeparator)); err != nil { return nil, err @@ -136,7 +138,7 @@ func (fs *OS) Join(elem ...string) string { // Dir returns a new Filesystem from the same type of fs using as baseDir the // given path -func (fs *OS) Dir(path string) Filesystem { +func (fs *OS) Dir(path string) fs.Filesystem { return NewOS(fs.Join(fs.base, path)) } @@ -147,15 +149,13 @@ func (fs *OS) Base() string { // osFile represents a file in the os filesystem type osFile struct { - filename string - closed bool - file *os.File + fs.BaseFile + file *os.File } -func newOSFile(filename string, file *os.File) File { +func newOSFile(filename string, file *os.File) fs.File { return &osFile{ - filename: filename, - closed: false, + BaseFile: fs.BaseFile{BaseFilename: filename}, file: file, } } @@ -173,7 +173,7 @@ func (f *osFile) Write(p []byte) (int, error) { } func (f *osFile) Close() error { - f.closed = true + f.BaseFile.Closed = true return f.file.Close() } @@ -181,13 +181,3 @@ func (f *osFile) Close() error { func (f *osFile) ReadAt(p []byte, off int64) (n int, err error) { return f.file.ReadAt(p, off) } - -//Filename returns the filename from the File -func (f *osFile) Filename() string { - return f.filename -} - -//IsClosed returns if te file is closed -func (f *osFile) IsClosed() bool { - return f.closed -} |