4

This is a hypothetical scenario. Say that I have a particular folder that is being synchronized by a process which is receiving a files/folders via the internet. Now while this operation is in progress, for the synchronizer's sake, there should be no cp operations to its directory. Anyway this can be acheived?

1
  • Do you have control over what starts the cp process? Do other programs need to be able to write to the directory while it's being synchronized? What about reading? Why is this an issue: what are your requirements in terms of consistency? Commented Jul 8, 2012 at 0:42

3 Answers 3

3

There are at least two possible solutions:

  1. Create a wrapper script for cp which checks if the synchronization is running, either by checking if the process is running or testing a lockfile, and abort or delay the operation.
  2. Use LD_PRELOAD to hook up write and other calls.

The problem with first solution is that you'd have to wrap all commands used to write into the folder, e.g. mv, cp, rsync, rm, ln, cat … On the other hand it is quite simple but will also work only for your current user.

For the second solution you'd have to write a shared library with all the syscalls used to change/modify the directory, e.g. write, unlink, rename. But this solution would work with all dynamically linked executables - so no need to adjust every other program.

2

If you know that the synchronization process is running and know that the only possible operation is cp, then you could send SIGSTOP to all processes to pause execution. After the synchronization completes, you send SIGCONT to all processes to continue.

For that purpose, you can use kill -s SIGSTOP $(pidof cp) or killall -s SIGSTOP cp. In a terminal, you can use Ctrl + Z to background a task ("pause"). To continue execution, use the fg command ("foreground", see help fg).

1

Your synchronizer is writing to the directory rather than reading, right? So taking a filesystem snapshot or setting the immutable bit is out.

If you're running the synchronizer as root, you can change the ownership of the directory to root and the permissions to 700 (rwx------; only owner can do anything). This will keep out any new processes not running as root, but any process that already has a file or subdirectory open will still be able to make changes.

On Linux, if you want to find all processes that have currently files open in your tree, you can do lsof +D my_directory. What you do with that knowledge, though, depends on the situation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .