Set Execute Permissions On a File in Rust

September 2025

All the ususal security cavets about making files executable and watching out for privididate excelation, etc... apply.

--- 
[dependencies]
anyhow = "1"
---

use anyhow::Result;
use std::path::PathBuf;
use std::fs;
use std::os::unix::fs::PermissionsExt;

fn main() -> Result<()> {
  let path = PathBuf::from("permissions-example/test.py");
  make_executable(&path)?;
  Ok(())
}

pub fn make_executable(path: &PathBuf) -> Result<()> {
  let file = fs::File::open(path)?;
  let bit_update = 0o700;
  let mut permissions = file.metadata()?.permissions();
  dbg!(&permissions);
  permissions.set_mode(permissions.mode() | bit_update);
  dbg!(&permissions);
  file.set_permissions(permissions)?;
  Ok(())
}
Output:
[/Users/alan/.cargo/target/4b/0baba35be9214e/_active_nvim_run:21:3] &permissions = Permissions(
    FilePermissions {
        mode: 0o100644 (-rw-r--r--),
    },
)
[/Users/alan/.cargo/target/4b/0baba35be9214e/_active_nvim_run:23:3] &permissions = Permissions(
    FilePermissions {
        mode: 0o100744 (-rwxr--r--),
    },
)
end of line

Endnotes

This sets the executable bit for the user without updating the `group` or `world` bits.

Changing the update to

let bit_update = 0o600;

does not change the permission back to `644`. TBD on how to do that.

References