Improve zip read-only filesystem

readdir() will now always yield an inode that's consistent with stat()
on ZipOS and Windows in general. More APIs have been updated to return
the appropriate error code when inappropriately trying to do ops, like
sockets, with a zip file descriptor. The path normalization algorithms
are now fully fleshed out. Some socket APIs have been fixed so they'll
raise EBADF vs. ENOTSOCK appropriately. Lastly seekdir() will now work
properly on NetBSD and FreeBSD (not sure why anyone would even use it)
This commit is contained in:
Justine Tunney 2023-08-16 15:53:06 -07:00
parent dc6c67256f
commit b76b2be2d0
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
47 changed files with 644 additions and 269 deletions

View file

@ -136,3 +136,10 @@ TEST(critbit0, duplicate) {
ASSERT_FALSE(critbit0_insert(&tree, "hi"));
critbit0_clear(&tree);
}
TEST(critbit0, manual_clear) {
struct critbit0 tree = {0};
ASSERT_TRUE(critbit0_insert(&tree, "hi"));
ASSERT_TRUE(critbit0_delete(&tree, "hi"));
ASSERT_EQ(NULL, tree.root);
}

View file

@ -39,7 +39,7 @@ int Worker(void *arg, int tid) {
int i, fd;
char *data;
for (i = 0; i < 20; ++i) {
ASSERT_NE(-1, (fd = open("/zip/libc/testlib/hyperion.txt", O_RDONLY)));
ASSERT_NE(-1, (fd = open("/zip//./libc/testlib//hyperion.txt", O_RDONLY)));
data = malloc(kHyperionSize);
ASSERT_EQ(kHyperionSize, read(fd, data, kHyperionSize));
ASSERT_EQ(0, memcmp(data, kHyperion, kHyperionSize));
@ -57,30 +57,13 @@ TEST(zipos, test) {
__print_maps();
}
TEST(zipos, normpath) {
{
char s[] = "";
__zipos_normpath(s);
ASSERT_STREQ("", s);
}
{
char s[] = "usr/";
__zipos_normpath(s);
ASSERT_STREQ("usr", s);
}
{
char s[] = "usr/./";
__zipos_normpath(s);
ASSERT_STREQ("usr", s);
}
TEST(zipos, erofs) {
ASSERT_SYS(EROFS, -1, creat("/zip/foo.txt", 0644));
}
#if 0
TEST(zipos_O_DIRECTORY, blocksOpeningOfNormalFiles) {
ASSERT_SYS(ENOTDIR, -1,
open("/zip/libc/testlib/hyperion.txt", O_RDONLY | O_DIRECTORY));
TEST(zipos, enoent) {
ASSERT_SYS(ENOENT, -1, open("/zip/foo.txt", O_RDONLY));
}
#endif
TEST(zipos, readPastEof) {
char buf[512];
@ -89,6 +72,13 @@ TEST(zipos, readPastEof) {
EXPECT_SYS(0, 0, pread(3, buf, 512, INT64_MAX));
EXPECT_SYS(EINVAL, -1, lseek(3, UINT64_MAX, SEEK_SET));
EXPECT_SYS(0, INT64_MAX, lseek(3, INT64_MAX, SEEK_SET));
EXPECT_SYS(EBADF, -1, write(3, buf, 512));
EXPECT_SYS(EBADF, -1, pwrite(3, buf, 512, 0));
EXPECT_SYS(0, 0, read(3, buf, 512));
EXPECT_SYS(0, 0, close(3));
}
TEST(zipos_O_DIRECTORY, blocksOpeningOfNormalFiles) {
ASSERT_SYS(ENOTDIR, -1,
open("/zip/libc/testlib/hyperion.txt", O_RDONLY | O_DIRECTORY));
}

View file

@ -21,10 +21,17 @@
#include "libc/calls/struct/stat.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/mem/critbit0.h"
#include "libc/mem/gc.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/append.h"
#include "libc/stdio/ftw.h"
#include "libc/stdio/rand.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/dt.h"
@ -37,6 +44,8 @@
__static_yoink("zipos");
__static_yoink("usr/share/zoneinfo/");
__static_yoink("usr/share/zoneinfo/New_York");
__static_yoink("libc/testlib/hyperion.txt");
__static_yoink("libc/testlib/moby.txt");
char testlib_enable_tmp_setup_teardown;
@ -64,55 +73,6 @@ TEST(opendir, enotdir) {
ASSERT_SYS(ENOTDIR, NULL, opendir("yo/there"));
}
TEST(opendir, zipTest_fake) {
ASSERT_NE(NULL, (dir = opendir("/zip")));
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ(".", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("..", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("echo.com", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("usr", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ(".cosmo", ent->d_name);
EXPECT_EQ(NULL, (ent = readdir(dir)));
EXPECT_EQ(0, closedir(dir));
ASSERT_NE(NULL, (dir = opendir("/zip/")));
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ(".", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("..", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("echo.com", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("usr", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ(".cosmo", ent->d_name);
EXPECT_EQ(NULL, (ent = readdir(dir)));
EXPECT_EQ(0, closedir(dir));
ASSERT_NE(NULL, (dir = opendir("/zip/usr")));
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ(".", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("..", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("share", ent->d_name);
EXPECT_EQ(NULL, (ent = readdir(dir)));
EXPECT_EQ(0, closedir(dir));
ASSERT_NE(NULL, (dir = opendir("/zip/usr/")));
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ(".", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("..", ent->d_name);
EXPECT_NE(NULL, (ent = readdir(dir)));
EXPECT_STREQ("share", ent->d_name);
EXPECT_EQ(NULL, (ent = readdir(dir)));
EXPECT_EQ(0, closedir(dir));
EXPECT_EQ(NULL, (dir = opendir("/zip/us")));
EXPECT_EQ(NULL, (dir = opendir("/zip/us/")));
}
TEST(opendir, openSyntheticDirEntry) {
struct stat st;
ASSERT_SYS(0, 3, open("/zip", O_RDONLY | O_DIRECTORY));
@ -227,8 +187,7 @@ TEST(dirstream, zipTest_notDir) {
ASSERT_EQ(ENOTDIR, errno);
}
TEST(dirstream, seek) {
if (IsNetbsd()) return; // omg
TEST(dirstream, ino) {
ASSERT_SYS(0, 0, mkdir("boop", 0755));
EXPECT_SYS(0, 0, touch("boop/a", 0644));
EXPECT_SYS(0, 0, touch("boop/b", 0644));
@ -253,8 +212,7 @@ TEST(dirstream, seek) {
ASSERT_SYS(0, 0, closedir(dir));
}
TEST(dirstream, ino) {
if (IsNetbsd()) return; // omg
TEST(dirstream, seek) {
ASSERT_SYS(0, 0, mkdir("boop", 0755));
EXPECT_SYS(0, 0, touch("boop/a", 0644));
EXPECT_SYS(0, 0, touch("boop/b", 0644));
@ -279,6 +237,80 @@ TEST(dirstream, ino) {
ASSERT_SYS(0, 0, closedir(dir));
}
TEST(dirstream, seeky) {
char name[256];
char path[512];
struct stat golden;
int i, j, n = 1000;
int goodindex = 500;
struct critbit0 tree = {0};
ASSERT_SYS(0, 0, mkdir("boop", 0755));
ASSERT_EQ(1, critbit0_insert(&tree, "."));
ASSERT_EQ(1, critbit0_insert(&tree, ".."));
for (i = 0; i < n; ++i) {
for (j = 0; j < 255; ++j) {
name[j] = '0' + rand() % 10;
}
// TODO(jart): why does Windows croak with 255
name[100] = 0;
strcpy(path, "boop/");
strcat(path, name);
path[255] = 0;
*FormatInt32(path + 5, i) = '-';
ASSERT_EQ(1, critbit0_insert(&tree, path + 5));
ASSERT_SYS(0, 0, touch(path, 0644));
if (i == goodindex) {
ASSERT_SYS(0, 0, stat(path, &golden));
}
}
// do a full pass
{
ASSERT_NE(NULL, (dir = opendir("boop")));
long tell = -1;
long prev = telldir(dir);
while ((ent = readdir(dir))) {
if (atoi(ent->d_name) == goodindex) {
ASSERT_EQ(golden.st_ino, ent->d_ino);
tell = prev;
}
prev = telldir(dir);
ASSERT_EQ(1, critbit0_delete(&tree, ent->d_name));
}
ASSERT_EQ(NULL, tree.root); // all entries were found
ASSERT_NE(-1, tell);
seekdir(dir, tell);
ASSERT_NE(NULL, (ent = readdir(dir)));
ASSERT_EQ(goodindex, atoi(ent->d_name));
ASSERT_EQ(golden.st_ino, ent->d_ino);
ASSERT_SYS(0, 0, closedir(dir));
}
// do a partial pass, and seek midway
{
ASSERT_NE(NULL, (dir = opendir("boop")));
int abort = 700;
long tell = -1;
bool foundit = false;
long prev = telldir(dir);
while ((ent = readdir(dir))) {
if (atoi(ent->d_name) == goodindex) {
ASSERT_EQ(golden.st_ino, ent->d_ino);
tell = prev;
foundit = true;
}
prev = telldir(dir);
if (--abort <= 0 && foundit) {
break;
}
}
ASSERT_NE(-1, tell);
seekdir(dir, tell);
ASSERT_NE(NULL, (ent = readdir(dir)));
ASSERT_EQ(goodindex, atoi(ent->d_name));
ASSERT_EQ(golden.st_ino, ent->d_ino);
ASSERT_SYS(0, 0, closedir(dir));
}
}
TEST(dirstream, dots) {
bool got_dot = false;
bool got_dot_dot = false;
@ -321,3 +353,79 @@ TEST(dirstream_zipos, inoFile_isConsistentWithStat) {
}
ASSERT_SYS(0, 0, closedir(dir));
}
static const char *DescribeDt(int dt) {
static char buf[12];
switch (dt) {
case DT_UNKNOWN:
return "DT_UNKNOWN";
case DT_FIFO:
return "DT_FIFO";
case DT_CHR:
return "DT_CHR";
case DT_DIR:
return "DT_DIR";
case DT_BLK:
return "DT_BLK";
case DT_REG:
return "DT_REG";
case DT_LNK:
return "DT_LNK";
case DT_SOCK:
return "DT_SOCK";
default:
FormatInt32(buf, dt);
return buf;
}
}
static const char *DescribeFtw(int dt) {
static char buf[12];
switch (dt) {
case FTW_F:
return "FTW_F";
case FTW_D:
return "FTW_D";
case FTW_DNR:
return "FTW_DNR";
case FTW_NS:
return "FTW_NS";
case FTW_SL:
return "FTW_SL";
case FTW_DP:
return "FTW_DP";
case FTW_SLN:
return "FTW_SLN";
default:
FormatInt32(buf, dt);
return buf;
}
}
char *b;
static int walk(const char *fpath, //
const struct stat *st, //
int typeflag, //
struct FTW *ftwbuf) { //
appendf(&b, "%-6s %s\n", DescribeFtw(typeflag), fpath);
return 0;
}
TEST(dirstream, walk) {
ASSERT_SYS(0, 0, nftw("/zip", walk, 128, FTW_PHYS | FTW_DEPTH));
ASSERT_STREQ("FTW_F /zip/echo.com\n"
"FTW_F /zip/libc/testlib/hyperion.txt\n"
"FTW_F /zip/libc/testlib/moby.txt\n"
"FTW_DP /zip/libc/testlib\n"
"FTW_DP /zip/libc\n"
"FTW_F /zip/usr/share/zoneinfo/New_York\n"
"FTW_DP /zip/usr/share/zoneinfo\n"
"FTW_DP /zip/usr/share\n"
"FTW_DP /zip/usr\n"
"FTW_F /zip/.cosmo\n"
"FTW_DP /zip\n",
b);
free(b);
b = 0;
}

View file

@ -17,8 +17,13 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/dirent.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/dt.h"
#include "libc/testlib/testlib.h"
@ -68,3 +73,103 @@ TEST(dirstream, hasDirectoryEntry) {
ASSERT_SYS(0, 0, closedir(dir));
EXPECT_TRUE(gotsome);
}
TEST(__zipos_normpath, emptyBuf_wontNulTerminate) {
__zipos_normpath(0, "hello", 0);
}
TEST(__zipos_normpath, overflows_willNulTerminate) {
char buf[2];
ASSERT_EQ(2, __zipos_normpath(buf, "hello", 2));
ASSERT_STREQ("h", buf);
}
TEST(__zipos_normpath, vectors) {
static const char V[][2][128] = {
{"", ""},
{"/..", ""},
{"/../", ""},
{".", ""},
{"./", ""},
{"..", ""},
{"../", ""},
{"../abc/def", "abc/def"},
{"../abc/def/..", "abc"},
{"../abc/././././def/..", "abc"},
{"////../abc/def", "abc/def"},
{"/../def", "def"},
{"../def", "def"},
{"/abc////../def", "def"},
{"abc/../def/ghi", "def/ghi"},
{"/abc/def/../ghi", "abc/ghi"},
{"/abc/..abc////../def", "abc/def"},
{"/abc/..abc/../def", "abc/def"},
{"/abc/..abc/def", "abc/..abc/def"},
{"abc/../def", "def"},
{"abc/../../def", "def"},
{"abc/../../../def", "def"},
{"././", ""},
{"abc/..", ""},
{"abc/../", ""},
{"abc/../..", ""},
{"abc/../../", ""},
{"a/..", ""},
{"a/../", ""},
{"a/../..", ""},
{"a/../../", ""},
{"../../a", "a"},
{"../../../a", "a"},
{"../a../../a", "a"},
{"cccc/abc////..////.//../", ""},
{"aaaa/cccc/abc////..////.//../", "aaaa"},
{"..//////.///..////..////.//////abc////.////..////def//abc/..", "def"},
{"////////////..//////.///..////..////.//////abc////.////..////def//abc/"
"..",
"def"},
};
int fails = 0;
// test non-overlapping arguments
// duplicate input string for better asan checking
for (int i = 0; i < ARRAYLEN(V); ++i) {
char tmp[128];
__zipos_normpath(tmp, gc(strdup(V[i][0])), sizeof(tmp));
if (strcmp(tmp, V[i][1])) {
if (++fails < 8) {
fprintf(stderr,
"\n%s: zipos path normalization test failed\n"
"\tinput = %`'s\n"
"\t want = %`'s\n"
"\t got = %`'s\n"
"\t i = %d\n",
program_invocation_name, V[i][0], V[i][1], tmp, i);
}
}
}
// test overlapping arguments
if (!fails) {
for (int i = 0; i < ARRAYLEN(V); ++i) {
char tmp[128];
strcpy(tmp, V[i][0]);
__zipos_normpath(tmp, tmp, sizeof(tmp));
if (strcmp(tmp, V[i][1])) {
if (++fails < 8) {
fprintf(stderr,
"\n%s: zipos path normalization breaks w/ overlapping args\n"
"\tinput = %`'s\n"
"\t want = %`'s\n"
"\t got = %`'s\n"
"\t i = %d\n",
program_invocation_name, V[i][0], V[i][1], tmp, i);
}
}
}
}
if (fails) {
fprintf(stderr, "\n%d / %zd zipos path norm tests failed\n", fails,
ARRAYLEN(V));
exit(1);
}
}