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