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