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 best solutions below
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
- How do I recursively find and replace only in files named index.php on Linux webserver?
- passing text with \n as one argument in shell
- kernel module does not print packet info
- How to send ESC/POS commands to thermal printer in Linux
- (x64 Nasm) Writeline function on Linux
- How do I set the Hive user to something different than the Spark user from within a Spark program?
- Default priority of thread with SCHED_FIFO
- Calling a python function with options from shell script
- How to split a directory into parts without compressing or archiving?
- Cross compile simple standard C program on Linux for Mac
- How to offload NAPI poll function to workqueue
- python netifaces - How to get currently used network interface
- Unexpected output from function
- mingw-64 conflicting declarations when cross-compiling
- Different behavior of async with Visual Studio 2013(Windows8.1) and GCC 4.9(Ubuntu14.10)
Related Questions in LOCKING
- Excel 2013, Cells Will Not Protect
- Persist the value of one column in two simultaneous update
- Expired time for acquiring lock pentaho
- JPA JPQL Lock not working
- Erratic StampedLock.unlock(long) behaviour?
- Hazelcast Distributed Lock with iMap
- Hibernate increments version on both sides of many to many association
- I want to sleep while holding a mutex
- Simultaneous DML operations on same table based on different where clause
- Penalty of AtomicVariables over locks in Java
- Java combine explicit locks with synchronized methods
- lock function in php only for one user
- Multiple Simultaneous file writes with HMC4 on Classic ASP
- Java Multithreading - What Really Happens When Accessing A "Locked" Object?
- Mysql table simultaneous update of rows in single table
Related Questions in DIRECTORY
- Optimum directory structure for large number of files to display on a page
- install a R package from directory
- Are stringified MongoDB ObjectID's safe as folder names?
- Generate TCPDF output to a shared drive folder
- Get number of files in various subdirectories relative to the current page - ColdFusion
- Ruby on Windows XP: How to change directory of SSL certificates
- Google Drive API VB.NET Parent Folder of a Folder
- Count files in a directory while excluding others by directory and subdirectories using PHP
- Media files end up in in a pycharm subdirectory when uploading
- Remove part of filename of files that are in different folders
- Group items based on x number characters of basename
- Android Studio missing drawable Folder
- Updating folder structure with Mac Terminal
- Matlab error in file path for a sound
- Rails: Include Javascript_include_tag outside default folders
Related Questions in FLOCK
- Why my file is always empty when trying to read it using PHP?
- flock() doesn't preventing other process to get exclusive lock
- Should I use flock when reading a file in PHP?
- using flock inside crontab with node script throws an error
- Php Lock files when writte
- How does LOCK_SH work?
- Open File Description Locks confusion
- Open File Description Locks confusion(EDIT)
- Multiple users write to the same file at the same time using PHP
- PHP Lock file while reading - flock() makes my file blank
- getting notified on flock/lockf/fcntl changes without polling
- How to synchronise writes from multiple containers to the same volume on a swarm?
- How can I lock a directory in C on a linux machine
- Deadlock with flock, fork and terminating parent process
- Test if file is locked
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)
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?
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.