annotate MPIWGStor.pm @ 60:5bee75ca9eb3 default tip

added old makemeta-quantum.pl that was not in CVS.
author casties
date Thu, 16 Mar 2017 18:29:58 +0100
parents 116162d2d06e
children
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
49
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
15 $VERSION = 0.6.3; # ROC 6.3.2007
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 = (
49
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
30 # netatalk stuff
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
31 '.AppleDB' => '.AppleDB',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
32 '.AppleDesktop' => '.AppleDesktop',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
33 '.AppleDouble' => '.AppleDouble',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
34 '.FBCIndex' => '.FBCIndex',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
35 '.FBCLockFolder' => '.FBCLockFolder',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
36 ':2eTemporaryItems' => ':2eTemporaryItems',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
37 ':2eDS_Store' => ':2eDS_Store',
51
116162d2d06e added silly adobe files to junk file list
casties
parents: 49
diff changeset
38 ':2eBridgeSort' => ':2eBridgeSort',
49
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
39 # standard appleshare stuff
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
40 '.DS_Store' => '.DS_Store',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
41 '.TemporaryItems' => '.TemporaryItems',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
42 'Network Trash Folder' => 'Network Trash Folder',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
43 'TheVolumeSettingsFolder' => 'TheVolumeSettingsFolder',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
44 # old appleshare software turds
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
45 '.HSResource' => '.HSResource',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
46 '.HSancillary' => '.HSancillary',
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
47 '.HSicon' => '.HSicon'
4d958249d337 updated junk file list for netatalk special files
casties
parents: 30
diff changeset
48 );
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
49
30497c6a3eca Initial revision
casties
parents:
diff changeset
50 # filenames to not delete (but archive)
30497c6a3eca Initial revision
casties
parents:
diff changeset
51 %index_files = (
30497c6a3eca Initial revision
casties
parents:
diff changeset
52 'index.meta' => 'index.meta',
30497c6a3eca Initial revision
casties
parents:
diff changeset
53 'index.meta.old' => 'index.meta.old'
30497c6a3eca Initial revision
casties
parents:
diff changeset
54 );
30497c6a3eca Initial revision
casties
parents:
diff changeset
55
30497c6a3eca Initial revision
casties
parents:
diff changeset
56 # default permissions for normal index files (rw-rw-r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
57 $index_file_perm = 0664;
30497c6a3eca Initial revision
casties
parents:
diff changeset
58 # default permissions for archived index files (rw-r--r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
59 $archived_index_file_perm = 0644;
30497c6a3eca Initial revision
casties
parents:
diff changeset
60 # default permissions for other files (rw-rw-r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
61 $file_perm = 0664;
30497c6a3eca Initial revision
casties
parents:
diff changeset
62
30497c6a3eca Initial revision
casties
parents:
diff changeset
63
30497c6a3eca Initial revision
casties
parents:
diff changeset
64 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
65 # $param_hash = parseargs;
30497c6a3eca Initial revision
casties
parents:
diff changeset
66 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
67 # reads @ARGV and returns a hash with all options like "-option=value"
30497c6a3eca Initial revision
casties
parents:
diff changeset
68 # and the last part of the string as "path"
30497c6a3eca Initial revision
casties
parents:
diff changeset
69 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
70 sub parseargs {
30497c6a3eca Initial revision
casties
parents:
diff changeset
71 my %opts;
30497c6a3eca Initial revision
casties
parents:
diff changeset
72 foreach my $s (@ARGV) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
73 if ($s =~ /^-([^=]+)=*(.*)$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
74 $opts{$1} = ($2) ? $2 : $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
75 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
76 $opts{'path'} = $s;
30497c6a3eca Initial revision
casties
parents:
diff changeset
77 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
78 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
79 return \%opts;
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 # logger($level, $message)
30497c6a3eca Initial revision
casties
parents:
diff changeset
85 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
86 # logs the $message (mostly to stdout)
30497c6a3eca Initial revision
casties
parents:
diff changeset
87 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
88 sub logger {
30497c6a3eca Initial revision
casties
parents:
diff changeset
89 my ($level, $message) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
90 if ($debug || ($level ne "DEBUG")) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
91 print "$level: $message\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
92 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
93 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
94
30497c6a3eca Initial revision
casties
parents:
diff changeset
95 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
96 # $stime = stime($utime)
30497c6a3eca Initial revision
casties
parents:
diff changeset
97 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
98 # format utime (seconds since epoch) into string
30497c6a3eca Initial revision
casties
parents:
diff changeset
99 # representation: "YYYY/MM/DD HH:MM:SS"
30497c6a3eca Initial revision
casties
parents:
diff changeset
100 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
101 sub stime {
30497c6a3eca Initial revision
casties
parents:
diff changeset
102 my ($utime) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
103 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
30497c6a3eca Initial revision
casties
parents:
diff changeset
104 localtime($utime);
30497c6a3eca Initial revision
casties
parents:
diff changeset
105 my $yy = $year + 1900;
30497c6a3eca Initial revision
casties
parents:
diff changeset
106 my $mm = $mon + 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
107 my $stime = sprintf("%d/%02d/%02d %02d:%02d:%02d",
30497c6a3eca Initial revision
casties
parents:
diff changeset
108 $yy, $mm, $mday, $hour, $min, $sec);
30497c6a3eca Initial revision
casties
parents:
diff changeset
109 return $stime;
30497c6a3eca Initial revision
casties
parents:
diff changeset
110 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
111
30497c6a3eca Initial revision
casties
parents:
diff changeset
112 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
113 # $stime = s2stime($stime2)
30497c6a3eca Initial revision
casties
parents:
diff changeset
114 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
115 # format db-like time (2003-09-19 12:43:32+02) into string
30497c6a3eca Initial revision
casties
parents:
diff changeset
116 # representation: "YYYY/MM/DD HH:MM:SS"
30497c6a3eca Initial revision
casties
parents:
diff changeset
117 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
118 sub s2stime {
30497c6a3eca Initial revision
casties
parents:
diff changeset
119 my ($s2time) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
120 my $stime = "";
30497c6a3eca Initial revision
casties
parents:
diff changeset
121 if ($s2time =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
122 my ($year,$mon,$mday,$hour,$min,$sec) = ($1, $2, $3, $4, $5, $6);
30497c6a3eca Initial revision
casties
parents:
diff changeset
123 $stime = sprintf("%d/%02d/%02d %02d:%02d:%02d",
30497c6a3eca Initial revision
casties
parents:
diff changeset
124 $year, $mon, $mday, $hour, $min, $sec);
30497c6a3eca Initial revision
casties
parents:
diff changeset
125 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
126 return $stime;
30497c6a3eca Initial revision
casties
parents:
diff changeset
127 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
128
30497c6a3eca Initial revision
casties
parents:
diff changeset
129 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
130 # $date = ymd_date($olddate)
30497c6a3eca Initial revision
casties
parents:
diff changeset
131 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
132 # converts a date string in mm/dd/yy format to yyyy/mm/dd format
30497c6a3eca Initial revision
casties
parents:
diff changeset
133 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
134 sub ymd_date {
30497c6a3eca Initial revision
casties
parents:
diff changeset
135 my ($mdy) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
136 my $ydm;
30497c6a3eca Initial revision
casties
parents:
diff changeset
137 if ($mdy =~ /(\d+)\/(\d+)\/(\d+)/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
138 my $m = $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
139 my $d = $2;
30497c6a3eca Initial revision
casties
parents:
diff changeset
140 my $y = $3;
30497c6a3eca Initial revision
casties
parents:
diff changeset
141 # extend yy to yyyy
30497c6a3eca Initial revision
casties
parents:
diff changeset
142 $y = ($y < 70) ? (2000 + $y) : (($y < 100) ? ($y + 1900) : $y);
30497c6a3eca Initial revision
casties
parents:
diff changeset
143 $ydm = sprintf "%d/%02d/%02d", $y, $m, $d;
30497c6a3eca Initial revision
casties
parents:
diff changeset
144 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
145 return $ydm;
30497c6a3eca Initial revision
casties
parents:
diff changeset
146 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
147
30497c6a3eca Initial revision
casties
parents:
diff changeset
148 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
149 # ($name, $path) = split_file_path($fn)
30497c6a3eca Initial revision
casties
parents:
diff changeset
150 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
151 # splits the file path $fn into
30497c6a3eca Initial revision
casties
parents:
diff changeset
152 # file name (the last path element) and path
30497c6a3eca Initial revision
casties
parents:
diff changeset
153 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
154 sub split_file_path {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
155 my ($fn, $nodot) = @_;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
156
30497c6a3eca Initial revision
casties
parents:
diff changeset
157 if ($fn =~ /^(.*)\/([^\/]+)$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
158 return ($2, $1);
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
159 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
160 # only file name
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
161 if ($nodot) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
162 return ($fn, '');
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
163 } else {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 16
diff changeset
164 return ($fn, '.');
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
165 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
166 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
167
30497c6a3eca Initial revision
casties
parents:
diff changeset
168
30497c6a3eca Initial revision
casties
parents:
diff changeset
169 #
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
170 # $name = sstrip($name, $slash)
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
171 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
172 # strips leading and trailing whitespace from $name
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
173 # replaces double slashes with single ones with $slash.
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
174 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
175 sub sstrip {
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
176 my ($name, $slash) = @_;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
177
27
2890dd75d2f6 fixed bug in sstrip with whitespace at the end of the string...
casties
parents: 18
diff changeset
178 if ($name =~ /^\s*(.*?)\s*$/) {
30
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
179 $name = $1;
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
180 }
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
181 if ($slash) {
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
182 # trim multiple slashes
398ef4b8f072 added -online-base option to makemeta-lib
casties
parents: 27
diff changeset
183 $name =~ s/\/+/\//g;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
184 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
185 return $name;
30497c6a3eca Initial revision
casties
parents:
diff changeset
186 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
187
30497c6a3eca Initial revision
casties
parents:
diff changeset
188
30497c6a3eca Initial revision
casties
parents:
diff changeset
189 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
190 # $valid = valid_file_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
191 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
192 # checks if $name is a valid file name
30497c6a3eca Initial revision
casties
parents:
diff changeset
193 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
194 sub valid_file_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
195 return valid_name($_[0], 0);
30497c6a3eca Initial revision
casties
parents:
diff changeset
196 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
197
30497c6a3eca Initial revision
casties
parents:
diff changeset
198
30497c6a3eca Initial revision
casties
parents:
diff changeset
199 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
200 # $valid = valid_dir_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
201 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
202 # checks if $name is a valid directory name
30497c6a3eca Initial revision
casties
parents:
diff changeset
203 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
204 sub valid_dir_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
205 return valid_name($_[0], 1);
30497c6a3eca Initial revision
casties
parents:
diff changeset
206 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
207
30497c6a3eca Initial revision
casties
parents:
diff changeset
208
30497c6a3eca Initial revision
casties
parents:
diff changeset
209 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
210 # $valid = valid_name($name, $mode)
30497c6a3eca Initial revision
casties
parents:
diff changeset
211 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
212 # checks if $name is a valid file ($mode=0) or directory name (mode=1)
30497c6a3eca Initial revision
casties
parents:
diff changeset
213 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
214 sub valid_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
215 my ($name, $mode) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
216
30497c6a3eca Initial revision
casties
parents:
diff changeset
217 # whitespace?
30497c6a3eca Initial revision
casties
parents:
diff changeset
218 if ($name =~ /\s+/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
219 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
220 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
221 # invalid characters
30497c6a3eca Initial revision
casties
parents:
diff changeset
222 if ($name !~ /^[-\w.\/]+$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
223 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
224 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
225 # files have extension
30497c6a3eca Initial revision
casties
parents:
diff changeset
226 if ($mode == 0) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
227 if ($name !~ /\.[-\w]+$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
228 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
229 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
230 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
231 return 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
232 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
233
30497c6a3eca Initial revision
casties
parents:
diff changeset
234
30497c6a3eca Initial revision
casties
parents:
diff changeset
235 #
10
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
236 # $newfilename = park_file($filename)
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 # parks a file under a new name (*.bak)
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 sub park_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
241 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
242 my $newfn = "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
243 if (-f $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
244 $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
245 if (! rename $filename, $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
246 logger("ABORT", "unable to rename file $filename!");
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 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
250 return $newfn;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
251 }
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 # $filename = unpark_file($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 # unparks a file
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 sub unpark_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
259 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
260 my $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
261 if (-f $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
262 if (! rename $newfn, $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
263 logger("ABORT", "unable to rename file $newfn!");
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
264 exit 1;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
265 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
266 return $filename;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
267 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
268 return "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
269 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
270
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
271
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
272
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
273 #
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
274 # ($document, $rootnode) = read_xml($file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
275 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
276 # reads xml meta file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
277 # returns root node element
30497c6a3eca Initial revision
casties
parents:
diff changeset
278 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
279 sub read_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
280 my ($file) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
281 my $document;
30497c6a3eca Initial revision
casties
parents:
diff changeset
282
30497c6a3eca Initial revision
casties
parents:
diff changeset
283 my $parser = XML::LibXML->new();
30497c6a3eca Initial revision
casties
parents:
diff changeset
284 if (! $parser) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
285 logger("ABORT", "unable to create parser for metadata index file!!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
286 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
287 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
288
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
289 logger("DEBUG", "index file: $file");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
290 eval { $document = $parser->parse_file($file) };
30497c6a3eca Initial revision
casties
parents:
diff changeset
291 # catch parsing errors
30497c6a3eca Initial revision
casties
parents:
diff changeset
292 if ($@) {
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
293 logger("ABORT", "XML syntax error in file $file!!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
294 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
295 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
296 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
297 logger("DEBUG", "root element: \'" . $rootnode->nodeName . "\'");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
298
30497c6a3eca Initial revision
casties
parents:
diff changeset
299 return ($document, $rootnode);
30497c6a3eca Initial revision
casties
parents:
diff changeset
300 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
301
30497c6a3eca Initial revision
casties
parents:
diff changeset
302
30497c6a3eca Initial revision
casties
parents:
diff changeset
303 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
304 # write_xml($document, $file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
305 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
306 # saves the XML tree of $document to the file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
307 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
308 sub write_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
309 my ($document, $filename) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
310 # keep backup copy
30497c6a3eca Initial revision
casties
parents:
diff changeset
311 if (-f $filename) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
312 if (! rename $filename, "$filename.old") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
313 logger("ABORT", "unable to change file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
314 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
315 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
316 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
317 # 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
318 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
319 if (-d $dir && $document->toFile($filename)) {
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
320 logger("INFO", "written new file $filename");
30497c6a3eca Initial revision
casties
parents:
diff changeset
321 chmod $index_file_perm, $filename;
30497c6a3eca Initial revision
casties
parents:
diff changeset
322 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
323 logger("ABORT", "unable to write file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
324 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
325 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
326 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
327
12
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 = 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
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 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
332 # $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
333 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
334 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
335 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
336
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
337 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
338 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
339 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
340 $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
341
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
342 return $newnode;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
343 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
344
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
345 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
346 # $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
347 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
348 # 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
349 # 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
350 # 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
351 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
352 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
353 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
354 my $attribute = "";
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
355 # get attribute
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
356 if ($path =~ /^(.+)@(.+)$/) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
357 $attribute = $2;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
358 $path = $1;
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 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
361 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
362 # 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
363 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
364 if ($n) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
365 $point = $n;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
366 } else {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
367 # 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
368 $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
369 }
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 # 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
372 if ($attribute) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
373 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
374 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
375 $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
376 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
377 return $point;
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 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
381 # $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
382 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
383 # 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
384 # 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
385 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
386 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
387 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
388 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
389 return $elem;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
390 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
391
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
392
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
393
30497c6a3eca Initial revision
casties
parents:
diff changeset
394 # module init
30497c6a3eca Initial revision
casties
parents:
diff changeset
395 return 1;