From 33fdc43714e34f5c4bc02c416ef4c6534acc56ca Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Thu, 13 Oct 2022 22:18:35 -0700 Subject: [PATCH] std.fs: Add MAX_NAME_BYTES Also add some NAME_MAX or equivalent definitions where necessary --- lib/std/c/darwin.zig | 1 + lib/std/c/dragonfly.zig | 1 + lib/std/c/haiku.zig | 2 ++ lib/std/fs.zig | 17 +++++++++++++++++ lib/std/os/windows.zig | 6 ++++++ 5 files changed, 27 insertions(+) diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index 0dfe7a2500..17a4957e94 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -1014,6 +1014,7 @@ pub const vm_machine_attribute_val_t = isize; pub const CALENDAR_CLOCK = 1; pub const PATH_MAX = 1024; +pub const NAME_MAX = 255; pub const IOV_MAX = 16; pub const STDIN_FILENO = 0; diff --git a/lib/std/c/dragonfly.zig b/lib/std/c/dragonfly.zig index 35436d7017..ddc0db2709 100644 --- a/lib/std/c/dragonfly.zig +++ b/lib/std/c/dragonfly.zig @@ -234,6 +234,7 @@ pub const SA = struct { }; pub const PATH_MAX = 1024; +pub const NAME_MAX = 255; pub const IOV_MAX = KERN.IOV_MAX; pub const ino_t = c_ulong; diff --git a/lib/std/c/haiku.zig b/lib/std/c/haiku.zig index ba7e55ccb1..300dc9076c 100644 --- a/lib/std/c/haiku.zig +++ b/lib/std/c/haiku.zig @@ -266,6 +266,7 @@ pub const area_info = extern struct { }; pub const MAXPATHLEN = PATH_MAX; +pub const MAXNAMLEN = NAME_MAX; pub const image_info = extern struct { id: u32, @@ -371,6 +372,7 @@ pub const KERN = struct {}; pub const IOV_MAX = 1024; pub const PATH_MAX = 1024; +pub const NAME_MAX = 256; pub const STDIN_FILENO = 0; pub const STDOUT_FILENO = 1; diff --git a/lib/std/fs.zig b/lib/std/fs.zig index dfadb144eb..208f456184 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -48,6 +48,23 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) { @compileError("PATH_MAX not implemented for " ++ @tagName(builtin.os.tag)), }; +/// This represents the maximum size of a UTF-8 encoded file name component that the +/// operating system will accept. All file name components returned by file system +/// operations are assumed to fit into a UTF-8 encoded array of this length. +/// The byte count does not include a null sentinel byte. +pub const MAX_NAME_BYTES = switch (builtin.os.tag) { + .linux, .macos, .ios, .freebsd, .dragonfly, .haiku => os.NAME_MAX, + .netbsd, .openbsd, .solaris => os.MAXNAMLEN, + // Each UTF-16LE character may be expanded to 3 UTF-8 bytes. + // If it would require 4 UTF-8 bytes, then there would be a surrogate + // pair in the UTF-16LE, and we (over)account 3 bytes for it that way. + .windows => os.NAME_MAX * 3, + else => if (@hasDecl(root, "os") and @hasDecl(root.os, "NAME_MAX")) + root.os.NAME_MAX + else + @compileError("NAME_MAX not implemented for " ++ @tagName(builtin.os.tag)), +}; + pub const base64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*; /// Base64 encoder, replacing the standard `+/` with `-_` so that it can be used in a file name on any filesystem. diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index d68a66ed87..7bed48a2bf 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -2977,6 +2977,12 @@ pub const PMEMORY_BASIC_INFORMATION = *MEMORY_BASIC_INFORMATION; /// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation pub const PATH_MAX_WIDE = 32767; +/// > [Each file name component can be] up to the value returned in the +/// > lpMaximumComponentLength parameter of the GetVolumeInformation function +/// > (this value is commonly 255 characters) +/// from https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation +pub const NAME_MAX = 255; + pub const FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; pub const FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; pub const FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;