Dir listing: special-case ".." to come first.

Suggested by: @frogtile

Fixes #14
This commit is contained in:
Emil Mikulic 2022-01-19 20:10:50 +11:00
parent f0ca481fd1
commit 3641c2f50f
3 changed files with 14 additions and 1 deletions

View File

@ -1817,6 +1817,9 @@ struct dlent {
};
static int dlent_cmp(const void *a, const void *b) {
if (strcmp((*((const struct dlent * const *)a))->name, "..") == 0) {
return -1; /* Special-case ".." to come first. */
}
return strcmp((*((const struct dlent * const *)a))->name,
(*((const struct dlent * const *)b))->name);
}
@ -1843,7 +1846,7 @@ static ssize_t make_sorted_dirlist(const char *path, struct dlent ***output) {
while ((ent = readdir(dir)) != NULL) {
struct stat s;
if ((ent->d_name[0] == '.') && (ent->d_name[1] == '\0'))
if (strcmp(ent->d_name, ".") == 0)
continue; /* skip "." */
assert(strlen(ent->d_name) <= MAXNAMLEN);
sprintf(currname, "%s%s", path, ent->d_name);

View File

@ -22,6 +22,7 @@ runtests() {
rm -rf $DIR || exit 1
fi
mkdir $DIR || exit 1
mkdir $DIR/\( || exit 1
mkdir $DIR/forbidden || exit 1
chmod 0 $DIR/forbidden || exit 1
mkdir $DIR/unreadable || exit 1

View File

@ -174,6 +174,15 @@ class TestDirList(TestHelper):
self.assertEqual(ord("#"), 0x23)
self.assertContains(body, "escape%28this%29name", "12345")
def test_dotdot_first(self):
resp = self.get("/")
status, hdrs, body = parse(resp)
self.assertLess(b'(', b'..') # ( sorts before ..
paren_pos = body.index(b'<a href="%28">(</a>/')
dotdot_pos = body.index(b'<a href="..">..</a>/')
# But we special-case .. to come first
self.assertLess(dotdot_pos, paren_pos)
class TestCases(TestHelper):
pass # these get autogenerated in setUpModule()