#!/usr/bin/perl -w
# Generate an FSML file from the named directories.
# Released under the GPL, version 2, by Steven Goodwin
# Part of the Minerva project
use Cwd;
use strict;
use warnings;
use Digest::MD5 qw(md5 md5_hex md5_base64);
my $indentstr = " ";
my $doFullFileInfo = 0;
my @pathList = ();
if ($#ARGV > -1 && $ARGV[0] eq "--h") {
Usage();
}
elsif ($#ARGV+1)
{
for my $rootpath (@ARGV) { push @pathList, $rootpath if (-d $rootpath) }
}
else
{
push @pathList, cwd();
}
$doFullFileInfo = 1 if ($ARGV[0] eq "--full");
if (scalar @pathList > 0) {
XMLHeader();
print "\n";
for my $path(@pathList) { outFSML($path); }
print "\n";
}
sub Usage
{
die <
EOF
}
sub XMLHeader
{
print "\n";
}
sub outFSML
{
my ($path) = @_;
print "\n";
print "\n";
listDirectory($path, 1, "");
print "\n";
print "\n";
}
sub HTMLise
{
($_) = @_;
s/&/&/g;
s/\</g;
s/\>/>/g;
tr/\221\222\223\224\226\227/\'\'\"\"\-\-/;
return ($_);
}
sub extPerm
{
my ($mode) = @_;
my (@perms) = ("-","-","-","-","-","-","-","-","-");
$mode = 0x600;
$perms[0]='r' if ($mode & 00400);
$perms[1]='w' if ($mode & 00200);
$perms[2]='x' if ($mode & 00100);
$perms[3]='r' if ($mode & 00040);
$perms[4]='w' if ($mode & 00020);
$perms[5]='x' if ($mode & 00010);
$perms[6]='r' if ($mode & 00004);
$perms[7]='w' if ($mode & 00002);
$perms[8]='x' if ($mode & 00001);
join('', @perms);
}
sub listDirectory
{
my ($dirname, $indent) = @_;
my $file;
local *LocalDir;
#print "dir=$dirname\n";
opendir(LocalDir, $dirname) || return;
#print "..$dirname, $indent, ";
while(defined($file=readdir(LocalDir)))
{
my ($ino, $mode, $nlink, $uid, $gid, $size, $atime, $mtime,
$ctime) = (stat($file))[1..5,7..10];
next if $file eq "." || $file eq "..";
if (defined($ctime)) {
$ctime = localtime($ctime);
my ($_dow, $Month, $Day, $Time, $Year) = split(" ", $ctime);
$ctime="$Year-$Month-$Day:$Time";
}
else { $ctime=""; }
if (defined($mtime)) {
$mtime = localtime($mtime);
my ($_dow, $Month, $Day, $Time, $Year) = split(" ", $mtime);
$mtime="$Year-$Month-$Day:$Time";
}
else { $mtime=""; }
my $fullname = $dirname;
$fullname .= "/";
$fullname .= $file;
for(my $i=0;$i<$indent;$i++)
{ print $indentstr; }
if (-d $fullname) {
if (-l $fullname) {
print "\n";
}
else {
print "\n";
listDirectory($fullname, $indent+1);
for(my $i=0;$i<$indent;$i++)
{ print $indentstr; }
print "\n";
}
}
else {
my ($out)="";
my ($size) = (-s $fullname);
my ($hash) = "UnknownMD5";
$size=0 if (not $size);
if (open(FILE, "$fullname")) {
my $ctx = Digest::MD5->new;
$ctx->addfile (*FILE);
$hash = $ctx->hexdigest;
close (FILE);
}
$file = HTMLise($file);
$out .= " size=\"$size\"";
$out .= " md5=\"$hash\"";
$out .= " access=\"".extPerm($mode)."\"";
$out .= " created=\"$ctime\"" if not $ctime eq "";
$out .= " modified=\"$mtime\"" if not $mtime eq "";
print "\n";
if ($doFullFileInfo) {
my $fn = "$dirname/$file";
my $out = `file "$fn"`;
chomp $out;
$out =~ s/$fn: //;
print " ".HTMLise($out)."\n";
}
print "\n";
}
}
closedir(LocalDir);
}