#! /usr/bin/perl use Digest::MD5 qw(md5 md5_hex md5_base64); use strict; use diagnostics; use warnings; our $debug = 0; sub dir { print "----------------- dir($_[0]) -----------------\n"; if ( !defined( $_[0] ) ) { die("Nothing passed to the dir function\n"); } my $path = $_[0]; foreach my $file ( lsdir("/") ) { #--------------------------------------------------------- # Step through the results of the lsdir # and determine if it is a) a directory # or b) a file. For each one, run it's # respective function. #--------------------------------------------------------- print "$file\n" if ( 0 == $debug ); #my $holder = "$path/$file/"; #print "HOLDER:\t$holder\n" if ( 0 == $debug ); my $isdir_result = isdir("$path/$file") if ( 0 == $debug ); print "ISDIR RESULTS:\t$isdir_result\n" if ( 0 == $debug ); if ( 0 == isdir("$path/$file") ) { print "Dir:\t/$path/$file/\n"; if ( '/' eq $path ) { # if it's the root directory, just use the file print "$file is a directory.\n" if ( 0 == $debug ); print "instance 1: dir($file)\n"; dir("/$file"); } else { #otherwise, just use the path print "$file is a file.\n" if ( 0 == $debug ); print "instance 2: dir($file)\n"; if ( $file !~ m/^\// ){ $file = "/" . $file; } dir($path); } } elsif ( 1 == isdir("$path/$file") ) { print "File:\t/$file\n"; my %file_info = file("/$file"); if ( 0 == $debug ) { foreach my $key ( keys %file_info ) { } } } } } sub file { print "o0o0o0o0o0o file($_[0]) o0o0o0o0o0o\n"; my $file = $_[0]; my %attrib; $attrib{name} = $file; # stat file ( $attrib{dev}, $attrib{ino}, $attrib{mode}, $attrib{nlink}, $attrib{uid}, $attrib{gid}, $attrib{rdev}, $attrib{size}, $attrib{atime}, $attrib{mtime}, $attrib{ctime}, $attrib{blksize}, $attrib{blocks} ) = stat($file); # compute md5 open( FILE, $file ) or print "Couldn't open $file!"; if ( -e $file ) { binmode(FILE); my $md5 = Digest::MD5->new; while () { $md5->add($_); } close(FILE); $attrib{md5} = $md5->b64digest; } else { print "What the hell.... $file doesn't exist?\n"; } # give it back return (%attrib); } sub isdir { print "/\/\/\/\/\/\ isdir() /\/\/\/\/\/\ \n"; print "isdir($_[0])\n"; my $file = $_[0]; if ( -d "$file" ) { print "ISDIR = YES\n" if ( 0 == $debug ); return(0); } else { print "ISDIR = NO\n" if ( 0 == $debug ); return(1); } } sub lsdir { print "************ lsdir($_[0]) **************\n"; my $dir = $_[0]; my @files; opendir( DIRHANDLE, "$dir" ) || die "Cannot opendir /some/path: $!"; foreach my $file ( sort readdir(DIRHANDLE) ) { push( @files, $file ) unless ( $file =~ m/^(\.){1,2}$/ ); print $file, "\n" if ( 2 == $debug ); } return @files; } my $path = "/"; print $path; dir("$path"); #|| die( "Couldn't run dir($path)"); #### /----------------- dir(/) ----------------- ************ lsdir(/) ************** bin ////// isdir() ////// isdir(//bin) ISDIR = YES ISDIR RESULTS: 0 ////// isdir() ////// isdir(//bin) ISDIR = YES Dir: ///bin/ bin is a directory. instance 1: dir(bin) ----------------- dir(/bin) ----------------- ************ lsdir(/) ************** bin ////// isdir() ////// isdir(/bin/bin) ISDIR = NO ISDIR RESULTS: 1 ////// isdir() ////// isdir(/bin/bin) ISDIR = NO ////// isdir() ////// isdir(/bin/bin) ISDIR = NO File: /bin o0o0o0o0o0o file(/bin) o0o0o0o0o0o boot ////// isdir() ////// isdir(/bin/boot) ISDIR = NO ISDIR RESULTS: 1 ////// isdir() ////// isdir(/bin/boot) ISDIR = NO ////// isdir() ////// isdir(/bin/boot) ISDIR = NO File: /boot o0o0o0o0o0o file(/boot) o0o0o0o0o0o dbmsrv.prt ////// isdir() ////// isdir(/bin/dbmsrv.prt) ISDIR = NO ISDIR RESULTS: 1 ////// isdir() ////// isdir(/bin/dbmsrv.prt) ISDIR = NO ////// isdir() ////// isdir(/bin/dbmsrv.prt) ISDIR = NO File: /dbmsrv.prt o0o0o0o0o0o file(/dbmsrv.prt) o0o0o0o0o0o dev ////// isdir() ////// isdir(/bin/dev) ISDIR = NO ISDIR RESULTS: 1 ////// isdir() ////// isdir(/bin/dev) ISDIR = NO ////// isdir() ////// isdir(/bin/dev) ISDIR = NO File: /dev o0o0o0o0o0o file(/dev) o0o0o0o0o0o etc