How to access NAME_MAX in rust

I see it defined as an enum. Is this the right way to get at NAME_MAX which is typicaly 255 for linux.

I also see in the code that NAME_MAX to what I think is libc NAME_MAX
/// Maximum number of bytes in a filename (not including the terminating
/// null of a filename string).

Yet I run into

if (file_bytes.len() as i32 > nix::unistd::PathconfVar::NAME_MAX) {
    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found enum `nix::unistd::PathconfVar`

If I try to type cast it as

if (file_bytes.len() as i32 > nix::unistd::PathconfVar::NAME_MAX as i32) {

It compiles, it seems translate to 3, instead of 255 that I expect.

_PC_NAME_MAX is the int name parameter to be passed to pathconf, not the actual limit value.

Thanks. Is there access to NAME_MAX from within RUST?

I see [

libffi_sys::NAME_MAX - Rust -


Would this be a better way?
Tring to avoid hard coding it to 255

That libffi-sys value looks like something that was mass-defined by bindgen, not something you should rely on unless you're using it for that crate in particular.

The best way probably depends on what you want to use it for -- you're showing a length check, but are you generating a name? What will you do if it's too long? It might not be worth worrying about, just try to create a file and let ENAMETOOLONG error out.

I suppose if you really want to check, the ideal is a runtime pathconf(dir, _PC_NAME_MAX), because this will account for the specific filesystem limitations in that path.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.