Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cmds): encapsulate ipfs rm logic in another function #8574

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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