Large diffs are not rendered by default.

@@ -8,6 +8,8 @@ const struct fs_ops *filesystems[] = {
&realfs,
&procfs,
&devptsfs,
[IOS_FILESYSTEM_ID] = NULL,
[IOS_UNSAFE_FILESYSTEM_ID] = NULL
};

struct mount *mount_find(char *path) {
@@ -119,7 +121,7 @@ dword_t sys_mount(addr_t source_addr, addr_t point_addr, addr_t type_addr, dword

const struct fs_ops *fs = NULL;
for (size_t i = 0; i < sizeof(filesystems)/sizeof(filesystems[0]); i++) {
if (strcmp(filesystems[i]->name, type) == 0) {
if (filesystems[i] && (strcmp(filesystems[i]->name, type) == 0)) {
fs = filesystems[i];
break;
}
@@ -73,17 +73,21 @@ static int open_flags_fake_from_real(int flags) {
return fake_flags;
}

static struct fd *realfs_open(struct mount *mount, const char *path, int flags, int mode) {
struct fd *realfs_open_with_fdops(struct mount *mount, const char *path, int flags, int mode, const struct fd_ops *fdops) {

This comment has been minimized.

@tbodt

tbodt Dec 16, 2019
Member

Instead of a new function here, how about setting fd->ops after calling realfs_open?

int real_flags = open_flags_real_from_fake(flags);
int fd_no = openat(mount->root_fd, fix_path(path), real_flags, mode);
if (fd_no < 0)
return ERR_PTR(errno_map());
struct fd *fd = fd_create(&realfs_fdops);
struct fd *fd = fd_create(fdops);
fd->real_fd = fd_no;
fd->dir = NULL;
return fd;
}

struct fd *realfs_open(struct mount *mount, const char *path, int flags, int mode) {
return realfs_open_with_fdops(mount, path, flags, mode, &realfs_fdops);
}

int realfs_close(struct fd *fd) {
if (fd->dir != NULL)
closedir(fd->dir);
@@ -118,15 +122,15 @@ static void copy_stat(struct statbuf *fake_stat, struct stat *real_stat) {
#undef TIMESPEC
}

static int realfs_stat(struct mount *mount, const char *path, struct statbuf *fake_stat, bool follow_links) {
int realfs_stat(struct mount *mount, const char *path, struct statbuf *fake_stat, bool follow_links) {
struct stat real_stat;
if (fstatat(mount->root_fd, fix_path(path), &real_stat, follow_links ? 0 : AT_SYMLINK_NOFOLLOW) < 0)
return errno_map();
copy_stat(fake_stat, &real_stat);
return 0;
}

static int realfs_fstat(struct fd *fd, struct statbuf *fake_stat) {
int realfs_fstat(struct fd *fd, struct statbuf *fake_stat) {
struct stat real_stat;
if (fstat(fd->real_fd, &real_stat) < 0)
return errno_map();
@@ -148,7 +152,7 @@ ssize_t realfs_write(struct fd *fd, const void *buf, size_t bufsize) {
return res;
}

static void realfs_opendir(struct fd *fd) {
void realfs_opendir(struct fd *fd) {
if (fd->dir == NULL) {
int dirfd = dup(fd->real_fd);
fd->dir = fdopendir(dirfd);
@@ -248,7 +252,7 @@ int realfs_mmap(struct fd *fd, struct mem *mem, page_t start, pages_t pages, off
return pt_map(mem, start, pages, memory, correction, prot);
}

static ssize_t realfs_readlink(struct mount *mount, const char *path, char *buf, size_t bufsize) {
ssize_t realfs_readlink(struct mount *mount, const char *path, char *buf, size_t bufsize) {
ssize_t size = readlinkat(mount->root_fd, fix_path(path), buf, bufsize);
if (size < 0)
return errno_map();
@@ -266,42 +270,42 @@ int realfs_getpath(struct fd *fd, char *buf) {
return 0;
}

static int realfs_link(struct mount *mount, const char *src, const char *dst) {
int realfs_link(struct mount *mount, const char *src, const char *dst) {
int res = linkat(mount->root_fd, fix_path(src), mount->root_fd, fix_path(dst), 0);
if (res < 0)
return errno_map();
return res;
}

static int realfs_unlink(struct mount *mount, const char *path) {
int realfs_unlink(struct mount *mount, const char *path) {
int res = unlinkat(mount->root_fd, fix_path(path), 0);
if (res < 0)
return errno_map();
return res;
}

static int realfs_rmdir(struct mount *mount, const char *path) {
int realfs_rmdir(struct mount *mount, const char *path) {
int err = unlinkat(mount->root_fd, fix_path(path), AT_REMOVEDIR);
if (err < 0)
return errno_map();
return 0;
}

static int realfs_rename(struct mount *mount, const char *src, const char *dst) {
int realfs_rename(struct mount *mount, const char *src, const char *dst) {
int err = renameat(mount->root_fd, fix_path(src), mount->root_fd, fix_path(dst));
if (err < 0)
return errno_map();
return err;
}

static int realfs_symlink(struct mount *mount, const char *target, const char *link) {
int realfs_symlink(struct mount *mount, const char *target, const char *link) {
int err = symlinkat(target, mount->root_fd, link);
if (err < 0)
return errno_map();
return err;
}

static int realfs_mknod(struct mount *mount, const char *path, mode_t_ mode, dev_t_ UNUSED(dev)) {
int realfs_mknod(struct mount *mount, const char *path, mode_t_ mode, dev_t_ UNUSED(dev)) {
int err;
if (S_ISFIFO(mode)) {
lock_fchdir(mount->root_fd);
@@ -330,7 +334,7 @@ int realfs_truncate(struct mount *mount, const char *path, off_t_ size) {
return err;
}

static int realfs_setattr(struct mount *mount, const char *path, struct attr attr) {
int realfs_setattr(struct mount *mount, const char *path, struct attr attr) {
path = fix_path(path);
int root = mount->root_fd;
int err;
@@ -354,7 +358,7 @@ static int realfs_setattr(struct mount *mount, const char *path, struct attr att
return err;
}

static int realfs_fsetattr(struct fd *fd, struct attr attr) {
int realfs_fsetattr(struct fd *fd, struct attr attr) {
int real_fd = fd->real_fd;
int err;
switch (attr.type) {
@@ -384,7 +388,7 @@ int realfs_utime(struct mount *mount, const char *path, struct timespec atime, s
return 0;
}

static int realfs_mkdir(struct mount *mount, const char *path, mode_t_ mode) {
int realfs_mkdir(struct mount *mount, const char *path, mode_t_ mode) {
int err = mkdirat(mount->root_fd, fix_path(path), mode);
if (err < 0)
return errno_map();
@@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
40616E3C236642B6008CDF19 /* mount.c in Sources */ = {isa = PBXBuildFile; fileRef = 40616E3B236642B5008CDF19 /* mount.c */; };
408A263A236440F8008A4E81 /* iOSFS.m in Sources */ = {isa = PBXBuildFile; fileRef = 408A2639236440F8008A4E81 /* iOSFS.m */; };
650B337422EA235C00B4C03E /* PasteboardDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 650B337322EA235C00B4C03E /* PasteboardDevice.m */; };
8632A7BF219A59FB00F02325 /* UserPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 8632A7BE219A59FB00F02325 /* UserPreferences.m */; };
9A28E4EA219A8B670073D200 /* AboutAppearanceViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A28E4E9219A8B670073D200 /* AboutAppearanceViewController.m */; };
@@ -96,6 +98,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
40616E3B236642B5008CDF19 /* mount.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mount.c; sourceTree = "<group>"; };
408A2639236440F8008A4E81 /* iOSFS.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = iOSFS.m; sourceTree = "<group>"; };
408A263B23644102008A4E81 /* iOSFS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = iOSFS.h; sourceTree = "<group>"; };
650B335A22E9E46A00B4C03E /* mem.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mem.c; sourceTree = "<group>"; tabWidth = 4; };
650B335B22E9E46A00B4C03E /* mem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mem.h; sourceTree = "<group>"; tabWidth = 4; };
650B335D22E9EF9B00B4C03E /* proc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = proc.c; sourceTree = "<group>"; };
@@ -431,6 +436,8 @@
BBCC9D952365430800424C83 /* SceneDelegate.m */,
BB792B561F96D90D00FFB7A4 /* TerminalViewController.h */,
BB792B571F96D90D00FFB7A4 /* TerminalViewController.m */,
408A263B23644102008A4E81 /* iOSFS.h */,
408A2639236440F8008A4E81 /* iOSFS.m */,

This comment has been minimized.

@tbodt

tbodt Dec 16, 2019
Member

Could you put these under Devices?

BBFB557D21587B2B00DFE6DE /* Bar */,
BB78AB291FAD22440013E782 /* TerminalView.h */,
BB78AB2A1FAD22440013E782 /* TerminalView.m */,
@@ -548,6 +555,7 @@
650B335A22E9E46A00B4C03E /* mem.c */,
650B335B22E9E46A00B4C03E /* mem.h */,
BB101B512364EAF0000A93BC /* mount.c */,
40616E3B236642B5008CDF19 /* mount.c */,

This comment has been minimized.

@tbodt

tbodt Dec 16, 2019
Member

Seems to be a duplicate

BB7D93742087C2890008DA78 /* path.c */,
BB7D93752087C2890008DA78 /* path.h */,
BB7D93762087C2890008DA78 /* pipe.c */,
@@ -977,6 +985,7 @@
9A28E4EA219A8B670073D200 /* AboutAppearanceViewController.m in Sources */,
BB1D9D93234A8FE100F364E8 /* AboutNavigationController.m in Sources */,
BB235534235D488500139E00 /* LocationDevice.m in Sources */,
40616E3C236642B6008CDF19 /* mount.c in Sources */,
BB792B581F96D90D00FFB7A4 /* TerminalViewController.m in Sources */,
BB78AB2B1FAD22440013E782 /* TerminalView.m in Sources */,
BB23F58D231E1D1400585522 /* ScrollbarView.m in Sources */,
@@ -993,6 +1002,7 @@
BBFB5579215876CD00DFE6DE /* AboutViewController.m in Sources */,
650B337422EA235C00B4C03E /* PasteboardDevice.m in Sources */,
BBFB557121586C4800DFE6DE /* UIViewController+Back.m in Sources */,
408A263A236440F8008A4E81 /* iOSFS.m in Sources */,
BBFB558021587B6800DFE6DE /* ArrowBarButton.m in Sources */,
BB792B551F96D90D00FFB7A4 /* AppDelegate.m in Sources */,
);
@@ -108,6 +108,7 @@ void mount_release(struct mount *mount);

// must hold mounts_lock while calling these, or traversing mounts
int do_mount(const struct fs_ops *fs, const char *source, const char *point, int flags);
int do_mount_with_data(const struct fs_ops *fs, const char *source, const char *point, int flags, void *data);

This comment has been minimized.

@tbodt

tbodt Dec 16, 2019
Member

Unused, please remove

int do_umount(const char *point);
int mount_remove(struct mount *mount);
extern struct list mounts;
@@ -174,6 +175,25 @@ const char *fix_path(const char *path); // TODO reconsider
// real fs
extern const struct fd_ops realfs_fdops;

struct fd *realfs_open_with_fdops(struct mount *mount, const char *path, int flags, int mode, const struct fd_ops *fdops);

This comment has been minimized.

@tbodt

tbodt Dec 16, 2019
Member

At this point there should be a separate fs/real.h header.

struct fd *realfs_open(struct mount *mount, const char *path, int flags, int mode);

ssize_t realfs_readlink(struct mount *mount, const char *path, char *buf, size_t bufsize);
int realfs_link(struct mount *mount, const char *src, const char *dst);
int realfs_unlink(struct mount *mount, const char *path);
int realfs_rmdir(struct mount *mount, const char *path);
int realfs_rename(struct mount *mount, const char *src, const char *dst);
int realfs_symlink(struct mount *mount, const char *target, const char *link);
int realfs_mknod(struct mount *mount, const char *path, mode_t_ mode, dev_t_ UNUSED(dev));

int realfs_stat(struct mount *mount, const char *path, struct statbuf *fake_stat, bool follow_links);
int realfs_statfs(struct mount *mount, struct statfsbuf *stat);
int realfs_fstat(struct fd *fd, struct statbuf *fake_stat);
int realfs_setattr(struct mount *mount, const char *path, struct attr attr);
int realfs_fsetattr(struct fd *fd, struct attr attr);

int realfs_mkdir(struct mount *mount, const char *path, mode_t_ mode);

int realfs_truncate(struct mount *mount, const char *path, off_t_ size);
int realfs_utime(struct mount *mount, const char *path, struct timespec atime, struct timespec mtime);

@@ -182,7 +202,16 @@ int realfs_flock(struct fd *fd, int operation);
int realfs_getpath(struct fd *fd, char *buf);
ssize_t realfs_read(struct fd *fd, void *buf, size_t bufsize);
ssize_t realfs_write(struct fd *fd, const void *buf, size_t bufsize);

int realfs_readdir(struct fd *fd, struct dir_entry *entry);
unsigned long realfs_telldir(struct fd *fd);
void realfs_seekdir(struct fd *fd, unsigned long ptr);

off_t realfs_lseek(struct fd *fd, off_t offset, int whence);

int realfs_poll(struct fd *fd);
int realfs_mmap(struct fd *fd, struct mem *mem, page_t start, pages_t pages, off_t offset, int prot, int flags);
int realfs_fsync(struct fd *fd);
int realfs_getflags(struct fd *fd);
int realfs_setflags(struct fd *fd, dword_t arg);
int realfs_close(struct fd *fd);
@@ -196,4 +225,8 @@ extern const struct fs_ops procfs;
extern const struct fs_ops fakefs;
extern const struct fs_ops devptsfs;

extern const struct fs_ops *filesystems[];

This comment has been minimized.

@tbodt

tbodt Dec 16, 2019
Member

I think a function filesystem_register that appends a filesystem to the array makes more sense than static filesystem ids.

#define IOS_FILESYSTEM_ID 3
#define IOS_UNSAFE_FILESYSTEM_ID 4

#endif