@@ -91,24 +91,14 @@ func MakeRecursive(ctx context.Context, db dbForMakeRecursive, pathToCreate stri
9191// where `~` - is a root of database
9292func RemoveRecursive (ctx context.Context , db dbFoRemoveRecursive , pathToRemove string ) error {
9393 fullSysTablePath := path .Join (db .Name (), sysDirectory )
94- var removePathRecursively func (int , string ) error
95- removePathRecursively = rmPath (ctx , db , fullSysTablePath , removePathRecursively )
96- pathToRemove = removeWithPrefix (pathToRemove , db )
97-
98- return removePathRecursively (0 , pathToRemove )
99- }
100-
101- // rmPath removes a path recursively from the database
102- func rmPath (
103- ctx context.Context ,
104- db dbFoRemoveRecursive ,
105- fullSysTablePath string ,
106- removePathRecursively func (int , string ) error ,
107- ) func (i int , p string ) error {
108- return func (i int , p string ) error {
109- err := checkDirectoryExists (ctx , db , p )
110- if err != nil {
111- return err
94+ var rmPath func (int , string ) error
95+ rmPath = func (i int , p string ) error {
96+ if exists , err := IsDirectoryExists (ctx , db .Scheme (), p ); err != nil {
97+ return xerrors .WithStackTrace (
98+ fmt .Errorf ("check directory %q exists failed: %w" , p , err ),
99+ )
100+ } else if ! exists {
101+ return nil
112102 }
113103
114104 entry , err := db .Scheme ().DescribePath (ctx , p )
@@ -118,9 +108,15 @@ func rmPath(
118108 )
119109 }
120110
121- dir , err := checkEntryAndListDirectory (ctx , db , & entry , p )
111+ if entry .Type != scheme .EntryDirectory && entry .Type != scheme .EntryDatabase {
112+ return nil
113+ }
114+
115+ dir , err := db .Scheme ().ListDirectory (ctx , p )
122116 if err != nil {
123- return err
117+ return xerrors .WithStackTrace (
118+ fmt .Errorf ("listing directory %q failed: %w" , p , err ),
119+ )
124120 }
125121
126122 for j := range dir .Children {
@@ -130,7 +126,7 @@ func rmPath(
130126 }
131127 switch t := dir .Children [j ].Type ; t {
132128 case scheme .EntryDirectory :
133- if err = removePathRecursively (i + 1 , pt ); err != nil {
129+ if err = rmPath (i + 1 , pt ); err != nil {
134130 return xerrors .WithStackTrace (
135131 fmt .Errorf ("recursive removing directory %q failed: %w" , pt , err ),
136132 )
@@ -161,13 +157,22 @@ func rmPath(
161157 }
162158 }
163159
164- err = removeDirectoryIfNotEntry (ctx , db , & entry , p )
160+ if entry .Type != scheme .EntryDirectory {
161+ return nil
162+ }
163+
164+ err = db .Scheme ().RemoveDirectory (ctx , p )
165165 if err != nil {
166- return err
166+ return xerrors .WithStackTrace (
167+ fmt .Errorf ("removing directory %q failed: %w" , p , err ),
168+ )
167169 }
168170
169171 return nil
170172 }
173+ pathToRemove = removeWithPrefix (pathToRemove , db )
174+
175+ return rmPath (0 , pathToRemove )
171176}
172177
173178// removeWithPrefix prepends the db.Name() to the pathToRemove string if it does not already have the prefix.
@@ -178,59 +183,3 @@ func removeWithPrefix(pathToRemove string, db dbFoRemoveRecursive) string {
178183
179184 return pathToRemove
180185}
181-
182- // checkDirectoryExists checks if a directory exists in the specified database.
183- func checkDirectoryExists (ctx context.Context , db dbFoRemoveRecursive , p string ) error {
184- exists , err := IsDirectoryExists (ctx , db .Scheme (), p )
185- if err != nil {
186- return xerrors .WithStackTrace (
187- fmt .Errorf ("check directory %q exists failed: %w" , p , err ),
188- )
189- } else if ! exists {
190- return nil
191- }
192-
193- return nil
194- }
195-
196- // removeDirectoryIfNotEntry removes a directory if it is not an entry.
197- func removeDirectoryIfNotEntry (
198- ctx context.Context ,
199- db dbFoRemoveRecursive ,
200- entry * scheme.Entry ,
201- p string ,
202- ) error {
203- if entry .Type == scheme .EntryDirectory {
204- err := db .Scheme ().RemoveDirectory (ctx , p )
205- if err != nil {
206- return xerrors .WithStackTrace (
207- fmt .Errorf ("removing directory %q failed: %w" , p , err ),
208- )
209- }
210- }
211-
212- return nil
213- }
214-
215- // checkEntryAndListDirectory checks if the given entry is not a EntryDirectory or a EntryDatabase
216- // and lists its children directories and files.
217- func checkEntryAndListDirectory (
218- ctx context.Context ,
219- db dbFoRemoveRecursive ,
220- entry * scheme.Entry ,
221- p string ,
222- ) (scheme.Directory , error ) {
223- var dir scheme.Directory
224- if entry .Type != scheme .EntryDirectory && entry .Type != scheme .EntryDatabase {
225- return dir , nil
226- }
227-
228- dir , err := db .Scheme ().ListDirectory (ctx , p )
229- if err != nil {
230- return dir , xerrors .WithStackTrace (
231- fmt .Errorf ("listing directory %q failed: %w" , p , err ),
232- )
233- }
234-
235- return dir , nil
236- }
0 commit comments