File: multiCD

package info (click to toggle)
multicd 1.6.3-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 100 kB
  • ctags: 17
  • sloc: perl: 804; makefile: 36
file content (1228 lines) | stat: -rwxr-xr-x 38,294 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
#!/usr/bin/perl -w

# This program is copyright (c) 2001 by Daniel Born <djb4@cs.geneseo.edu>
# and is released under the GNU General Public License Version 2
# (http://www.fsf.org/copyleft/gpl.txt).  This program is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.

# Dan Born
# djb4@cs.geneseo.edu
# 1/11/01
#
# See the included sample multiCDrc configuration file for usage info.
#

# Change Log:
#  01/15/01  Fixed bug in command line arg handling.
#
## 01/15/01  Version 1.0 released.
#
#  01/16/01  Fixed bug in get_userconfig() where it wasn't being verified that
#            image_file1 and image_file2 are absolute paths.
#  01/18/01  Fixed bugs.  Disabled use of image_dir and image_dir2, making image_file1
#            and image_file2 the only way to specify image files. Added noburn option.
#  01/19/01  Added first_disc option.
#  01/21/01  Made it compatible with perl version 5 or better.  Used to require 5.6.
#
## 01/21/01  Version 1.01 released.
#
#  01/23/01  Bug fix.
#  01/31/01  cd_size can now be specified in term of Megs or Kilobytes.
#  02/04/01  Added addfiles option.
#  02/05/01  Made it so that the modification time and access time of files is preserved
#            in the backup set.
#
## 02/07/01  Version 1.02 released.
#
#  04/26/01  Made it so it skips files whose size is >= $config{cd_size}, that is, files
#            that are larger than a CD.  This doesn't completely solve the problem,
#            though.  With a filesystem, the amount of free space on a disc is
#            significantly less than the size of the disc.
#  05/05/01  Added support for global config file.
#
## 05/05/01  Version 1.5 released.
#
#  05/22/01  Added code so that it stores an index, but it doesn't write the index to a
#            file yet.
#  06/04/01  Added some filesystem options for ext2.
#            Added ability to retry failed burn attempts.
#            Made it so that the image files and the image mount point are excluded from the
#            backups automatically.
#            Added --help and --check_config options.
#            When read errors occur, the source file is skipped with a warning.
#            Added ability to create an index file.
#  06/05/01  Fixed bug where the access/mod times of directories weren't correct.
#  06/06/01  Made it so that "Device full" is not assumed when a write error occurs.
#
## 06/07/01  Version 1.6 released.
#
#  06/16/01  Changed a "die" to a "warn" for a file read error.
#  06/24/01  Changed help and check_config options.
#  07/03/01  Fixed issue with file names that had spaces in the config file.
#  07/03/01  Did some better error checking with the mount command.
#
## 07/03/01  Version 1.6.1 released.
#
#  07/07/01  Fixed bug with chown of symlinks.
#
## 07/07/01  Version 1.6.2 released.
#
#  07/15/01  Fixed security issue.  Image files were created with insecure
#            permissions.
#            Automatically create the image_mount directory if it doesn't
#            exists, and make sure it has secure permissions.
#            Set defaults for image_file1 and image_file2 that should
#            automatically work for most people.
#
## 07/15/01  Version 1.6.3 released.
#


require 5;

use strict;
use integer;
use Fcntl;
use Cwd qw(chdir cwd);

# Constants related to the values returned by the stat function.
my $MODE_I = 2;
my $UID_I = 4;
my $GID_I = 5;
my $SIZE_I = 7;
my $ATIME_I = 8;
my $MTIME_I = 9;
my $TYPE_MASK = 0770000;
my $MODE_MASK = 0007777;
my $TYPE_DIR = 04;
my $TYPE_FILE = 010;
my $TYPE_SYMLINK = 012;

my $old_umask = umask;
$ENV{PATH} = "$ENV{PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin";

my %config;
if(get_userconfig()) {
    if($config{check_config}) {
        print "Configuration was valid.  Exiting...\n";
        exit 0;
    }
} else {
    die "Invalid configuration.  Exiting...\n";
}
if($config{help}) {
    print "See the sample config file for information on how to use this program.\n";
    exit 0;
}


# For debugging.
#select STDERR; $| = 1;
#select STDOUT; $| = 1;

# A tree node is represented by a hash ref and looks like:
# {
#   name => filename
#   uid  => user id of owner
#   gid  => group id of owner
#   type => type of file
#   mode => permissions of file
#   atime => access time
#   mtime => modification time
#   parent => parent of this node
#   kids => reference to an array of the children of this node
# }

# The image currently being used.
my $cur_image;
if($config{noburn}) {
    $cur_image = "$config{image_file1}1";
} else {
    $cur_image = $config{image_file1};
}

# $disc_ready is true if a disc is in the writer and ready to be used.
my $disc_ready;
if($config{first_disc} or $config{only_one}) {
    $disc_ready = 1;
} else {
    $disc_ready = 0;
}

# Process id of the child process used to run cdrecord.  Used when multi is enabled.
my $pid;

my $image_count = 0;

# @path stores part of the directory tree.  I backup files by going depth first
# through the directory tree.  The leftmost node at every level of the tree is
# descended into, until all the leaf nodes (which are files) are backed up.
# Once all the children of a node, along with the node itself, are backed up,
# that node can be deleted from the tree.  @path is like a stack.  It stores
# the current path from the root to a leaf for the current tree to backup.  The
# elements of @path are treenodes (which are hash references as described above).
# I originally started out by storing the entire directory tree, but that used
# up way too much RAM.  This new algorithm is a way to store only the nodes that
# need to be stored.
my @path;

if(defined $config{index_file}) {
    open INDEXFILE, "> $config{index_file}" or die "couldn't create index file: $!\n";
}

# Each item in this array is a reference to an array of files in a path.  For example,
# the entry to exclude /usr/local/bin would look like [qw(usr local bin)].
my @excludes;
foreach (@{$config{exclude}}) {
    push @excludes, ['/', grep $_, split m|/|, $_];
}

# Check to see if an image file is already mounted.  If so, umount it.
open MTAB, '/etc/mtab' or die "no /etc/mtab: $!\n";
my $mtab;
while (<MTAB>) {	
    ($mtab) = (split)[1];
    if($mtab eq $config{image_mount} or ($mtab . '/') eq $config{image_mount}) {
	system 'umount', $config{image_mount}
	  and print "couldn't umount $config{image_mount}: $!", exit 1;
    }
    if($config{addfiles} and ($mtab eq $config{cd_mount} or ($mtab . '/') eq
			      $config{cd_mount})) {
	system 'umount', $config{cd_mount}
	  and print "couldn't umount $config{cd_mount}: $!", exit 1;
    }
}
close MTAB;
umask 0;

my($type_mode, $uid, $gid, $type, $mode, $size, $atime, $mtime);

# Ensure that the image_mount directory exists and has secure permissions.
if(not(-d $config{image_mount})) {
    mkdir $config{image_mount}, 0700
      or die "Couldn't create the image_mount directory $config{image_mount}: $!\n";
} elsif( ((lstat $config{image_mount})[$MODE_I] & $MODE_MASK) != 0700 ) {
    chmod 0700, $config{image_mount} or die
      "Couldn't put secure permissions on image_mount directory $config{image_mount}: $!\n";
}

# Each iteration of this CD loop creates/burns a new CD.  @path stores the directorty
# tree currently being worked on.  Users can specify more than one directory tree
# that they want backed up, and this list of trees is stored as an array ref in
# $config{files}.
CD:
while(@{$config{files}} or @path) {
    if(defined $config{index_file}) {
        print INDEXFILE "\n" if $image_count > 0;
        print INDEXFILE "CD number ", ($image_count + 1), ":\n";
    }

    # Create a new file system on the image file.
    check_image($cur_image) or die "problem creating image file $cur_image: $!\n";

    print "Creating $config{fs_type} filesystem on image file...\n";
    my $mkfsopts;
    if(defined $config{mkfs_opts}) {
        $mkfsopts = $config{mkfs_opts};
    } else {
        $mkfsopts = '';
    }
    system "echo y | mkfs -t '$config{fs_type}' $mkfsopts '$cur_image' 1>&2"
      and print "couldn't mkfs: $!\n", exit 1;

    # Mount the image file.
    my $loopnum = 0;
    # system returns true on failure... all so confusing...
    while($loopnum < 4 and (system 'mount', '-t', $config{fs_type}, $cur_image, '-o',
                            "loop=/dev/loop$loopnum", $config{image_mount})) {
        $loopnum++;
    }
#        print "couldn't mount $cur_image: $!\n", exit 1;

    # If addfiles is enabled, then we want to copy the files from a disc to the
    # image file.
    if($config{addfiles}) {
	unless($disc_ready) {
	    print "In order to add files, the CD must be in the burner.  Press enter ",
	    "when it is ready. \n";
	    $_ = <STDIN>;
	    $disc_ready = 1;
	}
	# Mount the disc.
	system 'mount', '-o', 'ro', '-t', $config{fs_type}, $config{cd_dev},
	  $config{cd_mount} and print "couldn't mount CD: $!\n", exit 1;
	
	print "Saving current CD contents...\n";

	## Fork start
	# If you try to copy files from the CD in the current process, you will get
	# device busy errors when you try to unmount the CD.  The workaround: copy
	# the files in a subprocess, and then exit the subprocess before trying to do the
	# unmount.

	my $tarforkpid = fork();
	unless(defined $tarforkpid) {
	    die "can't fork: $!\n";
	}
	if($tarforkpid == 0) {  # If child process
	    chdir $config{cd_mount} or die "couldn't chdir: $!\n";
	    # Only try to copy files if the CD has any.  I use tar to copy the files
	    # because tar preserves special file attributes, e.g., permissions,
	    # ownership, access/mod times, symlinks, etc.
	    if (opendir(DIR, '.') and (grep !/^\.\.?$/, readdir DIR)) {
		open TARIN, 'tar cf - * |' or
		  print "couldn't save old files from CD: $!\n", exit 1;
		chdir $config{image_mount} or die "no chdir: $!\n";
		open TAROUT, '| tar xf -' or
		  print "couldn't save old files from CD: $!\n", exit 1;
		my($buf, $bytes_read);
		my $try_read = 16384;
		while (1) {
		    $buf = '';
		    $bytes_read = read(TARIN, $buf, $try_read);
		    if (not defined $bytes_read) {
			print "Error saving old files from CD: $!\n";
			exit 1;
		    }
		    unless(print TAROUT $buf) {
			close TARIN;
			close TAROUT;
			print "Error saving old files from CD: $!\n";
			exit 1;
		    }
		    last if $bytes_read < $try_read;
		}
		close TARIN;
		close TAROUT;
	    }
	    chdir '/' or die "couldn't chdir: $!\n";
	    exit 0; # Exit from child process.
	}

	## Fork end

	waitpid $tarforkpid, 0;
	my $status = $? >> 8;
	if($status != 0) {
	    die "Error saving old files from CD: $!\n";
	}

	# Unmount the disc.
	system 'umount', $config{cd_mount}
	  and die "couldn't unmount rewriteable CD: $!\n";
    }

    print "Copying files to CD image...\n";

    # Each iteration of this COPY_FILE loop copies one file to the backup
    # set.  The loop exits when copying a file results in a "device full"
    # error, or if there are no more files to backup.  When the target CD
    # is full, it is burned, and we start over where we left off with the
    # file that got the "device full" error.
  COPY_FILE:
    while(1) {
	# If the current tree is empty (which is in @path), then get the
	# next one.
	if(not @path) {
	    unless(@{$config{files}}) {
		last COPY_FILE;
	    }
	    @path = map{ {name => $_} } grep $_, split m|/|, shift @{$config{files}};
	    unshift @path, {name => '/'};
	    if (@path > 1) {
		$path[0]{kids} = [$path[1]];
	    }
	    for(my $i = 1; $i < @path; $i++) {
		$path[$i]{parent} = $path[$i - 1];
		$path[$i - 1]{kids} = [$path[$i]];
	    }
            my $oldfile;
	    for(my $i = 0; $i < @path - 1; $i++) {
                if($i == 0) {
                    $oldfile = $path[0]{name};
                    if(substr($oldfile, -1, 1) ne '/') {
                        $oldfile .= '/';
                    }
                } else {
                    $oldfile .= $path[$i]{name} . '/';
                }
		unless(($type_mode, $uid, $gid, $atime, $mtime) =
		       (lstat $oldfile)[$MODE_I, $UID_I, $GID_I, $ATIME_I, $MTIME_I]) {
		    warn "couldn't lstat $oldfile: $!  Skipping this tree\n";
		    @path = ();
		    next COPY_FILE;
		}
		unless((($type_mode & $TYPE_MASK) / ($MODE_MASK + 1)) == $TYPE_DIR) {
		    warn "$oldfile in a backup path was not a directory: $!\n";
		    @path = ();
		    next COPY_FILE;
		}
		$path[$i]{uid} = $uid;
		$path[$i]{gid} = $gid;
		$path[$i]{type} = $TYPE_DIR;
		$path[$i]{mode} = ($type_mode & $MODE_MASK);
                $path[$i]{atime} = $atime;
                $path[$i]{mtime} = $mtime;
	    }
	}

	### Print the tree for debugging
	#print STDERR '-' x 40, "\n";
	#print_treepath($path[0], \@path);
	#print STDERR '-' x 40, "\n";
	###

	# Remove from the tree the files that the user asked to exclude.
	for(my $j = 0; $j < @excludes; $j++) {
	    next if @{$excludes[$j]} != @path;
	    my $i;
	    for ($i = 0; $i < @path; $i++) {		
		if ($excludes[$j][$i] ne $path[$i]{name}) {
		    last;
		}
	    }
	    if($i == @path) {
		splice @excludes, $j, 1;
		delete_tos(\@path);
		next COPY_FILE;
	    }
	}

	my $oldfile = get_fullpath(\@path);
	my $newfile = $config{image_mount} . $oldfile;
	
	### Debugging
	#print STDERR "-- image $image_count --\n", "newfile: $newfile\n\n";
	###
	unless(($type_mode, $uid, $gid, $size, $atime, $mtime) =
	       (lstat $oldfile)[$MODE_I, $UID_I, $GID_I, $SIZE_I, $ATIME_I, $MTIME_I]) {
	    warn "couldn't lstat $oldfile: $!\n";
	    delete_tos(\@path);
	    next COPY_FILE;
	}
        unless($size <= $config{cd_size}) {
            warn "$oldfile is too big - $size bytes - skipping\n";
            delete_tos(\@path);
	    next COPY_FILE;
        }
	$type = ($type_mode & $TYPE_MASK) / ($MODE_MASK + 1);
	$mode = ($type_mode & $MODE_MASK);
	$path[$#path]{type} = $type;
	$path[$#path]{mode} = $mode;
	$path[$#path]{uid}  = $uid;
	$path[$#path]{gid}  = $gid;
        $path[$#path]{atime} = $atime;
        $path[$#path]{mtime} = $mtime;

	if($type == $TYPE_FILE or $type == $TYPE_SYMLINK or $type == $TYPE_DIR) {
	    unless(check_path(\@path, $config{image_mount})) {
		#warn "check path failed: $!";
		# Don't change stack. Device is full.
                writeerror(\@path);
		last COPY_FILE;
	    }

	    ## Handle a regular file.
	    if($type == $TYPE_FILE) {
		unless(sysopen FROM, $oldfile, 0) {
		    warn "Error reading, skipping $oldfile: $!\n";
		    delete_tos(\@path);
		    next COPY_FILE;
		}
		unless(sysopen TO, $newfile, O_WRONLY | O_CREAT | O_TRUNC, $mode) {
		    close FROM;
		    #warn "couldn't create backup file $newfile: $!";
		    # Don't change stack. Device is full.
                    writeerror(\@path);
		    last COPY_FILE;
		}
		my($buf, $bytes_read);
		my $try_read;
		# Try to get the preferred blocksize from stat.  Using the preferred
		# block size as the buffer size will probably speed up the copy.
		($try_read) = (lstat _)[11];
		unless($try_read) {
		    $try_read = 16384;
		}
		while(1) {
		    $buf = '';
		    $bytes_read = read(FROM, $buf, $try_read);
		    if(not defined $bytes_read) {
			warn "Error reading, skipping $oldfile: $!\n";
                        close FROM;
                        close TO;
                        unlink $newfile;
                        delete_tos(\@path);
                        next COPY_FILE;
		    }
		    unless(print TO $buf) {
			close FROM;
			close TO;
			unlink $newfile;
			#warn "couldn't write to $newfile: $!";
			# Don't change stack. Device is full.
                        writeerror(\@path);
			last COPY_FILE;
		    }
		    last if $bytes_read < $try_read;
		}
		close FROM;
		close TO;
                filedone($oldfile, $newfile, $uid, $gid, $atime, $mtime);
		delete_tos(\@path);
	    }
	    ## Handle a directory.
	    elsif($type == $TYPE_DIR) {
		if($oldfile ne '/' and not (-d $newfile)) {
		    unless(mkdir $newfile, $mode) {
			#warn "couldn't mkdir $newfile: $!";
			# Don't change stack. Device is full.
                        writeerror(\@path);
			last COPY_FILE;
		    }
		}
                filedone($oldfile, $newfile, $uid, $gid, $atime, $mtime);

		# Add the contents of this directory to our tree path.  The contents
		# of a directory are it's child nodes.
		if(opendir DIR, $oldfile) {
		    my @dir = map{{name => $_, parent => $path[$#path]}}
		      grep !/^\.\.?$/, readdir DIR;
		    closedir DIR;
		    if(@dir) {
			$path[$#path]{kids} = \@dir;
			push @path, $dir[0];
		    } else {
			delete_tos(\@path);
		    }
		} else {
		    warn "couldn't read directory $oldfile: $!\n";
		    delete_tos(\@path);
		    next COPY_FILE;
		}
	    }
	    ## Handle a symbolic link.
	    elsif($type == $TYPE_SYMLINK) {
		my $readlink;
		unless($readlink = readlink $oldfile) {
		    warn "couldn't read $oldfile, skipping symlink: $!\n";
		    delete_tos(\@path);
		    next COPY_FILE;
		}
		unlink $newfile;
		unless(symlink $readlink, $newfile) {
		    #warn "couldn't create symlink $newfile --> $readlink: $!";
		    # Don't change stack. Device is full.
                    writeerror(\@path);
		    last COPY_FILE;
		}
                if(defined $config{index_file}) {
                    print INDEXFILE "  $oldfile\n";
                }

		# The perl chown function will change the file that a
		# symlink points to and not the symlink itself.  Command
		# line chown works the way it should.
		system 'chown', "$uid.$gid", $newfile
		  and die "couldn't chown symlink $newfile: $!\n";

		# As it turns out, there is no way to change the access/mod
		# times of a symlink.  All attempts to do this result in
		# changing the file that the symlink points to.

		delete_tos(\@path);
	    }
	}
	## All other types of files are not saved to the backup set.
	else {
	    warn "skipping file $oldfile because type not recognized\n";
	    delete_tos(\@path);
	    next COPY_FILE;
	}
    }

    ### Done copying files.

    system 'umount', $config{image_mount}
      and die "couldn't umount $config{image_mount}\n";
    if($config{noburn}) {
	$cur_image = $config{image_file1} . ($image_count + 2);
        push @excludes, ['/', grep $_, split m|/|, $cur_image];
    } else {
	print "Ready to burn CD number " . ($image_count + 1) . ".\n";
	
	# Wait for the previous burn to finish if another process was
	# forked off the last time around.
	if(defined $pid) {
	    print "Waiting for previous burn process (CD number ", $image_count,
	      ") to finish...\n";
	    waitpid $pid, 0;
	    my $status = $? >> 8;
            my $err = undef;
            while($status != 0) {
                my $cont = cont_prompt($image_count, $err);
                if($cont eq 's') {
                    last;
                } elsif($cont eq 'q') {
                    last CD;
                } elsif($cont eq 'r') {
                    my $image;
                    if($cur_image eq $config{image_file1}) {
                        $image = $config{image_file2};
                    } elsif($cur_image eq $config{image_file2}) {
                        $image = $config{image_file1};
                    } else {
                        die "This should never happen.  Program is broken!\n";
                    }
                    $status = system "$config{cdrecord} '$image'";
                    $err = $!;
                }
            }
	    if($status == 0) {
		print "Previous CD (number ", $image_count, ") created successfully.\n\n";
	    }
	}

	# Prepare to run cdrecord.
	my $cdrecord = "$config{cdrecord} '$cur_image'";
	unless($disc_ready) {
	    {
		print "*** Make sure a CD is in the drive. ***\n",
		      "About to run cdrecord like this:\n",
		      "$cdrecord\n";
		print "\n";
		print "(s)kip CD, (q)uit program, or (c)ontinue? ";
		$_ = <STDIN>;
		chomp;
		if(/^\s*s/i) {
		    next CD;
		} elsif(/^\s*q/i) {
		    last CD;
		} elsif(not /^\s*c/i) {
		    redo;
		}
	    }
	    $disc_ready = 1;
	}

	# Run cdrecord.  If multi is on, run cdrecord in a child process.
	# If no more CDs need to be created, then there is no need to fork
	# another process, and so the parent process can run cdrecord.
	if($config{multi} and (@{$config{files}} or @path)) {
	    # Change the current image file so the parent process can add files to a
	    # new one while the child burns the other to cd.
	    if($cur_image eq $config{image_file1}) {
		$cur_image = $config{image_file2};
	    } elsif($cur_image eq $config{image_file2}) {
		$cur_image = $config{image_file1};
	    } else {
		die "Never happen.  Unless the code is screwed up.\n";
	    }

	    # Creating a new image file tends to bog the system down, and cdrecord will
	    # choke if we try running it at the same time, so create the new image file
	    # before forking if one needs to be created.
	    print "Checking for an extra image file before the burn starts...\n";
	    check_image($cur_image)
	      or die"problem creating image file $cur_image: $!\n";

	    print "Burning your CD...\n";
	    $pid = fork();
	    unless(defined $pid) {
		die "can't fork: $!\n";
	    }
	    if($pid == 0) {
		# Child process here.
		select STDERR; $| = 1;
		select STDOUT; $| = 1;
		open STDOUT, ">&STDERR"; # Make all the cdrecord output go to stderr.
                my $status = system $cdrecord;
                if(defined $config{cd_done}) {
                    system $config{cd_done};
                }
                # Exit code of child process depends on success of the cdrecord command.
                if($status != 0) {
                    exit 1;
                } else {
                    exit 0;
                }
	    }
	    # Parent process still going here.
	} else {
            # If running without multi, burn the cd here.
	    print "Burning your CD...\n";
	    open SAVEOUT, ">&STDOUT";
	    open STDOUT, ">&STDERR";
	    my $status = system $cdrecord;
	    open STDOUT, ">&SAVEOUT";
	    close SAVEOUT;
            if(defined $config{cd_done}) {
                system $config{cd_done};
            }
	    if($status != 0) {
		if($config{only_one}) {
		    print "CD burn failed: $!";
		} else {
                    while($status != 0) {
                        my $cont = cont_prompt($image_count + 1, $!);
                        if($cont eq 's') {
                            last;
                        } elsif($cont eq 'q') {
                            last CD;
                        } elsif($cont eq 'r') {
                            $status = system "$config{cdrecord} '$cur_image'";
                        }
                    }
		}
	    }
            if($status == 0) {
		print "CD number " . ($image_count + 1) . " created successfully.\n\n";
	    }
	}
    }
    $disc_ready = 0;
    last CD if $config{only_one};
} continue {
    $image_count++;
}

close INDEXFILE if defined $config{index_file};
print "\nAll done.\n";
#
# End main program.
#


##
# boolean get_userconfig()
#
# Get the configuration info for this user and put it in global hash
# %config.
#
# Configuration can come from any of three places:
# A global configuration file: /etc/multiCDrc
# A config file in a user's home directory: $HOME/.multiCDrc
# The command line.
# Options given on the command line have the same name as the ones in
# the config files.  If the same option is specified in more than one
# place, then options in the home directory file override the options
# in the global file, and the command line options override the other
# two.
#
# Returns true if successful.
#
sub get_userconfig {

    my @optnames =   qw(multi only_one image_file1 image_file2 image_mount
                        cd_size fs_type files exclude cdrecord cd_done noburn
                        image_dir image_dir2 first_disc addfiles cd_dev cd_mount
                        mkfs_opts index_file help check_config);
    my @booleans =   qw(multi only_one noburn first_disc addfiles help check_config);
    my @listvalues = qw(files exclude);

    my %optnames;
    @optnames{@optnames} = (1) x @optnames;
    my %booleans;
    @booleans{@booleans} = (1) x @booleans;
    my %listvalues;
    @listvalues{@listvalues} = (1) x @listvalues;

    my $global_cf_file = '/etc/multiCDrc';
    my $local_cf_file = "$ENV{HOME}/.multiCDrc";
    my($key);

    # Load things from the global file, and then override those with whatever is
    # found in a local file.
    foreach ($global_cf_file, $local_cf_file) {
        open CF, $_ or next;
        while(<CF>) {
            if (/^(.*?)\#/) {           # Strip away comments.
                $_ = $1;
            }
            next if /^\s*$/;	        # Skip blank lines.
            if(/^(.*?)(\s*)$/) {	# Trim whitespace off the end of the line.
                $_ = $1;
            }
            my $value;
            ($key, $value) = split /\s*=\s*/, $_, 2;
            unless($optnames{$key}) {
                print "Unknown option: '$key'.  Check your $_ file.\n";
                close CF;
                return 0;
            }
            if($listvalues{$key}) {
                $config{$key} = get_listvalue($value);
            } else {
                $config{$key} = $value;
            }
        }
        close CF;
    }

    # Handle command line args.  Command line args override values from the two
    # config files.
    while(@ARGV) {
	$_ = shift @ARGV;
	unless(/^--/) {
	    print "Bad command line arg: '$_'\n";
	    return 0;
	}
        my $value;
	$key = '';
	substr($_, 0, 2) = '';
	if(/=/) {
	    ($key, $value) = split /=/, $_, 2;
	} else {
	    $key = $_;
            while(@ARGV) {
                $_ = shift @ARGV;
                if(/^--/) {
                    unshift @ARGV, $_;
                    last;
                }
                if($listvalues{$key}) {
                    push @{$value}, $_;
                } else {
                    $value = $_;
                    last;
                }
            }
	}
        unless($optnames{$key}) {
            print "Unknown command line option: '$key'.\n";
            return 0;
        }

	if(not defined $value) {
            if($booleans{$key}) {
                $config{$key} = 1;
            } elsif ($key eq 'exclude') {
		delete $config{exclude};
	    } elsif($config{$key} != 1) {
		print "Value for command line option '$key' not specified.\n";
		return 0;
	    }
	} else {
            $config{$key} = $value;
        }
    }

    if(defined $config{image_dir} or defined $config{image_dir2}) {
	print "The use of options image_dir and image_dir2 is no longer supported.\n",
     	      "You must specify image file locations with the image_file1\n",
	      "and image_file2 options.\n";
	return 0;
    }
    foreach (qw(cd_size image_file1 image_mount fs_type files cdrecord)) {
	unless(defined $config{$_}) {
	    print "Required option '$_' not found.\n";
	    return 0;
	}
    }

    # Make sure all path values are absolute.
    foreach (@{$config{files}}, @{$config{exclude}},
	     @config{qw(image_mount image_file1 image_file2 index_file)}) {
	next unless defined $_;
	if (/\.\./ or not m|^/|) {
	    print "$_: Relative paths are bad.  Absolute paths only.\n";
	    print "Bad option.\n";
	    return 0;
	}
    }
    push @{$config{exclude}}, $config{image_mount}, $config{image_file1};
    if(defined $config{image_file2}) {
        push @{$config{exclude}}, $config{image_file2};
    }
    if(defined $config{index_file}) {
        my($min, $hour, $mday, $mon, $year) = (localtime)[1, 2, 3, 4, 5];
        $year -= 100;
        foreach($min, $hour) {
            if($_ < 10) {
                $_ = "0$_";
            }
        }
        my $date = "$mon-$mday-$year" . "_$hour:$min";
        $config{index_file} =~ s/%d/$date/g;
    }

    if(defined $config{image_mount}) {
	$config{image_mount} .= '/' unless substr($config{image_mount}, -1, 1) eq '/';
    }
    if($config{only_one} or $config{noburn}) {
	$config{multi} = 0;
    }
    if($config{multi} and not defined($config{image_file2})) {
	print "When multi is enabled, image_file2 must be specified.\n";
	return 0;
    }
    if($config{addfiles} and not (defined $config{cd_dev} and
				  defined $config{cd_mount})) {
	print "When addfiles is enabled, both cd_dev and cd_mount must ",
	"be specified.\n";
	return 0;
    }

    # Allow users to specify cd_size in terms of Megs or Kilobytes.  cd_size is
    # converted to bytes for usage in the program.
    if($config{cd_size} =~ /^(.+)M$/i) {
	$config{cd_size} = $1;
	$config{cd_size} *= 2**20;
    } elsif($config{cd_size} =~ /^(.+)K$/i) {
	$config{cd_size} = $1;
	$config{cd_size} *= 2**10;
    }

    # show_options() was originally for debugging only, but it's useful for
    # the user to see the set of options they are running with, and when
    # they email me about stuff it's great to have this info.
    show_options();

    return 1;
}


# void show_options()
#
# Prints all the options to stderr.
#
sub show_options {

    print STDERR "-- Options --\n";
    print STDERR "Files to backup:\n", map{ "  '$_'\n" } @{$config{files}};
    print STDERR "\n";
    print STDERR "Files to exclude:\n", map{ "  '$_'\n" } @{$config{exclude}};
    print STDERR "\n";
    print STDERR "All the rest:\n", map{ "  $_: '$config{$_}'\n" }
      sort grep {defined $config{$_} and $_ ne 'files' and $_ ne 'exclude'} keys %config;
    print STDERR "-- Options --\n";
}


##
# boolean create_imagefile($image_file)
#
# Creates the image file named in the parameter.  The size is specified in
# %config{cd_size}.
#
sub create_imagefile {
    my($image_file) = @_;

    my($blksize, $buf);
    # Get preferred block size.
    ($blksize) = (stat $image_file)[11];
    unless($blksize) {
	$blksize = 16384;
    }
    $buf = "\0" x $blksize;

    my $bytes_togo = $config{cd_size};
    sysopen IMAGE, $image_file, O_WRONLY | O_CREAT | O_TRUNC, 0600 or return 0;
    while($bytes_togo) {
	if ($bytes_togo < $blksize) {
	    print IMAGE "\0" x $bytes_togo or close IMAGE, return 0;
	    $bytes_togo = 0;
	} else {
	    print IMAGE $buf or close IMAGE, return 0;
	    $bytes_togo -= $blksize;
	}
    }
    close IMAGE;
    return 1;
}


##
# void delete_tos(\@path)
#
# Deletes the last item in one of my path structures and replaces it with a new one.
#
sub delete_tos {
    my($path_ref) = @_;

    # Go to a sibling if available, otherwise go up the tree.
    my($kids, $file, $last);
    while(defined $$path_ref[$#path]{parent}) {
	$kids = $$path_ref[$#path]{parent}{kids};
	pop @$path_ref;
	if(@$kids > 1) {	# If the top of stack node has a sibling
	    shift @$kids;
	    push @$path_ref, $$kids[0];
	    last;
	} else {
            # Set the access/mod times on a directory as soon as we are done adding
            # things to the directory.
            $file = $config{image_mount} . get_fullpath(\@path);
            $last = @$path_ref - 1;
            utime $$path_ref[$last]{atime}, $$path_ref[$last]{mtime}, $file
              or warn "couldn't change access/modification times for file $file: $!\n";
        }
    }

    # If we have come back to the root node, then the tree is empty.
    if(not defined($$path_ref[$#path]{parent})) {
        utime $$path_ref[0]{atime}, $$path_ref[0]{mtime}, $config{image_mount} or warn
          "couldn't change access/modification times for file $config{image_mount}: $!\n";
	@$path_ref = ();
    }
}


##
# boolean check_path(\@path, $prefix)
#
# Check to see if all of the directories leading up to the last item in the given
# path structure have been created, and create any directories that are missing.
# $prefix is typically the mount point for the backup filesystem.
# Returns false if there is an error creating directories, true otherwise.
#
sub check_path {
    my($path_ref, $prefix) = @_;

    my($node, $name);
    my $old_dir = cwd();
    chdir $prefix or die "couldn't chdir to $prefix: $!\n";
    for(my $i = 0; $i < @$path_ref - 1; $i++) {
	$node = $$path_ref[$i];
	$name = $$node{name};
	if ($name ne '/' and $name =~ m|^/|) {
	    substr($name, 0, 1) = '';
	}
	unless(-e $name) {
	    if($name ne '/') {
		mkdir $name, $$node{mode} or chdir $old_dir, return 0;
	    }
	    chown $$node{uid}, $$node{gid}, $name or die "couldn't chown $name: $!";
	}
	if($name ne '/') {
	    chdir $name or die "couldn't chdir $name: $!\n";
	}
    }

    chdir $old_dir;
    return 1;
}


##
# $fullpath get_fullpath(\@path)
#
# Return the full path name of the file at the end of the given path structure.
#
sub get_fullpath {
    my($path_ref) = @_;

    my $fullpath = $$path_ref[0]{name};
    if(substr($fullpath, -1, 1) ne '/') {
        $fullpath .= '/';
    }
    for(my $i = 1; $i < @$path_ref; $i++) {
        $fullpath .= $$path_ref[$i]{name} . '/';
    }
    substr($fullpath, -1, 1) = '' unless $fullpath eq '/';

    return $fullpath;
}


##
# void print_treepath($root, \@path)
#
# Useful for debugging.  This will print out the tree rooted at $root, and
# will highlight the path through the tree indicated by @path.
#
# Implementation: Since @path contains references to the nodes in $tree, go
# through the nodes listed in @path and add a special field called in_path
# that has the name at that node highligted.  Then just print the tree,
# checking for in_path.  The in_path fields are deleted before the function
# returns.
#
sub print_treepath {
    my($root, $path_ref) = @_;

    foreach (@$path_ref) {
	$$_{in_path} = 1;
    }
    print_tree($root);
    foreach (@$path_ref) {
	delete $$_{in_path};
    }
}


##
# void print_tree($root, $depth)
#
# Useful for debugging.  Prints the tree at the given root.  $depth is the
# depth in the tree of the current $root node.  $depth is used to format the
# output a little better.
#
sub print_tree {
    my($root, $depth) = @_;
    $depth = 0 if not defined $depth;

    my $child;
    foreach $child (@{$$root{kids}}) {
	print_tree($child, $depth + 1);
    }

    print STDERR ' ' x ($depth * 3);
    if ($$root{in_path}) {
	print STDERR "*$$root{name}*";
    } else {
	print STDERR $$root{name};
    }
    print STDERR "\n";
}


##
# $code cont_prompt($num[, $errormsg])
#
# Prompts the user to continue after a burn has failed.  Returns true if user wants
# to continue, false otherwise.  The parameters should be the number of the CD to
# use in the print statements, and the error message if any.  The error message
# is an optional parameter.
#
sub cont_prompt {
    my($num, $errormsg) = @_;

    if ($errormsg) {
	print "Burn of CD number $num failed: $errormsg.\n";
    } else {
	print "Burn of CD number $num failed.\n";
    }

    {
        print "(s)kip CD, (q)uit program, or (r)etry? ";
        $_ = <STDIN>;
        chomp;
        if (/s/i) {
            return 's';
        } elsif (/q/i) {
            return 'q';
        } elsif (/r/i) {
            return 'r';
        } else {
            redo;
        }
    }
}


##
# boolean check_image($image_file)
#
# Checks to see if the given image file exists, is readable, and is writeable.  If
# not, create a new one.  Creating image files takes a while, so we avoid doing
# it if it's not necessary.
#
sub check_image {
    my($image_file) = @_;

    unless(-r $image_file and -w $image_file and (stat _)[7] == $config{cd_size}) {
	print "Creating a new image file.  This takes a while...\n";
	umask $old_umask;
	create_imagefile($image_file) or return 0;
	umask 0;
    }

    return 1;
}


##
# void filedone
#
sub filedone {
    my($oldfile, $newfile, $uid, $gid, $atime, $mtime) = @_;

    if(defined $config{index_file}) {
        print INDEXFILE "  $oldfile\n";
    }
    chown $uid, $gid, $newfile
      or die "couldn't chown $newfile: $!\n";
    utime $atime, $mtime, $newfile
      or warn "couldn't change access/modification times for file $newfile: $!\n";
}


##
# void writeerror(\@path)
#
# This is called when an error occurs while writing a file to a CD image.
#
sub writeerror {
    my($path_ref) = @_;

    # If a write error occured because the CD image is full, then that means
    # we should go onto the next CD.  Any other error should cause the
    # program to die.
    if($! != 28) { # 28 is the error code for the "No space left on device" error.
        die "Error writing file $config{image_mount}" . get_fullpath($path_ref) . "\n";
    }

    # Set the access/mod times on the directory (or parent) containing the
    # file that is currently at the top of the stack.
    my $tmp = pop @$path_ref;
    my $file = $config{image_mount} . get_fullpath($path_ref);
    push @$path_ref, $tmp;
    my $i = $#path - 1;
    utime $$path_ref[$i]{atime}, $$path_ref[$i]{mtime}, $file
      or warn "couldn't change access/modification times for file $file: $!\n";
}


##
# \@list get_listvalue($value)
#
# This sub will take a list of values as a string, where the seperate values
# are delimited by either whitespace or double quotes, and return a reference
# to an array of these values.  Example:
# Given a string like this: "value one" value2 "value3"
# will return an array of strings like this: ["value one", "value2", "value3"]
# If the list is empty, undef is returned.
#
sub get_listvalue {
    my($value) = @_;

    my @value = map chr $_, unpack 'C*', $value;
    my(@list, $i, $cur, $dq);
    $dq = 0;
    for($i = 0; $i < @value; $i++) {
        if($value[$i] eq '"') {
            if(not $dq) {
                $dq = 1;
            } else {
                push @list, $cur if defined $cur;
                $cur = undef;
                $dq = 0;
            }
        } elsif($value[$i] eq '\\') {
            if(($i +1) < @value and $value[$i + 1] eq '"') {
                $cur .= '\\"';
                $i++;
            } else {
                $cur .= '\\';
            }
        } elsif(($value[$i] =~ /\s/ and $dq) or $value[$i] !~ /\s/) {
            $cur .= $value[$i];
        } elsif($value[$i] =~ /\s/) {
            push @list, $cur if defined $cur;
            $cur = undef;
        }
    }
    push @list, $cur if defined $cur;

    if(@list) {
        return \@list;
    } else {
        return undef;
    }
}