authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-10-28 01:58:47-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-10-29 14:30:46-07:00
logdb80225a973049f77f0a3080aae4bd2ea6084bc4
treeb768010c4588b5394edecf0b5ca55ce4540e6b47
parent348f73502e81621b7cdb8ecf9a64bc8ebcf9db97

fs: Some NAME_MAX/MAX_NAME_BYTES improvements


4 files changed, 26 insertions(+), 8 deletions(-)

lib/std/c/haiku.zig+3-1
...@@ -372,7 +372,9 @@ pub const KERN = struct {};...@@ -372,7 +372,9 @@ pub const KERN = struct {};
372pub const IOV_MAX = 1024;372pub const IOV_MAX = 1024;
373373
374pub const PATH_MAX = 1024;374pub const PATH_MAX = 1024;
375pub const NAME_MAX = 256;375/// NOTE: Contains room for the terminating null character (despite the POSIX
376/// definition saying that NAME_MAX does not include the terminating null).
377pub const NAME_MAX = 256; // limits.h
376378
377pub const STDIN_FILENO = 0;379pub const STDIN_FILENO = 0;
378pub const STDOUT_FILENO = 1;380pub const STDOUT_FILENO = 1;
lib/std/fs.zig+8-5
...@@ -48,19 +48,22 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {...@@ -48,19 +48,22 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
48 @compileError("PATH_MAX not implemented for " ++ @tagName(builtin.os.tag)),48 @compileError("PATH_MAX not implemented for " ++ @tagName(builtin.os.tag)),
49};49};
5050
51/// This represents the maximum size of a UTF-8 encoded file name component that the51/// This represents the maximum size of a UTF-8 encoded file name component that
52/// operating system will accept. All file name components returned by file system52/// the platform's common file systems support. File name components returned by file system
53/// operations are assumed to fit into a UTF-8 encoded array of this length.53/// operations are likely to fit into a UTF-8 encoded array of this length, but
54/// (depending on the platform) this assumption may not hold for every configuration.
54/// The byte count does not include a null sentinel byte.55/// The byte count does not include a null sentinel byte.
55pub const MAX_NAME_BYTES = switch (builtin.os.tag) {56pub const MAX_NAME_BYTES = switch (builtin.os.tag) {
56 .linux, .macos, .ios, .freebsd, .dragonfly, .haiku => os.NAME_MAX,57 .linux, .macos, .ios, .freebsd, .dragonfly => os.NAME_MAX,
58 // Haiku's NAME_MAX includes the null terminator, so subtract one.
59 .haiku => os.NAME_MAX - 1,
57 .netbsd, .openbsd, .solaris => os.MAXNAMLEN,60 .netbsd, .openbsd, .solaris => os.MAXNAMLEN,
58 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.61 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
59 // If it would require 4 UTF-8 bytes, then there would be a surrogate62 // If it would require 4 UTF-8 bytes, then there would be a surrogate
60 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.63 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
61 .windows => os.windows.NAME_MAX * 3,64 .windows => os.windows.NAME_MAX * 3,
62 // For WASI, the MAX_NAME will depend on the host OS, so it needs to be65 // For WASI, the MAX_NAME will depend on the host OS, so it needs to be
63 // as large as the largest MAX_NAME_BYTES in order to work on any host OS.66 // as large as the largest MAX_NAME_BYTES (Windows) in order to work on any host OS.
64 // TODO determine if this is a reasonable approach67 // TODO determine if this is a reasonable approach
65 .wasi => os.windows.NAME_MAX * 3,68 .wasi => os.windows.NAME_MAX * 3,
66 else => if (@hasDecl(root, "os") and @hasDecl(root.os, "NAME_MAX"))69 else => if (@hasDecl(root, "os") and @hasDecl(root.os, "NAME_MAX"))
lib/std/fs/test.zig+3-2
...@@ -726,7 +726,7 @@ fn testFilenameLimits(iterable_dir: IterableDir, maxed_filename: []const u8) !vo...@@ -726,7 +726,7 @@ fn testFilenameLimits(iterable_dir: IterableDir, maxed_filename: []const u8) !vo
726 try iterable_dir.dir.deleteTree(maxed_filename);726 try iterable_dir.dir.deleteTree(maxed_filename);
727}727}
728728
729test "filename limits" {729test "max file name component lengths" {
730 var tmp = tmpIterableDir(.{});730 var tmp = tmpIterableDir(.{});
731 defer tmp.cleanup();731 defer tmp.cleanup();
732732
...@@ -737,7 +737,8 @@ test "filename limits" {...@@ -737,7 +737,8 @@ test "filename limits" {
737 try testFilenameLimits(tmp.iterable_dir, &maxed_windows_filename);737 try testFilenameLimits(tmp.iterable_dir, &maxed_windows_filename);
738 } else if (builtin.os.tag == .wasi) {738 } else if (builtin.os.tag == .wasi) {
739 // On WASI, the maxed filename depends on the host OS, so in order for this test to739 // On WASI, the maxed filename depends on the host OS, so in order for this test to
740 // work on any host, we need to use a length that will work for all platforms.740 // work on any host, we need to use a length that will work for all platforms
741 // (i.e. the minimum MAX_NAME_BYTES of all supported platforms).
741 const maxed_wasi_filename = [_]u8{'1'} ** 255;742 const maxed_wasi_filename = [_]u8{'1'} ** 255;
742 try testFilenameLimits(tmp.iterable_dir, &maxed_wasi_filename);743 try testFilenameLimits(tmp.iterable_dir, &maxed_wasi_filename);
743 } else {744 } else {
lib/std/os/windows.zig+12
...@@ -2981,6 +2981,18 @@ pub const PATH_MAX_WIDE = 32767;...@@ -2981,6 +2981,18 @@ pub const PATH_MAX_WIDE = 32767;
2981/// > lpMaximumComponentLength parameter of the GetVolumeInformation function2981/// > lpMaximumComponentLength parameter of the GetVolumeInformation function
2982/// > (this value is commonly 255 characters)2982/// > (this value is commonly 255 characters)
2983/// from https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation2983/// from https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
2984///
2985/// > The value that is stored in the variable that *lpMaximumComponentLength points to is
2986/// > used to indicate that a specified file system supports long names. For example, for
2987/// > a FAT file system that supports long names, the function stores the value 255, rather
2988/// > than the previous 8.3 indicator. Long names can also be supported on systems that use
2989/// > the NTFS file system.
2990/// from https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationw
2991///
2992/// The assumption being made here is that while lpMaximumComponentLength may vary, it will never
2993/// be larger than 255.
2994///
2995/// TODO: More verification of this assumption.
2984pub const NAME_MAX = 255;2996pub const NAME_MAX = 255;
29852997
2986pub const FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;2998pub const FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;