tags:

views:

120

answers:

4

I have a linux (Fedora) program that detects the insertion of a USB stick and does an automatic backup via a call to

system("rsync...

Once the system call returns I prompt the user to remove the USB stick. The problem is that the system call returns before rsync has finished copying all of the files to the USB stick. Once the files are big enough the user could easily pull the stick out before the files are all copied.

Short of writing my own cpdir command does anyone know if there is a way around this? I don't see any options on either the system() call or rsync (or cp for that matter) that force a "wait until copy completed" state.

Thanks in advance.

Dave

+1  A: 

How do you determine that the call returns earlier than expected? Normally, it shouldn't unless you explicitly send it to background with a trailing & character after the command.

I think that rsync finishes but the data isn't really written to the usb-stick. You have to unmount the drive properly and wait until the umount command finishes.

Otto Allmendinger
Try cp bigfile /media/floppy/bigfile. The cp command returns way before the copy is complete.I have the system set up to mount and unmount the drive automatically but I will try unmounting the drive and see what happens.Thanks
David Huff
A: 

Use 2 threads which you'll synchronize ... make the one that removes the stick to wait for the one that does rsync?

hyperboreean
A: 

before running umount on the USB device, check to see if any processes (i.e. rsync) are still using it:

http://stackoverflow.com/questions/624154/linux-which-process-is-causing-device-busy-when-doing-umount/624216

Finer Recliner
A: 

You need to call sync() after the call to rsync. Sync won't return until all pending data is written to the disk.

From the man page:

The sync() function forces a write of dirty (modified) buffers in the block buffer cache out to disk. The kernel keeps this information in core to reduce the number of disk I/O transfers required by the system. As informa- tion in the cache is lost after a system crash a sync() call is issued fre- quently by the user process update(8) (about every 30 seconds).

KeithB