/* Guest path mapping and the small file service behind SYS_*_FILE. */ #include #include #include #include #include #include #include #include #include "host.h" #include "fs.h" #include "syscall.h" static int mkdir_p(const char *path) { char tmp[HOST_PATH_MAX]; size_t n; size_t i; n = strlen(path); if (n == 0 || n >= sizeof(tmp)) return -1; memcpy(tmp, path, n + 1); if (tmp[n - 1] == '/') tmp[n - 1] = '\0'; for (i = 1; tmp[i]; i++) { if (tmp[i] != '/') continue; tmp[i] = '\0'; if (mkdir(tmp, 0755) != 0 && errno != EEXIST) return -1; tmp[i] = '/'; } if (mkdir(tmp, 0755) != 0 && errno != EEXIST) return -1; return 0; } static int prefix(const char *path, const char *p) { size_t n = strlen(p); return strncmp(path, p, n) == 0 && (path[n] == '\0' || path[n] == '/'); } int host_guest_to_host(const char *guest, char *out, size_t cap) { if (!guest || !guest[0]) return -1; if (guest[0] != '/') { /* Relative paths resolve against the harness cwd, like a * normal Linux program. */ snprintf(out, cap, "%s", guest); return 0; } if (prefix(guest, "/ramfs") || prefix(guest, "/share") || prefix(guest, "/data")) { int n = snprintf(out, cap, "%s%s", host_root, guest); return (n > 0 && (size_t)n < cap) ? 0 : -1; } /* Any other absolute path is a real host path (testdata pages, * /tmp files) and is passed through unchanged. */ snprintf(out, cap, "%s", guest); return 0; } /* Expose / (or /) as /ramfs/, the way the * pack mounts the UI fonts and icons on the target. */ static void link_repo_dir(const char *root, const char *dir) { char src[HOST_PATH_MAX]; char link[HOST_PATH_MAX]; struct stat st; snprintf(src, sizeof(src), "%s/%s", host_repo, dir); if (access(src, R_OK) != 0) { char cwd[HOST_PATH_MAX]; if (getcwd(cwd, sizeof(cwd))) snprintf(src, sizeof(src), "%s/%s", cwd, dir); } snprintf(link, sizeof(link), "%s/ramfs/%s", root, dir); if (access(src, R_OK) == 0 && lstat(link, &st) != 0) symlink(src, link); } int host_fs_init(const char *root) { char path[HOST_PATH_MAX]; if (mkdir_p(root) != 0) return -1; snprintf(path, sizeof(path), "%s/share", root); if (mkdir_p(path) != 0) return -1; snprintf(path, sizeof(path), "%s/data", root); if (mkdir_p(path) != 0) return -1; snprintf(path, sizeof(path), "%s/ramfs", root); if (mkdir_p(path) != 0) return -1; link_repo_dir(root, "fonts"); link_repo_dir(root, "icons"); /* built by `make icons/adwaita16.icn` */ return 0; } int host_fs_stat(const char *path, unsigned int *size, unsigned int *is_dir) { char host[HOST_PATH_MAX]; struct stat st; if (host_guest_to_host(path, host, sizeof(host)) != 0) return -1; if (stat(host, &st) != 0) return -1; if (size) *size = (unsigned int)st.st_size; if (is_dir) *is_dir = S_ISDIR(st.st_mode) ? 1u : 0u; return 0; } int host_fs_read(const char *path, void *buf, unsigned int max) { char host[HOST_PATH_MAX]; unsigned char *p = (unsigned char *)buf; unsigned int done = 0; int fd; if (host_guest_to_host(path, host, sizeof(host)) != 0) return -1; fd = open(host, O_RDONLY); if (fd < 0) return -1; while (done < max) { ssize_t n = read(fd, p + done, max - done); if (n < 0) { if (errno == EINTR) continue; close(fd); return -1; } if (n == 0) break; done += (unsigned int)n; } close(fd); return (int)done; } int host_fs_write(const char *path, const void *buf, unsigned int len) { char host[HOST_PATH_MAX]; const unsigned char *p = (const unsigned char *)buf; unsigned int done = 0; int fd; if (host_guest_to_host(path, host, sizeof(host)) != 0) return -1; fd = open(host, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) return -1; while (done < len) { ssize_t n = write(fd, p + done, len - done); if (n < 0) { if (errno == EINTR) continue; close(fd); return -1; } if (n == 0) break; done += (unsigned int)n; } close(fd); return done == len ? 0 : -1; } int host_fs_unlink(const char *path) { char host[HOST_PATH_MAX]; if (host_guest_to_host(path, host, sizeof(host)) != 0) return -1; return unlink(host) == 0 ? 0 : -1; } int host_fs_mkdir(const char *path) { char host[HOST_PATH_MAX]; if (host_guest_to_host(path, host, sizeof(host)) != 0) return -1; return mkdir(host, 0755) == 0 ? 0 : -1; } int host_fs_rmdir(const char *path) { char host[HOST_PATH_MAX]; if (host_guest_to_host(path, host, sizeof(host)) != 0) return -1; return rmdir(host) == 0 ? 0 : -1; } int host_fs_list(const char *path, unsigned int index, void *out) { char host[HOST_PATH_MAX]; struct sys_dirent *de = (struct sys_dirent *)out; struct dirent *d; DIR *dir; unsigned int n = 0; if (host_guest_to_host(path, host, sizeof(host)) != 0) return -1; dir = opendir(host); if (!dir) return -1; while ((d = readdir(dir)) != 0) { char full[HOST_PATH_MAX]; struct stat st; if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) continue; if (n++ != index) continue; memset(de, 0, sizeof(*de)); snprintf(de->name, sizeof(de->name), "%s", d->d_name); snprintf(full, sizeof(full), "%s/%s", host, d->d_name); if (stat(full, &st) == 0) { de->size = (unsigned int)st.st_size; de->is_dir = S_ISDIR(st.st_mode) ? 1u : 0u; } closedir(dir); return 1; } closedir(dir); return 0; }