/Fill/fill.php
PHP | 336 lines | 236 code | 53 blank | 47 comment | 68 complexity | 396648665a6b9d8cdceb56e3d8eb2f19 MD5 | raw file
Possible License(s): GPL-2.0
- #!/usr/bin/env php
- <?PHP
- /*
- * Copyright (C) 2011 Marco Ratto
- *
- * This file is part of the project Fill.
- *
- * Fill is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * any later version.
- *
- * Fill is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Fill; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- $SYNTAX = "Parameters: [OPTIONS] [SOURCE] [TARGET] [MAX_BYTES]\n";
- class Log4GPHP {
-
- // Only for this class
- public static $INTERNAL=6;
- // Severe errors that cause premature termination. Expect these to be immediately visible on a status console.
- public static $FATAL=5;
-
- // Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
- public static $ERROR = 4;
-
- // Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
- public static $WARN = 3;
-
- // Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
- public static $INFO = 2;
- // Detailed information on flow of through the system. Expect these to be written to logs only.
- public static $DEBUG = 1;
- // Detailed information on flow of through the system. Expect these to be written to logs only.
- public static $TRACE = 0;
-
- private static $LEVEL_NAMES = array(0=>"TRACE", 1=>"DEBUG", 2=>"INFO", 3=>"WARN", 4=>"ERROR", 5=>"FATAL", 6=>"INTERNAL");
-
- private static $loggingLevel = 0;
-
- private static function log($level, $message) {
- if ($level < self::$TRACE || $level > self::$INTERNAL) {
- echo("Logging level must be between TRACE(" . self::$DEBUG . ") and FATAL(" . self::$FATAL . ")");
- }
- if (($level < self::$loggingLevel) && ($level != self::$INTERNAL)) {
- return;
- }
- echo self::$LEVEL_NAMES[$level] . ": " . $message . "\n";
- }
-
- public static function print_r($level, $var) {
- if ($level < self::$TRACE || $level > self::$INTERNAL) {
- echo("Logging level must be between TRACE(" . self::$DEBUG . ") and FATAL(" . self::$FATAL . ")");
- }
- if (($level < self::$loggingLevel) && ($level != self::$INTERNAL)) {
- return;
- }
- $results = print_r($var, true);
- echo self::$LEVEL_NAMES[$level] . "-" . $results . "\n";
- }
- public function setLoggingLevel($level) {
- if ($level < self::$TRACE || $level>self::$FATAL) {
- return;
- }
- self::$loggingLevel = $level;
- self::log(self::$INTERNAL, "Level set to " . self::$LEVEL_NAMES[self::$loggingLevel]);
- }
-
- public static function getLoggingLevel() {
- return self::$loggingLevel;
- }
-
- public static function trace ($message) {
- self::log(self::$TRACE, $message);
- }
-
- public static function debug ($message) {
- self::log(self::$DEBUG, $message);
- }
-
- public static function info($message) {
- self::log(self::$INFO, $message);
- }
-
- public static function warn($message) {
- self::log(self::$WARN, $message);
- }
-
- public static function error($message) {
- self::log(self::$ERROR, $message);
- }
-
- public static function fatal($message) {
- self::log(self::$FATAL, $message);
- }
- }
- function parseArgs($argv) {
- array_shift($argv);
- Log4GPHP::print_r(Log4GPHP::$TRACE, $argv);
- $out = array();
- for ($j=0; $j <count($argv) ; $j++) {
- // foreach ($argv as $arg) {
- $arg = $argv[$j];
- if (substr($arg,0,2) == '--'){
- $eqPos = strpos($arg,'=');
- if ($eqPos === false) {
- $key = substr($arg,2);
- $out[$key] = isset($out[$key]) ? $out[$key] : true;
- } else {
- $key = substr($arg,2,$eqPos-2);
- $out[$key] = substr($arg,$eqPos+1);
- }
- } else if (substr($arg,0,1) == '-'){
- $eqPos = strpos($arg,'=');
- if ($eqPos === false){
- $key = substr($arg,1);
- $out[$key] = isset($out[$key]) ? $out[$key] : true;
- } else {
- $key = substr($arg,1,$eqPos-1);
- $out[$key] = substr($arg,$eqPos+1);
- }
- } else {
- $out[] = $arg;
- }
- }
- return $out;
- }
- Log4GPHP::setLoggingLevel(Log4GPHP::$TRACE);
- Log4GPHP::info("argc=$argc");
- if ($argc < 3) {
- print $SYNTAX;
- exit(1);
- }
- $params = parseArgs($argv);
- Log4GPHP::print_r(Log4GPHP::$INFO, $params);
- $size = $params['size'];
- Log4GPHP::info("size=$size");
- $mv = 0;
- if (array_key_exists('mv', $params)) {
- $mv = 1;
- }
- Log4GPHP::info("mv=$mv");
- $counter_target_dir = 1;
- $flag_counter = 0;
- if (array_key_exists('counter', $params)) {
- $counter_target_dir = 1;
- $flag_counter = 1;
- }
- Log4GPHP::info("flag_counter=$flag_counter");
- $mkdir = 0;
- if (array_key_exists('mkdir', $params)) {
- $mkdir = 1;
- }
- Log4GPHP::info("mkdir=$mkdir");
- Log4GPHP::info("gb=" . array_key_exists('gb', $params));
- Log4GPHP::info("mb=" . array_key_exists('mb', $params));
- Log4GPHP::info("kb=" . array_key_exists('kb', $params));
- $multiplier = 1;
- if (array_key_exists('gb', $params)) {
- Log4GPHP::info("-gb setted");
- $multiplier = 1024 * 1024 * 1024;
- }
- if (array_key_exists('mb', $params)) {
- Log4GPHP::info("-mb setted");
- $multiplier = 1024 * 1024;
- }
- if (array_key_exists('kb', $params)) {
- Log4GPHP::info("-kb setted");
- $multiplier = 1024;
- }
- Log4GPHP::info("multiplier=" . $multiplier);
- $target_max_bytes= $size * $multiplier;
- Log4GPHP::info("target_max_bytes=$target_max_bytes");
- $source = $params['source'];
- $target_dir = $params['target'];
- if ($flag_counter == 1) {
- $target= $target_dir . $counter_target_dir;
- }
- if (($mkdir == 1) && (is_dir($target) == false)) {
- Log4GPHP::info("Try to create directory $target.");
- mkdir($target);
- }
- if (is_dir($target) == false) {
- echo "ERROR: $target not exist!\n";
- exit(1);
- }
- $listOfFiles = array();
- scan_directory_recursively($source, 'my_handler');
- Log4GPHP::info("target_max_bytes=$target_max_bytes");
- arsort($listOfFiles);
- // Log4GPHP::print_r(Log4GPHP::$INFO, $listOfFiles);
- $counter_max_bytes = $target_max_bytes;
- while ($counter_max_bytes > 0) {
- Log4GPHP::info("Remaining $counter_max_bytes bytes");
-
- $key = "";
- $value = -1;
- Log4GPHP::info("Try to search a file on the total " . count($listOfFiles));
- foreach ($listOfFiles as $filename=>$filesize) {
- if ($filesize <= $counter_max_bytes) {
- Log4GPHP::info("Found file $filename ($filesize bytes).");
- $key = $filename;
- $value = $filesize;
- break;
- }
- }
- if ($value != -1) {
- $target= $target_dir . $counter_target_dir;
- if (($mkdir == 1) && (is_dir($target) == false)) {
- Log4GPHP::info("Try to create directory $target.");
- mkdir($target);
- }
- if (is_dir($target) == false) {
- Log4GPHP::error("Directory $target not exist!");
- exit(1);
- }
- $target_file = $target . "/" . basename($key);
- if (file_exists($target_file)) {
- Log4GPHP::warn("File $target_file ($value bytes) already exists! Skipped.");
- } else {
- Log4GPHP::info("Copy $key to $target_file ($value bytes)");
- if ($mv) {
- rename($key, $target_file);
- } else {
- copy($key, $target_file);
- }
- }
- $counter_max_bytes -= $value;
- unset($listOfFiles[$key]);
- } else {
- if (count($listOfFiles) == 0) {
- Log4GPHP::info("Finished to copy all files.");
- $counter_max_bytes = 0;
- } else {
- $counter_max_bytes = $target_max_bytes;
- $counter_target_dir++;
- Log4GPHP::info("Pass to a new directory with counter $counter_target_dir.");
- }
- }
- }
- exit(0);
- function my_handler($filename) {
- GLOBAL $target_max_bytes;
- GLOBAL $listOfFiles;
- $size = filesize($filename);
- $listOfFiles["$filename"] = $size;
- // echo "$filename=$size\n";
- }
-
- function scan_directory_recursively($directory, $callback) {
- // if the path has a slash at the end we remove it here
- if(substr($directory,-1) == '/') {
- $directory = substr($directory, 0, -1);
- }
- // if the path is not valid or is not a directory ...
- if(!file_exists($directory) || !is_dir($directory)) {
- // ... we return false and exit the function
- return FALSE;
- // ... else if the path is readable
- } elseif (is_readable($directory)) {
- // we open the directory
- $handle = opendir($directory);
- // and scan through the items inside
- while (FALSE !== ($file = readdir($handle))) {
- // if the filepointer is not the current directory
- // or the parent directory
- if($file != '.' && $file != '..') {
- // we build the new path to scan
- $path = $directory.'/'.$file;
- // if the path is readable
- if (is_readable($path)) {
- // we split the new path by directories
- $subdirectories = explode('/', $path);
- // if the new path is a directory
- if (is_dir($path)) {
- // print end($subdirectories);
- scan_directory_recursively($path, $callback);
- } else {
- call_user_func($callback, $path);
- }
- }
- }
- }
- // close the directory
- closedir($handle);
- // return file list
- return TRUE;
- // if the path is not readable ...
- }else{
- // ... we return false
- return FALSE;
- }
- }
- ?>