From e84340fb3807362e3709a9e7c08c214f1d4ceeaa Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Tue, 8 Aug 2023 14:35:16 -0700 Subject: [PATCH] Remove all fs.fooAbsolute usages throughout the Zig codebase None of these are necessary and in fact all of the used functions are purely wrappers around their `fs.cwd().foo` counterparts. Therefore, they can all be replaced by their `Dir` equivalents with no functionality differences. - lib/std/crypto/Certificate/Bundle.zig: `Absolute` versions of `addCerts` functions were used likely out of either (1) a mistaken belief that the Absolute versions would be more efficient when the path is known to be absolute beforehand or (2) to ensure that the paths used were absolute (but this is something that can be verified at compile time if that was the intention, since all the absolute paths are compile-time known) - lib/std/crypto/Cerficiate/Bundle/macos.zig: `openFileAbsolute` was used instead of `cwd().openFile` but that has no benefit - lib/std/debug.zig: `path.isAbsolute` was used to decide between `openFileAbsolute` and `openFile`, but that has no benefit and is fully unnecessary (`cwd().openFile` can handle absolute paths). Also, `openFileAbsoluteW` was used instead of `cwd().openFileW` but that has no benefit - lib/std/net.zig: `openFileAbsoluteZ` was used instead of `cwd().openFileZ` but that has no benefit - lib/std/process.zig: `openFileAbsolute`/`openFileAbsoluteZ` was used instead of `cwd().openFile`/`openFileZ` but that has no benefit - lib/std/zig/system/NativeTargetInfo.zig: `openFileAbsolute` was used instead of `cwd().openFile` but that has no benefit - lib/std/zig/system/linux.zig: `openFileAbsolute` was used instead of `cwd().openFile` but that has no benefit - src/windows_sdk.zig: `openDirAbsolute`/`openIterableDirAbsolute` was used instead of the `Dir` versions, but that has no benefit (and there were duplicate `path.isAbsolute` calls making the `Absolute` version usage fully redundant) - tools/generate_linux_syscalls.zig: `openDirAbsolute` was used instead of `cwd().openDir` but that has no benefit (if the intention was to enforce absolute paths passed via `args[2]`, then that could be checked and a proper error could be returned) --- lib/std/crypto/Certificate/Bundle.zig | 28 +++------------------ lib/std/crypto/Certificate/Bundle/macos.zig | 2 +- lib/std/debug.zig | 11 +++----- lib/std/net.zig | 4 +-- lib/std/process.zig | 4 +-- lib/std/zig/system/NativeTargetInfo.zig | 2 +- lib/std/zig/system/linux.zig | 2 +- src/windows_sdk.zig | 6 ++--- tools/generate_linux_syscalls.zig | 2 +- 9 files changed, 17 insertions(+), 44 deletions(-) diff --git a/lib/std/crypto/Certificate/Bundle.zig b/lib/std/crypto/Certificate/Bundle.zig index 3e28d12d06ad..539a43a0412a 100644 --- a/lib/std/crypto/Certificate/Bundle.zig +++ b/lib/std/crypto/Certificate/Bundle.zig @@ -96,7 +96,7 @@ fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void { scan: { for (cert_file_paths) |cert_file_path| { - if (addCertsFromFilePathAbsolute(cb, gpa, cert_file_path)) |_| { + if (addCertsFromFilePath(cb, gpa, std.fs.cwd(), cert_file_path)) |_| { break :scan; } else |err| switch (err) { error.FileNotFound => continue, @@ -105,7 +105,7 @@ fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void { } for (cert_dir_paths) |cert_dir_path| { - addCertsFromDirPathAbsolute(cb, gpa, cert_dir_path) catch |err| switch (err) { + addCertsFromDirPath(cb, gpa, std.fs.cwd(), cert_dir_path) catch |err| switch (err) { error.FileNotFound => continue, else => |e| return e, }; @@ -120,7 +120,7 @@ const RescanBSDError = AddCertsFromFilePathError; fn rescanBSD(cb: *Bundle, gpa: Allocator, cert_file_path: []const u8) RescanBSDError!void { cb.bytes.clearRetainingCapacity(); cb.map.clearRetainingCapacity(); - try addCertsFromFilePathAbsolute(cb, gpa, cert_file_path); + try addCertsFromFilePath(cb, gpa, std.fs.cwd(), cert_file_path); cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len); } @@ -164,17 +164,6 @@ pub fn addCertsFromDirPath( return addCertsFromDir(cb, gpa, iterable_dir); } -pub fn addCertsFromDirPathAbsolute( - cb: *Bundle, - gpa: Allocator, - abs_dir_path: []const u8, -) AddCertsFromDirPathError!void { - assert(fs.path.isAbsolute(abs_dir_path)); - var iterable_dir = try fs.openIterableDirAbsolute(abs_dir_path, .{}); - defer iterable_dir.close(); - return addCertsFromDir(cb, gpa, iterable_dir); -} - pub const AddCertsFromDirError = AddCertsFromFilePathError; pub fn addCertsFromDir(cb: *Bundle, gpa: Allocator, iterable_dir: fs.IterableDir) AddCertsFromDirError!void { @@ -191,17 +180,6 @@ pub fn addCertsFromDir(cb: *Bundle, gpa: Allocator, iterable_dir: fs.IterableDir pub const AddCertsFromFilePathError = fs.File.OpenError || AddCertsFromFileError; -pub fn addCertsFromFilePathAbsolute( - cb: *Bundle, - gpa: Allocator, - abs_file_path: []const u8, -) AddCertsFromFilePathError!void { - assert(fs.path.isAbsolute(abs_file_path)); - var file = try fs.openFileAbsolute(abs_file_path, .{}); - defer file.close(); - return addCertsFromFile(cb, gpa, file); -} - pub fn addCertsFromFilePath( cb: *Bundle, gpa: Allocator, diff --git a/lib/std/crypto/Certificate/Bundle/macos.zig b/lib/std/crypto/Certificate/Bundle/macos.zig index 028275a06b49..533e4a4c6929 100644 --- a/lib/std/crypto/Certificate/Bundle/macos.zig +++ b/lib/std/crypto/Certificate/Bundle/macos.zig @@ -11,7 +11,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { cb.bytes.clearRetainingCapacity(); cb.map.clearRetainingCapacity(); - const file = try fs.openFileAbsolute("/System/Library/Keychains/SystemRootCertificates.keychain", .{}); + const file = try fs.cwd().openFile("/System/Library/Keychains/SystemRootCertificates.keychain", .{}); defer file.close(); const bytes = try file.readToEndAlloc(gpa, std.math.maxInt(u32)); diff --git a/lib/std/debug.zig b/lib/std/debug.zig index db9b93dff652..7f7b5f599f0d 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -1071,13 +1071,8 @@ pub fn readElfDebugInfo( parent_mapped_mem: ?[]align(mem.page_size) const u8, ) !ModuleDebugInfo { nosuspend { - - // TODO https://github.com/ziglang/zig/issues/5525 const elf_file = (if (elf_filename) |filename| blk: { - break :blk if (fs.path.isAbsolute(filename)) - fs.openFileAbsolute(filename, .{ .intended_io_mode = .blocking }) - else - fs.cwd().openFile(filename, .{ .intended_io_mode = .blocking }); + break :blk fs.cwd().openFile(filename, .{ .intended_io_mode = .blocking }); } else fs.openSelfExe(.{ .intended_io_mode = .blocking })) catch |err| switch (err) { error.FileNotFound => return error.MissingDebugInfo, else => return err, @@ -1702,7 +1697,7 @@ pub const DebugInfo = struct { // a binary is produced with -gdwarf, since the section names are longer than 8 bytes. if (coff_obj.strtabRequired()) { var name_buffer: [windows.PATH_MAX_WIDE + 4:0]u16 = undefined; - // openFileAbsoluteW requires the prefix to be present + // openFileW requires the NT namespace prefix to be present mem.copy(u16, name_buffer[0..4], &[_]u16{ '\\', '?', '?', '\\' }); const process_handle = windows.kernel32.GetCurrentProcess(); @@ -1714,7 +1709,7 @@ pub const DebugInfo = struct { ); if (len == 0) return error.MissingDebugInfo; - const coff_file = fs.openFileAbsoluteW(name_buffer[0 .. len + 4 :0], .{}) catch |err| switch (err) { + const coff_file = fs.cwd().openFileW(name_buffer[0 .. len + 4 :0], .{}) catch |err| switch (err) { error.FileNotFound => return error.MissingDebugInfo, else => return err, }; diff --git a/lib/std/net.zig b/lib/std/net.zig index b03c7e09dc38..af976072bdb9 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1242,7 +1242,7 @@ fn linuxLookupNameFromHosts( family: os.sa_family_t, port: u16, ) !void { - const file = fs.openFileAbsoluteZ("/etc/hosts", .{}) catch |err| switch (err) { + const file = fs.cwd().openFileZ("/etc/hosts", .{}) catch |err| switch (err) { error.FileNotFound, error.NotDir, error.AccessDenied, @@ -1443,7 +1443,7 @@ fn getResolvConf(allocator: mem.Allocator, rc: *ResolvConf) !void { }; errdefer rc.deinit(); - const file = fs.openFileAbsoluteZ("/etc/resolv.conf", .{}) catch |err| switch (err) { + const file = fs.cwd().openFileZ("/etc/resolv.conf", .{}) catch |err| switch (err) { error.FileNotFound, error.NotDir, error.AccessDenied, diff --git a/lib/std/process.zig b/lib/std/process.zig index fe5a44d3ad2c..149c2adf59f7 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -967,7 +967,7 @@ pub fn getUserInfo(name: []const u8) !UserInfo { /// TODO this reads /etc/passwd. But sometimes the user/id mapping is in something else /// like NIS, AD, etc. See `man nss` or look at an strace for `id myuser`. pub fn posixGetUserInfo(name: []const u8) !UserInfo { - const file = try std.fs.openFileAbsolute("/etc/passwd", .{}); + const file = try std.fs.cwd().openFile("/etc/passwd", .{}); defer file.close(); const reader = file.reader(); @@ -1207,7 +1207,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { } fn totalSystemMemoryLinux() !usize { - var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); + var file = try std.fs.cwd().openFileZ("/proc/meminfo", .{}); defer file.close(); var buf: [50]u8 = undefined; const amt = try file.read(&buf); diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig index 99a1a8f2efac..6f843bc1418f 100644 --- a/lib/std/zig/system/NativeTargetInfo.zig +++ b/lib/std/zig/system/NativeTargetInfo.zig @@ -320,7 +320,7 @@ fn detectAbiAndDynamicLinker( // #! (2) + 255 (max length of shebang line since Linux 5.1) + \n (1) var buffer: [258]u8 = undefined; while (true) { - const file = fs.openFileAbsolute(file_name, .{}) catch |err| switch (err) { + const file = fs.cwd().openFile(file_name, .{}) catch |err| switch (err) { error.NoSpaceLeft => unreachable, error.NameTooLong => unreachable, error.PathAlreadyExists => unreachable, diff --git a/lib/std/zig/system/linux.zig b/lib/std/zig/system/linux.zig index f2f89f8317b6..23fbda8ffbb1 100644 --- a/lib/std/zig/system/linux.zig +++ b/lib/std/zig/system/linux.zig @@ -331,7 +331,7 @@ fn CpuinfoParser(comptime impl: anytype) type { } pub fn detectNativeCpuAndFeatures() ?Target.Cpu { - var f = fs.openFileAbsolute("/proc/cpuinfo", .{ .intended_io_mode = .blocking }) catch |err| switch (err) { + var f = fs.cwd().openFile("/proc/cpuinfo", .{ .intended_io_mode = .blocking }) catch |err| switch (err) { else => return null, }; defer f.close(); diff --git a/src/windows_sdk.zig b/src/windows_sdk.zig index 85c10226bd52..dbc6afdb8144 100644 --- a/src/windows_sdk.zig +++ b/src/windows_sdk.zig @@ -476,7 +476,7 @@ pub const Windows81Sdk = struct { if (!std.fs.path.isAbsolute(sdk_lib_dir_path)) return error.Windows81SdkNotFound; // enumerate files in sdk path looking for latest version - var sdk_lib_dir = std.fs.openIterableDirAbsolute(sdk_lib_dir_path, .{}) catch |err| switch (err) { + var sdk_lib_dir = std.fs.cwd().openIterableDir(sdk_lib_dir_path, .{}) catch |err| switch (err) { error.NameTooLong => return error.PathTooLong, else => return error.Windows81SdkNotFound, }; @@ -727,7 +727,7 @@ const MsvcLibDir = struct { if (!std.fs.path.isAbsolute(visualstudio_folder_path)) return error.PathNotFound; // enumerate folders that contain `privateregistry.bin`, looking for all versions // f.i. %localappdata%\Microsoft\VisualStudio\17.0_9e9cbb98\ - var visualstudio_folder = std.fs.openIterableDirAbsolute(visualstudio_folder_path, .{}) catch return error.PathNotFound; + var visualstudio_folder = std.fs.cwd().openIterableDir(visualstudio_folder_path, .{}) catch return error.PathNotFound; defer visualstudio_folder.close(); var iterator = visualstudio_folder.iterate(); @@ -874,7 +874,7 @@ const MsvcLibDir = struct { fn verifyLibDir(lib_dir_path: []const u8) bool { std.debug.assert(std.fs.path.isAbsolute(lib_dir_path)); // should be already handled in `findVia*` - var dir = std.fs.openDirAbsolute(lib_dir_path, .{}) catch return false; + var dir = std.fs.cwd().openDir(lib_dir_path, .{}) catch return false; defer dir.close(); const stat = dir.statFile("vcruntime.lib") catch return false; diff --git a/tools/generate_linux_syscalls.zig b/tools/generate_linux_syscalls.zig index 32e287b43403..989771541b3e 100644 --- a/tools/generate_linux_syscalls.zig +++ b/tools/generate_linux_syscalls.zig @@ -36,7 +36,7 @@ pub fn main() !void { // As of 5.17.1, the largest table is 23467 bytes. // 32k should be enough for now. var buf = try allocator.alloc(u8, 1 << 15); - const linux_dir = try std.fs.openDirAbsolute(linux_path, .{}); + const linux_dir = try std.fs.cwd().openDir(linux_path, .{}); try writer.writeAll( \\// This file is automatically generated.