chore(cmds): encapsulate ipfs rm logic in another function (#8574)
Lucas Molas committed
Feb 15, 2022 at 19:30 UTC
898065e09c2dd81703a0f50e77e9dc090680b69a
1 file changed
+55
-63
core/commands/files.go
+55
-63
@@ -1052,74 +1052,13 @@ Remove files or directories.
1052
for _, arg := range req.Arguments {
1053
path, err := checkPath(arg)
1054
if err != nil {
1055
- errs = append(errs, fmt.Errorf("%s: %w", arg, err))
1055
+ errs = append(errs, fmt.Errorf("%s is not a valid path: %w", arg, err))
1056
continue
1057
}
1058
1059
- if path == "/" {
1060
- errs = append(errs, fmt.Errorf("%s: cannot delete root", path))
1061
- continue
1062
- }
1063
-
1064
- // 'rm a/b/c/' will fail unless we trim the slash at the end
1065
- if path[len(path)-1] == '/' {
1066
- path = path[:len(path)-1]
1067
- }
1068
-
1069
- dir, name := gopath.Split(path)
1070
-
1071
- pdir, err := getParentDir(nd.FilesRoot, dir)
1072
- if err != nil {
1073
- if force && err == os.ErrNotExist {
1074
- continue
1075
- }
1076
- errs = append(errs, fmt.Errorf("%s: parent lookup: %w", path, err))
1077
- continue
1078
- }
1079
-
1080
- if force {
1081
- err := pdir.Unlink(name)
1082
- if err != nil {
1083
- if err == os.ErrNotExist {
1084
- continue
1085
- }
1086
- errs = append(errs, fmt.Errorf("%s: %w", path, err))
1087
- continue
1088
- }
1089
- err = pdir.Flush()
1090
- if err != nil {
1091
- errs = append(errs, fmt.Errorf("%s: %w", path, err))
1092
- }
1093
- continue
1094
- }
1095
-
1096
- // get child node by name, when the node is corrupted and nonexistent,
1097
- // it will return specific error.
1098
- child, err := pdir.Child(name)
1099
- if err != nil {
1100
- errs = append(errs, fmt.Errorf("%s: %w", path, err))
1101
- continue
1102
- }
1103
-
1104
- switch child.(type) {
1105
- case *mfs.Directory:
1106
- if !dashr {
1107
- errs = append(errs, fmt.Errorf("%s is a directory, use -r to remove directories", path))
1108
- continue
1109
- }
1110
- }
1111
-
1112
- err = pdir.Unlink(name)
1113
- if err != nil {
1114
- errs = append(errs, fmt.Errorf("%s: %w", path, err))
1115
- continue
1116
- }
1117
-
1118
- err = pdir.Flush()
1119
- if err != nil {
1059
+ if err := removePath(nd.FilesRoot, path, force, dashr); err != nil {
1060
errs = append(errs, fmt.Errorf("%s: %w", path, err))
1061
}
1122
- continue
1062
}
1063
if len(errs) > 0 {
1064
for _, err = range errs {
@@ -1134,6 +1073,59 @@ Remove files or directories.
1073
},
1074
}
1075
1076
+func removePath(filesRoot *mfs.Root, path string, force bool, dashr bool) error {
1077
+ if path == "/" {
1078
+ return fmt.Errorf("cannot delete root")
1079
+ }
1080
+
1081
+ // 'rm a/b/c/' will fail unless we trim the slash at the end
1082
+ if path[len(path)-1] == '/' {
1083
+ path = path[:len(path)-1]
1084
+ }
1085
+
1086
+ dir, name := gopath.Split(path)
1087
+
1088
+ pdir, err := getParentDir(filesRoot, dir)
1089
+ if err != nil {
1090
+ if force && err == os.ErrNotExist {
1091
+ return nil
1092
+ }
1093
+ return err
1094
+ }
1095
+
1096
+ if force {
1097
+ err := pdir.Unlink(name)
1098
+ if err != nil {
1099
+ if err == os.ErrNotExist {
1100
+ return nil
1101
+ }
1102
+ return err
1103
+ }
1104
+ return pdir.Flush()
1105
+ }
1106
+
1107
+ // get child node by name, when the node is corrupted and nonexistent,
1108
+ // it will return specific error.
1109
+ child, err := pdir.Child(name)
1110
+ if err != nil {
1111
+ return err
1112
+ }
1113
+
1114
+ switch child.(type) {
1115
+ case *mfs.Directory:
1116
+ if !dashr {
1117
+ return fmt.Errorf("path is a directory, use -r to remove directories")
1118
+ }
1119
+ }
1120
+
1121
+ err = pdir.Unlink(name)
1122
+ if err != nil {
1123
+ return err
1124
+ }
1125
+
1126
+ return pdir.Flush()
1127
+}
1128
+
1129
func getPrefixNew(req *cmds.Request) (cid.Builder, error) {
1130
cidVer, cidVerSet := req.Options[filesCidVersionOptionName].(int)
1131
hashFunStr, hashFunSet := req.Options[filesHashOptionName].(string)