Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Dir.writeFile2 -> Dir.writeFile and update all callsites #19844

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/compiler/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ pub fn main() !void {
}
const s = std.fs.path.sep_str;
const tmp_sub_path = "tmp" ++ s ++ (output_tmp_nonce orelse fatal("missing -Z arg", .{}));
local_cache_directory.handle.writeFile2(.{
local_cache_directory.handle.writeFile(.{
.sub_path = tmp_sub_path,
.data = buffer.items,
.flags = .{ .exclusive = true },
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/reduce.zig
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn main() !void {
}
}

try std.fs.cwd().writeFile(root_source_file_path, rendered.items);
try std.fs.cwd().writeFile(.{ .sub_path = root_source_file_path, .data = rendered.items });
// std.debug.print("trying this code:\n{s}\n", .{rendered.items});

const interestingness = try runCheck(arena, interestingness_argv.items);
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn main() !void {
fixups.clearRetainingCapacity();
rendered.clearRetainingCapacity();
try tree.renderToArrayList(&rendered, fixups);
try std.fs.cwd().writeFile(root_source_file_path, rendered.items);
try std.fs.cwd().writeFile(.{ .sub_path = root_source_file_path, .data = rendered.items });

return std.process.cleanExit();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/resinator/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn main() !void {
defer allocator.free(full_input);

if (options.preprocess == .only) {
try std.fs.cwd().writeFile(options.output_filename, full_input);
try std.fs.cwd().writeFile(.{ .sub_path = options.output_filename, .data = full_input });
return;
}

Expand Down
14 changes: 7 additions & 7 deletions lib/std/Build/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ pub fn readSmallFile(dir: fs.Dir, sub_path: []const u8, buffer: []u8) ![]u8 {
pub fn writeSmallFile(dir: fs.Dir, sub_path: []const u8, data: []const u8) !void {
assert(data.len <= 255);
if (builtin.os.tag == .windows) {
return dir.writeFile(sub_path, data);
return dir.writeFile(.{ .sub_path = sub_path, .data = data });
} else {
return dir.symLink(data, sub_path, .{});
}
Expand Down Expand Up @@ -1052,7 +1052,7 @@ test "cache file and then recall it" {
const temp_file = "test.txt";
const temp_manifest_dir = "temp_manifest_dir";

try tmp.dir.writeFile(temp_file, "Hello, world!\n");
try tmp.dir.writeFile(.{ .sub_path = temp_file, .data = "Hello, world!\n" });

// Wait for file timestamps to tick
const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
Expand Down Expand Up @@ -1120,7 +1120,7 @@ test "check that changing a file makes cache fail" {
const original_temp_file_contents = "Hello, world!\n";
const updated_temp_file_contents = "Hello, world; but updated!\n";

try tmp.dir.writeFile(temp_file, original_temp_file_contents);
try tmp.dir.writeFile(.{ .sub_path = temp_file, .data = original_temp_file_contents });

// Wait for file timestamps to tick
const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
Expand Down Expand Up @@ -1156,7 +1156,7 @@ test "check that changing a file makes cache fail" {
try ch.writeManifest();
}

try tmp.dir.writeFile(temp_file, updated_temp_file_contents);
try tmp.dir.writeFile(.{ .sub_path = temp_file, .data = updated_temp_file_contents });

{
var ch = cache.obtain();
Expand Down Expand Up @@ -1241,8 +1241,8 @@ test "Manifest with files added after initial hash work" {
const temp_file2 = "cache_hash_post_file_test2.txt";
const temp_manifest_dir = "cache_hash_post_file_manifest_dir";

try tmp.dir.writeFile(temp_file1, "Hello, world!\n");
try tmp.dir.writeFile(temp_file2, "Hello world the second!\n");
try tmp.dir.writeFile(.{ .sub_path = temp_file1, .data = "Hello, world!\n" });
try tmp.dir.writeFile(.{ .sub_path = temp_file2, .data = "Hello world the second!\n" });

// Wait for file timestamps to tick
const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
Expand Down Expand Up @@ -1292,7 +1292,7 @@ test "Manifest with files added after initial hash work" {
try testing.expect(mem.eql(u8, &digest1, &digest2));

// Modify the file added after initial hash
try tmp.dir.writeFile(temp_file2, "Hello world the second, updated\n");
try tmp.dir.writeFile(.{ .sub_path = temp_file2, .data = "Hello world the second, updated\n" });

// Wait for file timestamps to tick
const initial_time2 = try testGetCurrentFileTimestamp(tmp.dir);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
);

const args_file = "args" ++ fs.path.sep_str ++ args_hex_hash;
try b.cache_root.handle.writeFile(args_file, args);
try b.cache_root.handle.writeFile(.{ .sub_path = args_file, .data = args });

const resolved_args_file = try mem.concat(arena, u8, &.{
"@",
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step/ConfigHeader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
});
};

b.cache_root.handle.writeFile(sub_path, output.items) catch |err| {
b.cache_root.handle.writeFile(.{ .sub_path = sub_path, .data = output.items }) catch |err| {
return step.fail("unable to write file '{}{s}': {s}", .{
b.cache_root, sub_path, @errorName(err),
});
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
});
};

b.cache_root.handle.writeFile(tmp_sub_path, self.contents.items) catch |err| {
b.cache_root.handle.writeFile(.{ .sub_path = tmp_sub_path, .data = self.contents.items }) catch |err| {
return step.fail("unable to write options to '{}{s}': {s}", .{
b.cache_root, tmp_sub_path, @errorName(err),
});
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ fn runCommand(
b.cache_root, sub_path_dirname, @errorName(err),
});
};
b.cache_root.handle.writeFile(sub_path, stream.bytes.?) catch |err| {
b.cache_root.handle.writeFile(.{ .sub_path = sub_path, .data = stream.bytes.? }) catch |err| {
return step.fail("unable to write file '{}{s}': {s}", .{
b.cache_root, sub_path, @errorName(err),
});
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Build/Step/WriteFile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
}
switch (output_source_file.contents) {
.bytes => |bytes| {
b.build_root.handle.writeFile(output_source_file.sub_path, bytes) catch |err| {
b.build_root.handle.writeFile(.{ .sub_path = output_source_file.sub_path, .data = bytes }) catch |err| {
return step.fail("unable to write file '{}{s}': {s}", .{
b.build_root, output_source_file.sub_path, @errorName(err),
});
Expand Down Expand Up @@ -311,7 +311,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
}
switch (file.contents) {
.bytes => |bytes| {
cache_dir.writeFile(file.sub_path, bytes) catch |err| {
cache_dir.writeFile(.{ .sub_path = file.sub_path, .data = bytes }) catch |err| {
return step.fail("unable to write file '{}{s}{c}{s}': {s}", .{
b.cache_root, cache_path, fs.path.sep, file.sub_path, @errorName(err),
});
Expand Down
9 changes: 6 additions & 3 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ test printLineFromFileAnyOs {
{
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile("one_line.zig", "no new lines in this file, but one is printed anyway");
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });

try expectError(error.EndOfFile, printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 }));

Expand All @@ -1535,11 +1535,14 @@ test printLineFromFileAnyOs {
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile("three_lines.zig",
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
.data =
\\1
\\2
\\3
);
,
});

try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
try expectEqualStrings("1\n", output.items);
Expand Down
16 changes: 3 additions & 13 deletions lib/std/fs/Dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2339,18 +2339,6 @@ fn deleteTreeOpenInitialSubpath(self: Dir, sub_path: []const u8, kind_hint: File

pub const WriteFileError = File.WriteError || File.OpenError;

/// Deprecated: use `writeFile2`.
/// On Windows, `sub_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
/// On WASI, `sub_path` should be encoded as valid UTF-8.
/// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding.
pub fn writeFile(self: Dir, sub_path: []const u8, data: []const u8) WriteFileError!void {
return writeFile2(self, .{
.sub_path = sub_path,
.data = data,
.flags = .{},
});
}

pub const WriteFileOptions = struct {
/// On Windows, `sub_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
/// On WASI, `sub_path` should be encoded as valid UTF-8.
Expand All @@ -2361,12 +2349,14 @@ pub const WriteFileOptions = struct {
};

/// Writes content to the file system, using the file creation flags provided.
pub fn writeFile2(self: Dir, options: WriteFileOptions) WriteFileError!void {
pub fn writeFile(self: Dir, options: WriteFileOptions) WriteFileError!void {
var file = try self.createFile(options.sub_path, options.flags);
defer file.close();
try file.writeAll(options.data);
}

pub const writeFile2 = @compileError("deprecated; renamed to writeFile");

pub const AccessError = posix.AccessError;

/// Test accessing `sub_path`.
Expand Down
48 changes: 28 additions & 20 deletions lib/std/fs/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ test "Dir.readLink" {
fn impl(ctx: *TestContext) !void {
// Create some targets
const file_target_path = try ctx.transformPath("file.txt");
try ctx.dir.writeFile(file_target_path, "nonsense");
try ctx.dir.writeFile(.{ .sub_path = file_target_path, .data = "nonsense" });
const dir_target_path = try ctx.transformPath("subdir");
try ctx.dir.makeDir(dir_target_path);

Expand Down Expand Up @@ -398,7 +398,7 @@ test "readLinkAbsolute" {
defer tmp.cleanup();

// Create some targets
try tmp.dir.writeFile("file.txt", "nonsense");
try tmp.dir.writeFile(.{ .sub_path = "file.txt", .data = "nonsense" });
try tmp.dir.makeDir("subdir");

// Get base abs path
Expand Down Expand Up @@ -620,7 +620,7 @@ test "Dir.realpath smoke test" {
try testing.expectError(error.FileNotFound, ctx.dir.realpath(test_dir_path, &buf));

// Now create the file and dir
try ctx.dir.writeFile(test_file_path, "");
try ctx.dir.writeFile(.{ .sub_path = test_file_path, .data = "" });
try ctx.dir.makeDir(test_dir_path);

const base_path = try ctx.transformPath(".");
Expand Down Expand Up @@ -695,7 +695,7 @@ test "Dir.statFile" {

try testing.expectError(error.FileNotFound, ctx.dir.statFile(test_file_name));

try ctx.dir.writeFile(test_file_name, "");
try ctx.dir.writeFile(.{ .sub_path = test_file_name, .data = "" });

const stat = try ctx.dir.statFile(test_file_name);
try testing.expectEqual(File.Kind.file, stat.kind);
Expand Down Expand Up @@ -803,7 +803,7 @@ test "deleteDir" {

// deleting a non-empty directory
try ctx.dir.makeDir(test_dir_path);
try ctx.dir.writeFile(test_file_path, "");
try ctx.dir.writeFile(.{ .sub_path = test_file_path, .data = "" });
try testing.expectError(error.DirNotEmpty, ctx.dir.deleteDir(test_dir_path));

// deleting an empty directory
Expand Down Expand Up @@ -1071,7 +1071,7 @@ test "deleteTree on a symlink" {
defer tmp.cleanup();

// Symlink to a file
try tmp.dir.writeFile("file", "");
try tmp.dir.writeFile(.{ .sub_path = "file", .data = "" });
try setupSymlink(tmp.dir, "file", "filelink", .{});

try tmp.dir.deleteTree("filelink");
Expand All @@ -1094,8 +1094,14 @@ test "makePath, put some files in it, deleteTree" {
const dir_path = try ctx.transformPath("os_test_tmp");

try ctx.dir.makePath(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c" }));
try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }), "nonsense");
try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }), "blah");
try ctx.dir.writeFile(.{
.sub_path = try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }),
.data = "nonsense",
});
try ctx.dir.writeFile(.{
.sub_path = try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }),
.data = "blah",
});

try ctx.dir.deleteTree(dir_path);
try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));
Expand All @@ -1110,8 +1116,14 @@ test "makePath, put some files in it, deleteTreeMinStackSize" {
const dir_path = try ctx.transformPath("os_test_tmp");

try ctx.dir.makePath(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c" }));
try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }), "nonsense");
try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }), "blah");
try ctx.dir.writeFile(.{
.sub_path = try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }),
.data = "nonsense",
});
try ctx.dir.writeFile(.{
.sub_path = try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }),
.data = "blah",
});

try ctx.dir.deleteTreeMinStackSize(dir_path);
try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));
Expand All @@ -1134,7 +1146,7 @@ test "makePath but sub_path contains pre-existing file" {
defer tmp.cleanup();

try tmp.dir.makeDir("foo");
try tmp.dir.writeFile("foo/bar", "");
try tmp.dir.writeFile(.{ .sub_path = "foo/bar", .data = "" });

try testing.expectError(error.NotDir, tmp.dir.makePath("foo/bar/baz"));
}
Expand Down Expand Up @@ -1227,7 +1239,7 @@ fn testFilenameLimits(iterable_dir: Dir, maxed_filename: []const u8) !void {
var maxed_dir = try iterable_dir.makeOpenPath(maxed_filename, .{});
defer maxed_dir.close();

try maxed_dir.writeFile(maxed_filename, "");
try maxed_dir.writeFile(.{ .sub_path = maxed_filename, .data = "" });

var walker = try iterable_dir.walk(testing.allocator);
defer walker.deinit();
Expand Down Expand Up @@ -1357,7 +1369,7 @@ test "access file" {
try ctx.dir.makePath(dir_path);
try testing.expectError(error.FileNotFound, ctx.dir.access(file_path, .{}));

try ctx.dir.writeFile(file_path, "");
try ctx.dir.writeFile(.{ .sub_path = file_path, .data = "" });
try ctx.dir.access(file_path, .{});
try ctx.dir.deleteTree(dir_path);
}
Expand Down Expand Up @@ -1463,7 +1475,7 @@ test "copyFile" {
const dest_file = try ctx.transformPath("tmp_test_copy_file2.txt");
const dest_file2 = try ctx.transformPath("tmp_test_copy_file3.txt");

try ctx.dir.writeFile(src_file, data);
try ctx.dir.writeFile(.{ .sub_path = src_file, .data = data });
defer ctx.dir.deleteFile(src_file) catch {};

try ctx.dir.copyFile(src_file, ctx.dir, dest_file, .{});
Expand Down Expand Up @@ -1740,7 +1752,7 @@ test "'.' and '..' in fs.Dir functions" {
renamed_file.close();
try ctx.dir.deleteFile(rename_path);

try ctx.dir.writeFile(update_path, "something");
try ctx.dir.writeFile(.{ .sub_path = update_path, .data = "something" });
const prev_status = try ctx.dir.updateFile(file_path, ctx.dir, update_path, .{});
try testing.expectEqual(fs.Dir.PrevStatus.stale, prev_status);

Expand Down Expand Up @@ -2009,11 +2021,7 @@ test "invalid UTF-8/WTF-8 paths" {
try testing.expectError(expected_err, ctx.dir.deleteTree(invalid_path));
try testing.expectError(expected_err, ctx.dir.deleteTreeMinStackSize(invalid_path));

try testing.expectError(expected_err, ctx.dir.writeFile(invalid_path, ""));
try testing.expectError(expected_err, ctx.dir.writeFile2(.{
.sub_path = invalid_path,
.data = "",
}));
try testing.expectError(expected_err, ctx.dir.writeFile(.{ .sub_path = invalid_path, .data = "" }));

try testing.expectError(expected_err, ctx.dir.access(invalid_path, .{}));
try testing.expectError(expected_err, ctx.dir.accessZ(invalid_path, .{}));
Expand Down
10 changes: 5 additions & 5 deletions lib/std/posix/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ test "symlink with relative paths" {
cwd.deleteFile("symlinked") catch {};

// First, try relative paths in cwd
try cwd.writeFile("file.txt", "nonsense");
try cwd.writeFile(.{ .sub_path = "file.txt", .data = "nonsense" });

if (native_os == .windows) {
std.os.windows.CreateSymbolicLink(
Expand Down Expand Up @@ -268,7 +268,7 @@ test "link with relative paths" {
cwd.deleteFile("example.txt") catch {};
cwd.deleteFile("new.txt") catch {};

try cwd.writeFile("example.txt", "example");
try cwd.writeFile(.{ .sub_path = "example.txt", .data = "example" });
try posix.link("example.txt", "new.txt", 0);

const efd = try cwd.openFile("example.txt", .{});
Expand Down Expand Up @@ -312,7 +312,7 @@ test "linkat with different directories" {
cwd.deleteFile("example.txt") catch {};
tmp.dir.deleteFile("new.txt") catch {};

try cwd.writeFile("example.txt", "example");
try cwd.writeFile(.{ .sub_path = "example.txt", .data = "example" });
try posix.linkat(cwd.fd, "example.txt", tmp.dir.fd, "new.txt", 0);

const efd = try cwd.openFile("example.txt", .{});
Expand Down Expand Up @@ -348,7 +348,7 @@ test "fstatat" {

// create dummy file
const contents = "nonsense";
try tmp.dir.writeFile("file.txt", contents);
try tmp.dir.writeFile(.{ .sub_path = "file.txt", .data = contents });

// fetch file's info on the opened fd directly
const file = try tmp.dir.openFile("file.txt", .{});
Expand All @@ -366,7 +366,7 @@ test "readlinkat" {
defer tmp.cleanup();

// create file
try tmp.dir.writeFile("file.txt", "nonsense");
try tmp.dir.writeFile(.{ .sub_path = "file.txt", .data = "nonsense" });

// create a symbolic link
if (native_os == .windows) {
Expand Down