Skip to content

Standard library masks away bits in permissions struct #44147

Closed
@tmerr

Description

@tmerr

Behavior

On linux, mkdir mydir, chmod 2777 mydir then run:

fn main() {
  let path = "mydir";
  let perms = std::fs::metadata(path).unwrap().permissions();
  std::fs::set_permissions(path, perms).unwrap();
}

This changes the permissions of mydir to 777. Is this expected?

The cause

Metadata.permissions() masks away everything other than the lower 3 triads when it calls out to this code:

pub fn perm(&self) -> FilePermissions {
FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o777 }
}

Then std::fs::set_permissions sends these mode bits into into chmod which considers four triads, not three. Possibly useful references: chmod and mode_t

Should we change Metadata.permissions to preserve that triad, maybe by masking with 0o7777 instead of 0o777?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions