Will flock or lockf work on a directory? I there another way to lock a directory in C on a linux machine?
How can I lock a directory in C on a linux machine
9.8k views Asked by user357498 At
2
There are 2 answers
0
rustyx
On
Will
flockorlockfwork on a directory?
You can open the directory for reading and get an advisory exclusive lock using flock:
int fd = open("somedir", O_RDONLY);
if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
// success
} else {
// failed
}
But you can't open a directory for writing, which means you can't get a fcntl-style exclusive lock on it, neither advisory nor mandatory.
Of course, if you actually need to prevent shared access to a directory, that's an entirely different question from locking - you can change its permissions, rename it, lock the entire filesystem, use IPC, etc.
Related Questions in LINUX
- Is there some way to use printf to print a horizontal list of decrementing hex digits in NASM assembly on Linux
- Why does Hugo generate different taxonomy-related HTML on different OS's?
- Writes in io_uring do not advance the file offset
- Why `set -o pipefail` gives different output even though the pipe is not failing
- what really controls the permissions: UID or eUID?
- Compiling eBPF program in Docker fails due to missing '__u64' type
- Docker container unable to make HTTPS requests to external API
- Whow to use callback_query_handler in Python 3.10
- Create kea runtime directory at startup in Yocto image
- Problem on CPU scheduling algorithms in OS
- How to copy files into the singularity sandbox?
- Android kernel error: undefined reference to `get_hw_version_platform'
- Is there a need for BPF Linux namespace?
- Error when trying to execute a binary compiled in a Kali Linux machine on an Ubuntu system
- Issue with launching application after updating ElectronJs to version 28.0.0 on Windows and Linux
Related Questions in LOCKING
- The problem of "fine-grained locks and two-phase locking algorithm"
- Two Update statements on a row are running simultaneously with no locking in MYSQL
- When does shared and exclusive locks are acquired and released in a MySql transactions?
- SQL Server opportunistic locking
- Choosing the right hints during select for update in SQL Server
- Locking PDF after filling out text field
- Using dask.distributed with rioxarray rio.to_raster results in `ValueError: Lock is not yet acquired`
- Oracle 19c. REF Partitioning. Start redefinition Holds TM lock on parent table in 4 mode (when insert data into interim table)
- How to avoid LFS lock conflicts after merging master into feature branch?
- Maatwebsite excel global transaction problems
- How to implement read and write locking for a MongoDB collection?
- How to synchronize a python dictionary across a flask app and a background thread
- SQL Server SELECT WITH (NOLOCK) causing exclusive lock in tempdb
- Lock Acquisition Order in MYSQL
- Why is this giving me an IllegalMonitorStateException when all I'm doing is locking and then awaiting a condition?
Related Questions in DIRECTORY
- I cant access file directory with PHP local host on XAMPP. it just shows one of the files I have in my visual studio code
- PHP string variable to multiple rows in table sql insert
- Android - Why my app picks values on default folder (instead XXHDPI)
- Use htaccess to add subdirectory to url while displaying base url
- Dockerfile and package-json are in different folders
- VS2022 C++ empty directory for audio file. CMake; SFML
- Visual Studio Error: The system cannot find the file specified
- Email from SharePoint: "Heads up! We noticed that you recently deleted a large number of files from a site" but the user doesn't deleting anything
- Duplicate folder into new folder one level up in the directionary
- Shapefiles not showing up in assigned directory in R - says it exists but can't find it
- Recursively calculate SHA256 sum of all files in directory using OpenSSL
- $WinREAgent folder keeps to automatically appear
- thunderbird local archive folder missing
- Python folder path string escape character error
- Switch r settings to not have to create directory when saving files into new subfolders
Related Questions in FLOCK
- How to flock turtles within vision distance and heading degrees?
- What Happens to POSIX File Lock When the Underlying File is Overwritten
- Having a multi line if condition inside a flock command in a Makefile target
- flock intermittently deadlocking parallel processes
- PHP why can i get 2 concurrent LOCK_EX on the same handle?
- php -- is a file on a local or networked file system?
- Write() works even if i blocked my file by other process
- flock does not block
- Why flock() in fork() double blocks the same file with different descriptors?
- File descriptor from flock put in a variable is not found when releasing it with exec
- What does flock actually lock?
- Is there a way to run a PHP script using Apache2's context right after its startup?
- Perl flock: prevent multiple invocation based on variable
- Sharing a file between two processes in Python. One process writes into it, another reads information and processes it. The process that reads is slow
- What happens when a process has exclusive lock on a file and another process tries to write on it?
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
You can't open a directory for writing, so that means you can't get a write lock on it.
Even if you could, please keep in mind that
flockandfcntland other kinds of POSIX locks are advisory, so they don't actually prevent software that doesn't respect the lock from doing things.Maybe you want to look at something like
xfs_freezewhich locks an entire filesystem. It's probably not useful for your use case though.