filer: option to create file only if it is new, O_EXCL

This commit is contained in:
Chris Lu
2020-01-22 11:42:40 -08:00
parent 9b01a99d9a
commit d4e75a0d18
11 changed files with 140 additions and 122 deletions

View File

@@ -72,7 +72,7 @@ func (f *Filer) RollbackTransaction(ctx context.Context) error {
return f.store.RollbackTransaction(ctx)
}
func (f *Filer) CreateEntry(ctx context.Context, entry *Entry) error {
func (f *Filer) CreateEntry(ctx context.Context, entry *Entry, o_excl bool) error {
if string(entry.FullPath) == "/" {
return nil
@@ -160,6 +160,9 @@ func (f *Filer) CreateEntry(ctx context.Context, entry *Entry) error {
return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
}
} else {
if o_excl {
return fmt.Errorf("EEXIST: entry %s already exists", entry.FullPath)
}
if err := f.UpdateEntry(ctx, oldEntry, entry); err != nil {
glog.Errorf("update entry %s: %v", entry.FullPath, err)
return fmt.Errorf("update entry %s: %v", entry.FullPath, err)

View File

@@ -30,7 +30,7 @@ func TestCreateAndFind(t *testing.T) {
},
}
if err := filer.CreateEntry(ctx, entry1); err != nil {
if err := filer.CreateEntry(ctx, entry1, false); err != nil {
t.Errorf("create entry %v: %v", entry1.FullPath, err)
return
}

View File

@@ -30,7 +30,7 @@ func TestCreateAndFind(t *testing.T) {
},
}
if err := filer.CreateEntry(ctx, entry1); err != nil {
if err := filer.CreateEntry(ctx, entry1, false); err != nil {
t.Errorf("create entry %v: %v", entry1.FullPath, err)
return
}