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