App-FileCleanerByDiskUage

 view release on metacpan or  search on metacpan

lib/App/FileCleanerByDiskUage.pm  view on Meta::CPAN

    - du :: Disk usage to remove files down till.
        Default :: undef

    - min_files :: Minimum number of files to keep, regardless of disk usage.
        Default :: undef

    - ignore :: A regexp to use for ignoring files. So lets say you want to ignore,
                files matching /\.pcap$/, it would be '\.pcap$'.
        Default :: undef

    - dry_run :: Do not actually remove anything. Just check to see if the file writable by the
                 current user.

    -use_pid :: Create a PID to make sure multiple instances can't run at once.
        Default :: undef

    -pid_dir :: Create a PID to make sure multiple instances can't run at once.
        Default :: /var/run

    - pid_name :: Append this to the the name of the pid file created. If specified with a
            value of 'foo' then the file would be 'file_cleaner_by_du-foo.pid'.
        Default :: undef

The returned value is a hash ref.

    - dry_run :: Boolean for fir it was a dry run or not.

    - found_files :: Array of hashes of data for all files found. This will only be defined if du is above
                     threshold for removing files. If it is below it, the function will return instead of taking
                     the time required to run a search.

    - found_files_count :: A count of files found.

    - path :: The value of path that it was called with. This will always be a array, regardless of if a array or
              scalar was passed as internally converts a scalars into a array containing just a single item.

    - missing_paths :: Paths that were passed to it, but don't exist.

    - unlinked :: Array of hashes of data for files that have been removed.

    - unlinked_count :: A count of how many files were unlinked

    - unlink_errors :: Array of strings containing error descriptions.

    - unlink_failed :: Array of hashes of data for files that could not removed. The corresponding
                       index in .errors will be the error in question. So $results->{unlink_failed}[0]
                       would be $results->{unlink_errors}[0]

    - unlink_fialed_count :: A count of how many files unlinking failed for.

The files hash is composed as below.

    - name :: Name of the file, including it's path.

    # following are provided via the Perl function stat
    - dev
    - ino
    - mode
    - nlink
    - uid
    - gid
    - rdev
    - size
    - atime
    - mtime
    - ctime
    - blksize
    - blocks

=cut

sub clean {
	my ( $empty, %opts ) = @_;

	my $pid_file;
	if ( $opts{use_pid} ) {
		if ( !defined( $opts{pid_dir} ) ) {
			$opts{pid_dir} = '/var/run';
		} else {
			if ( !-d $opts{pid_dir} ) {
				die( $opts{pid_dir} . ' does not exist or is not a dir' );
			}
		}

		if ( !defined( $opts{pid_name} ) ) {
			$pid_file = $opts{pid_dir} . '/file_cleaner_by_du.pid';
		} else {
			if ( $opts{pid_name} =~ /\// || $opts{pid_name} =~ /\\/ ) {
				die( 'PID name of "' . $opts{pid_name} . '" can not contain either / or \\' );
			}
			$pid_file = $opts{pid_dir} . '/file_cleaner_by_du-' . $opts{pid_name} . '.pid';
		}

		check_pid_file($pid_file);
		create_pid_file($pid_file);
	} ## end if ( $opts{use_pid} )

	my @missing_paths;
	my @paths;

	my $du_path;
	# file paths should end with / or other wise if it is a symlink File::Find::Rule will skip it
	# so fix that up while we are doing the path check
	if ( !defined( $opts{path} ) ) {
		if ( $opts{use_pid} ) {
			unlink_pid_file($pid_file);
		}
		die('$opts{path} is not defined');
	} elsif ( ref( $opts{path} ) ne 'ARRAY' && !-d $opts{path} ) {
		push( @missing_paths, $opts{path} );
	} elsif ( ref( $opts{path} ) eq 'ARRAY' ) {
		if ( !defined( $opts{path}[0] ) ) {
			if ( $opts{use_pid} ) {
				unlink_pid_file($pid_file);
			}
			die('$opts{path}[0] is not defined');
		}
		my $int = 0;
		while ( defined( $opts{path}[$int] ) ) {
			$opts{path}[$int] = $opts{path}[$int] . '/';
			$opts{path}[$int] =~ s/\/+$/\//;



( run in 0.651 second using v1.01-cache-2.11-cpan-ceb78f64989 )