annotate MPIWGStor.pm @ 49:4d958249d337

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