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 {};
372372pub const IOV_MAX = 1024;
373373
374374pub 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
377379pub const STDIN_FILENO = 0;
378380pub const STDOUT_FILENO = 1;
lib/std/fs.zig+8-5
......@@ -48,19 +48,22 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
4848 @compileError("PATH_MAX not implemented for " ++ @tagName(builtin.os.tag)),
4949};
5050
51/// This represents the maximum size of a UTF-8 encoded file name component that the
52/// operating system will accept. All file name components returned by file system
53/// operations are assumed to fit into a UTF-8 encoded array of this length.
51/// This represents the maximum size of a UTF-8 encoded file name component that
52/// the platform's common file systems support. File name components returned by file system
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.
5455/// The byte count does not include a null sentinel byte.
5556pub 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,
5760 .netbsd, .openbsd, .solaris => os.MAXNAMLEN,
5861 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
5962 // If it would require 4 UTF-8 bytes, then there would be a surrogate
6063 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
6164 .windows => os.windows.NAME_MAX * 3,
6265 // 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.
6467 // TODO determine if this is a reasonable approach
6568 .wasi => os.windows.NAME_MAX * 3,
6669 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
726726 try iterable_dir.dir.deleteTree(maxed_filename);
727727}
728728
729test "filename limits" {
729test "max file name component lengths" {
730730 var tmp = tmpIterableDir(.{});
731731 defer tmp.cleanup();
732732
......@@ -737,7 +737,8 @@ test "filename limits" {
737737 try testFilenameLimits(tmp.iterable_dir, &maxed_windows_filename);
738738 } else if (builtin.os.tag == .wasi) {
739739 // 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).
741742 const maxed_wasi_filename = [_]u8{'1'} ** 255;
742743 try testFilenameLimits(tmp.iterable_dir, &maxed_wasi_filename);
743744 } else {
lib/std/os/windows.zig+12
......@@ -2981,6 +2981,18 @@ pub const PATH_MAX_WIDE = 32767;
29812981/// > lpMaximumComponentLength parameter of the GetVolumeInformation function
29822982/// > (this value is commonly 255 characters)
29832983/// 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.
29842996pub const NAME_MAX = 255;
29852997
29862998pub const FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;