From 1166729eee5aca1bc98f5a5d102da37689f4f2b8 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 22 May 2016 17:15:27 -0400 Subject: index: speed up Mostly avoid doing multiple filesystem stat()s. --- index.php | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/index.php b/index.php index 5a09cd6..cef99d9 100644 --- a/index.php +++ b/index.php @@ -22,8 +22,8 @@ $dirname = explode("?", $_SERVER["REQUEST_URI"], 2)[0]; function normalizeN($filename) { $parts = preg_split("|/+|", $filename, -1, PREG_SPLIT_NO_EMPTY); - $abs = substr($filename, 0, 1) === '/'; - return array(($abs?'/':'').implode('/', $parts), count($parts)); + $abs = $filename[0] === '/'; + return [ ($abs?'/':'').implode('/', $parts), count($parts) ]; } function normalize($filename) { @@ -36,20 +36,25 @@ function is_root($root, $dirname) { return (normalize($path) === normalize($root)); } +function s_is_type($mode, $mask) { return ($mode & 0170000/*:S_IFMT*/) === $mask; } // :__S_ISTYPE +function stat_is_dir( $stat) { return s_is_type($stat['mode'], 0040000/*:S_IFDIR*/); } +function stat_is_file($stat) { return s_is_type($stat['mode'], 0100000/*:S_IFREG*/); } +function stat_is_link($stat) { return s_is_type($stat['mode'], 0120000/*:S_IFLNK*/); } + function classify($root, $dirname, $filename) { global $repos_arch_project, $repos_arch_community, $repos_para_project, $repos_para_community; - if ($filename === '.') { return [ 'dir' ]; } - if ($filename === '..') { return [ 'dir', 'parent' ]; } - list ($dirname, $parts) = normalizeN($dirname); - $dirpath = normalize($root.'/'.$dirname); - $filepath = normalize($dirpath.'/'.$filename); + $filepath = $root.'/'.$dirname.'/'.$filename; + $stat = stat($filepath); + + if ($filename === '.' ) { return [ [ 'dir' ], $stat]; } + if ($filename === '..') { return [ [ 'dir', 'parent' ], $stat]; } $classes = array(); if ($parts == 0) { // in the top-level - if (is_dir($filepath) && is_dir($filepath.'/os')) { + if (stat_is_dir($stat) && is_dir($filepath.'/os')) { array_push($classes, 'repo'); if (in_array($filename, $repos_arch_project )) { array_push($classes, 'arch'); } if (in_array($filename, $repos_para_project )) { array_push($classes, 'para'); } @@ -65,15 +70,15 @@ function classify($root, $dirname, $filename) { } } if ($filename[0] === '.' || substr($filename, -1) === '~') { $classes[] = 'hidden'; } - if (is_link($filepath)) { $classes[] = 'link'; } - if (is_dir( $filepath)) { $classes[] = 'dir'; } - if (is_file($filepath)) { $classes[] = 'file'; } + if (stat_is_link($stat)) { $classes[] = 'link'; } + if (stat_is_dir( $stat)) { $classes[] = 'dir'; } + if (stat_is_file($stat)) { $classes[] = 'file'; } if (preg_match("/\.pkg\.tar(\..z)?$/" , $filename) == 1) { $classes[] = 'pkg'; } if (preg_match("/\.tar(\..z|\.bz2)?$/", $filename) == 1) { $classes[] = 'tar'; } $classes[] = pathinfo($filename, PATHINFO_EXTENSION); - return $classes; + return [ $classes, $stat ]; } //////////////////////////////////////////////////////////////////////////////// @@ -136,26 +141,25 @@ if (!is_dir($root.'/'.$dirname)) { foreach ($filenames as $filename) { if ($filename === '.') { continue; } if ($filename === '..' && is_root($root,$dirname)) { continue; } - $filepath = normalize($root.'/'.$dirname.'/'.$filename); - $classes = classify($root, $dirname, $filename); + list($classes, $stat) = classify($root, $dirname, $filename); echo "\t\t\t\t"; - if (is_link($filepath)) { - echo "".htmlentities($filename)." -> ".htmlentities(readlink($filepath)).""; + if (stat_is_link($stat)) { + echo "".htmlentities($filename)." -> ".htmlentities(readlink($root.'/'.$dirname.'/'.$filename)).""; } else { echo "".htmlentities($filename).""; } - - if ($filename == '..') { + + if ($filename === '..') { echo ''; } else { - echo "".date("Y-m-d H:i", filemtime($filepath)).""; - if (is_dir($filepath)) { + echo "".date("Y-m-d H:i", $stat['mtime']).""; + if (stat_is_dir($stat)) { echo "-"; } else { - echo "".filesize($filepath).""; + echo "".$stat['size'].""; } - echo "\n"; } + echo "\n"; } } ?> -- cgit v1.2.2