| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 2 | ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, |
| 3 | fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, |
| 4 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#, |
| 5 | osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/::{AsRawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/AsRawFd#, RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#}, |
| 6 | }; |
| 7 | |
| 8 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().; |
| 9 | |
| 10 | pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) struct FileLockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock# { |
| 11 | fdrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#fd.: RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#, |
| 12 | } |
| 13 | |
| 14 | impl FileLockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock# { |
| 15 | /// Get an exclusive lock on the file, waits if there is currently a lock |
| 16 | /// on the file if `nonblocking` is true. |
| 17 | pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn exclusiverust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock]exclusive().(file: &Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, nonblocking: bool) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock]> { |
| 18 | let fd = file.as_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/impl#[`fs::File`][AsRawFd]as_raw_fd().(); |
| 19 | flockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/flock().(fd, LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#::LockExclusiverust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#LockExclusive#, nonblocking)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 20 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock] { fdrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#fd. }) |
| 21 | } |
| 22 | |
| 23 | /// Release the lock on the file. |
| 24 | pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn unlockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock]unlock().(self) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 25 | flockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/flock().(self.fdrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#fd., LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#::Unlockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#Unlock#, false) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for FileLockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock# { |
| 30 | fn droprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock][Drop]drop().(&mut self) { |
| 31 | flockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/flock().(self.fdrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#fd., LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#::Unlockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#Unlock#, false).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 36 | enum LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp# { |
| 37 | LockExclusiverust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#LockExclusive#, |
| 38 | Unlockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#Unlock#, |
| 39 | } |
| 40 | |
| 41 | impl LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp# { |
| 42 | fn as_flock_operationrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[LockOp]as_flock_operation().(self) -> c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int# { |
| 43 | match self { |
| 44 | LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#::LockExclusiverust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#LockExclusive# => libcrust-analyzer cargo libc 0.2.186 crate/::LOCK_EXrust-analyzer cargo libc 0.2.186 unix/linux_like/LOCK_EX., |
| 45 | LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#::Unlockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#Unlock# => libcrust-analyzer cargo libc 0.2.186 crate/::LOCK_UNrust-analyzer cargo libc 0.2.186 unix/linux_like/LOCK_UN., |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn flockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/flock().(fd: RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#, action: LockOprust-analyzer cargo sudo-rs 0.2.14 system/file/lock/LockOp#, nonblocking: bool) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 51 | let mut operation = action.as_flock_operationrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[LockOp]as_flock_operation().(); |
| 52 | if nonblocking { |
| 53 | operation rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().libcrust-analyzer cargo libc 0.2.186 crate/::LOCK_NBrust-analyzer cargo libc 0.2.186 unix/linux_like/LOCK_NB.; |
| 54 | } |
| 55 | |
| 56 | // SAFETY: even if `fd` would not be a valid file descriptor, that would merely |
| 57 | // result in an error condition, not UB |
| 58 | cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::flockrust-analyzer cargo libc 0.2.186 flock().(fd, operation) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 59 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 60 | } |
| 61 | |
| 62 | #[cfg(test)] |
| 63 | mod testsrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/tests/ { |
| 64 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::testsrust-analyzer cargo sudo-rs 0.2.14 system/tests/::tempfilerust-analyzer cargo sudo-rs 0.2.14 system/tests/tempfile().; |
| 65 | |
| 66 | use superrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/::*; |
| 67 | |
| 68 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 69 | fn test_locking_of_tmp_filerust-analyzer cargo sudo-rs 0.2.14 system/file/lock/tests/test_locking_of_tmp_file().() { |
| 70 | let f = tempfilerust-analyzer cargo sudo-rs 0.2.14 system/tests/tempfile().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 71 | |
| 72 | FileLockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#::exclusiverust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock]exclusive().(&f, false).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unlockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock]unlock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 73 | } |
| 74 | } |