From bab0697f0e8a98cfa28c7527ee616e954c7afe91 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Tue, 8 Aug 2017 08:05:52 +0200 Subject: Add OpenWritable to fs interfaces --- backup/backup_test.go | 6 +++--- backup/restore.go | 12 +++++++----- fs/fs.go | 3 ++- fs/memory.go | 6 +++++- fs/os.go | 10 +++++++++- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/backup/backup_test.go b/backup/backup_test.go index 67fbb6a..da14117 100644 --- a/backup/backup_test.go +++ b/backup/backup_test.go @@ -49,13 +49,13 @@ func mkfile(t *testing.T, d fs.Dir, name string, exec bool, content []byte) { t.Fatalf("Could not create file %s: %s", name, err) } - rwc, err := f.Open() + wc, err := f.OpenWritable() if err != nil { t.Fatalf("Could not create file %s: %s", name, err) } - defer rwc.Close() + defer wc.Close() - if _, err := rwc.Write(content); err != nil { + if _, err := wc.Write(content); err != nil { t.Fatalf("Could not create file %s: %s", name, err) } } diff --git a/backup/restore.go b/backup/restore.go index 6a03200..3a384a9 100644 --- a/backup/restore.go +++ b/backup/restore.go @@ -7,6 +7,7 @@ import ( "code.laria.me/petrific/storage" "fmt" "io" + "math/rand" "os" ) @@ -48,21 +49,22 @@ func RestoreDir(s storage.Storage, id objects.ObjectId, root fs.Dir) error { for name, file_info := range tree { switch file_info.Type() { case objects.TETFile: - tmpname := fmt.Sprintf(".petrific-%d-%s", os.Getpid(), id) + tmpname := fmt.Sprintf(".petrific-%d-%08x%08x%08x%08x", os.Getpid(), rand.Uint32(), rand.Uint32(), rand.Uint32(), rand.Uint32()) new_file, err := root.CreateChildFile(tmpname, execBitFromACL(file_info.ACL())) if err != nil { return err } - rwc, err := new_file.Open() + + wc, err := new_file.OpenWritable() if err != nil { return err } - if err := RestoreFile(s, file_info.(objects.TreeEntryFile).Ref, rwc); err != nil { - rwc.Close() + if err := RestoreFile(s, file_info.(objects.TreeEntryFile).Ref, wc); err != nil { + wc.Close() return err } - rwc.Close() + wc.Close() if err := root.RenameChild(tmpname, name); err != nil { return err diff --git a/fs/fs.go b/fs/fs.go index b06ded8..d6d0618 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -23,7 +23,8 @@ type File interface { type RegularFile interface { File - Open() (io.ReadWriteCloser, error) + Open() (io.ReadCloser, error) + OpenWritable() (io.WriteCloser, error) } type Dir interface { diff --git a/fs/memory.go b/fs/memory.go index 221ff36..2bc11dc 100644 --- a/fs/memory.go +++ b/fs/memory.go @@ -40,7 +40,11 @@ type memfsFile struct { func (memfsFile) Type() FileType { return FFile } -func (f memfsFile) Open() (io.ReadWriteCloser, error) { +func (f memfsFile) Open() (io.ReadCloser, error) { + return f, nil +} + +func (f memfsFile) OpenWritable() (io.WriteCloser, error) { return f, nil } diff --git a/fs/os.go b/fs/os.go index f41d111..166cf92 100644 --- a/fs/os.go +++ b/fs/os.go @@ -58,7 +58,7 @@ func (f osFile) Delete() error { return os.RemoveAll(f.fullpath) } -func (f osFile) Open() (io.ReadWriteCloser, error) { +func (f osFile) Open() (io.ReadCloser, error) { fh, err := os.Open(f.fullpath) if err != nil { return nil, err @@ -66,6 +66,14 @@ func (f osFile) Open() (io.ReadWriteCloser, error) { return fh, nil } +func (f osFile) OpenWritable() (io.WriteCloser, error) { + fh, err := os.Create(f.fullpath) + if err != nil { + return nil, err + } + return fh, nil +} + func (f osFile) Readdir() (list []File, err error) { fh, err := os.Open(f.fullpath) if err != nil { -- cgit v1.2.3-70-g09d2