Annotation of wpscripts/ztransfer/send.pm, Revision 1.4
1.1 yason 1: #!/usr/bin/perl
1.4 ! yason 2: # $Id: send.pm,v 1.3 2011/07/01 12:44:28 yason Exp $
1.1 yason 3: package send;
4: use fetch;
5: use db;
6: use log;
7: use fileops;
8: use warnings;
9:
10: # spool path constants (without leading slash)
1.2 yason 11: our $SPOOL_ZAYAVKI = "/mnt/archive/archive/wp/zayavki";
12: our $SPOOL_PROBAS = "/mnt/archive/archive/wp/proba";
13: our $SPOOL_TMPDIR = "/mnt/archive/archive/temp";
1.1 yason 14:
15: # temp variables for map table lookup
16: our $ret;
17: our $tmpd;
18: our $tmpf;
19:
20: sub zayavki()
21: {
22: my @dbids;
23: my @localpaths;
24: my @remotepaths;
25: my $dbids_ref;
26: my $localpaths_ref;
27: my $i = 0;
28: my $okay = 0;
29: my $curfile;
30:
31: my $size_orig;
32: my $size_arc;
33:
34: my $spool;
35: my $delete;
36:
37: ($dbids_ref, $localpaths_ref) = fetch::zayavki_list();
38:
39: @dbids = @$dbids_ref;
40: @localpaths = @$localpaths_ref;
41: log::main("Fetched " . scalar(@localpaths) . " zayavki from database");
42: log::zayavki("<< Starting transfer process >>");
43:
44: @remotepaths = @localpaths; # clone
45:
46:
47: while($localpaths[$i])
48: {
49: # check if there is a void element
50: # ???
51: next if(!$localpaths[$i]);
52:
53: #
54: #
55: # fill $spool with right spool directory
56: if( db::get_tx_count($dbids[$i]) > 0 )
57: {
58: # retransmission
59: # set $spool to temporary directory to make backup collector happy :)
60: # in this case also let $delete = 1 (removing garbage after copy, as in old times)
61:
62: # considering okay to not to check /usr/backup for availability
63: $spool = $SPOOL_TMPDIR;
64: $delete = 1;
65: }
66: else
67: {
68: # this is the first time copy
69: # set $spool to backup spool directory
70: # and not to delete archive
71:
72: $spool = $SPOOL_ZAYAVKI;
73: $delete = 0;
74: }
75:
76: # lookup in table first
77: chomp( $tmpf = `basename $remotepaths[$i]` );
78: chomp( $_ = `dirname $remotepaths[$i]` );
79: $tmpd = hash::lookup($_);
80: print "\$remotepaths[\$i] is $remotepaths[$i] \$_ is $_ \$tmpd is $tmpd\n";
81: if ( $tmpd ne '' )
82: {
83: $remotepaths[$i] = $tmpd. "/". $tmpf;
84: }
85: else
86: {
87: # hardcoded
1.3 yason 88: $remotepaths[$i] =~ s/\/mnt\/maket/\/mnt\/rmt_maket/;
1.1 yason 89: }
90:
91: if(-e $remotepaths[$i] || -e $remotepaths[$i] . '.rar')
92: {
93: log::error("Remote file $remotepaths[$i] or $remotepaths[$i].rar already exists");
94: db::change_status($dbids[$i], 1);
95: }
96: else
97: {
98: # In this place we must be sure that local file is ok
99: if( -e $localpaths[$i] )
100: {
101: # set $size_orig
102: $size_orig = -s "$localpaths[$i]";
103:
104: # rar and copy local file
105: fileops::checkdir($remotepaths[$i]);
106: chomp($curfile = `basename $localpaths[$i]`) , $curfile .= '.rar';
107:
108: # two functions in log.pm makes newer versions of zlist happy
109: log::file_work_on($curfile, "rar");
110:
111: log::zayavki("compressing '$localpaths[$i]' -> '$spool/$curfile'");
112:
1.4 ! yason 113: `/usr/bin/rar a -rr10% -ep '$spool/$curfile' '$localpaths[$i]'`;
1.1 yason 114: if( ($? >> 8) == 0)
115: {
116: # rar exited successfully
117: # set $size_arc
118: $size_arc = -s "$spool/$curfile";
119:
120: # it can be possible that file already uploaded by hand while rar copressed one
121: # so we should check for existence of archive in remote before send it
122: if( -e $remotepaths[$i] || -e $remotepaths[$i] . '.rar')
123: {
124: log::error("Can not save $remotepaths[$i].rar cause of file has appeared after compressing");
125: log::zayavki("Can not save $remotepaths[$i].rar cause of file has appeared after compressing");
126: # file was uploaded by hand, let us change its status in db
127: db::change_status($dbids[$i], 1);
128:
129: # log::file_work_done();
130: }
131: else
132: {
133: # copy archive and then delete it from /usr/backup/spool
134: log::zayavki("transferring $spool/$curfile -> $remotepaths[$i].rar");
135:
136: log::file_work_on($curfile, "copy");
137:
138: if( system("cp '$spool/$curfile' $remotepaths[$i].rar") == 0 )
139: {
140: # OKAY
141:
142: log::zayavki("saved $localpaths[$i] -> $remotepaths[$i].rar [size_orig=$size_orig, size_arc=$size_arc]");
143:
144: log::file_work_done();
145:
146: log::diff($localpaths[$i], $size_orig, $size_arc);
147: # update database
148: db::change_status($dbids[$i], 1);
149: db::write_size_orig($dbids[$i], $size_orig);
150: db::write_size_arc($dbids[$i], $size_arc);
151:
152: # write ux_rpaths
153: db::write_ux_rpath($dbids[$i], "$remotepaths[$i].rar");
154:
155: # increment tx_count
156: db::increment_tx_count($dbids[$i]);
157:
158: $okay++;
159: }
160: else
161: {
162: # error in the copy process
163: log::zayavki("error in copy process (see error log)");
164: log::error("error in copy process: $?");
165:
166: # additional cleanup for log::file_work_on()
167: # log::file_work_done();
168:
169: }
170:
171: # finally, delete $spool/XXXXX.rar if $delete
172: if($delete)
173: {
174: if( (system("/bin/rm -f '$spool/$curfile'")) == 0 )
175: {
176: log::zayavki("removed temporary file $spool/$curfile");
177: }
178: else
179: {
180: # /bin/rm exits with error
181: log::zayavki("error while removing temporary file $spool/$curfile (see error log)");
182: log::error("error while removing temporary file $spool/$curfile: $?");
183: }
184: }
185: }
186:
187: }
188: else
189: {
190: # an error occurs in the archivation process
191: log::zayavki("error in archivation process (see error log)");
192: log::error("error in archivation process: $?");
193:
194: # clean out compressed file in /usr/backup/spool on error
195: #system("/bin/rm -f '/usr/backup/spool/$curfile'");
196: `/bin/rm -f '$spool/$curfile'`;
197:
198: }
199: }
200: else
201: {
202: # error, local file doesn't exists
203: log::error("Can not open local file $localpaths[$i]");
204: log::zayavki("Can not open local file $localpaths[$i]");
205:
206: }
207: }
208:
209: # next iteration of 'while' and increment of $i
210: $i++;
211:
212: log::file_work_done();
213: }
214: # end while
215:
216: log::main("Saved $okay zayavki on remote host");
217: log::zayavki("<< End transfer process >>");
218: }
219:
220:
221:
222: sub probas()
223: {
224: my @dbids;
225: my @localpaths;
226: my @remotepaths;
227: my $dbids_ref;
228: my $localpaths_ref;
229: my $i = 0;
230: my $okay = 0;
231: my $curfile;
232:
233: my $size_orig;
234: my $size_arc;
235:
236: my $spool;
237: my $delete;
238:
239: ($dbids_ref, $localpaths_ref) = fetch::probas_list();
240:
241: @dbids = @$dbids_ref;
242: @localpaths = @$localpaths_ref;
243: log::main("Fetched " . scalar(@localpaths) . " probas from database");
244: log::probas("<< Starting transfer process >>");
245:
246: @remotepaths = @localpaths; # clone
247:
248: while($localpaths[$i])
249: {
250: # cloned from send::zayavki()
251:
252: # check if there is a void element
253: # ???
254: next if(!$localpaths[$i]);
255:
256: #
257: #
258: # fill $spool with right spool directory
259: if( db::get_tx_count($dbids[$i]) > 0 )
260: {
261: # retransmission
262: # set $spool to temporary directory to make backup collector happy :)
263: # in this case also let $delete = 1 (removing garbage after copy, as in old times)
264:
265: # considering okay to not to check /usr/backup for availability
266: $spool = $SPOOL_TMPDIR;
267: $delete = 1;
268: }
269: else
270: {
271: # this is the first time copy
272: # set $spool to backup spool directory
273: # and not to delete archive
274:
275: $spool = $SPOOL_PROBAS;
276: $delete = 0;
277: }
278:
279: # lookup in table first
280: chomp( $tmpf = `basename $remotepaths[$i]` );
281: chomp( $_ = `dirname $remotepaths[$i]` );
282: $tmpd = hash::lookup($_);
283: if ( $tmpd ne '' )
284: {
285: $remotepaths[$i] = $tmpd. "/". $tmpf;
286: }
287: else
288: {
1.3 yason 289: # /mnt/maket/proba/* goes on /mnt/rmt_maket/proba/
290: substr($remotepaths[$i], 1, 2, 'mnt/rmt_maket');
1.1 yason 291: }
292:
293: if(-e $remotepaths[$i] || -e $remotepaths[$i] . '.rar')
294: {
295: log::error("Remote file $remotepaths[$i] already exists");
296: db::change_status($dbids[$i], 1);
297: }
298: else
299: {
300: if( -e $localpaths[$i] )
301: {
302: # rar and copy local file
303: fileops::checkdir($remotepaths[$i]);
304:
305: # set $size_orig
306: $size_orig = -s "$localpaths[$i]";
307:
308: chomp($curfile = `basename $localpaths[$i]`) , $curfile .= '.rar';
309:
310: # two functions in log.pm makes newer versions of zlist happy
311: log::file_work_on($curfile, "rar");
312:
313: log::probas("compressing '$localpaths[$i]' -> '$spool/$curfile'");
314:
1.4 ! yason 315: `/usr/bin/rar a -rr10% -ep '$spool/$curfile' '$localpaths[$i]'`;
1.1 yason 316: if( ($? >> 8) == 0)
317: {
318: # rar exited successfully
319: # set $size_arc
320: $size_arc = -s "$spool/$curfile";
321:
322: # it can be possible that file already uploaded by hand while rar copressed one
323: # so we should check for existence of archive in remote before send it
324: if( -e $remotepaths[$i] || -e $remotepaths[$i] . '.rar')
325: {
326: log::error("Can not save $remotepaths[$i].rar cause of file has appeared after compressing");
327: log::probas("Can not save $remotepaths[$i].rar cause of file has appeared after compressing");
328:
329: db::change_status($dbids[$i], 1);
330: }
331: else
332: {
333: # copy archive and then delete it from $spool
334: log::file_work_on($curfile, "copy");
335:
336: log::probas("transferring $spool/$curfile -> $remotepaths[$i].rar");
337:
338: if( system("cp '$spool/$curfile' $remotepaths[$i].rar") == 0 )
339: {
340: log::probas("saved $localpaths[$i] -> $remotepaths[$i].rar [size_orig=$size_orig, size_arc=$size_arc]");
341: log::diff($localpaths[$i], $size_orig, $size_arc);
342: # update database
343: db::change_status($dbids[$i], 1);
344: db::write_size_orig($dbids[$i], $size_orig);
345: db::write_size_arc($dbids[$i], $size_arc);
346:
347: db::write_ux_rpath($dbids[$i], "$remotepaths[$i].rar");
348:
349: # increment tx_count
350: db::increment_tx_count($dbids[$i]);
351:
352: $okay++;
353: }
354: else
355: {
356: # error in the copy process
357: log::probas("error in copy process (see error log)");
358: log::error("error in copy process: $?");
359: }
360:
361: if ($delete)
362: {
363: # finally, delete $spool/XXXXX.rar
364: if( (system("/bin/rm -f '$spool/$curfile'")) == 0 )
365: {
366: log::probas("removed temporary file $spool/$curfile");
367: }
368: else
369: {
370: # /bin/rm exits with error
371: log::probas("error while removing temporary file $spool/$curfile (see error log)");
372: log::error("error while removing temporary file $spool/$curfile: $?");
373: }
374: } # !$delete
375: }
376:
377: }
378: else
379: {
380: # an error occurs in the archivation process
381: log::probas("error in archivation process (see error log)");
382: log::error("error in archivation process: $?");
383:
384: system("/bin/rm -f '$spool/$curfile'");
385: }
386: }
387: else
388: {
389: # error, local file doesn't exists
390: log::error("Can not open local file $localpaths[$i]");
391: log::probas("Can not open local file $localpaths[$i]");
392: }
393: }
394:
395: # next iteration of 'while' and increment of $i
396: $i++;
397:
398: log::file_work_done();
399: }
400:
401: log::main("Saved $okay probas on remote host");
402: log::probas("<< End transfer process >>");
403: }
404:
405: return(1);
CVSweb