From 3d645268b0030fba5cc2c66bdcc715274cd5bb1c Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Wed, 12 Jul 2017 17:18:52 +0200 Subject: Implement restoring --- fs/os.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'fs/os.go') diff --git a/fs/os.go b/fs/os.go index f251c66..0a4f614 100644 --- a/fs/os.go +++ b/fs/os.go @@ -3,9 +3,14 @@ package fs import ( "io" "os" + "strings" "time" ) +func pathJoin(parts ...string) string { + return strings.Join(parts, string(os.PathSeparator)) +} + func openOSFile(path string) (osFile, error) { fi, err := os.Lstat(path) if err != nil { @@ -79,7 +84,7 @@ func (f osFile) Readdir() (list []File, err error) { } list = append(list, osFile{ - fullpath: f.fullpath + string(os.PathSeparator) + fi.Name(), + fullpath: pathJoin(f.fullpath, fi.Name()), fi: fi, }) } @@ -87,6 +92,15 @@ func (f osFile) Readdir() (list []File, err error) { return } +func (f osFile) GetChild(name string) (File, error) { + path := pathJoin(f.fullpath, name) + fi, err := os.Lstat(path) + if err != nil { + return nil, err + } + return osFile{path, fi}, nil +} + func perms(executable bool) os.FileMode { if executable { return 0755 @@ -96,7 +110,7 @@ func perms(executable bool) os.FileMode { } func (f osFile) CreateChildFile(name string, exec bool) (RegularFile, error) { - p := f.fullpath + string(os.PathSeparator) + name + p := pathJoin(f.fullpath, name) fh, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE, perms(exec)) if err != nil { @@ -108,7 +122,7 @@ func (f osFile) CreateChildFile(name string, exec bool) (RegularFile, error) { } func (f osFile) CreateChildDir(name string) (Dir, error) { - p := f.fullpath + string(os.PathSeparator) + name + p := pathJoin(f.fullpath, name) if err := os.Mkdir(p, perms(true)); err != nil { return nil, err @@ -118,7 +132,7 @@ func (f osFile) CreateChildDir(name string) (Dir, error) { } func (f osFile) CreateChildSymlink(name string, target string) (Symlink, error) { - p := f.fullpath + string(os.PathSeparator) + name + p := pathJoin(f.fullpath, name) err := os.Symlink(target, p) if err != nil { @@ -128,6 +142,10 @@ func (f osFile) CreateChildSymlink(name string, target string) (Symlink, error) return openOSFile(p) } +func (f osFile) RenameChild(oldname, newname string) error { + return os.Rename(pathJoin(f.fullpath, oldname), pathJoin(f.fullpath, newname)) +} + func (f osFile) Readlink() (string, error) { return os.Readlink(f.fullpath) } -- cgit v1.2.3-54-g00ecf