Skip to content

Commit 250c446

Browse files
committed
If file creation fails, try to delete the file first
1 parent c91aadb commit 250c446

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

src/processor.zig

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,17 @@ fn encryptFileZeroCopy(
108108
io_hints.adviseMemory(input_mapped.ptr, input_size, .willneed);
109109

110110
// Create output file with correct size and read/write permissions for mmap
111-
const output_file = try std.fs.cwd().createFile(output_path, .{ .read = true });
111+
// If it fails due to existing read-only file, delete and retry
112+
const output_file = blk: {
113+
break :blk std.fs.cwd().createFile(output_path, .{ .read = true }) catch |err| {
114+
if (err == error.AccessDenied) {
115+
// Try deleting the existing file (might be read-only) and retry
116+
std.fs.cwd().deleteFile(output_path) catch {};
117+
break :blk try std.fs.cwd().createFile(output_path, .{ .read = true });
118+
}
119+
return err;
120+
};
121+
};
112122
defer output_file.close();
113123

114124
// Check for integer overflow when calculating output size
@@ -164,7 +174,17 @@ fn encryptFileBuffered(
164174
defer allocator.free(encrypted);
165175

166176
// Write output file
167-
const output_file = try std.fs.cwd().createFile(output_path, .{});
177+
// If it fails due to existing read-only file, delete and retry
178+
const output_file = blk: {
179+
break :blk std.fs.cwd().createFile(output_path, .{}) catch |err| {
180+
if (err == error.AccessDenied) {
181+
// Try deleting the existing file (might be read-only) and retry
182+
std.fs.cwd().deleteFile(output_path) catch {};
183+
break :blk try std.fs.cwd().createFile(output_path, .{});
184+
}
185+
return err;
186+
};
187+
};
168188
defer output_file.close();
169189

170190
try output_file.writeAll(encrypted);
@@ -261,7 +281,17 @@ fn decryptFileZeroCopy(
261281
io_hints.adviseMemory(input_mapped.ptr, input_size, .willneed);
262282

263283
// Create output file with correct size and read/write permissions for mmap
264-
const output_file = try std.fs.cwd().createFile(output_path, .{ .read = true });
284+
// If it fails due to existing read-only file, delete and retry
285+
const output_file = blk: {
286+
break :blk std.fs.cwd().createFile(output_path, .{ .read = true }) catch |err| {
287+
if (err == error.AccessDenied) {
288+
// Try deleting the existing file (might be read-only) and retry
289+
std.fs.cwd().deleteFile(output_path) catch {};
290+
break :blk try std.fs.cwd().createFile(output_path, .{ .read = true });
291+
}
292+
return err;
293+
};
294+
};
265295
defer output_file.close();
266296

267297
// Check for integer underflow when calculating output size
@@ -318,7 +348,17 @@ fn decryptFileBuffered(
318348
defer allocator.free(plaintext);
319349

320350
// Write output file
321-
const output_file = try std.fs.cwd().createFile(output_path, .{});
351+
// If it fails due to existing read-only file, delete and retry
352+
const output_file = blk: {
353+
break :blk std.fs.cwd().createFile(output_path, .{}) catch |err| {
354+
if (err == error.AccessDenied) {
355+
// Try deleting the existing file (might be read-only) and retry
356+
std.fs.cwd().deleteFile(output_path) catch {};
357+
break :blk try std.fs.cwd().createFile(output_path, .{});
358+
}
359+
return err;
360+
};
361+
};
322362
defer output_file.close();
323363

324364
try output_file.writeAll(plaintext);

0 commit comments

Comments
 (0)