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
68 changes: 30 additions & 38 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,30 @@ 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 err := removePath(nd.FilesRoot, path, force, dashr); err != nil {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
}
}
if len(errs) > 0 {
for _, err = range errs {
e := res.Emit(err.Error())
if e != nil {
return e
}
}
return fmt.Errorf("can't remove some files")
}
return nil
},
}

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

// 'rm a/b/c/' will fail unless we trim the slash at the end
Expand All @@ -1068,70 +1085,45 @@ Remove files or directories.

dir, name := gopath.Split(path)

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

if force {
err := pdir.Unlink(name)
if err != nil {
if err == os.ErrNotExist {
continue
}
errs = append(errs, fmt.Errorf("%s: %w", path, err))
continue
return nil
}
err = pdir.Flush()
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
return err
}
continue
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 {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
continue
return err
}

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

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

err = pdir.Flush()
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", path, err))
}
continue
}
if len(errs) > 0 {
for _, err = range errs {
e := res.Emit(err.Error())
if e != nil {
return e
}
}
return fmt.Errorf("can't remove some files")
}
return nil
},
return pdir.Flush()
}

func getPrefixNew(req *cmds.Request) (cid.Builder, error) {
Expand Down