| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- /* Guest path mapping and the small file service behind SYS_*_FILE. */
- #include <dirent.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #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;
- }
- int host_fs_init(const char *root)
- {
- char path[HOST_PATH_MAX];
- char fonts[HOST_PATH_MAX];
- char link[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;
- snprintf(fonts, sizeof(fonts), "%s/fonts", host_repo);
- if (access(fonts, R_OK) != 0) {
- char cwd[HOST_PATH_MAX];
- if (getcwd(cwd, sizeof(cwd)))
- snprintf(fonts, sizeof(fonts), "%s/fonts", cwd);
- }
- snprintf(link, sizeof(link), "%s/ramfs/fonts", root);
- if (access(fonts, R_OK) == 0) {
- struct stat st;
- if (lstat(link, &st) != 0)
- symlink(fonts, link);
- }
- 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;
- }
|