aboutsummaryrefslogtreecommitdiffstats
path: root/utils/fs/os.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/fs/os.go')
-rw-r--r--utils/fs/os.go66
1 files changed, 52 insertions, 14 deletions
diff --git a/utils/fs/os.go b/utils/fs/os.go
index a0d197d..e775a5a 100644
--- a/utils/fs/os.go
+++ b/utils/fs/os.go
@@ -7,21 +7,21 @@ import (
"path/filepath"
)
-// OSClient a filesystem based on OSClient
+// OS a filesystem base on the os filesystem
type OS struct {
- RootDir string
+ base string
}
-// NewOSClient returns a new OSClient
-func NewOS(rootDir string) *OS {
+// NewOS returns a new OS filesystem
+func NewOS(baseDir string) *OS {
return &OS{
- RootDir: rootDir,
+ base: baseDir,
}
}
// Create creates a new GlusterFSFile
func (fs *OS) Create(filename string) (File, error) {
- fullpath := path.Join(fs.RootDir, filename)
+ fullpath := path.Join(fs.base, filename)
if err := fs.createDir(fullpath); err != nil {
return nil, err
@@ -32,6 +32,11 @@ func (fs *OS) Create(filename string) (File, error) {
return nil, err
}
+ filename, err = filepath.Rel(fs.base, fullpath)
+ if err != nil {
+ return nil, err
+ }
+
return &OSFile{
BaseFile: BaseFile{filename: filename},
file: f,
@@ -52,7 +57,7 @@ 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.RootDir, path)
+ fullpath := fs.Join(fs.base, path)
l, err := ioutil.ReadDir(fullpath)
if err != nil {
@@ -68,8 +73,8 @@ func (fs *OS) ReadDir(path string) ([]FileInfo, error) {
}
func (fs *OS) Rename(from, to string) error {
- from = fs.Join(fs.RootDir, from)
- to = fs.Join(fs.RootDir, to)
+ from = fs.Join(fs.base, from)
+ to = fs.Join(fs.base, to)
if err := fs.createDir(to); err != nil {
return err
@@ -78,9 +83,10 @@ 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.RootDir, filename)
-
+ fullpath := fs.Join(fs.base, filename)
f, err := os.Open(fullpath)
if err != nil {
return nil, err
@@ -92,24 +98,56 @@ func (fs *OS) Open(filename string) (File, error) {
}, nil
}
+// Stat returns the FileInfo structure describing file.
func (fs *OS) Stat(filename string) (FileInfo, error) {
- fullpath := fs.Join(fs.RootDir, filename)
+ fullpath := fs.Join(fs.base, filename)
return os.Stat(fullpath)
}
+func (fs *OS) TempFile(dir, prefix string) (File, error) {
+ fullpath := fs.Join(fs.base, dir)
+ if err := fs.createDir(fullpath + string(os.PathSeparator)); err != nil {
+ return nil, err
+ }
+
+ f, err := ioutil.TempFile(fullpath, prefix)
+ if err != nil {
+ return nil, err
+ }
+
+ s, err := f.Stat()
+ if err != nil {
+ return nil, err
+ }
+
+ filename, err := filepath.Rel(fs.base, fs.Join(fullpath, s.Name()))
+ if err != nil {
+ return nil, err
+ }
+
+ return &OSFile{
+ BaseFile: BaseFile{filename: filename},
+ file: f,
+ }, nil
+}
+
// Join joins the specified elements using the filesystem separator.
func (fs *OS) Join(elem ...string) string {
return filepath.Join(elem...)
}
+// Dir returns a new Filesystem from the same type of fs using as baseDir the
+// given path
func (fs *OS) Dir(path string) Filesystem {
- return NewOS(fs.Join(fs.RootDir, path))
+ return NewOS(fs.Join(fs.base, path))
}
+// Base returns the base path of the filesytem
func (fs *OS) Base() string {
- return fs.RootDir
+ return fs.base
}
+// OSFile represents a file in the os filesystem
type OSFile struct {
file *os.File
BaseFile