Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ feat_common_core = [
"chcpu",
"ctrlaltdel",
"dmesg",
"flock",
"fsfreeze",
"hexdump",
"last",
Expand Down Expand Up @@ -102,6 +103,7 @@ cal = { optional = true, version = "0.0.1", package = "uu_cal", path = "src/uu/c
chcpu = { optional = true, version = "0.0.1", package = "uu_chcpu", path = "src/uu/chcpu" }
ctrlaltdel = { optional = true, version = "0.0.1", package = "uu_ctrlaltdel", path = "src/uu/ctrlaltdel" }
dmesg = { optional = true, version = "0.0.1", package = "uu_dmesg", path = "src/uu/dmesg" }
flock = { optional = true, version = "0.0.1", package = "uu_flock", path = "src/uu/flock" }
fsfreeze = { optional = true, version = "0.0.1", package = "uu_fsfreeze", path = "src/uu/fsfreeze" }
hexdump = { optional = true, version = "0.0.1", package = "uu_hexdump", path = "src/uu/hexdump" }
last = { optional = true, version = "0.0.1", package = "uu_last", path = "src/uu/last" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ First, reimplement the most important tools from util-linux:
## Utility Tools
- [ ] `lsblk`: Lists block devices.
- [ ] `fallocate`: Preallocates file space.
- [ ] `flock`: Manages file locks.
- [ ] `flock`: Manages file locks. (started)
- [ ] `getopt`: Parses command options.
- [ ] `hardlink`: Creates hard links.
- [x] `mcookie`: Generates random numbers.
Expand Down
17 changes: 17 additions & 0 deletions src/uu/flock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "uu_flock"
version = "0.0.1"
edition = "2021"

[dependencies]
uucore = { workspace = true }
thiserror = { workspace = true }
libc = { workspace = true }
clap = { workspace = true }

[lib]
path = "src/flock.rs"

[[bin]]
name = "flock"
path = "src/main.rs"
9 changes: 9 additions & 0 deletions src/uu/flock/flock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# flock

```
flock [options] <file>|<directory> <command> [<argument>...]
flock [options] <file>|<directory> -c <command>
flock [options] <file descriptor number>
```

Manage file locks from shell scripts.
53 changes: 53 additions & 0 deletions src/uu/flock/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use std::{ffi::OsString, io};
use uucore::{
display::Quotable,
error::{strip_errno, UError},
};

// Exit codes
pub const EX_USAGE: i32 = 64;
pub const EX_DATAERR: i32 = 65;
pub const EX_NOINPUT: i32 = 66;
pub const EX_UNAVAILABLE: i32 = 69;
pub const EX_OSERR: i32 = 71;

#[derive(Debug, thiserror::Error)]
pub enum FlockError {
#[error("cannot open lock file {}: {}", .path.to_string_lossy(), strip_errno(.source))]
OpenLockFile { path: OsString, source: io::Error },

#[error("bad file descriptor: {}", .file_or_fd.quote())]
BadFileDescriptor { file_or_fd: OsString },

#[error("cannot set up timer: Invalid argument")]
TimerSetup,

#[error("{}: {}", .file_or_fd.to_string_lossy(), strip_errno(.source))]
CannotAcquireLock {
file_or_fd: OsString,
source: io::Error,
},

#[error("failed to execute {}: {}", .program.to_string_lossy(), strip_errno(.source))]
FailedToExecute {
program: OsString,
source: io::Error,
},
}

impl UError for FlockError {
fn code(&self) -> i32 {
match self {
Self::BadFileDescriptor { .. } => EX_USAGE,
Self::CannotAcquireLock { .. } => EX_DATAERR,
Self::OpenLockFile { .. } => EX_NOINPUT,
Self::FailedToExecute { .. } => EX_UNAVAILABLE,
Self::TimerSetup => EX_OSERR,
}
}
}
Loading
Loading