Skip to content

flock: add first implementation - #697

Open
lmilz wants to merge 1 commit into
uutils:mainfrom
lmilz:flock-implement
Open

lmilz wants to merge 1 commit into
uutils:mainfrom
lmilz:flock-implement

Conversation

@lmilz

@lmilz lmilz commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #694

Adds a first implemention of flock with the core functionality from flock(1):

  • the three forms: flock FILE CMD [ARGS…], flock FILE -c CMD and flock FD
  • -s/-x/-e/-u (the last one wins, as in util-linux), -n/--nb, -w, -E, -c
  • lock files are created if needed; directories and read-only files are opened read-only
  • exit statuses follow util-linux: the -E code on conflict or timeout, the child's status (128+signal if it was killed), and the sysexits codes 64/65/66/69/71 for errors, with the same messages

Differences from util-linux:

  • usage errors exit with 1 (clap) instead of 64
  • -w polls with LOCK_NB every 5 ms instead of using a timer

Not implemented yet (follow-up PRs): -o, -F, --verbose, --fcntl, --start, --length.

@lmilz lmilz changed the title flock: add first implement flock: add first implementation Nov 14, 2026
Comment thread src/uu/flock/src/flock.rs Outdated
.open(path)
.or_else(|err| OpenOptions::new().read(true).open(path).map_err(|_| err))
.map_err(|err| {
USimpleError::new(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you please use a thiserror enum for the errors (like chcpu's errors.rs)? there are 5 USimpleError::new + format! here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. The errors are now a FlockError enum in errors.rs. I checked all error paths against util-linux 2.42 and messages and exit codes are unchanged

Comment thread src/uu/flock/src/flock.rs
.arg(
Arg::new(options::NONBLOCK)
.short('n')
.long("nonblocking")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

util-linux calls it --nonblock, not --nonblocking
it only works now because of infer_long_args

@lmilz lmilz Nov 15, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The util-linux 2.42 man page and --help list -n, --nb, --nonblocking, so I kept that as the long name. I added nonblock as an explicit alias, so it no longer depends on infer_long_args. All three spellings are covered by test_nonblocking_long_and_alias.

Comment thread src/uu/flock/src/flock.rs
.create(true)
.truncate(false)
.open(path)
.or_else(|err| OpenOptions::new().read(true).open(path).map_err(|_| err))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add a test for the read-only file fallback

@lmilz lmilz Nov 15, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added test_read_only_lock_file. I checked that it fails without the fallback.

Comment thread src/uu/flock/src/flock.rs Outdated
continue;
}
}
return Err(USimpleError::new(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a blocking flock interrupted by a signal (EINTR) ends up here with 65, is it expected?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, not intended. EINTR is now retried.

Comment thread src/uu/flock/src/flock.rs Outdated
let nonblock = matches.get_flag(options::NONBLOCK);
let conflict_exit_code = *matches.get_one::<u8>(options::CONFLICT_EXIT_CODE).unwrap();
let operation = lock_mode(&matches).operation();
let file_or_fd = matches.get_one(options::FILE_OR_FD).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

already read in lock_target, could be dedup, no?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. It is read once in uumain and passed to lock_target.

Comment thread tests/by-util/test_flock.rs Outdated
let file = File::create(&lockfile).unwrap();

assert_eq!(
unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

almost the same code 5 times, could you please add a helper that holds the lock?
and a SAFETY comment on the unsafe

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. Added hold_lock() with SAFETY comment.

Comment thread tests/by-util/test_flock.rs Outdated
}

#[test]
fn test_short_v_is_invalid() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this will break as soon as --verbose is implemented, do we need it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed.

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the patch!
please use a thiserror enum instead of the ad-hoc USimpleError strings (details inline).
please also squash the 5 commits into one.

Support the file, directory and file descriptor forms with -s/-x/-u, -n, -w, -E and -c.
Exit codes follow util-linux.

  Not yet implemented: -o, -F, --verbose, --fcntl.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement flock

2 participants