Skip to content

Commit

Permalink
chore(cmds): encapsulate ipfs rm logic in another function
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis committed Nov 30, 2021
1 parent 029d82c commit 367aa90
Showing 1 changed file with 55 additions and 63 deletions.
118 changes: 55 additions & 63 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,74 +1052,13 @@ Remove files or directories.
for _, arg := range req.Arguments {
path, err := checkPath(arg)
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", arg, err))
errs = append(errs, fmt.Errorf("%s is not a valid path: %w", arg, err))
continue
}

if path == "/" {
errs = append(errs, fmt.Errorf("%s: cannot delete root", path))
continue
}

// 'rm a/b/c/' will fail unless we trim the slash at the end
if path[len(path)-1] == '/' {
path = path[:len(path)-1]
}

dir, name := gopath.Split(path)

pdir, err := getParentDir(nd.FilesRoot, dir)
if err != nil {
if force && err == os.ErrNotExist {
continue
}
errs = append(errs, fmt.Errorf("%s: parent lookup: %w", path, err))
continue
}

if force {
err := pdir.Unlink(name)
if err != nil {
if err == os.ErrNotExist {
continue
}
errs = append(errs, fmt.Errorf("%s: %w", path, err))
continue
}
err = pdir.Flush()
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
}
continue
}

// get child node by name, when the node is corrupted and nonexistent,
// it will return specific error.
child, err := pdir.Child(name)
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
continue
}

switch child.(type) {
case *mfs.Directory:
if !dashr {
errs = append(errs, fmt.Errorf("%s is a directory, use -r to remove directories", path))
continue
}
}

err = pdir.Unlink(name)
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
continue
}

err = pdir.Flush()
if err != nil {
if err := removePath(nd.FilesRoot, path, force, dashr); err != nil {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
}
continue
}
if len(errs) > 0 {
for _, err = range errs {
Expand All @@ -1134,6 +1073,59 @@ Remove files or directories.
},
}

func removePath(filesRoot *mfs.Root, path string, force bool, dashr bool) error {
if path == "/" {
return fmt.Errorf("cannot delete root")
}

// 'rm a/b/c/' will fail unless we trim the slash at the end
if path[len(path)-1] == '/' {
path = path[:len(path)-1]
}

dir, name := gopath.Split(path)

pdir, err := getParentDir(filesRoot, dir)
if err != nil {
if force && err == os.ErrNotExist {
return nil
}
return err
}

if force {
err := pdir.Unlink(name)
if err != nil {
if err == os.ErrNotExist {
return nil
}
return err
}
return pdir.Flush()
}

// get child node by name, when the node is corrupted and nonexistent,
// it will return specific error.
child, err := pdir.Child(name)
if err != nil {
return err
}

switch child.(type) {
case *mfs.Directory:
if !dashr {
return fmt.Errorf("path is a directory, use -r to remove directories")
}
}

err = pdir.Unlink(name)
if err != nil {
return err
}

return pdir.Flush()
}

func getPrefixNew(req *cmds.Request) (cid.Builder, error) {
cidVer, cidVerSet := req.Options[filesCidVersionOptionName].(int)
hashFunStr, hashFunSet := req.Options[filesHashOptionName].(string)
Expand Down

0 comments on commit 367aa90

Please sign in to comment.