[lug] how to handle open files - upload server

rm at fabula.de rm at fabula.de
Tue Jan 13 12:16:48 MST 2004

On Tue, Jan 13, 2004 at 12:03:18PM -0700, Bear Giles wrote:
> Backups have a similar problem and the way I handle it is to get a 
> nonexclusive lock on all files being saved.  This doesn't always 
> work, but if sftp doesn't get an exclusive lock (either directly 
> or indirectly via fopen()) it's an easy patch.
> Once you know that sftp locks files being uploaded, you can write 
> a quick C program to produce a list of all lockable files in the 
> subdirectory.

I'd go this way too. There are two problems arising in the OP's 

   - his program opens a file allready opened by another application.

   - some other application mutating the file during his applications
     reads (i.e. opening it _after_ the program in question opens it).

 For the first i'd suggest to first get the directory entry for the
 file (via opendir etc.) from there  get the inode number and access
 the inode structure by that. The inode structure should have a 'link
 count' field (too far away from my refences :) which tells you how
 many filehandles/directory references point to the file.

 But unfortunately that's not enough (second problem from above).
 An exclusive lock is almost a working solution -- iff all programs
 involved honor it ...
 Another approach might be to change the file's attributes to 'imutable'
 (works only on filesystems that support those attributes).

 A _big_ caveat: all these techniques use non-atomic operations and
 hence are vulnerable to race conditions (well, i'm not shure about
 exclusive locking allone, i'd have to look it up).

 HTH Ralf Mattes
> Bear
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug

More information about the LUG mailing list