Assignment p6 - Jacobs University

Operating Systems
Jacobs University Bremen
¨ alder
¨
Dr. Jurgen
Schonw
¨
Course: 320202
Date: 2014-05-08
Deadline: 2014-05-14
Problem Sheet #6
Problem 6.1: syncbox
(4+3+3 = 10 points)
The Linux operating system provides an API called inotify that allows user space programs to
monitor changes in the filesystem. You will use this API to write a program called syncbox that
will detect changes in a directory of the filesystem and synchronize the monitored directory to a
second directory. The synchronization will be done by running a child process executing rsync
with proper arguments. Here is a sample executing of the program:
$ mkdir /tmp/foo
$ mkdir /tmp/bar
$ ./syncbox -v /tmp/foo /tmp/bar &
syncbox: added watch for /tmp/foo
syncbox: synchronizing /tmp/foo to /tmp/bar
syncbox: rsync -az --delete /tmp/foo /tmp/bar
syncbox: synchronization finished
$ touch /tmp/foo/a
syncbox: file a created
syncbox: file a closed for writing
syncbox: synchronizing /tmp/foo to /tmp/bar
syncbox: rsync -az --delete /tmp/foo /tmp/bar
syncbox: synchronization finished
$ ls -l /tmp/foo /tmp/bar/foo
/tmp/bar/foo:
total 0
-rw-r--r-- 1 schoenw schoenw 0 May 8 11:28 a
/tmp/foo:
total 0
-rw-r--r-- 1 schoenw schoenw 0 May 8 11:28 a
$ rm -rf /tmp/foo
syncbox: file a deleted
syncbox: watched file or directory deleted - exiting
syncbox: removed watch for /tmp/foo
This problem is best solved in several steps:
1. Read about the inotify API and write a program that uses the inotify API to watch for
changes in a directory specified on the command line. The program should print out which
changes have been detected. Make sure your program exists properly if the monitored directory is removed.
2. Add an event-loop to the program (e.g., using the select() system call. Write the logic to
trigger a synchronization once a change has been detected and a timeout period (e.g. 5
seconds) has passed.
3. Implement the synchronization by forking a child process that executes rsync -az --delete
src dst. Make sure only at most one rsync child process is running.
Your program should support the following command line syntax:
syncbox [-t seconds] [-v] [-n] src dst
The -t option allows to set the timeout (5 seconds by default). The -v option requests verbose
output (see the example above). The -n option requests a dry run where no synchronization is
performed. The src argument defines the local directory being monitored and the dst argument
defines the location of the copy.