eventmanager: don't fail if a directory to be created already exists

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2022-08-10 19:33:02 +02:00
parent 4cd340e07f
commit 78f75cdcb9
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
2 changed files with 20 additions and 11 deletions

View file

@ -589,17 +589,18 @@ func executeDeleteFsAction(deletes []string, replacer *strings.Replacer, usernam
if conn.IsNotExistError(err) {
continue
}
return err
return fmt.Errorf("unable to check item to delete %q, user %q: %w", item, user.Username, err)
}
if info.IsDir() {
if err = conn.RemoveDir(item); err != nil {
return err
return fmt.Errorf("unable to remove dir %q, user %q: %w", item, user.Username, err)
}
} else {
if err = executeDeleteFileFsAction(conn, item, info); err != nil {
return err
return fmt.Errorf("unable to remove file %q, user %q: %w", item, user.Username, err)
}
}
eventManagerLog(logger.LevelDebug, "item %q removed for user %q", item, user.Username)
}
return nil
}
@ -619,11 +620,12 @@ func executeMkDirsFsAction(dirs []string, replacer *strings.Replacer, username s
for _, item := range dirs {
item = replaceWithReplacer(item, replacer)
if err = conn.CheckParentDirs(path.Dir(item)); err != nil {
return err
return fmt.Errorf("unable to check parent dirs for %q, user %q: %w", item, user.Username, err)
}
if err = conn.CreateDir(item, false); err != nil {
return err
if err = conn.createDirIfMissing(item); err != nil {
return fmt.Errorf("unable to create dir %q, user %q: %w", item, user.Username, err)
}
eventManagerLog(logger.LevelDebug, "directory %q created for user %q", item, user.Username)
}
return nil
}
@ -644,8 +646,9 @@ func executeRenameFsAction(renames []dataprovider.KeyValue, replacer *strings.Re
source := replaceWithReplacer(item.Key, replacer)
target := replaceWithReplacer(item.Value, replacer)
if err = conn.Rename(source, target); err != nil {
return err
return fmt.Errorf("unable to rename %q->%q, user %q: %w", source, target, user.Username, err)
}
eventManagerLog(logger.LevelDebug, "rename %q->%q ok, user %q", source, target, user.Username)
}
return nil
}

View file

@ -790,12 +790,18 @@ func TestFilesystemActionErrors(t *testing.T) {
err = os.Chmod(dirPath, 0555)
assert.NoError(t, err)
err = executeDeleteFsAction([]string{"/adir/sub/f.dat"}, testReplacer, username)
assert.Error(t, err)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "unable to remove file")
}
err = executeMkDirsFsAction([]string{"/adir/sub"}, testReplacer, username)
assert.Error(t, err)
err = executeMkDirsFsAction([]string{"/adir/sub/sub"}, testReplacer, username)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "unable to create dir")
}
err = executeMkDirsFsAction([]string{"/adir/sub/sub/sub"}, testReplacer, username)
assert.Error(t, err)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "unable to check parent dirs")
}
err = os.Chmod(dirPath, os.ModePerm)
assert.NoError(t, err)