Skip to content

Commit

Permalink
move std.fs.Dir.cwd to std.fs.cwd
Browse files Browse the repository at this point in the history
update to non-deprecated std.fs APIs throughout the codebase

Related: #3811
  • Loading branch information
andrewrk committed Nov 30, 2019
1 parent d039fed commit 413f9a5
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 88 deletions.
2 changes: 1 addition & 1 deletion lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,7 @@ fn findVcpkgRoot(allocator: *Allocator) !?[]const u8 {
const path_file = try fs.path.join(allocator, [_][]const u8{ appdata_path, "vcpkg.path.txt" });
defer allocator.free(path_file);

const file = fs.File.openRead(path_file) catch return null;
const file = fs.cwd().openFile(path_file, .{}) catch return null;
defer file.close();

const size = @intCast(usize, try file.getEndPos());
Expand Down
4 changes: 2 additions & 2 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
}

fn printLineFromFileAnyOs(out_stream: var, line_info: LineInfo) !void {
var f = try File.openRead(line_info.file_name);
var f = try fs.cwd().openFile(line_info.file_name, .{});
defer f.close();
// TODO fstat and make sure that the file has the correct size

Expand Down Expand Up @@ -2089,7 +2089,7 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
const ofile_path = mem.toSliceConst(u8, @ptrCast([*:0]const u8, di.strings.ptr + ofile.n_strx));

gop.kv.value = MachOFile{
.bytes = try std.fs.Dir.cwd().readFileAllocAligned(
.bytes = try std.fs.cwd().readFileAllocAligned(
di.ofiles.allocator,
ofile_path,
maxInt(usize),
Expand Down
172 changes: 137 additions & 35 deletions lib/std/fs.zig

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions lib/std/fs/file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,58 +51,58 @@ pub const File = struct {

/// Deprecated; call `std.fs.Dir.openFile` directly.
pub fn openRead(path: []const u8) OpenError!File {
return std.fs.Dir.cwd().openFile(path, .{});
return std.fs.cwd().openFile(path, .{});
}

/// Deprecated; call `std.fs.Dir.openFileC` directly.
pub fn openReadC(path_c: [*:0]const u8) OpenError!File {
return std.fs.Dir.cwd().openFileC(path_c, .{});
return std.fs.cwd().openFileC(path_c, .{});
}

/// Deprecated; call `std.fs.Dir.openFileW` directly.
pub fn openReadW(path_w: [*]const u16) OpenError!File {
return std.fs.Dir.cwd().openFileW(path_w, .{});
return std.fs.cwd().openFileW(path_w, .{});
}

/// Deprecated; call `std.fs.Dir.createFile` directly.
pub fn openWrite(path: []const u8) OpenError!File {
return std.fs.Dir.cwd().createFile(path, .{});
return std.fs.cwd().createFile(path, .{});
}

/// Deprecated; call `std.fs.Dir.createFile` directly.
pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File {
return std.fs.Dir.cwd().createFile(path, .{ .mode = file_mode });
return std.fs.cwd().createFile(path, .{ .mode = file_mode });
}

/// Deprecated; call `std.fs.Dir.createFileC` directly.
pub fn openWriteModeC(path_c: [*:0]const u8, file_mode: Mode) OpenError!File {
return std.fs.Dir.cwd().createFileC(path_c, .{ .mode = file_mode });
return std.fs.cwd().createFileC(path_c, .{ .mode = file_mode });
}

/// Deprecated; call `std.fs.Dir.createFileW` directly.
pub fn openWriteModeW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File {
return std.fs.Dir.cwd().createFileW(path_w, .{ .mode = file_mode });
return std.fs.cwd().createFileW(path_w, .{ .mode = file_mode });
}

/// Deprecated; call `std.fs.Dir.createFile` directly.
pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File {
return std.fs.Dir.cwd().createFile(path, .{
return std.fs.cwd().createFile(path, .{
.mode = file_mode,
.exclusive = true,
});
}

/// Deprecated; call `std.fs.Dir.createFileC` directly.
pub fn openWriteNoClobberC(path_c: [*:0]const u8, file_mode: Mode) OpenError!File {
return std.fs.Dir.cwd().createFileC(path_c, .{
return std.fs.cwd().createFileC(path_c, .{
.mode = file_mode,
.exclusive = true,
});
}

/// Deprecated; call `std.fs.Dir.createFileW` directly.
pub fn openWriteNoClobberW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File {
return std.fs.Dir.cwd().createFileW(path_w, .{
return std.fs.cwd().createFileW(path_w, .{
.mode = file_mode,
.exclusive = true,
});
Expand Down
33 changes: 32 additions & 1 deletion lib/std/fs/path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ test "join" {
testJoinPosix([_][]const u8{ "a/", "/c" }, "a/c");
}

pub fn isAbsoluteC(path_c: [*:0]const u8) bool {
if (builtin.os == .windows) {
return isAbsoluteWindowsC(path_c);
} else {
return isAbsolutePosixC(path_c);
}
}

pub fn isAbsolute(path: []const u8) bool {
if (builtin.os == .windows) {
return isAbsoluteWindows(path);
Expand All @@ -136,7 +144,7 @@ pub fn isAbsolute(path: []const u8) bool {
}
}

pub fn isAbsoluteW(path_w: [*]const u16) bool {
pub fn isAbsoluteW(path_w: [*:0]const u16) bool {
if (path_w[0] == '/')
return true;

Expand Down Expand Up @@ -174,10 +182,33 @@ pub fn isAbsoluteWindows(path: []const u8) bool {
return false;
}

pub fn isAbsoluteWindowsC(path_c: [*:0]const u8) bool {
if (path_c[0] == '/')
return true;

if (path_c[0] == '\\') {
return true;
}
if (path_c[0] == 0 or path_c[1] == 0 or path_c[2] == 0) {
return false;
}
if (path_c[1] == ':') {
if (path_c[2] == '/')
return true;
if (path_c[2] == '\\')
return true;
}
return false;
}

pub fn isAbsolutePosix(path: []const u8) bool {
return path[0] == sep_posix;
}

pub fn isAbsolutePosixC(path_c: [*:0]const u8) bool {
return path_c[0] == sep_posix;
}

test "isAbsoluteWindows" {
testIsAbsoluteWindows("/", true);
testIsAbsoluteWindows("//", true);
Expand Down
11 changes: 4 additions & 7 deletions lib/std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,14 @@ pub const COutStream = @import("io/c_out_stream.zig").COutStream;
pub const InStream = @import("io/in_stream.zig").InStream;
pub const OutStream = @import("io/out_stream.zig").OutStream;

/// TODO move this to `std.fs` and add a version to `std.fs.Dir`.
/// Deprecated; use `std.fs.Dir.writeFile`.
pub fn writeFile(path: []const u8, data: []const u8) !void {
var file = try File.openWrite(path);
defer file.close();
try file.write(data);
return fs.cwd().writeFile(path, data);
}

/// On success, caller owns returned buffer.
/// This function is deprecated; use `std.fs.Dir.readFileAlloc`.
/// Deprecated; use `std.fs.Dir.readFileAlloc`.
pub fn readFileAlloc(allocator: *mem.Allocator, path: []const u8) ![]u8 {
return fs.Dir.cwd().readFileAlloc(allocator, path, math.maxInt(usize));
return fs.cwd().readFileAlloc(allocator, path, math.maxInt(usize));
}

pub fn BufferedInStream(comptime Error: type) type {
Expand Down
28 changes: 15 additions & 13 deletions lib/std/io/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ test "write a file, read it, then delete it" {
var raw_bytes: [200 * 1024]u8 = undefined;
var allocator = &std.heap.FixedBufferAllocator.init(raw_bytes[0..]).allocator;

const cwd = fs.cwd();

var data: [1024]u8 = undefined;
var prng = DefaultPrng.init(1234);
prng.random.bytes(data[0..]);
const tmp_file_name = "temp_test_file.txt";
{
var file = try File.openWrite(tmp_file_name);
var file = try cwd.createFile(tmp_file_name, .{});
defer file.close();

var file_out_stream = file.outStream();
Expand All @@ -32,16 +34,16 @@ test "write a file, read it, then delete it" {
}

{
// make sure openWriteNoClobber doesn't harm the file
if (File.openWriteNoClobber(tmp_file_name, File.default_mode)) |file| {
// Make sure the exclusive flag is honored.
if (cwd.createFile(tmp_file_name, .{ .exclusive = true })) |file| {
unreachable;
} else |err| {
std.debug.assert(err == File.OpenError.PathAlreadyExists);
}
}

{
var file = try File.openRead(tmp_file_name);
var file = try cwd.openFile(tmp_file_name, .{});
defer file.close();

const file_size = try file.getEndPos();
Expand All @@ -58,7 +60,7 @@ test "write a file, read it, then delete it" {
expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data));
expect(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
}
try fs.deleteFile(tmp_file_name);
try cwd.deleteFile(tmp_file_name);
}

test "BufferOutStream" {
Expand Down Expand Up @@ -274,7 +276,7 @@ test "BitOutStream" {
test "BitStreams with File Stream" {
const tmp_file_name = "temp_test_file.txt";
{
var file = try File.openWrite(tmp_file_name);
var file = try fs.cwd().createFile(tmp_file_name, .{});
defer file.close();

var file_out = file.outStream();
Expand All @@ -291,7 +293,7 @@ test "BitStreams with File Stream" {
try bit_stream.flushBits();
}
{
var file = try File.openRead(tmp_file_name);
var file = try fs.cwd().openFile(tmp_file_name, .{});
defer file.close();

var file_in = file.inStream();
Expand All @@ -316,7 +318,7 @@ test "BitStreams with File Stream" {

expectError(error.EndOfStream, bit_stream.readBitsNoEof(u1, 1));
}
try fs.deleteFile(tmp_file_name);
try fs.cwd().deleteFile(tmp_file_name);
}

fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime packing: io.Packing) !void {
Expand Down Expand Up @@ -599,7 +601,7 @@ test "c out stream" {
const out_file = std.c.fopen(filename, "w") orelse return error.UnableToOpenTestFile;
defer {
_ = std.c.fclose(out_file);
fs.deleteFileC(filename) catch {};
fs.cwd().deleteFileC(filename) catch {};
}

const out_stream = &io.COutStream.init(out_file).stream;
Expand All @@ -608,10 +610,10 @@ test "c out stream" {

test "File seek ops" {
const tmp_file_name = "temp_test_file.txt";
var file = try File.openWrite(tmp_file_name);
var file = try fs.cwd().createFile(tmp_file_name, .{});
defer {
file.close();
fs.deleteFile(tmp_file_name) catch {};
fs.cwd().deleteFile(tmp_file_name) catch {};
}

try file.write([_]u8{0x55} ** 8192);
Expand All @@ -632,10 +634,10 @@ test "File seek ops" {

test "updateTimes" {
const tmp_file_name = "just_a_temporary_file.txt";
var file = try File.openWrite(tmp_file_name);
var file = try fs.cwd().createFile(tmp_file_name, .{});
defer {
file.close();
std.fs.deleteFile(tmp_file_name) catch {};
std.fs.cwd().deleteFile(tmp_file_name) catch {};
}
var stat_old = try file.stat();
// Set atime and mtime to 5s before
Expand Down
4 changes: 2 additions & 2 deletions lib/std/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ fn linuxLookupNameFromHosts(
family: os.sa_family_t,
port: u16,
) !void {
const file = fs.File.openReadC("/etc/hosts") catch |err| switch (err) {
const file = fs.openFileAbsoluteC("/etc/hosts", .{}) catch |err| switch (err) {
error.FileNotFound,
error.NotDir,
error.AccessDenied,
Expand Down Expand Up @@ -1006,7 +1006,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void {
};
errdefer rc.deinit();

const file = fs.File.openReadC("/etc/resolv.conf") catch |err| switch (err) {
const file = fs.openFileAbsoluteC("/etc/resolv.conf", .{}) catch |err| switch (err) {
error.FileNotFound,
error.NotDir,
error.AccessDenied,
Expand Down
12 changes: 7 additions & 5 deletions lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ pub fn execvpeC(file: [*:0]const u8, child_argv: [*:null]const ?[*:0]const u8, e
path_buf[search_path.len] = '/';
mem.copy(u8, path_buf[search_path.len + 1 ..], file_slice);
path_buf[search_path.len + file_slice.len + 1] = 0;
// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
err = execveC(@ptrCast([*:0]u8, &path_buf), child_argv, envp);
switch (err) {
error.AccessDenied => seen_eacces = true,
Expand Down Expand Up @@ -834,15 +834,15 @@ pub fn execvpe(
@memcpy(arg_buf.ptr, arg.ptr, arg.len);
arg_buf[arg.len] = 0;

// TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731
// TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3770
argv_buf[i] = @ptrCast([*:0]u8, arg_buf.ptr);
}
argv_buf[argv_slice.len] = null;

const envp_buf = try createNullDelimitedEnvMap(allocator, env_map);
defer freeNullDelimitedEnvMap(allocator, envp_buf);

// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
const argv_ptr = @ptrCast([*:null]?[*:0]u8, argv_buf.ptr);

return execvpeC(argv_buf.ptr[0].?, argv_ptr, envp_buf.ptr);
Expand All @@ -863,12 +863,12 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.
@memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len);
env_buf[env_buf.len - 1] = 0;

// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
envp_buf[i] = @ptrCast([*:0]u8, env_buf.ptr);
}
assert(i == envp_count);
}
// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
// TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
assert(envp_buf[envp_count] == null);
return @ptrCast([*:null]?[*:0]u8, envp_buf.ptr)[0..envp_count];
}
Expand Down Expand Up @@ -1087,7 +1087,9 @@ pub const UnlinkatError = UnlinkError || error{
};

/// Delete a file name and possibly the file it refers to, based on an open directory handle.
/// Asserts that the path parameter has no null bytes.
pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
if (std.debug.runtime_safety) for (file_path) |byte| assert(byte != 0);
if (builtin.os == .windows) {
const file_path_w = try windows.sliceToPrefixedFileW(file_path);
return unlinkatW(dirfd, &file_path_w, flags);
Expand Down
7 changes: 3 additions & 4 deletions lib/std/os/linux/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const linux = std.os.linux;
const mem = std.mem;
const elf = std.elf;
const expect = std.testing.expect;
const fs = std.fs;

test "getpid" {
expect(linux.getpid() != 0);
Expand Down Expand Up @@ -45,14 +46,12 @@ test "timer" {
err = linux.epoll_wait(@intCast(i32, epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1);
}

const File = std.fs.File;

test "statx" {
const tmp_file_name = "just_a_temporary_file.txt";
var file = try File.openWrite(tmp_file_name);
var file = try fs.cwd().createFile(tmp_file_name, .{});
defer {
file.close();
std.fs.deleteFile(tmp_file_name) catch {};
fs.cwd().deleteFile(tmp_file_name) catch {};
}

var statx_buf: linux.Statx = undefined;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/os/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test "makePath, put some files in it, deleteTree" {
try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");
try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
try fs.deleteTree("os_test_tmp");
if (fs.Dir.cwd().openDirTraverse("os_test_tmp")) |dir| {
if (fs.cwd().openDirTraverse("os_test_tmp")) |dir| {
@panic("expected error");
} else |err| {
expect(err == error.FileNotFound);
Expand Down Expand Up @@ -111,7 +111,7 @@ test "AtomicFile" {
const content = try io.readFileAlloc(allocator, test_out_file);
expect(mem.eql(u8, content, test_content));

try fs.deleteFile(test_out_file);
try fs.cwd().deleteFile(test_out_file);
}

test "thread local storage" {
Expand Down
Loading

0 comments on commit 413f9a5

Please sign in to comment.