@@ -48,6 +48,30 @@ 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
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.
55+ /// The byte count does not include a null sentinel byte.
56+ pub const MAX_NAME_BYTES = switch (builtin .os .tag ) {
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 ,
60+ .netbsd , .openbsd , .solaris = > os .MAXNAMLEN ,
61+ // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
62+ // If it would require 4 UTF-8 bytes, then there would be a surrogate
63+ // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
64+ .windows = > os .windows .NAME_MAX * 3 ,
65+ // For WASI, the MAX_NAME will depend on the host OS, so it needs to be
66+ // as large as the largest MAX_NAME_BYTES (Windows) in order to work on any host OS.
67+ // TODO determine if this is a reasonable approach
68+ .wasi = > os .windows .NAME_MAX * 3 ,
69+ else = > if (@hasDecl (root , "os" ) and @hasDecl (root .os , "NAME_MAX" ))
70+ root .os .NAME_MAX
71+ else
72+ @compileError ("NAME_MAX not implemented for " ++ @tagName (builtin .os .tag )),
73+ };
74+
5175pub const base64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" .* ;
5276
5377/// Base64 encoder, replacing the standard `+/` with `-_` so that it can be used in a file name on any filesystem.
@@ -680,7 +704,7 @@ pub const IterableDir = struct {
680704 index : usize ,
681705 end_index : usize ,
682706 first_iter : bool ,
683- name_data : [256 ]u8 ,
707+ name_data : [MAX_NAME_BYTES ]u8 ,
684708
685709 const Self = @This ();
686710
0 commit comments