pub fn new_connection_validate_flags(
    mut flags: u32,
    mode: u32,
    mut readable: bool,
    mut writable: bool,
    append_allowed: bool,
) -> Result<u32, Status> {
    // There should be no MODE_TYPE_* flags set, except for, possibly, MODE_TYPE_FILE when the
    // target is a pseudo file.
    if (mode & !MODE_PROTECTION_MASK) & !MODE_TYPE_FILE != 0 {
        if mode & MODE_TYPE_MASK == MODE_TYPE_DIRECTORY {
            return Err(Status::NOT_DIR);
        } else {
            return Err(Status::INVALID_ARGS);
        };
    }

    if flags & OPEN_FLAG_NODE_REFERENCE != 0 {
        flags &= OPEN_FLAGS_ALLOWED_WITH_NODE_REFERENCE;
        readable = false;
        writable = false;
    }

    if flags & OPEN_FLAG_DIRECTORY != 0 {
        return Err(Status::NOT_DIR);
    }

    if flags & OPEN_FLAG_NOT_DIRECTORY != 0 {
        flags &= !OPEN_FLAG_NOT_DIRECTORY;
    }

    // For files OPEN_FLAG_POSIX is just ignored, as it has meaning only for directories.
    flags &= !OPEN_FLAG_POSIX;

    let allowed_flags = OPEN_FLAG_NODE_REFERENCE
        | OPEN_FLAG_DESCRIBE
        | OPEN_FLAG_CREATE
        | OPEN_FLAG_CREATE_IF_ABSENT
        | if readable { OPEN_RIGHT_READABLE } else { 0 }
        | if writable { OPEN_RIGHT_WRITABLE | OPEN_FLAG_TRUNCATE } else { 0 }
        | if writable && append_allowed { OPEN_FLAG_APPEND } else { 0 };

    let prohibited_flags = (0 | if readable {
            OPEN_FLAG_TRUNCATE
        } else {
            0
        } | if writable {
            OPEN_FLAG_APPEND
        } else {
            0
        })
        // allowed_flags takes precedence over prohibited_flags.
        & !allowed_flags
        // TRUNCATE is only allowed if the file is writable.
        | if flags & OPEN_RIGHT_WRITABLE == 0 {
            OPEN_FLAG_TRUNCATE
        } else {
            0
        };

    if !readable && flags & OPEN_RIGHT_READABLE != 0 {
        return Err(Status::ACCESS_DENIED);
    }

    if !writable && flags & OPEN_RIGHT_WRITABLE != 0 {
        return Err(Status::ACCESS_DENIED);
    }

    if flags & prohibited_flags != 0 {
        return Err(Status::INVALID_ARGS);
    }

    if flags & !allowed_flags != 0 {
        return Err(Status::NOT_SUPPORTED);
    }

    Ok(flags)
}
/// Validate that the requested flags for a new connection are valid.  This function will make sure
/// that `flags` only requests read access when `readable` is true, or only write access when
/// `writable` is true, or either when both are true.  It also does some sanity checking and
/// normalization, matching `flags` with the `mode` value.
///
/// On success, returns the validated flags, with some ambiguities cleaned up.  On failure, it
/// returns a [`Status`] indicating the problem.
///
/// `OPEN_FLAG_NODE_REFERENCE` is preserved and prohibits both read and write access.
///
/// Changing this function can be dangerous!  Flags operations may have security implications.
Validate that the requested flags for a new connection are valid.

On success, returns the validated flags, with some ambiguities cleaned up.  On failure, it
returns a [`Status`] indicating the problem.

`OPEN_FLAG_NODE_REFERENCE` is preserved and prohibits both read and write access.

Changing this function can be dangerous.  Flags operations may have security implications.
Validate that the requested flags for a new connection are valid . On success returns the validated flags with some ambiguities cleaned up . On failure it returns a [ ` Status ` ] indicating the problem . ` OPEN_FLAG_NODE_REFERENCE ` is preserved and prohibits both read and write access . Changing this function can be dangerous . Flags operations may have security implications .
pub fn new_connection_validate_flags(
    mut flags: u32,
    mode: u32,
    mut readable: bool,
    mut writable: bool,
    append_allowed: bool,
) -> Result<u32, Status> {
    // There should be no MODE_TYPE_* flags set, except for, possibly, MODE_TYPE_FILE when the
    // target is a pseudo file.
    if (mode & !MODE_PROTECTION_MASK) & !MODE_TYPE_FILE != 0 {
        if mode & MODE_TYPE_MASK == MODE_TYPE_DIRECTORY {
            return Err(Status::NOT_DIR);
        } else {
            return Err(Status::INVALID_ARGS);
        };
    }
    if flags & OPEN_FLAG_NODE_REFERENCE != 0 {
        flags &= OPEN_FLAGS_ALLOWED_WITH_NODE_REFERENCE;
        readable = false;
        writable = false;
    }
    if flags & OPEN_FLAG_DIRECTORY != 0 {
        return Err(Status::NOT_DIR);
    }
    if flags & OPEN_FLAG_NOT_DIRECTORY != 0 {
        flags &= !OPEN_FLAG_NOT_DIRECTORY;
    }
    // For files OPEN_FLAG_POSIX is just ignored, as it has meaning only for directories.
    flags &= !OPEN_FLAG_POSIX;
    let allowed_flags = OPEN_FLAG_NODE_REFERENCE
        | OPEN_FLAG_DESCRIBE
        | OPEN_FLAG_CREATE
        | OPEN_FLAG_CREATE_IF_ABSENT
        | if readable { OPEN_RIGHT_READABLE } else { 0 }
        | if writable { OPEN_RIGHT_WRITABLE | OPEN_FLAG_TRUNCATE } else { 0 }
        | if writable && append_allowed { OPEN_FLAG_APPEND } else { 0 };
    let prohibited_flags = (0 | if readable {
            OPEN_FLAG_TRUNCATE
        } else {
            0
        } | if writable {
            OPEN_FLAG_APPEND
        } else {
            0
        })
        // allowed_flags takes precedence over prohibited_flags.
        & !allowed_flags
        // TRUNCATE is only allowed if the file is writable.
        | if flags & OPEN_RIGHT_WRITABLE == 0 {
            OPEN_FLAG_TRUNCATE
        } else {
            0
        };
    if !readable && flags & OPEN_RIGHT_READABLE != 0 {
        return Err(Status::ACCESS_DENIED);
    }
    if !writable && flags & OPEN_RIGHT_WRITABLE != 0 {
        return Err(Status::ACCESS_DENIED);
    }
    if flags & prohibited_flags != 0 {
        return Err(Status::INVALID_ARGS);
    }
    if flags & !allowed_flags != 0 {
        return Err(Status::NOT_SUPPORTED);
    }
    Ok(flags)
}
pub fn new_connection_validate_flags ( mut flags : u32 , mode : u32 , mut readable : bool , mut writable : bool , append_allowed : bool , ) -> Result < u32 , Status > { // There should be no MODE_TYPE_* flags set, except for, possibly, MODE_TYPE_FILE when the // target is a pseudo file. if ( mode & ! MODE_PROTECTION_MASK ) & ! MODE_TYPE_FILE != 0 { if mode & MODE_TYPE_MASK == MODE_TYPE_DIRECTORY { return Err ( Status :: NOT_DIR ) ; } else { return Err ( Status :: INVALID_ARGS ) ; } ; } if flags & OPEN_FLAG_NODE_REFERENCE != 0 { flags &= OPEN_FLAGS_ALLOWED_WITH_NODE_REFERENCE ; readable = false ; writable = false ; } if flags & OPEN_FLAG_DIRECTORY != 0 { return Err ( Status :: NOT_DIR ) ; } if flags & OPEN_FLAG_NOT_DIRECTORY != 0 { flags &= ! OPEN_FLAG_NOT_DIRECTORY ; } // For files OPEN_FLAG_POSIX is just ignored, as it has meaning only for directories. flags &= ! OPEN_FLAG_POSIX ; let allowed_flags = OPEN_FLAG_NODE_REFERENCE | OPEN_FLAG_DESCRIBE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_IF_ABSENT | if readable { OPEN_RIGHT_READABLE } else { 0 } | if writable { OPEN_RIGHT_WRITABLE | OPEN_FLAG_TRUNCATE } else { 0 } | if writable && append_allowed { OPEN_FLAG_APPEND } else { 0 } ; let prohibited_flags = ( 0 | if readable { OPEN_FLAG_TRUNCATE } else { 0 } | if writable { OPEN_FLAG_APPEND } else { 0 } ) // allowed_flags takes precedence over prohibited_flags. & ! allowed_flags // TRUNCATE is only allowed if the file is writable. | if flags & OPEN_RIGHT_WRITABLE == 0 { OPEN_FLAG_TRUNCATE } else { 0 } ; if ! readable && flags & OPEN_RIGHT_READABLE != 0 { return Err ( Status :: ACCESS_DENIED ) ; } if ! writable && flags & OPEN_RIGHT_WRITABLE != 0 { return Err ( Status :: ACCESS_DENIED ) ; } if flags & prohibited_flags != 0 { return Err ( Status :: INVALID_ARGS ) ; } if flags & ! allowed_flags != 0 { return Err ( Status :: NOT_SUPPORTED ) ; } Ok ( flags ) }
Validate that the requested flags for a new connection are valid.
Validate that the requested flags for a new connection are valid .
flags
u32
mode
u32
readable
bool
writable
bool
append_allowed
bool
flags
u32
mode
u32
readable
bool
writable
bool
append_allowed
bool