fs.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Guest path mapping and the small file service behind SYS_*_FILE. */
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include "host.h"
  11. #include "fs.h"
  12. #include "syscall.h"
  13. static int mkdir_p(const char *path)
  14. {
  15. char tmp[HOST_PATH_MAX];
  16. size_t n;
  17. size_t i;
  18. n = strlen(path);
  19. if (n == 0 || n >= sizeof(tmp))
  20. return -1;
  21. memcpy(tmp, path, n + 1);
  22. if (tmp[n - 1] == '/')
  23. tmp[n - 1] = '\0';
  24. for (i = 1; tmp[i]; i++) {
  25. if (tmp[i] != '/')
  26. continue;
  27. tmp[i] = '\0';
  28. if (mkdir(tmp, 0755) != 0 && errno != EEXIST)
  29. return -1;
  30. tmp[i] = '/';
  31. }
  32. if (mkdir(tmp, 0755) != 0 && errno != EEXIST)
  33. return -1;
  34. return 0;
  35. }
  36. static int prefix(const char *path, const char *p)
  37. {
  38. size_t n = strlen(p);
  39. return strncmp(path, p, n) == 0 &&
  40. (path[n] == '\0' || path[n] == '/');
  41. }
  42. int host_guest_to_host(const char *guest, char *out, size_t cap)
  43. {
  44. if (!guest || !guest[0])
  45. return -1;
  46. if (guest[0] != '/') {
  47. /* Relative paths resolve against the harness cwd, like a
  48. * normal Linux program. */
  49. snprintf(out, cap, "%s", guest);
  50. return 0;
  51. }
  52. if (prefix(guest, "/ramfs") || prefix(guest, "/share") ||
  53. prefix(guest, "/data")) {
  54. int n = snprintf(out, cap, "%s%s", host_root, guest);
  55. return (n > 0 && (size_t)n < cap) ? 0 : -1;
  56. }
  57. /* Any other absolute path is a real host path (testdata pages,
  58. * /tmp files) and is passed through unchanged. */
  59. snprintf(out, cap, "%s", guest);
  60. return 0;
  61. }
  62. /* Expose <repo>/<dir> (or <cwd>/<dir>) as <root>/ramfs/<dir>, the way the
  63. * pack mounts the UI fonts and icons on the target. */
  64. static void link_repo_dir(const char *root, const char *dir)
  65. {
  66. char src[HOST_PATH_MAX];
  67. char link[HOST_PATH_MAX];
  68. struct stat st;
  69. snprintf(src, sizeof(src), "%s/%s", host_repo, dir);
  70. if (access(src, R_OK) != 0) {
  71. char cwd[HOST_PATH_MAX];
  72. if (getcwd(cwd, sizeof(cwd)))
  73. snprintf(src, sizeof(src), "%s/%s", cwd, dir);
  74. }
  75. snprintf(link, sizeof(link), "%s/ramfs/%s", root, dir);
  76. if (access(src, R_OK) == 0 && lstat(link, &st) != 0)
  77. symlink(src, link);
  78. }
  79. int host_fs_init(const char *root)
  80. {
  81. char path[HOST_PATH_MAX];
  82. if (mkdir_p(root) != 0)
  83. return -1;
  84. snprintf(path, sizeof(path), "%s/share", root);
  85. if (mkdir_p(path) != 0)
  86. return -1;
  87. snprintf(path, sizeof(path), "%s/data", root);
  88. if (mkdir_p(path) != 0)
  89. return -1;
  90. snprintf(path, sizeof(path), "%s/ramfs", root);
  91. if (mkdir_p(path) != 0)
  92. return -1;
  93. link_repo_dir(root, "fonts");
  94. link_repo_dir(root, "icons"); /* built by `make icons/adwaita16.icn` */
  95. return 0;
  96. }
  97. int host_fs_stat(const char *path, unsigned int *size, unsigned int *is_dir)
  98. {
  99. char host[HOST_PATH_MAX];
  100. struct stat st;
  101. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  102. return -1;
  103. if (stat(host, &st) != 0)
  104. return -1;
  105. if (size)
  106. *size = (unsigned int)st.st_size;
  107. if (is_dir)
  108. *is_dir = S_ISDIR(st.st_mode) ? 1u : 0u;
  109. return 0;
  110. }
  111. int host_fs_read(const char *path, void *buf, unsigned int max)
  112. {
  113. char host[HOST_PATH_MAX];
  114. unsigned char *p = (unsigned char *)buf;
  115. unsigned int done = 0;
  116. int fd;
  117. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  118. return -1;
  119. fd = open(host, O_RDONLY);
  120. if (fd < 0)
  121. return -1;
  122. while (done < max) {
  123. ssize_t n = read(fd, p + done, max - done);
  124. if (n < 0) {
  125. if (errno == EINTR)
  126. continue;
  127. close(fd);
  128. return -1;
  129. }
  130. if (n == 0)
  131. break;
  132. done += (unsigned int)n;
  133. }
  134. close(fd);
  135. return (int)done;
  136. }
  137. int host_fs_write(const char *path, const void *buf, unsigned int len)
  138. {
  139. char host[HOST_PATH_MAX];
  140. const unsigned char *p = (const unsigned char *)buf;
  141. unsigned int done = 0;
  142. int fd;
  143. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  144. return -1;
  145. fd = open(host, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  146. if (fd < 0)
  147. return -1;
  148. while (done < len) {
  149. ssize_t n = write(fd, p + done, len - done);
  150. if (n < 0) {
  151. if (errno == EINTR)
  152. continue;
  153. close(fd);
  154. return -1;
  155. }
  156. if (n == 0)
  157. break;
  158. done += (unsigned int)n;
  159. }
  160. close(fd);
  161. return done == len ? 0 : -1;
  162. }
  163. int host_fs_unlink(const char *path)
  164. {
  165. char host[HOST_PATH_MAX];
  166. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  167. return -1;
  168. return unlink(host) == 0 ? 0 : -1;
  169. }
  170. int host_fs_mkdir(const char *path)
  171. {
  172. char host[HOST_PATH_MAX];
  173. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  174. return -1;
  175. return mkdir(host, 0755) == 0 ? 0 : -1;
  176. }
  177. int host_fs_rmdir(const char *path)
  178. {
  179. char host[HOST_PATH_MAX];
  180. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  181. return -1;
  182. return rmdir(host) == 0 ? 0 : -1;
  183. }
  184. int host_fs_list(const char *path, unsigned int index, void *out)
  185. {
  186. char host[HOST_PATH_MAX];
  187. struct sys_dirent *de = (struct sys_dirent *)out;
  188. struct dirent *d;
  189. DIR *dir;
  190. unsigned int n = 0;
  191. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  192. return -1;
  193. dir = opendir(host);
  194. if (!dir)
  195. return -1;
  196. while ((d = readdir(dir)) != 0) {
  197. char full[HOST_PATH_MAX];
  198. struct stat st;
  199. if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  200. continue;
  201. if (n++ != index)
  202. continue;
  203. memset(de, 0, sizeof(*de));
  204. snprintf(de->name, sizeof(de->name), "%s", d->d_name);
  205. snprintf(full, sizeof(full), "%s/%s", host, d->d_name);
  206. if (stat(full, &st) == 0) {
  207. de->size = (unsigned int)st.st_size;
  208. de->is_dir = S_ISDIR(st.st_mode) ? 1u : 0u;
  209. }
  210. closedir(dir);
  211. return 1;
  212. }
  213. closedir(dir);
  214. return 0;
  215. }