diff --git a/Yuba.php b/Yuba.php index f6ea5a8..bb7a068 100755 --- a/Yuba.php +++ b/Yuba.php @@ -1,17 +1,13 @@ #!/usr/bin/php &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,9 +447,19 @@ 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; } + + } + // Filetypes for special handling $m_files = array( "mkv", @@ -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; } + $stat = chop(@shell_exec("stat -x ".$shellpath." 2>&1")); } 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); @@ -657,9 +669,7 @@ foreach ($files as $splFileInfo) { $stmt->BindValue(":MTime",$splFileInfo->getMTime()); } - - 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))) { @@ -708,9 +718,7 @@ foreach ($files as $splFileInfo) { if (file_exists($thumb) && filesize($thumb)) { $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 { @@ -774,9 +782,7 @@ foreach ($files as $splFileInfo) { stringPrint("->meta"); // 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 ($wopt_paranoid && $type != "link") { + if ($type != "link" && $wopt_paranoid) { + $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"]);