This commit is contained in:
2017-05-20 20:03:50 -07:00
parent e838e2617f
commit 07bd8e897e

166
Yuba.php
View File

@@ -2,26 +2,79 @@
<?php <?php
// Yuba // Yuba
//////////////////////////////////////////
$version = "0.0.1"; // File checks
$time_start = microtime(true);
$version = "0.1.1";
date_default_timezone_set("America/Los_Angeles"); date_default_timezone_set("America/Los_Angeles");
print_r($argv); $time_start = microtime(true);
$stamp = date("Y-m-d_H-i-s", time());
$tmpdir = "/tmp/WalkWalk_".$stamp."/";
if (!is_dir($tmpdir)) { mkdir($tmpdir); }
$zpath = realpath($argv[1]); $zpath = realpath($argv[1]);
$dbfile = $argv[2]; if (!$zpath) { echo "Usage: walk <path> <dbfile>"; die; }
if (isset($argv[2]) && is_dir($argv[2])) {
if (!$zpath | !$dbfile) { $dbprefix = chop($argv[2],"/");
echo "Usage: walk <path> <dbfile>"; } else {
die; $dbprefix = ".";
} }
$base = preg_replace("/[^A-Za-z0-9\.]/", "_", basename($zpath));
$dbfile = $dbprefix."/".$stamp."_".$base.".sqlite3";
if (file_exists($dbfile)) { echo "File \"".$dbfile."\" already exists!"; die; }
// Banner
echo "Yuba ".$version."\n"; echo "Yuba ".$version."\n";
echo "-----------------------------------------------\n"; echo "-----------------------------------------------\n";
echo date("F jS, Y h:i:s", time())."\n"; $banner = $zpath." -> ".$dbfile;
$banner = "Walking ".$zpath." to ".$dbfile;
echo $banner."\n"; echo $banner."\n";
echo str_repeat("-", strlen($banner))."\n"; echo str_repeat("-", strlen($banner))."\n";
die;
// Database
$profile = shell_exec("system_profiler SPHardwareDataType SPStorageDataType SPThunderboltDataType SPUSBDataType 2>&1");
$disks = shell_exec("diskutil list 2>&1");
$host = gethostname();
$dbo = new PDO("sqlite:".$dbfile);
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbo->exec("CREATE TABLE IF NOT EXISTS \"".$base."\" (
id INTEGER PRIMARY KEY,
parent INTEGER,
path TEXT,
ext TEXT,
type TEXT,
size INTEGER,
hash TEXT,
perms INTEGER,
owner TEXT,
inode INTEGER,
mtime INTEGER,
atime INTEGER,
ctime INTEGER,
mdls TEXT,
tags TEXT,
mediainfo TEXT,
exif TEXT,
thumb BLOB)");
$dbo->exec("CREATE TABLE IF NOT EXISTS _walkwalk (
version TEXT,
host TEXT,
profile TEXT,
disks TEXT)");
$insert = "INSERT INTO _walkwalk VALUES (:version, :host, :profile, :disks)";
$stmt = $dbo->prepare($insert);
$stmt->BindValue(":version",$version);
$stmt->BindValue(":host",$host);
$stmt->BindValue(":profile",$profile);
$stmt->BindValue(":disks",$disks);
$stmt->execute();
// Iterate
$dir_iter = new RecursiveDirectoryIterator($zpath, $dir_iter = new RecursiveDirectoryIterator($zpath,
RecursiveDirectoryIterator::SKIP_DOTS RecursiveDirectoryIterator::SKIP_DOTS
@@ -31,44 +84,81 @@ $iter = new RecursiveIteratorIterator($dir_iter,
RecursiveIteratorIterator::CATCH_GET_CHILD RecursiveIteratorIterator::CATCH_GET_CHILD
); );
$i = 1; // Insert
foreach ($iter as $splFileInfo) {
$key[$splFileInfo->getRealPath()] = $i;
$i++;
}
$line = array(); foreach ($iter as $splFileInfo) {
foreach ($key as $path => $id) { $path = $splFileInfo->getRealPath();
echo $path."\n"; echo $path."\n";
$line = array();
$info = pathinfo($path); $info = pathinfo($path);
$line[$id]['parent'] = @$key[$info['dirname']]; $select = "SELECT id from \"".$base."\" WHERE (path=\"".$info['dirname']."\")";
$line[$id]['path'] = $path; $slct = $dbo->query($select);
if (is_dir($path)) { $row = $slct->fetchObject();
$line[$id]['isdir'] = 1; if (@$row->id) {
$line['parent'] = $row->id;
} else { } else {
$line[$id]['isdir'] = 0; $line['parent'] = 0;
} }
$line[$id]['size'] = filesize($path); $line['path'] = $path;
$hash = md5_file($path); $line['ext'] = strtolower(@$info['extension']);
$line[$id]['hash'] = $hash; $line['type'] = filetype($path);
$line[$id]['perms'] = fileperms($path); if ($line['type'] == "dir") {
$line[$id]['owner'] = fileowner($path).":".filegroup($path); $line['size'] = @shell_exec("du -ks \"".$path."\"")*1024;
$line[$id]['type'] = filetype($path); } else {
$line[$id]['inode'] = fileinode($path); $line['size'] = filesize($path);
$line[$id]['mtime'] = filemtime($path); }
$line[$id]['atime'] = fileatime($path); $line['hash'] = md5_file($path);
$line[$id]['ctime'] = filectime($path); $line['perms'] = fileperms($path);
$line['owner'] = fileowner($path).":".filegroup($path);
$line['inode'] = fileinode($path);
$line['mtime'] = filemtime($path);
$line['atime'] = fileatime($path);
$line['ctime'] = filectime($path);
$spotlight = shell_exec("mdls \"".$path."\" 2>&1"); $spotlight = shell_exec("mdls \"".$path."\" 2>&1");
if ($spotlight != $path.": could not find ".$path.".\n") { if ($spotlight != $path.": could not find ".$path.".\n") {
$line[$id]['mdls'] = $spotlight; $line['mdls'] = $spotlight;
if (strpos($spotlight, "kMDItemUserTags")) {
$jlines = array_slice(explode("\n", @shell_exec("mdls -name kMDItemUserTags -raw \"".$path."\" 2>&1")), 1, -1);
$colors = array();
foreach ($jlines as $jline) {
$colors[] = chop($jline,",");
}
$line['tags'] = serialize($colors);
}
} }
$thumb = "/tmp/".$hash.".png"; $line['mediainfo'] = "";
exec("qlmanage -t -o \"".$thumb."\" \"".$path."\""); $line['exif'] = "";
@exec("qlmanage -t -o ".$tmpdir." \"".$path."\" 2>&1");
$thumb = $tmpdir.basename($path).".png";
if (file_exists($thumb)) { if (file_exists($thumb)) {
$line[$id]['thumb'] = file_get_contents($blob); $line['thumb'] = file_get_contents($thumb);
} }
$insert = "INSERT INTO \"".$base."\" VALUES (:id, :parent, :path, :ext, :type, :size, :hash, :perms, :owner, :inode, :mtime, :atime, :ctime, :mdls, :tags, :mediainfo, :exif, :thumb)";
$stmt = null;
$stmt = $dbo->prepare($insert);
$stmt->BindValue(":parent",$line['parent']);
$stmt->BindValue(":path",$line['path']);
$stmt->BindValue(":ext",$line['ext']);
$stmt->BindValue(":type",$line['type']);
$stmt->BindValue(":size",$line['size']);
$stmt->BindValue(":hash",$line['hash']);
$stmt->BindValue(":perms",$line['perms']);
$stmt->BindValue(":owner",$line['owner']);
$stmt->BindValue(":inode",$line['inode']);
$stmt->BindValue(":mtime",$line['mtime']);
$stmt->BindValue(":atime",$line['atime']);
$stmt->BindValue(":ctime",$line['ctime']);
$stmt->BindValue(":mdls",$line['mdls']);
$stmt->BindValue(":tags",@$line['tags']);
$stmt->BindValue(":mediainfo",$line['mediainfo']);
$stmt->BindValue(":exif",$line['exif']);
$stmt->BindValue(":thumb",@$line['thumb']);
$stmt->execute();
} }
print_r($line); $fbanner = "Finished in ".floor($time = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"])." seconds";
echo str_repeat("-", strlen($fbanner))."\n".$fbanner."\n";
?> ?>