summaryrefslogtreecommitdiff
path: root/index.php
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-05-22 17:15:27 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-05-22 17:15:27 -0400
commit1166729eee5aca1bc98f5a5d102da37689f4f2b8 (patch)
tree03b7d7c30049029a252b8fef3e7b72a9e508582e /index.php
parent3517c1aac1e844f91779e844b8030ad7b11e05d1 (diff)
index: speed up
Mostly avoid doing multiple filesystem stat()s.
Diffstat (limited to 'index.php')
-rw-r--r--index.php48
1 files 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/*<sys/stat.h>:S_IFMT*/) === $mask; } // <sys/stat.h>:__S_ISTYPE
+function stat_is_dir( $stat) { return s_is_type($stat['mode'], 0040000/*<sys/stat.h>:S_IFDIR*/); }
+function stat_is_file($stat) { return s_is_type($stat['mode'], 0100000/*<sys/stat.h>:S_IFREG*/); }
+function stat_is_link($stat) { return s_is_type($stat['mode'], 0120000/*<sys/stat.h>: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<tr class=\"".implode(' ', $classes)."\">";
- if (is_link($filepath)) {
- echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a> -> ".htmlentities(readlink($filepath))."</td>";
+ if (stat_is_link($stat)) {
+ echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a> -> ".htmlentities(readlink($root.'/'.$dirname.'/'.$filename))."</td>";
} else {
echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a></td>";
}
-
- if ($filename == '..') {
+
+ if ($filename === '..') {
echo '<td></td><td></td>';
} else {
- echo "<td>".date("Y-m-d H:i", filemtime($filepath))."</td>";
- if (is_dir($filepath)) {
+ echo "<td>".date("Y-m-d H:i", $stat['mtime'])."</td>";
+ if (stat_is_dir($stat)) {
echo "<td class=number>-</td>";
} else {
- echo "<td class=number>".filesize($filepath)."</td>";
+ echo "<td class=number>".$stat['size']."</td>";
}
- echo "</tr>\n";
}
+ echo "</tr>\n";
}
}
?>