annotate MPIWGStor.pm @ 18:fdf4ceb36db1

fixed problem with dir names in metacheck new version of metacheck defaults to not change index file new version of archiver uses new version of metacheck
author casties
date Tue, 20 Sep 2005 19:24:57 +0200
parents 8eee361fdec8
children 2890dd75d2f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
1
30497c6a3eca Initial revision
casties
parents:
diff changeset
2 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
3 # Library with definitions and common routines for MPIWG storage system
30497c6a3eca Initial revision
casties
parents:
diff changeset
4 # scripts
30497c6a3eca Initial revision
casties
parents:
diff changeset
5 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
6
30497c6a3eca Initial revision
casties
parents:
diff changeset
7 package MPIWGStor;
30497c6a3eca Initial revision
casties
parents:
diff changeset
8
30497c6a3eca Initial revision
casties
parents:
diff changeset
9 use strict;
30497c6a3eca Initial revision
casties
parents:
diff changeset
10 use vars qw(@ISA @EXPORT $VERSION
30497c6a3eca Initial revision
casties
parents:
diff changeset
11 $debug %junk_files %index_files $index_file_perm $archived_index_file_perm
30497c6a3eca Initial revision
casties
parents:
diff changeset
12 $file_perm);
30497c6a3eca Initial revision
casties
parents:
diff changeset
13 use Exporter;
30497c6a3eca Initial revision
casties
parents:
diff changeset
14
16
8eee361fdec8 weiter arbeit am digifiles modus
casties
parents: 12
diff changeset
15 $VERSION = 0.6.1; # ROC 8.8.2005
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
16
30497c6a3eca Initial revision
casties
parents:
diff changeset
17 @ISA = qw(Exporter);
30497c6a3eca Initial revision
casties
parents:
diff changeset
18
30497c6a3eca Initial revision
casties
parents:
diff changeset
19 @EXPORT = qw($debug %junk_files %index_files $index_file_perm $archived_index_file_perm
30497c6a3eca Initial revision
casties
parents:
diff changeset
20 $file_perm
30497c6a3eca Initial revision
casties
parents:
diff changeset
21 &parseargs &logger &stime &s2stime &ymd_date &split_file_path &sstrip
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
22 &valid_file_name &valid_dir_name &park_file &unpark_file
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
23 &read_xml &write_xml &add_text_element &create_element_path &create_text_path);
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
24
30497c6a3eca Initial revision
casties
parents:
diff changeset
25 # debug level
30497c6a3eca Initial revision
casties
parents:
diff changeset
26 $debug = 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
27
30497c6a3eca Initial revision
casties
parents:
diff changeset
28 # junk files
30497c6a3eca Initial revision
casties
parents:
diff changeset
29 %junk_files = (
30497c6a3eca Initial revision
casties
parents:
diff changeset
30 '.HSResource' => '.HSResource',
30497c6a3eca Initial revision
casties
parents:
diff changeset
31 '.HSancillary' => '.HSancillary',
30497c6a3eca Initial revision
casties
parents:
diff changeset
32 '.HSicon' => '.HSicon',
30497c6a3eca Initial revision
casties
parents:
diff changeset
33 'Network Trash Folder' => 'Network Trash Folder',
30497c6a3eca Initial revision
casties
parents:
diff changeset
34 'TheVolumeSettingsFolder' => 'TheVolumeSettingsFolder',
30497c6a3eca Initial revision
casties
parents:
diff changeset
35 '.DS_Store' => '.DS_Store'
30497c6a3eca Initial revision
casties
parents:
diff changeset
36 );
30497c6a3eca Initial revision
casties
parents:
diff changeset
37
30497c6a3eca Initial revision
casties
parents:
diff changeset
38 # filenames to not delete (but archive)
30497c6a3eca Initial revision
casties
parents:
diff changeset
39 %index_files = (
30497c6a3eca Initial revision
casties
parents:
diff changeset
40 'index.meta' => 'index.meta',
30497c6a3eca Initial revision
casties
parents:
diff changeset
41 'index.meta.old' => 'index.meta.old'
30497c6a3eca Initial revision
casties
parents:
diff changeset
42 );
30497c6a3eca Initial revision
casties
parents:
diff changeset
43
30497c6a3eca Initial revision
casties
parents:
diff changeset
44 # default permissions for normal index files (rw-rw-r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
45 $index_file_perm = 0664;
30497c6a3eca Initial revision
casties
parents:
diff changeset
46 # default permissions for archived index files (rw-r--r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
47 $archived_index_file_perm = 0644;
30497c6a3eca Initial revision
casties
parents:
diff changeset
48 # default permissions for other files (rw-rw-r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
49 $file_perm = 0664;
30497c6a3eca Initial revision
casties
parents:
diff changeset
50
30497c6a3eca Initial revision
casties
parents:
diff changeset
51
30497c6a3eca Initial revision
casties
parents:
diff changeset
52 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
53 # $param_hash = parseargs;
30497c6a3eca Initial revision
casties
parents:
diff changeset
54 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
55 # reads @ARGV and returns a hash with all options like "-option=value"
30497c6a3eca Initial revision
casties
parents:
diff changeset
56 # and the last part of the string as "path"
30497c6a3eca Initial revision
casties
parents:
diff changeset
57 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
58 sub parseargs {
30497c6a3eca Initial revision
casties
parents:
diff changeset
59 my %opts;
30497c6a3eca Initial revision
casties
parents:
diff changeset
60 foreach my $s (@ARGV) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
61 if ($s =~ /^-([^=]+)=*(.*)$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
62 $opts{$1} = ($2) ? $2 : $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
63 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
64 $opts{'path'} = $s;
30497c6a3eca Initial revision
casties
parents:
diff changeset
65 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
66 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
67 return \%opts;
30497c6a3eca Initial revision
casties
parents:
diff changeset
68 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
69
30497c6a3eca Initial revision
casties
parents:
diff changeset
70
30497c6a3eca Initial revision
casties
parents:
diff changeset
71 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
72 # logger($level, $message)
30497c6a3eca Initial revision
casties
parents:
diff changeset
73 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
74 # logs the $message (mostly to stdout)
30497c6a3eca Initial revision
casties
parents:
diff changeset
75 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
76 sub logger {
30497c6a3eca Initial revision
casties
parents:
diff changeset
77 my ($level, $message) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
78 if ($debug || ($level ne "DEBUG")) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
79 print "$level: $message\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
80 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
81 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
82
30497c6a3eca Initial revision
casties
parents:
diff changeset
83 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
84 # $stime = stime($utime)
30497c6a3eca Initial revision
casties
parents:
diff changeset
85 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
86 # format utime (seconds since epoch) into string
30497c6a3eca Initial revision
casties
parents:
diff changeset
87 # representation: "YYYY/MM/DD HH:MM:SS"
30497c6a3eca Initial revision
casties
parents:
diff changeset
88 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
89 sub stime {
30497c6a3eca Initial revision
casties
parents:
diff changeset
90 my ($utime) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
91 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
30497c6a3eca Initial revision
casties
parents:
diff changeset
92 localtime($utime);
30497c6a3eca Initial revision
casties
parents:
diff changeset
93 my $yy = $year + 1900;
30497c6a3eca Initial revision
casties
parents:
diff changeset
94 my $mm = $mon + 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
95 my $stime = sprintf("%d/%02d/%02d %02d:%02d:%02d",
30497c6a3eca Initial revision
casties
parents:
diff changeset
96 $yy, $mm, $mday, $hour, $min, $sec);
30497c6a3eca Initial revision
casties
parents:
diff changeset
97 return $stime;
30497c6a3eca Initial revision
casties
parents:
diff changeset
98 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
99
30497c6a3eca Initial revision
casties
parents:
diff changeset
100 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
101 # $stime = s2stime($stime2)
30497c6a3eca Initial revision
casties
parents:
diff changeset
102 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
103 # format db-like time (2003-09-19 12:43:32+02) into string
30497c6a3eca Initial revision
casties
parents:
diff changeset
104 # representation: "YYYY/MM/DD HH:MM:SS"
30497c6a3eca Initial revision
casties
parents:
diff changeset
105 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
106 sub s2stime {
30497c6a3eca Initial revision
casties
parents:
diff changeset
107 my ($s2time) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
108 my $stime = "";
30497c6a3eca Initial revision
casties
parents:
diff changeset
109 if ($s2time =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
110 my ($year,$mon,$mday,$hour,$min,$sec) = ($1, $2, $3, $4, $5, $6);
30497c6a3eca Initial revision
casties
parents:
diff changeset
111 $stime = sprintf("%d/%02d/%02d %02d:%02d:%02d",
30497c6a3eca Initial revision
casties
parents:
diff changeset
112 $year, $mon, $mday, $hour, $min, $sec);
30497c6a3eca Initial revision
casties
parents:
diff changeset
113 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
114 return $stime;
30497c6a3eca Initial revision
casties
parents:
diff changeset
115 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
116
30497c6a3eca Initial revision
casties
parents:
diff changeset
117 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
118 # $date = ymd_date($olddate)
30497c6a3eca Initial revision
casties
parents:
diff changeset
119 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
120 # converts a date string in mm/dd/yy format to yyyy/mm/dd format
30497c6a3eca Initial revision
casties
parents:
diff changeset
121 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
122 sub ymd_date {
30497c6a3eca Initial revision
casties
parents:
diff changeset
123 my ($mdy) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
124 my $ydm;
30497c6a3eca Initial revision
casties
parents:
diff changeset
125 if ($mdy =~ /(\d+)\/(\d+)\/(\d+)/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
126 my $m = $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
127 my $d = $2;
30497c6a3eca Initial revision
casties
parents:
diff changeset
128 my $y = $3;
30497c6a3eca Initial revision
casties
parents:
diff changeset
129 # extend yy to yyyy
30497c6a3eca Initial revision
casties
parents:
diff changeset
130 $y = ($y < 70) ? (2000 + $y) : (($y < 100) ? ($y + 1900) : $y);
30497c6a3eca Initial revision
casties
parents:
diff changeset
131 $ydm = sprintf "%d/%02d/%02d", $y, $m, $d;
30497c6a3eca Initial revision
casties
parents:
diff changeset
132 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
133 return $ydm;
30497c6a3eca Initial revision
casties
parents:
diff changeset
134 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
135
30497c6a3eca Initial revision
casties
parents:
diff changeset
136 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
137 # ($name, $path) = split_file_path($fn)
30497c6a3eca Initial revision
casties
parents:
diff changeset
138 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
139 # splits the file path $fn into
30497c6a3eca Initial revision
casties
parents:
diff changeset
140 # file name (the last path element) and path
30497c6a3eca Initial revision
casties
parents:
diff changeset
141 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
142 sub split_file_path {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
143 my ($fn, $nodot) = @_;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
144
30497c6a3eca Initial revision
casties
parents:
diff changeset
145 if ($fn =~ /^(.*)\/([^\/]+)$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
146 return ($2, $1);
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
147 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
148 # only file name
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
149 if ($nodot) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
150 return ($fn, '');
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
151 } else {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
152 return ($fn, '.');
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
153 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
154 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
155
30497c6a3eca Initial revision
casties
parents:
diff changeset
156
30497c6a3eca Initial revision
casties
parents:
diff changeset
157 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
158 # $name = sstrip($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
159 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
160 # strips leading and trailing whitespace from $name
30497c6a3eca Initial revision
casties
parents:
diff changeset
161 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
162 sub sstrip {
30497c6a3eca Initial revision
casties
parents:
diff changeset
163 my ($name) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
164
30497c6a3eca Initial revision
casties
parents:
diff changeset
165 if ($name =~ /\s*(.*)\s*/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
166 return $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
167 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
168 return $name;
30497c6a3eca Initial revision
casties
parents:
diff changeset
169 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
170
30497c6a3eca Initial revision
casties
parents:
diff changeset
171
30497c6a3eca Initial revision
casties
parents:
diff changeset
172 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
173 # $valid = valid_file_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
174 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
175 # checks if $name is a valid file name
30497c6a3eca Initial revision
casties
parents:
diff changeset
176 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
177 sub valid_file_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
178 return valid_name($_[0], 0);
30497c6a3eca Initial revision
casties
parents:
diff changeset
179 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
180
30497c6a3eca Initial revision
casties
parents:
diff changeset
181
30497c6a3eca Initial revision
casties
parents:
diff changeset
182 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
183 # $valid = valid_dir_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
184 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
185 # checks if $name is a valid directory name
30497c6a3eca Initial revision
casties
parents:
diff changeset
186 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
187 sub valid_dir_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
188 return valid_name($_[0], 1);
30497c6a3eca Initial revision
casties
parents:
diff changeset
189 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
190
30497c6a3eca Initial revision
casties
parents:
diff changeset
191
30497c6a3eca Initial revision
casties
parents:
diff changeset
192 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
193 # $valid = valid_name($name, $mode)
30497c6a3eca Initial revision
casties
parents:
diff changeset
194 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
195 # checks if $name is a valid file ($mode=0) or directory name (mode=1)
30497c6a3eca Initial revision
casties
parents:
diff changeset
196 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
197 sub valid_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
198 my ($name, $mode) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
199
30497c6a3eca Initial revision
casties
parents:
diff changeset
200 # whitespace?
30497c6a3eca Initial revision
casties
parents:
diff changeset
201 if ($name =~ /\s+/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
202 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
203 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
204 # invalid characters
30497c6a3eca Initial revision
casties
parents:
diff changeset
205 if ($name !~ /^[-\w.\/]+$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
206 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
207 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
208 # files have extension
30497c6a3eca Initial revision
casties
parents:
diff changeset
209 if ($mode == 0) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
210 if ($name !~ /\.[-\w]+$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
211 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
212 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
213 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
214 return 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
215 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
216
30497c6a3eca Initial revision
casties
parents:
diff changeset
217
30497c6a3eca Initial revision
casties
parents:
diff changeset
218 #
10
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
219 # $newfilename = park_file($filename)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
220 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
221 # parks a file under a new name (*.bak)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
222 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
223 sub park_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
224 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
225 my $newfn = "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
226 if (-f $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
227 $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
228 if (! rename $filename, $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
229 logger("ABORT", "unable to rename file $filename!");
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
230 exit 1;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
231 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
232 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
233 return $newfn;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
234 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
235
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
236 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
237 # $filename = unpark_file($filename)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
238 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
239 # unparks a file
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
240 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
241 sub unpark_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
242 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
243 my $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
244 if (-f $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
245 if (! rename $newfn, $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
246 logger("ABORT", "unable to rename file $newfn!");
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
247 exit 1;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
248 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
249 return $filename;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
250 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
251 return "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
252 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
253
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
254
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
255
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
256 #
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
257 # ($document, $rootnode) = read_xml($file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
258 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
259 # reads xml meta file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
260 # returns root node element
30497c6a3eca Initial revision
casties
parents:
diff changeset
261 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
262 sub read_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
263 my ($file) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
264 my $document;
30497c6a3eca Initial revision
casties
parents:
diff changeset
265
30497c6a3eca Initial revision
casties
parents:
diff changeset
266 my $parser = XML::LibXML->new();
30497c6a3eca Initial revision
casties
parents:
diff changeset
267 if (! $parser) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
268 logger("ABORT", "unable to create parser for metadata index file!!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
269 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
270 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
271
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
272 logger("DEBUG", "index file: $file");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
273 eval { $document = $parser->parse_file($file) };
30497c6a3eca Initial revision
casties
parents:
diff changeset
274 # catch parsing errors
30497c6a3eca Initial revision
casties
parents:
diff changeset
275 if ($@) {
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
276 logger("ABORT", "XML syntax error in file $file!!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
277 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
278 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
279 my $rootnode = $document->documentElement;
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
280 logger("DEBUG", "root element: \'" . $rootnode->nodeName . "\'");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
281
30497c6a3eca Initial revision
casties
parents:
diff changeset
282 return ($document, $rootnode);
30497c6a3eca Initial revision
casties
parents:
diff changeset
283 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
284
30497c6a3eca Initial revision
casties
parents:
diff changeset
285
30497c6a3eca Initial revision
casties
parents:
diff changeset
286 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
287 # write_xml($document, $file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
288 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
289 # saves the XML tree of $document to the file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
290 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
291 sub write_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
292 my ($document, $filename) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
293 # keep backup copy
30497c6a3eca Initial revision
casties
parents:
diff changeset
294 if (-f $filename) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
295 if (! rename $filename, "$filename.old") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
296 logger("ABORT", "unable to change file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
297 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
298 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
299 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
300 # write new file
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
301 my ($fn, $dir) = split_file_path($filename);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
302 if (-d $dir && $document->toFile($filename)) {
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
303 logger("INFO", "written new file $filename");
30497c6a3eca Initial revision
casties
parents:
diff changeset
304 chmod $index_file_perm, $filename;
30497c6a3eca Initial revision
casties
parents:
diff changeset
305 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
306 logger("ABORT", "unable to write file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
307 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
308 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
309 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
310
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
311 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
312 # $elem = add_text_element($node, $name, $value, $namespace)
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
313 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
314 # creates an XML element with the name $name and the text content
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
315 # $value attached to the node $node and returns it.
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
316 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
317 sub add_text_element {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
318 my ($node, $name, $value, $namespace) = @_;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
319
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
320 my $doc = $node->ownerDocument;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
321 my $text = $doc->createTextNode($value);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
322 my $newnode = $node->addNewChild($namespace, $name);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
323 $newnode->addChild($text);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
324
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
325 return $newnode;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
326 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
327
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
328 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
329 # $elem = create_element_path($path, $root, $namespace)
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
330 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
331 # creates and returns a DOM element at the given path from the
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
332 # given root. path is e.g. meta/bib@type=book. elements are separated
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
333 # by /, an additional attribute can be specified after the @.
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
334 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
335 sub create_element_path {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
336 my ($path, $root, $namespace) = @_;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
337 my $attribute = "";
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
338 # get attribute
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
339 if ($path =~ /^(.+)@(.+)$/) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
340 $attribute = $2;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
341 $path = $1;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
342 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
343 my $point = $root;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
344 for my $p (split /\//, $path) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
345 # check if the next path element exists
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
346 my $n = ($point->findnodes($p))[0];
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
347 if ($n) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
348 $point = $n;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
349 } else {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
350 # create if it doesn't exist
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
351 $point = $point->addNewChild($namespace, $p);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
352 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
353 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
354 # add the attribute
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
355 if ($attribute) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
356 my $dom = $root->getOwner();
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
357 my ($attkey, $attval) = split /=/, $attribute; #/ silly fontlock...
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
358 $point->addChild($dom->createAttributeNS($namespace, $attkey, $attval));
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
359 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
360 return $point;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
361 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
362
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
363 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
364 # $elem = create_text_path($path, $text, $root, $namespace)
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
365 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
366 # creates and returns a DOM text element with the given content at the
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
367 # given path from the given root.
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
368 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
369 sub create_text_path {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
370 my ($path, $text, $root, $namespace) = @_;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
371 my $elem = create_element_path($path, $root, $namespace)->appendTextNode($text);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
372 return $elem;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
373 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
374
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
375
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
376
30497c6a3eca Initial revision
casties
parents:
diff changeset
377 # module init
30497c6a3eca Initial revision
casties
parents:
diff changeset
378 return 1;