annotate MPIWGStor.pm @ 30:398ef4b8f072

added -online-base option to makemeta-lib
author casties
date Mon, 12 Jun 2006 19:01:39 +0200
parents 2890dd75d2f6
children 4d958249d337
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
27
2890dd75d2f6 fixed bug in sstrip with whitespace at the end of the string...
casties
parents: 18
diff changeset
15 $VERSION = 0.6.2; # ROC 10.2.2006
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 #
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
158 # $name = sstrip($name, $slash)
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
159 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
160 # strips leading and trailing whitespace from $name
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
161 # replaces double slashes with single ones with $slash.
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
162 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
163 sub sstrip {
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
164 my ($name, $slash) = @_;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
165
27
2890dd75d2f6 fixed bug in sstrip with whitespace at the end of the string...
casties
parents: 18
diff changeset
166 if ($name =~ /^\s*(.*?)\s*$/) {
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
167 $name = $1;
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
168 }
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
169 if ($slash) {
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
170 # trim multiple slashes
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
171 $name =~ s/\/+/\//g;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
172 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
173 return $name;
30497c6a3eca Initial revision
casties
parents:
diff changeset
174 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
175
30497c6a3eca Initial revision
casties
parents:
diff changeset
176
30497c6a3eca Initial revision
casties
parents:
diff changeset
177 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
178 # $valid = valid_file_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
179 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
180 # checks if $name is a valid file name
30497c6a3eca Initial revision
casties
parents:
diff changeset
181 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
182 sub valid_file_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
183 return valid_name($_[0], 0);
30497c6a3eca Initial revision
casties
parents:
diff changeset
184 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
185
30497c6a3eca Initial revision
casties
parents:
diff changeset
186
30497c6a3eca Initial revision
casties
parents:
diff changeset
187 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
188 # $valid = valid_dir_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
189 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
190 # checks if $name is a valid directory name
30497c6a3eca Initial revision
casties
parents:
diff changeset
191 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
192 sub valid_dir_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
193 return valid_name($_[0], 1);
30497c6a3eca Initial revision
casties
parents:
diff changeset
194 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
195
30497c6a3eca Initial revision
casties
parents:
diff changeset
196
30497c6a3eca Initial revision
casties
parents:
diff changeset
197 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
198 # $valid = valid_name($name, $mode)
30497c6a3eca Initial revision
casties
parents:
diff changeset
199 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
200 # checks if $name is a valid file ($mode=0) or directory name (mode=1)
30497c6a3eca Initial revision
casties
parents:
diff changeset
201 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
202 sub valid_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
203 my ($name, $mode) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
204
30497c6a3eca Initial revision
casties
parents:
diff changeset
205 # whitespace?
30497c6a3eca Initial revision
casties
parents:
diff changeset
206 if ($name =~ /\s+/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
207 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
208 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
209 # invalid characters
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 # files have extension
30497c6a3eca Initial revision
casties
parents:
diff changeset
214 if ($mode == 0) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
215 if ($name !~ /\.[-\w]+$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
216 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
217 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
218 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
219 return 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
220 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
221
30497c6a3eca Initial revision
casties
parents:
diff changeset
222
30497c6a3eca Initial revision
casties
parents:
diff changeset
223 #
10
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
224 # $newfilename = park_file($filename)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
225 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
226 # parks a file under a new name (*.bak)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
227 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
228 sub park_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
229 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
230 my $newfn = "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
231 if (-f $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
232 $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
233 if (! rename $filename, $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
234 logger("ABORT", "unable to rename file $filename!");
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
235 exit 1;
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 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
238 return $newfn;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
239 }
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 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
242 # $filename = unpark_file($filename)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
243 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
244 # unparks a file
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
245 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
246 sub unpark_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
247 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
248 my $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
249 if (-f $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
250 if (! rename $newfn, $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
251 logger("ABORT", "unable to rename file $newfn!");
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
252 exit 1;
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 return $filename;
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 return "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
257 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
258
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
259
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
260
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
261 #
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
262 # ($document, $rootnode) = read_xml($file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
263 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
264 # reads xml meta file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
265 # returns root node element
30497c6a3eca Initial revision
casties
parents:
diff changeset
266 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
267 sub read_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
268 my ($file) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
269 my $document;
30497c6a3eca Initial revision
casties
parents:
diff changeset
270
30497c6a3eca Initial revision
casties
parents:
diff changeset
271 my $parser = XML::LibXML->new();
30497c6a3eca Initial revision
casties
parents:
diff changeset
272 if (! $parser) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
273 logger("ABORT", "unable to create parser for metadata index file!!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
274 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
275 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
276
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
277 logger("DEBUG", "index file: $file");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
278 eval { $document = $parser->parse_file($file) };
30497c6a3eca Initial revision
casties
parents:
diff changeset
279 # catch parsing errors
30497c6a3eca Initial revision
casties
parents:
diff changeset
280 if ($@) {
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
281 logger("ABORT", "XML syntax error in file $file!!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
282 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
283 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
284 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
285 logger("DEBUG", "root element: \'" . $rootnode->nodeName . "\'");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
286
30497c6a3eca Initial revision
casties
parents:
diff changeset
287 return ($document, $rootnode);
30497c6a3eca Initial revision
casties
parents:
diff changeset
288 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
289
30497c6a3eca Initial revision
casties
parents:
diff changeset
290
30497c6a3eca Initial revision
casties
parents:
diff changeset
291 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
292 # write_xml($document, $file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
293 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
294 # saves the XML tree of $document to the file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
295 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
296 sub write_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
297 my ($document, $filename) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
298 # keep backup copy
30497c6a3eca Initial revision
casties
parents:
diff changeset
299 if (-f $filename) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
300 if (! rename $filename, "$filename.old") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
301 logger("ABORT", "unable to change file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
302 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
303 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
304 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
305 # 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
306 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
307 if (-d $dir && $document->toFile($filename)) {
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
308 logger("INFO", "written new file $filename");
30497c6a3eca Initial revision
casties
parents:
diff changeset
309 chmod $index_file_perm, $filename;
30497c6a3eca Initial revision
casties
parents:
diff changeset
310 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
311 logger("ABORT", "unable to write file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
312 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
313 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
314 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
315
12
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 # $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
318 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
319 # 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
320 # $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
321 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
322 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
323 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
324
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
325 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
326 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
327 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
328 $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
329
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
330 return $newnode;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
331 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
332
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
333 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
334 # $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
335 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
336 # 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
337 # 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
338 # 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
339 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
340 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
341 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
342 my $attribute = "";
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
343 # get attribute
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
344 if ($path =~ /^(.+)@(.+)$/) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
345 $attribute = $2;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
346 $path = $1;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
347 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
348 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
349 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
350 # 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
351 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
352 if ($n) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
353 $point = $n;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
354 } else {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
355 # 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
356 $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
357 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
358 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
359 # 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
360 if ($attribute) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
361 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
362 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
363 $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
364 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
365 return $point;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
366 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
367
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 # $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
370 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
371 # 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
372 # 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
373 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
374 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
375 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
376 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
377 return $elem;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
378 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
379
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
380
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
381
30497c6a3eca Initial revision
casties
parents:
diff changeset
382 # module init
30497c6a3eca Initial revision
casties
parents:
diff changeset
383 return 1;