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