Skip to content

Commit 4f6cca6

Browse files
authored
Merge pull request #312 from FoundatioFx/bugfix/delete-files-wildcard
Storage bug fixes with DeleteFilesAsync
2 parents f161567 + 29d112d commit 4f6cca6

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public virtual async Task CanGetEmptyFileListOnMissingDirectoryAsync()
3535

3636
using (storage)
3737
{
38-
Assert.Empty(await storage.GetFileListAsync(Guid.NewGuid() + "\\*"));
38+
Assert.Empty(await storage.GetFileListAsync($"{Guid.NewGuid()}\\*"));
3939
}
4040
}
4141

@@ -283,11 +283,12 @@ public virtual async Task CanDeleteEntireFolderAsync()
283283

284284
using (storage)
285285
{
286+
await storage.SaveFileAsync(@"x\README", "hello");
286287
await storage.SaveFileAsync(@"x\hello.txt", "hello");
287288
await storage.SaveFileAsync(@"x\nested\world.csv", "nested world");
288-
Assert.Equal(2, (await storage.GetFileListAsync()).Count);
289+
Assert.Equal(3, (await storage.GetFileListAsync()).Count);
289290

290-
await storage.DeleteFilesAsync(@"x\*");
291+
Assert.Equal(3, await storage.DeleteFilesAsync(@"x\*"));
291292
Assert.Empty(await storage.GetFileListAsync());
292293
}
293294
}
@@ -304,13 +305,14 @@ public virtual async Task CanDeleteEntireFolderWithWildcardAsync()
304305
{
305306
await storage.SaveFileAsync(@"x\hello.txt", "hello");
306307
await storage.SaveFileAsync(@"x\nested\world.csv", "nested world");
307-
Assert.Equal(2, (await storage.GetFileListAsync()).Count);
308+
await storage.SaveFileAsync(@"x\nested\docs\README", "manual");
308309
Assert.Single(await storage.GetFileListAsync(limit: 1));
309-
Assert.Equal(2, (await storage.GetFileListAsync(@"x\*")).Count);
310-
Assert.Single(await storage.GetFileListAsync(@"x\nested\*"));
311-
312-
await storage.DeleteFilesAsync(@"x\*");
310+
Assert.Equal(3, (await storage.GetFileListAsync()).Count);
311+
Assert.Equal(3, (await storage.GetFileListAsync(@"x\*")).Count);
312+
Assert.Equal(2, (await storage.GetFileListAsync(@"x\nested\*")).Count);
313+
Assert.Single(await storage.GetFileListAsync(@"x\nested\docs\*"));
313314

315+
Assert.Equal(3, await storage.DeleteFilesAsync(@"x\*"));
314316
Assert.Empty(await storage.GetFileListAsync());
315317
}
316318
}
@@ -388,20 +390,30 @@ public virtual async Task CanDeleteNestedFolderAsync()
388390
using (storage)
389391
{
390392
await storage.SaveFileAsync(@"x\hello.txt", "hello");
391-
await storage.SaveFileAsync(@"x\nested\world.csv", "nested world");
392393
await storage.SaveFileAsync(@"x\nested\hello.txt", "nested hello");
393-
Assert.Equal(3, (await storage.GetFileListAsync()).Count);
394+
await storage.SaveFileAsync(@"x\nested\world.csv", "nested world");
395+
await storage.SaveFileAsync(@"x\nested\docs\README", "README");
396+
await storage.SaveFileAsync(@"x\nested\media\README", "README");
397+
Assert.Equal(5, (await storage.GetFileListAsync()).Count);
394398
Assert.Single(await storage.GetFileListAsync(limit: 1));
395-
Assert.Equal(3, (await storage.GetFileListAsync(@"x\*")).Count);
396-
Assert.Equal(2, (await storage.GetFileListAsync(@"x\nested\*")).Count);
399+
Assert.Equal(5, (await storage.GetFileListAsync(@"x\*")).Count);
400+
Assert.Equal(4, (await storage.GetFileListAsync(@"x\nested\*")).Count);
397401
Assert.Equal(2, (await storage.GetFileListAsync(@"x\*.txt")).Count);
398402

399-
await storage.DeleteFilesAsync(@"x\nested\*");
403+
Assert.Equal(1, await storage.DeleteFilesAsync(@"x\nested\docs\"));
404+
Assert.Equal(1, await storage.DeleteFilesAsync(@"x\nested\media"));
405+
Assert.Equal(2, await storage.DeleteFilesAsync(@"x\nested\*"));
400406

401407
Assert.Single(await storage.GetFileListAsync());
402408
Assert.True(await storage.ExistsAsync(@"x\hello.txt"));
403409
Assert.False(await storage.ExistsAsync(@"x\nested\hello.txt"));
404410
Assert.False(await storage.ExistsAsync(@"x\nested\world.csv"));
411+
Assert.False(await storage.ExistsAsync(@"x\nested\docs\README"));
412+
Assert.False(await storage.ExistsAsync(@"x\nested\media\README"));
413+
414+
Assert.Equal(1, await storage.DeleteFilesAsync(@"x\hello*"));
415+
Assert.Empty(await storage.GetFileListAsync());
416+
Assert.False(await storage.ExistsAsync(@"x\hello.txt"));
405417
}
406418
}
407419

@@ -426,7 +438,7 @@ public virtual async Task CanDeleteSpecificFilesInNestedFolderAsync()
426438
Assert.Equal(3, (await storage.GetFileListAsync(@"x\nested\*")).Count);
427439
Assert.Equal(3, (await storage.GetFileListAsync(@"x\*.txt")).Count);
428440

429-
await storage.DeleteFilesAsync(@"x\nested\*.txt");
441+
Assert.Equal(2, await storage.DeleteFilesAsync(@"x\nested\*.txt"));
430442

431443
Assert.Equal(3, (await storage.GetFileListAsync()).Count);
432444
Assert.True(await storage.ExistsAsync(@"x\hello.txt"));

src/Foundatio/Storage/FolderFileStorage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public Task<int> DeleteFilesAsync(string searchPattern = null, CancellationToken
239239
if (Directory.Exists(Folder))
240240
{
241241
_logger.LogInformation("Deleting {Directory} directory", Folder);
242-
count += Directory.EnumerateFiles(Folder, "*,*", SearchOption.AllDirectories).Count();
242+
count += Directory.EnumerateFiles(Folder, "*.*", SearchOption.AllDirectories).Count();
243243
Directory.Delete(Folder, true);
244244
_logger.LogTrace("Finished deleting {Directory} directory with {FileCount} files", Folder, count);
245245
}
@@ -249,13 +249,13 @@ public Task<int> DeleteFilesAsync(string searchPattern = null, CancellationToken
249249

250250
searchPattern = searchPattern.NormalizePath();
251251
string path = Path.Combine(Folder, searchPattern);
252-
if (path[path.Length - 1] == Path.DirectorySeparatorChar || path.EndsWith(Path.DirectorySeparatorChar + "*"))
252+
if (path[path.Length - 1] == Path.DirectorySeparatorChar || path.EndsWith($"{Path.DirectorySeparatorChar}*"))
253253
{
254254
string directory = Path.GetDirectoryName(path);
255255
if (Directory.Exists(directory))
256256
{
257257
_logger.LogInformation("Deleting {Directory} directory", directory);
258-
count += Directory.EnumerateFiles(directory, "*,*", SearchOption.AllDirectories).Count();
258+
count += Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories).Count();
259259
Directory.Delete(directory, true);
260260
_logger.LogTrace("Finished deleting {Directory} directory with {FileCount} files", directory, count);
261261
return Task.FromResult(count);
@@ -267,7 +267,7 @@ public Task<int> DeleteFilesAsync(string searchPattern = null, CancellationToken
267267
if (Directory.Exists(path))
268268
{
269269
_logger.LogInformation("Deleting {Directory} directory", path);
270-
count += Directory.EnumerateFiles(path, "*,*", SearchOption.AllDirectories).Count();
270+
count += Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories).Count();
271271
Directory.Delete(path, true);
272272
_logger.LogTrace("Finished deleting {Directory} directory with {FileCount} files", path, count);
273273
return Task.FromResult(count);

src/Foundatio/Storage/InMemoryFileStorage.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,10 @@ public Task<int> DeleteFilesAsync(string searchPattern = null, CancellationToken
228228

229229
if (searchPattern[searchPattern.Length - 1] == Path.DirectorySeparatorChar)
230230
searchPattern = $"{searchPattern}*";
231-
else if (!searchPattern.EndsWith(Path.DirectorySeparatorChar + "*") && !Path.HasExtension(searchPattern))
231+
else if (!searchPattern.EndsWith("*") && !Path.HasExtension(searchPattern))
232232
searchPattern = Path.Combine(searchPattern, "*");
233233

234234
var regex = new Regex($"^{Regex.Escape(searchPattern).Replace("\\*", ".*?")}$");
235-
236235
var keys = _storage.Keys.Where(k => regex.IsMatch(k)).Select(k => _storage[k].Spec).ToList();
237236

238237
_logger.LogInformation("Deleting {FileCount} files matching {SearchPattern} (Regex={SearchPatternRegex})", keys.Count, searchPattern, regex);

0 commit comments

Comments
 (0)