Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Convert mode_t constants to octal #3634

Merged
merged 1 commit into from
Apr 26, 2024
Merged

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Mar 27, 2024

For example the "user read write execute" value is most appropriately represented in the following octal form:

pub const S_IRWXU: mode_t = 0o0700;

as opposed to what is currently found in libc, which is representations like:

pub const S_IRWXU: ::mode_t = 448;

pub const S_IRWXU: mode_t = 0x1c0;

The glibc manual consistently uses octal when describing values of mode_t, as in 0400 or 02000.


This PR is generated by this script:

#!/usr/bin/env -S cargo -Zscript
```cargo
[dependencies]
anyhow = "1"
glob = "0.3"
proc-macro2 = { version = "1", features = ["span-locations"] }
syn = { version = "2", default-features = false, features = ["full", "parsing"] }
```

use anyhow::Result;
use glob::glob;
use std::fs;
use syn::{Expr, Item, Lit};

fn main() -> Result<()> {
    for path in glob("src/**/*.rs")? {
        let path = path?;
        let mut src = fs::read_to_string(&path)?;
        let syntax_tree = syn::parse_file(&src)?;

        let mut offset = 0isize;
        let mut modified = false;
        for item in syntax_tree.items {
            // Find `pub const S_[IE] = $lit`.
            let Item::Const(item) = item else {
                continue;
            };
            let name = item.ident.to_string();
            if !["S_I", "S_E", "__S_I"].iter().any(|prefix| name.starts_with(prefix)) {
                continue;
            }
            let Expr::Lit(expr) = *item.expr else {
                continue;
            };
            let Lit::Int(lit) = expr.lit else {
                continue;
            };

            // Render integer value in octal.
            let value: u64 = lit.base10_parse()?;
            let mut octal = format!("0o{:04o}", value);
            if value >= 0o10000 {
                octal.insert(octal.len() - 4, '_');
            }

            // Replace original literal.
            let range = lit.span().byte_range();
            let start = (range.start as isize + offset) as usize;
            let end = (range.end as isize + offset) as usize;
            src.replace_range(start..end, &octal);
            offset += octal.len() as isize - range.len() as isize;
            modified = true;
        }

        if modified {
            fs::write(&path, src)?;
        }
    }
    Ok(())
}

@rustbot
Copy link
Collaborator

rustbot commented Mar 27, 2024

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Mar 27, 2024

Some changes occurred in OpenBSD module

cc @semarie

Some changes occurred in solarish module

cc @jclulow, @pfmooney

@bors
Copy link
Contributor

bors commented Apr 16, 2024

☔ The latest upstream changes (presumably #3654) made this pull request unmergeable. Please resolve the merge conflicts.

@dtolnay
Copy link
Member Author

dtolnay commented Apr 16, 2024

Rebased to fix conflict with #3654 in src/fuchsia/mod.rs.

Copy link
Member

@JohnTitor JohnTitor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@JohnTitor JohnTitor added this pull request to the merge queue Apr 26, 2024
Merged via the queue into rust-lang:main with commit 82806b3 Apr 26, 2024
42 checks passed
@dtolnay dtolnay deleted the mode_t branch April 26, 2024 14:10
@tgross35 tgross35 added the stable-nominated This PR should be considered for cherry-pick to libc's stable release branch label Nov 7, 2024
tgross35 pushed a commit to tgross35/rust-libc that referenced this pull request Nov 7, 2024
(backport <rust-lang#3634>)
(cherry picked from commit 3cc3f01)
tgross35 pushed a commit to tgross35/rust-libc that referenced this pull request Nov 7, 2024
(backport <rust-lang#3634>)
(cherry picked from commit 3cc3f01)
@tgross35 tgross35 mentioned this pull request Nov 7, 2024
tgross35 pushed a commit to tgross35/rust-libc that referenced this pull request Nov 7, 2024
(backport <rust-lang#3634>)
(cherry picked from commit 3cc3f01)
@tgross35 tgross35 added stable-applied This PR has been cherry-picked to libc's stable release branch and removed stable-nominated This PR should be considered for cherry-pick to libc's stable release branch labels Nov 7, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-review stable-applied This PR has been cherry-picked to libc's stable release branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants