File: lsparse.pl

package info (click to toggle)
ftpmirror 1.2l-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 260 kB
  • ctags: 139
  • sloc: perl: 3,318; makefile: 56; sh: 47
file content (478 lines) | stat: -rw-r--r-- 11,166 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
;#
;# Copyright (c) 1996, Ikuo Nakagawa.
;# All rights reserved.
;#
;# $Id: lsparse.pl,v 1.4 1997/05/22 08:38:55 ikuo Exp $
;#
;# Last update:
;#	1996/10/17 by Ikuo Nakagawa
;# Description:
;#	lsparse.pl - parsing an output of /bin/ls.
;#
;# timelocal.pl should be called in the package 'main'
require "timelocal.pl";

;# the name of this package
package lsparse;

;# we use log package
require "log.pl";

;# prototypes
;# sub findpath($);
;# sub lslR_open($);
;# sub lslR_close();
;# sub lslR_getdir(;$);
;# sub lslR_parse($;$);
;# sub lsparse($);
;# sub lstime($);

;# name of month, and conversion table
@nameofmonth = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@nametomonth{map(lc $_, @nameofmonth)} = (0..11);
$regexp_month = join('|', @nameofmonth);

;# set this to debug this package
$debug = 0;

;#
$path_gzip = &findpath("gzip");
$path_compress = &findpath("compress");

;#
sub findpath {
 my $foo = shift;
 my @path = split(':', $ENV{'PATH'});
 my $d;
 for $d (@path) {
  return "$d/$foo" if -x "$d/$foo";
 }
 for $d (qw(/usr/local/bin /usr/gnu/bin /usr/contrib/bin /usr/ucb)) {
  return "$d/$foo" if -x "$d/$foo";
 }
 $foo;
}

;#
;# If prototypes are commented out, lslR_close must be defined
;# before lslR_open.
;#
sub lslR_close {
 close(LSLR), undef $LSLR if defined($LSLR);
 1;
}
;#
;# lslR_open
;# Open lslR file, which may be .gz or .Z compressed file.
;# lslR_open must be called before lslR_getdir.
;#
sub lslR_open {
 my($lslR) = @_;

 &lslR_close;
 $LSLR = $lslR =~ /\.gz$/ ? "$path_gzip -cd $lslR|" :
	 $lslR =~ /\.Z$/ ? "$path_compress -cd $lslR|" :
	 $lslR;
 open(LSLR) || do { log::putl("NOTICE", "open: $!"); return undef };
 1;
}

;#
;# Parsing ls-lR format
;# CAUTION: This subroutine is not re-entrant!!
;# lslR_getdir() returns a dirinfo for a directory on each call.
;# Following variables are re-used:
;#  *LSLR, $topdir, $curdir;
;#
sub lslR_getdir {
 local($subst) = @_;
 local($_, $s, $info, $total);

 return undef if !defined($LSLR);

 while (<LSLR>) {

  chomp;

  if (/^$/) {			# end-of-directory
   return ($curdir, \%{$info}) if defined($curdir);
   next; # search next directory name...
  }

  if (/^total (\d+)/) {		# we may skip this
   $total = $1;
   next;
  }

  ;# normal entry
  if (/^(.)([-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT])\s*/) {
   my($type, $mode) = ($1, $2);
   if (!defined($s = &lsparse($_))) {
     next;			# ignore error
   }
   $info->{$s->{'file'}} = $s;
   if (defined($curdir)) {
    $s->{'path'} = "$curdir/$s->{'file'}";
   }
  }

  ;# new directory
  if (/:$/) {
   $curdir = $`;
   $curdir = &{$subst}($curdir) if defined($subst) && ref($subst) eq 'CODE';
   if (!defined($topdir)) {
    $topdir = $curdir =~ m%/[^/]+$% ? $` : '.';
    log::putl("DEBUG", "setting topdir to $topdir") if $debug;
    if (defined($info)) {
     for $key (keys %{$info}) {
      $s = $info->{$key};
      $s->{'path'} = "$topdir/$s->{'file'}";
     }
     return ($topdir, \%{$info});
    }
   }
  } # end of if (/:$/) { ... }

  ;# other case?
 }

 ;#
 if (!defined($topdir)) {
  $topdir = $curdir = '.';
  log::putl("DEBUG", "setting topdir to $topdir") if $debug;
 }

 ;#
 if (defined($info)) {
  for $key (keys %{$info}) {
   $s = $info->{$key};
   $s->{'path'} = "$curdir/$s->{'file'}";
  }
  return ($curdir, \%{$info});
 }

 ;#
 return undef;
}

;#
;# lslR_parse() parses ls-lR format file. This routine do
;#  call lslR_open;
;#  call lslR_getdir for ls-lR directory hierarcy
;#  call lslR_close;
;#  and returns directory information for the top directory
;#  of ls-lR.
;#
sub lslR_parse {
 my($lslR, $subst) = @_;
 my($d, $s, $x, $y, @d);

 # Let's open lslR file
 &lslR_open($lslR) || return undef;

 # Get top directory information...
 ($d, $x) = &lslR_getdir($subst);

 # Check directory information
 if (!defined($x)) {
  &lslR_close;
  log::putl("INFO", "lslR_parse: empty directory");
  return undef;
 }

 # DEBUG...
 if ($debug > 0) {
  log::putl("DEBUG", "%5 directory = $d");
  for (sort keys %{$x}) {
   log::putl("DEBUG", "%5  $x->{$_}->{'path'}");
  }
 }

 # We support only the case of $topdir eq '.'
 if ($d ne '.') {
  &lslR_close;
  log::putl("INFO", "lslR_parse: top directory `$d' must be `.'");
  return undef;
 }

 # Scan directory hierarcy
 while (($d, $s) = &lslR_getdir($subst)) {

  # Check result
  last if !defined($s);

  # DEBUG...
  if ($debug > 0) {
   log::putl("DEBUG", "%5 directory = $d");
   for (sort keys %{$s}) {
    log::putl("DEBUG", "%5  $s->{$_}->{'path'}");
   }
  }

  # search directory information HASH
  $y = $x;
  @d = split('/', $d);
  while (@d) {
   my $f = shift(@d);
   next if $f eq '.';
   if (!(defined($y->{$f}) && $y->{$f}->{'type'} eq 'directory')) {
    &lslR_close;
    log::putl("DEBUG", "lslR_parse: $y->{'path'} don't have $f");
    log::putl("INFO", "lslR_parse: unsupported format");
    return undef;
   }
   if (@d) { # something remains, down...
    $y = \%{$y->{$f}->{'hash'}};
   } else { # set HASH
    $y->{$f}->{'hash'} = $s;
   }
  }
 }

 # close, and...
 &lslR_close;

 # return the top of directory hierarcy
 $x;
}

;# parsing a line from outputs of `ls'
sub lsparse {
 local($_) = @_;
 local($[);
 my($s, $x, $tmp);
 my($type, $mode, $nlink, $owner, $group, $size, $date, $file);

 # backup
 $x = $_;

 # check DOS dirstyle
 if (/^\d\d-\d\d-\d\d\s+\d\d:\d\d(am|pm)?/i) { # DOS
  ($date, $_) = ($&, $');
  if (s/\s+\<DIR\>\s+//) {
   $s->{'type'} = 'directory';
  } elsif (s/\s+(\d+)\s+//) {
   ($s->{'type'}, $s->{'size'}, $s->{'date'}) = ('file', $1, $date);
  } else {
   log::putl("INFO", "unknown DOS format: \"$x\""), return undef;
  }
  $s->{'file'} = $_;
  return $s;
 }

 # UNIX style - check file modes
 s/^(.)([-r][-w].[-r][-w].[-r][-w].)\s*// || do {
  log::putl("DEBUG", "invalid format: \"$x\"");
  return undef;
 };
 $type = $1;
 $tmp = $2;

 # mode to octal value
 $mode = 0;
 $mode |= 04000 if $tmp =~ /^..s/;
 $mode |= 02000 if $tmp =~ /^.....s/;
 $mode |= 01000 if $tmp =~ /^........t/;
 $mode |= 00400 if $tmp =~ /^r/;
 $mode |= 00200 if $tmp =~ /^.w/;
 $mode |= 00100 if $tmp =~ /^..[xs]/;
 $mode |= 00040 if $tmp =~ /^...r/;
 $mode |= 00020 if $tmp =~ /^....w/;
 $mode |= 00010 if $tmp =~ /^.....[xs]/;
 $mode |= 00004 if $tmp =~ /^......r/;
 $mode |= 00002 if $tmp =~ /^.......w/;
 $mode |= 00001 if $tmp =~ /^........[xs]/;
 $mode = sprintf("%04o", ($mode & 0777));

 # find date string
 /($regexp_month)\s+(\d?\d)\s+((\d\d\d\d)|(\d+:\d+))/i || do {
  log::putl("DEBUG", "date not found: \"$x\""), return undef;
 };

  # save some values
 ($_, $date, $file) = ($`, $&, $');

 #
 s/\s*(\d+)\s*$// || do {
  log::putl("DEBUG", "size not found: \"$x\""), return undef;
 };
 $size = $1;
 s/^(\d+)\s*// || do {
  log::putl("DEBUG", "nlink not found: \"$x\""), return undef;
 };
 $nlink = $1;
 ($owner, $group) = /\s+/ ? ($`, $') : ($_, '');

 # set filename to $_
 $_ = $file;

 # kill leading/trailing spaces
 s/^\s+//; s/\s+$//;

 # logging
 if ($debug > 1) {
  log::putl("DEBUG", "parsing [$x]");
  log::putl("DEBUG", " ... filename = $_");
  log::putl("DEBUG", " ... type = $type");
  log::putl("DEBUG", " ... mode = $mode");
  log::putl("DEBUG", " ... owner = $owner");
  log::putl("DEBUG", " ... group = $group");
  log::putl("DEBUG", " ... size = $size");
  log::putl("DEBUG", " ... date = $date");
  my @a = reverse((localtime(&lstime($date) - 3600 * 9))[0..5]);
  $a[0] += 1900;
  $a[1]++;
  log::putl("DEBUG", " ... time = ".
    sprintf("%04d-%02d-%02d %02d:%02d:%02d", @a));
 }

 #
 if ($type eq 'd') {
  $s->{'type'} = 'directory';
  $s->{'file'} = $_;
  if ($debug > 1) {
   log::putl("DEBUG", "parse: directory $s->{'file'}");
  }
  return $s;
 }
 if ($type eq 'l') {
  $s->{'type'} = 'symlink';
  ($s->{'file'}, $s->{'linkto'}) = /\s+->\s+/ ? ($`, $') : ($_, '');
  if ($debug > 1) {
   log::putl("DEBUG", "parse: symlink $s->{'file'} -> $s->{'linkto'}");
  }
  return $s;
 }
 if ($type eq '-') {
  $s->{'type'} = 'file';
  $s->{'file'} = $_;
  $s->{'mode'} = $mode;
  $s->{'owner'} = $owner;
  $s->{'group'} = $group;
  $s->{'size'} = $size;
  $s->{'date'} = $date;
  $s->{'time'} = &lstime($date);
  log::putl("DEBUG", "parse: file $s->{'file'}") if $debug > 2;
  return $s;
 }
 return undef;
}

;# parse date strings and convert to time
sub lstime {
 local($_) = @_;

 my($sec, $min, $hour, $day, $mon, $year) =
  /^($regexp_month)\s+(\d+)\s+((\d\d\d\d)|((\d+):(\d+)))$/oi ?
   (0, $7, $6, $2, $1, $4) : # Unix ls
  /^($regexp_month)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d\d\d\d)$/oi ?
   ($5, $4, $3, $2, $1, $6) : # Unix ls -T
  /^(\d+)\s+($regexp_month)\s+((\d\d\d\d)|((\d+):(\d+)))$/oi ?
   (0, $7, $6, $1, $2, $4) : # dls and NetWare
  /(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)(AM|PM)?/oi ?
   (0, $5, ($6 eq 'PM' ? $4 + 12 : $4) , $2, $1, $3) :
  /(\d+)-(\S+)-(\d+)\s+(\d+):(\d+)/oi ?
   (0, $5, $4, $1, $2, $3) : # VMS style
  /^\w+\s+($regexp_month)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/oi ?
   ($5, $4, $3, $2, $1, $6) : # CTAN style (and HTTP)
  /^\w+,\s+(\d+)-($regexp_month)-(\d+)\s+(\d+):(\d+):(\d+)/oi ?
   ($6, $5, $4, $1, $2, $3) : # another HTTP
  undef;

 my $month = ($mon =~ /^\d+$/ ? $& - 1 : $nametomonth{lc $mon});

 if (!defined($year) || $year !~ /\d\d\d\d/){
  my($l_month, $l_year) = (gmtime)[4, 5];
  $year = $l_year;
  $year-- if $month > $l_month;
 }
 $year -= ($year >= 2000 ? 2000 : $year >= 1970 ? 1900 : 0);

 $sec = 0 unless defined($sec) && $sec >= 0 && $sec < 60;
 $min = 0 unless defined($min) && $min >= 0 && $min < 60;
 $hour = 0 unless defined($hour) && $hour >= 0 && $hour < 24;

 return &main::timegm($sec, $min, $hour, $day, $month, $year);
}

;#
;# search a directory information for the specified directory.
;#
sub scan_init {
 my($lslR, $subst) = @_;

 ;# First log
 log::putl("INFO", "scan: initialize") if $debug;

 # Let's open lslR file
 &lslR_open($lslR) || return undef;

 undef %scan_cache;
 undef $scan_subst;
 $scan_subst = $subst if defined($subst) && ref($subst) eq 'CODE';
 $scan_run = 1;
}

;#
;#
;#
sub scan_compare {
 my($x, $y) = @_;

 $x =~ s,/,\001,g;
 $y =~ s,/,\001,g;
 $x cmp $y;
}

;#
;# search a directory entry for scanning.
;#
sub scan_search {
 my($dir) = @_;

 # check scan directory
 if (exists($scan_cache{$dir})) {
  my $x = $scan_cache{$dir};
  delete($scan_cache{$dir});
  log::putl("INFO", "scan: + $dir... found in cache.") if $debug;
  return $x;
 }

 # read next information
 while ($scan_run) {
  my($d, $x) = &lslR_getdir($scan_subst);

  last if !defined($x);
  if ($d eq $dir) {
   log::putl("INFO", "scan: + $dir... found by lslR_getdir.") if $debug;
   return $x;
  }
  if (&scan_compare($d, $dir) < 0) {
   log::putl("DEBUG", "scan: + $dir... was skipped.") if $debug;
  } else { ## $d (> $dir) may be scaned later
   log::putl("DEBUG", "scan: + $dir... was cached.") if $debug;
   $scan_cache{$d} = $x;
  }
 }

 &lslR_close;
 $scan_run = 0;
 log::putl("INFO", "scan: ? $dir... not found.") if $debug;
 return undef;
}

;#
sub scan_done {
 &lslR_close if $scan_run;
 for $d (sort keys %scan_cache) {
  log::putl("DEBUG", "scan: - $d");
  delete($scan_cache{$d});
 }
 undef %scan_cache;
 undef $scan_subst;
 undef $scan_run;
 log::putl("INFO", "scan: done") if $debug;
}

;# success on this package
1;