Skip to content

Commit

Permalink
fix: create parent directories
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Aug 16, 2024
1 parent a56dc55 commit 4d74fea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 5 additions & 2 deletions DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ StateCounter counter

var batchedFileInfo = batchedFiles[hash];
var path = Path.Join(filesPath, outputPaths[0]);
await using var file = FileHelper.Create(path);
await using var file = FileHelper.Create(path, true);
// make a stream that's only capable of reading the
// amount of compressed bytes
await using var limited = new LimitedStream(stream, (int) batchedFileInfo.SizeCompressed);
Expand Down Expand Up @@ -723,6 +723,9 @@ async Task DuplicateInner(string dest) {
throw new DeleteFileException(dest);
}

var parent = PathHelper.GetParent(dest);
Plugin.Resilience.Execute(() => Directory.CreateDirectory(parent));

if (!PInvoke.CreateHardLink(@$"\\?\{dest}", @$"\\?\{path}")) {
throw new IOException($"failed to create hard link {path} -> {dest}");
}
Expand Down Expand Up @@ -811,7 +814,7 @@ await Plugin.Resilience.ExecuteAsync(
using var resp = await Plugin.Client.GetAsync2(uri, HttpCompletionOption.ResponseHeadersRead, this.CancellationToken.Token);
resp.EnsureSuccessStatusCode();
await using var file = FileHelper.Create(path);
await using var file = FileHelper.Create(path, true);
await using var stream = new GloballyThrottledStream(
await resp.Content.ReadAsStreamAsync(this.CancellationToken.Token),
this.Entries
Expand Down
7 changes: 6 additions & 1 deletion Util/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ internal static FileStream OpenRead(string path) {
/// <param name="path">path to file to create</param>
/// <returns>FileStream of created file</returns>
/// <exception cref="AlreadyInUseException"/>
internal static FileStream Create(string path) {
internal static FileStream Create(string path, bool createParents = false) {
if (createParents) {
var parent = PathHelper.GetParent(path);
Directory.CreateDirectory(parent);
}

return Wrap(path, File.Create);
}

Expand Down
4 changes: 4 additions & 0 deletions Util/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ internal static string GetBaseName(string path) {
return after;
}

internal static string GetParent(string path) {
return Path.GetFullPath(Path.Join(path, ".."));
}

internal static string ChangeExtension(string path, string? ext) {
return Path.ChangeExtension(GetBaseName(path), ext);
}
Expand Down

0 comments on commit 4d74fea

Please sign in to comment.