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