Annotation of wpscripts/ztransfer/fileops.pm, Revision 1.1.1.1
1.1 yason 1: #!/usr/bin/perl
2: # $Id: fileops.pm,v 1.1.1.4 2006/02/17 09:16:23 init Exp $
3: package fileops;
4: use log;
5:
6: my $FILE_LOCK = "/var/run/ztransfer.lock";
7:
8:
9: sub cleanup()
10: {
11: # clanup procedure
12: # remove all .rar files in /tmp
13: # THIS CODE SHOULD BE FIXED
14: `rm -f /tmp/*.rar`;
15: }
16:
17:
18: sub checkdir($)
19: {
20: my $file = shift;
21: chomp( my $dir = `dirname '$file'` );
22:
23: if(! -e $dir)
24: {
25: $_ = system("mkdir -p '$dir'");
26: if($_ == 0)
27: {
28: log::main("Created directory $dir");
29: }
30: else
31: {
32: # directory has not created
33: log::error("Can not create directory $dir");
34: }
35: }
36:
37: }
38:
39:
40: sub take_lock()
41: {
42: open( FILE, ">$FILE_LOCK") or log::critical("Can not create lock file $FILE_LOCK");
43: # write PID of current process to file
44: print FILE $$;
45: close(FILE);
46: }
47:
48:
49: sub release_lock()
50: {
51: # I think this is bad for a while
52: `rm $FILE_LOCK`;
53: }
54:
55:
56: sub check_lock()
57: {
58: # I'm really sure that this technique (file-locks) is not very
59: # good idea since it is no good way to determine if one copy
60: # of this script is already running now.
61: # My implementation based on writing a PID of a current process
62: # and (on startup) check if that file (or PID in it) is exists in
63: # processes tree (`ps ax`). It uses standart utility grep(1).
64: my $PID;
65:
66: if(! -e $FILE_LOCK)
67: {
68: # continue running main transfer code
69: return()
70: }
71: else
72: {
73: open(FILE, $FILE_LOCK) or log::critical("Can not open existing lock file $FILE_LOCK");
74: $PID = <FILE>;
75: close(FILE);
76: $_ = `ps ax | egrep '\\b$PID\\b'`;
77: if($_ ne '')
78: {
79: # another copy is running
80: log::critical("Another copy is running! Exiting..");
81: }
82: else
83: {
84: # previous run of script ends incorrectly
85: # and he hadn't remove his lock
86:
87: # return to main (should call take_lock() within it hardly)
88: return()
89: }
90: }
91: }
92:
93: return(1);
CVSweb