hn.today

You probably don't care about Unix File Mode and octmode bitwise comparison

delicate-pegasus-aac583.netlify.app9 points1 comments
Screenshot of You probably don't care about Unix File Mode and octmode bitwise comparison

Unix file permissions encode read, write, and execute rights for the owner, the owner’s group, and everyone else. Each permission is a bit: read is 4, write is 2, and execute is 1, so an octal mode such as 764 represents three groups of bits (owner, group, others). The account explains an R permission check that uses bitwise AND rather than comparing mode numbers directly. Applying `& "400"` keeps only the owner-read bit: the result is either 400, if that permission is set, or 000 if it is not. Comparing the result with 400 therefore identifies files their owner cannot read.

Masking matters because numeric comparison considers unrelated permission bits. A mode such as 1377 includes a sticky bit, making its numeric value greater than 400, even though the owner lacks read permission. A direct comparison would incorrectly treat it as sufficient; the mask isolates the permission the check actually needs. The same method checks directories against 700, requiring the owner to have read, write, and execute rights: if any of those bits is absent, the masked result falls below 700. The general pattern, `(mode & required_bits) < required_bits`, detects whether any required permission is missing. The discussion also demonstrates changing a file’s mode in R and explains why this logic appears in package checks, including checks for scripts that need 500.

Read on delicate-pegasus-aac583.netlify.app1 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Show HN: Rust Commander

Show HN: Rust Commander

Rust Commander is a Rust terminal file manager modeled on Midnight Commander, with dual-pane navigation, a persistent shell, archive and remote browsing, and background file operations. Version 0.3.1 adds file-change alerts, viewer copy controls, customizable skins, and faster SFTP previews and copying. (github.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.