aboutsummaryrefslogtreecommitdiff
path: root/lib/std/fs.zig
diff options
context:
space:
mode:
authorCarter Snook <cartersnook04@gmail.com>2024-06-12 20:39:18 -0500
committerAndrew Kelley <andrew@ziglang.org>2024-07-28 20:08:52 -0700
commit219acaa1d6ca4279085d7eb9449e7d0469a48432 (patch)
tree8d25f1122c8a168dc37177f82028b56307ba0a0d /lib/std/fs.zig
parent4a77c7f2588ce42c10df53fc1242f684ff506720 (diff)
downloadzig-219acaa1d6ca4279085d7eb9449e7d0469a48432.tar.gz
zig-219acaa1d6ca4279085d7eb9449e7d0469a48432.zip
std.fs.Dir: Refactor atomicSymLink from std.fs
Deprecates std.fs.atomicSymLink and removes the allocator requirement from the new std.fs.Dir.atomicSymLink. Replaces the two usages of this within std. I did not include the TODOs from the original code that were based off of `switch (err) { ..., else => return err }` not having correct inference that cases handled in `...` are impossible in the error union return type because these are not specified in many places but I can add them back if wanted. Thank you @squeek502 for help with fixing buffer overflows!
Diffstat (limited to 'lib/std/fs.zig')
-rw-r--r--lib/std/fs.zig34
1 files changed, 3 insertions, 31 deletions
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
index 8dc79f9ef4..bac4c42ff8 100644
--- a/lib/std/fs.zig
+++ b/lib/std/fs.zig
@@ -101,37 +101,9 @@ pub const base64_encoder = base64.Base64Encoder.init(base64_alphabet, null);
/// Base64 decoder, replacing the standard `+/` with `-_` so that it can be used in a file name on any filesystem.
pub const base64_decoder = base64.Base64Decoder.init(base64_alphabet, null);
-/// TODO remove the allocator requirement from this API
-/// TODO move to Dir
-/// On Windows, both paths should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
-/// On WASI, both paths should be encoded as valid UTF-8.
-/// On other platforms, both paths are an opaque sequence of bytes with no particular encoding.
-pub fn atomicSymLink(allocator: Allocator, existing_path: []const u8, new_path: []const u8) !void {
- if (cwd().symLink(existing_path, new_path, .{})) {
- return;
- } else |err| switch (err) {
- error.PathAlreadyExists => {},
- else => return err, // TODO zig should know this set does not include PathAlreadyExists
- }
-
- const dirname = path.dirname(new_path) orelse ".";
-
- var rand_buf: [AtomicFile.random_bytes_len]u8 = undefined;
- const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64_encoder.calcSize(rand_buf.len));
- defer allocator.free(tmp_path);
- @memcpy(tmp_path[0..dirname.len], dirname);
- tmp_path[dirname.len] = path.sep;
- while (true) {
- crypto.random.bytes(rand_buf[0..]);
- _ = base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);
-
- if (cwd().symLink(existing_path, tmp_path, .{})) {
- return cwd().rename(tmp_path, new_path);
- } else |err| switch (err) {
- error.PathAlreadyExists => continue,
- else => return err, // TODO zig should know this set does not include PathAlreadyExists
- }
- }
+/// Deprecated. Use `cwd().atomicSymLink()` instead.
+pub fn atomicSymLink(_: Allocator, existing_path: []const u8, new_path: []const u8) !void {
+ try cwd().atomicSymLink(existing_path, new_path, .{});
}
/// Same as `Dir.updateFile`, except asserts that both `source_path` and `dest_path`