This commit is contained in:
2017-06-18 03:17:42 -07:00
parent 403c9a8efc
commit 605bfd058a

143
Yuba.php
View File

@@ -1,17 +1,13 @@
#!/usr/bin/php
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
date_default_timezone_set("America/Los_Angeles");
$time_start = microtime(true);
// Yuba
// //
//////////////////////////////////////////
$version = "0.4.6";
// Stat each file before and after reading (will fail on ctime unless volume is RO)
if (in_array("-paranoid", $argv)) { $wopt_paranoid = 1; } else { $wopt_paranoid = 0; }
$version = "0.4.7.2";
// Treat these directories as files
$wopt_bundles = array( "app",
@@ -45,10 +41,6 @@ $wopt_ignore = array( ".DS_Store",
);
if (in_array("-hash", $argv)) { $wopt_hash = 1; } else { $wopt_hash = 0; }
//$wopt_hash_limit = ""; // don't hash large files nyi
//$wopt_mediainfo = 1; // not yet implemented
//$wopt_exiftool = 1; // not yet implemented
//$wopt_thumb = 1; // not yet implemented
$wopt_thumb_size = "256";
$wopt_thumb_factor = $wopt_thumb_size/128;
@@ -61,13 +53,6 @@ $bin_exiftool = "/opt/local/bin/exiftool";
// Functions
function debugStat() {
global $shellpath;
echo "\n\n";
echo $shellpath." - ".chop(@shell_exec("stat -x ".$shellpath." | tail -n1"));
echo "\n\n";
}
function stringPrint($string) {
echo $string.@str_repeat(" ", (10-strlen($string)));
}
@@ -174,6 +159,8 @@ if (isset($argv[2]) && is_dir($argv[2])) {
$dbprefix = ".";
}
if (is_writable($zpath)) { $wopt_paranoid = 1; } else { $wopt_paranoid = 0; }
// File checks
$stamp = date("Y-m-d_H-i-s", time());
@@ -251,14 +238,11 @@ $sysvers = shell_exec("sw_vers 2>&1");
$dbo = new PDO("sqlite:".$dbfile);
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// test: try to set values for better performance
//$dbo->query("PRAGMA page_size = 4096");
//$dbo->query("PRAGMA cache_size = 10000");
//$dbo->query("PRAGMA locking_mode = EXCLUSIVE");
//$dbo->query("PRAGMA synchronous = NORMAL");
//$dbo->query("PRAGMA journal_mode = WAL");
//$dbo->query("PRAGMA cache_size = 5000");
// end test
$dbo->query("PRAGMA page_size = 4096");
$dbo->query("PRAGMA cache_size = 10000");
$dbo->query("PRAGMA locking_mode = EXCLUSIVE");
$dbo->query("PRAGMA synchronous = NORMAL");
$dbo->query("PRAGMA journal_mode = WAL");
$dbo->exec("CREATE TABLE files (
id INTEGER PRIMARY KEY,
@@ -269,6 +253,7 @@ $dbo->exec("CREATE TABLE files (
Extension TEXT,
Type TEXT,
items INTEGER,
newest INTEGER,
stat TEXT,
LinkTarget TEXT,
RealPath TEXT,
@@ -435,6 +420,8 @@ if (posix_getuid()) {
echo bashcolor("You are not root. Checking file readability: ", "red");
echo "\n";
$oops = 0;
foreach ($files as $splFileInfo) {
$path = $splFileInfo->getRealPath();
@@ -460,6 +447,16 @@ if (posix_getuid()) {
} else {
echo bashcolor("Running as root. Some QuickLook plugins may not be available.", "red");
echo "\n";
}
$fixatimes = 0;
if ($wopt_paranoid) {
echo bashcolor("\nFilesystem is writable. You can choose:\n(c) Preserve ctimes (default)\n(a) Preserve atimes\n", "purple");
$line = trim(fgets(fopen("php://stdin","r"))) ?: "c";
if ($line == "a") { $fixatimes = 1; }
}
@@ -581,28 +578,26 @@ foreach ($files as $splFileInfo) {
$extension = $splFileInfo->getExtension();
$shellpath = escapeshellarg($pathname);
debugStat();
echo bashcolor("\n".$pathname."\n","blue");
if (!$type) { echo "\nBREAK: can't determine type of ".$pathname; die; }
if ($type != "link") {
$stat = chop(@shell_exec("stat -x ".$shellpath." 2>&1"));
if ($wopt_paranoid && !$atime) { echo "\nBREAK: Cannot determine atime of ".$pathname; die; }
} else {
$stat = null;
}
if ($type == "dir" || $type == "bundle" ) {
$items = chop(@shell_exec("find ".$shellpath." \( ! -regex '.*/\..*' \) | wc -l 2>&1"))-1;
} else {
$items = null;
}
$echolabel = basename($pathname);
echo $echolabel.": ";
$pad = 80;
if (strlen($echolabel) < $pad) {
echo str_repeat(" ",($pad-strlen($echolabel)));
if ($type != "link" && $wopt_paranoid) {
$pre_access = null;
$pre_modify = null;
$pre_change = null;
foreach (explode("\n", $stat) as $line) {
$check = substr($line, 0, 6);
if ($check == "Access") { $pre_access = $line; }
if ($check == "Modify") { $pre_modify = $line; }
if ($check == "Change") { $pre_change = $line; }
}
}
// Determine ID of parent dir by querying database
@@ -610,14 +605,12 @@ foreach ($files as $splFileInfo) {
$parent = $dbo->query("SELECT id FROM files WHERE (Pathname='".str_replace("'", "''", $path)."')")->fetch()['id'];
stringPrint("parent");
debugStat();
// Gather file attributes
$stmt = $dbo->prepare("INSERT INTO files VALUES (:id, :parent, :Pathname, :Path, :Filename, :Extension, :Type, :items, :stat, :LinkTarget, :RealPath, :Inode, :Size, :Perms, :Owner, :ATime, :MTime, :CTime, :gfi_type, :gfi_attr, :gfi_created, :hash, :tinfo)");
$stmt = $dbo->prepare("INSERT INTO files VALUES (:id, :parent, :Pathname, :Path, :Filename, :Extension, :Type, :items, :newest, :stat, :LinkTarget, :RealPath, :Inode, :Size, :Perms, :Owner, :ATime, :MTime, :CTime, :gfi_type, :gfi_attr, :gfi_created, :hash, :tinfo)");
if ($type == "dir") {
$size = shell_exec("du -ks ".$shellpath)*1024;
$size = trim(shell_exec("du -ks ".$shellpath." | cut -f1"))*1024;
} elseif ($type == "file" || $type == "bundle") {
$size = $splFileInfo->getSize();
} else {
@@ -632,7 +625,26 @@ foreach ($files as $splFileInfo) {
$stmt->BindValue(":parent",0);
}
stringPrint("items");
if ($type == "dir" || $type == "bundle" ) {
$items = chop(@shell_exec("find ".$shellpath." \( ! -regex '.*/\..*' \) | wc -l 2>&1"))-1;
} else {
$items = null;
}
$stmt->BindValue(":items",@$items);
stringPrint("newest");
if ($type == "dir") {
$newest = filemtime(chop(shell_exec("find ".$shellpath." -type f -not -path '*/\.*' -print0 | xargs -0 stat -f \"%m %N\" | sort -rn | head -1 | cut -f2- -d\" \"")));
} else {
$newest = null;
}
$stmt->BindValue(":newest",@$newest);
$stmt->BindValue(":stat",@$stat);
$stmt->BindValue(":Pathname",$pathname);
@@ -658,8 +670,6 @@ foreach ($files as $splFileInfo) {
}
debugStat();
stringPrint("attr");
$gfiparts = explode("\n", chop(shell_exec($bin_gfi." -P ".$shellpath." 2>&1")));
@@ -696,7 +706,7 @@ foreach ($files as $splFileInfo) {
$zprefix = "qlmanage";
}
*/
$zprefix = "qlmanage";
$zprefix = "sudo qlmanage"; // prevent hang
@exec($zprefix." -t -f ".$wopt_thumb_factor." -o ".$wopt_tmpdir." ".$shellpath." 2>&1");
stringPrint("thumb");
if ($size && !file_exists($thumb) && (in_array($extension, $m_files) || in_array($extension, $e_files))) {
@@ -709,8 +719,6 @@ foreach ($files as $splFileInfo) {
$stmt->BindValue(":tinfo",serialize(getimagesize($thumb)));
}
debugStat();
$stmt->execute();
stringPrint("->files");
@@ -749,7 +757,7 @@ foreach ($files as $splFileInfo) {
$stmt = $dbo->prepare("INSERT INTO metadata VALUES (:id, :duration, :mediainfo, :exiftool)");
if ($type != "dir" && in_array($extension, $m_files)) {
$minfo = parseMediaInfo(shell_exec($bin_mediainfo." --Output=XML ".$shellpath." 2>&1"));
$minfo = parseMediaInfo(shell_exec($bin_mediainfo." --Output=OLDXML ".$shellpath." 2>&1"));
if ($minfo['file']['general']['duration'][0]) {
$stmt->BindValue(":duration",$minfo['file']['general']['duration'][0]);
} else {
@@ -775,8 +783,6 @@ foreach ($files as $splFileInfo) {
// Gather thumbnail
debugStat();
$stmt = $dbo->prepare("INSERT INTO thumbs VALUES (:id, :thumb)");
if (file_exists($thumb) && filesize($thumb)) {
$stmt->BindValue(":thumb",file_get_contents($thumb));
@@ -788,25 +794,48 @@ foreach ($files as $splFileInfo) {
// Set fileatime back to original value
//if ($type != "link" && is_writable($pathname)) {
// @exec("touch -at `date -r ".$atime." +%Y%m%d%H%M.%S` ".$shellpath." 2>&1");
// stringPrint("touch");
// }
if ($type != "link" && is_writable($pathname) && $fixatimes) {
@exec("touch -at `date -r ".$atime." +%Y%m%d%H%M.%S` ".$shellpath." 2>&1");
stringPrint("touch");
}
echo "\n";
// Double check stat for file against pre-run value
echo bashcolor(str_replace(array("\n"," "),array(" ",""),$stat)."\n","blue");
if ($type != "link" && $wopt_paranoid) {
if ($wopt_paranoid && $type != "link") {
$restat = chop(@shell_exec("stat -x ".$shellpath." 2>&1"));
echo bashcolor(str_replace(array("\n"," "),array(" ",""),$restat)."\n","green");
if ($stat != $restat) { echo "\nBREAK: stat changed on ".$pathname; die; }
$post_access = null;
$post_modify = null;
$post_change = null;
foreach (explode("\n", $restat) as $line) {
$check = substr($line, 0, 6);
if ($check == "Access") { $post_access = $line; }
if ($check == "Modify") { $post_modify = $line; }
if ($check == "Change") { $post_change = $line; }
}
$message = array();
if ($pre_access != $post_access) {
$message[] = "ATIME CHANGED";
}
if ($pre_modify != $post_modify) {
$message[] = "MTIME CHANGED";
}
if ($pre_change != $post_change) {
$message[] = "CTIME CHANGED";
}
if (count($message)) { echo bashcolor(str_repeat(" ",99).implode(" ~ ", $message), "purple"); }
}
}
echo "\n\n\n";
// Footer
$seconds = floor($time = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]);