Source

libdaybo-fs-dirscan-perl / lib / Daybo / FS / DirScan.pm

#!/usr/bin/perl -w
#----------------------------------------------------------------------------
package Daybo::FS::DirScan;
use strict;
use warnings;
use diagnostics;

use constant SEEK_SET  => (-1);
use constant SEEK_CUR   => (0);
use constant SEEK_END   => (1);
#----------------------------------------------------------------------------
sub new
{
	my %Params;
	my $proto = shift;
	%Params = @_ if ( scalar(@_) );
	my $class = ref($proto) || $proto;
	my $self = {
		_debug => $Params{'Debug'},
		_files => [ ],
		_pos => -1
	};

	bless($self, $class);
	return $self;
}
#----------------------------------------------------------------------------
sub Seek($$$)
{
	my $F = 'Seek';
	my ( $self, $Whence, $Offset ) = @_;

	# Check that the reference to self is valid.
	die(sprintf('%s::%s(): Cannot Seek without reference to %s', __PACKAGE__, $F, __PACKAGE__))
		if ( !$self || !ref($self) || ref($self) ne __PACKAGE__ );

	# Check Whence and Offset are valid
	die(sprintf('%s::%s(): Incorrect parameters', __PACKAGE__, $F))
		if ( !$self->_ValidateWhence($Whence, $Offset) );

	# FIXME
}
#----------------------------------------------------------------------------
sub Scan($@)
{
	my ( $self, %Params ) = @_;

	if ( opendir(my $d, $Params{'Dir'}) ) {
		my @entries = ( );
		while ( my $ent = readdir($d) ) { push(@entries, $ent); }
		closedir($d); # Get the handle out of the way before recursion.

		foreach my $ent ( @entries ) {
			next if ( index($ent, '.') == 0 ); # Skip hidden/special
			my $fullPath = sprintf('%s/%s', $Params{'Dir'}, $ent);
			if ( -d $fullPath ) {
				if ( $Params{'Recurse'} ) {
					my %newParams = %Params;
					$newParams{'Dir'} = $fullPath;
					$self->Scan(%newParams);
				} else {
					$self->_Debug(
						'Skipped %s (not recursing into subdirectories)',
						$fullPath
					);
					next; # Skip directories where not recursing
				}
			} else {
				$self->_ProcessFile(
					FileName => $ent,
					FullPath => $fullPath
				);
			}
		}
	}
}
#----------------------------------------------------------------------------
sub _ProcessFile($@)
{
	my ( $self, %Params ) = @_;
	if ( $Params{'FileName'} =~ m/^CHECKSUM(\.\w+)?$/ ) {
		$self->_Debug(
			'%s::_ProcessFile(): FullPath %s FileName %s with extension %s',
			__PACKAGE__,
			$Params{'FullPath'},
			$Params{'FileName'},
			$1 || '(none)'
		);
	}
	push(@{ $self->{_files} }, $Params{'FullPath'});
}
#----------------------------------------------------------------------------
sub _Debug($$)
{
	my ( $self, $Msg ) = ( shift, shift );
	if ( $self->{_debug} ) {
		$Msg .= "\n";
		my $d = $self->{_debug};
		my $r = ref($d);
		if ( $r ) {
			if ( $r eq 'CODE' ) {
				$r->($Msg, @_);
			}
		} else {
			if ( index($d, '*') == 0 ) {
				printf($d $Msg, @_);
			} else {
				printf(main::STDERR $Msg, @_);
			}
		}
	}
}
#----------------------------------------------------------------------------
sub _ValidateWhence
{
	my $self = undef; # Optional reference to self
	my $absOffset;
	my ( $Whence, $Offset );
	if ( $_[0] ) {
		my $r = ref($_[0]);
		$self = shift if ( $r && $r eq __PACKAGE__ );
	}

	( $Whence, $Offset ) = @_;
	{ # Check Whence is valid
		my $whence_ok = FALSE();
		if ( $Whence =~ m/^\-?\d$/ ) {
			$whence_ok = TRUE()
				if ( $Whence >= _SEEK_MIN() && $Whence <= _SEEK_MAX() );
		}
		return $whence_ok if ( !$whence_ok );
	}

	# Change the relative offset into the absolute offset
	$absOffset = $Offset;
	if ( $self ) {
		my $curOffset = $self->{_pos};
		$curOffset = 0 if ( $curOffset < 0 );
		if ( $Whence == SEEK_CUR() ) {
			$absOffset += $self->{_pos};
		} elsif ( $Whence == SEEK_END() ) {
			xxx
#----------------------------------------------------------------------------
1;