fs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. int host_fs_init(const char *root)
  63. {
  64. char path[HOST_PATH_MAX];
  65. char fonts[HOST_PATH_MAX];
  66. char link[HOST_PATH_MAX];
  67. if (mkdir_p(root) != 0)
  68. return -1;
  69. snprintf(path, sizeof(path), "%s/share", root);
  70. if (mkdir_p(path) != 0)
  71. return -1;
  72. snprintf(path, sizeof(path), "%s/data", root);
  73. if (mkdir_p(path) != 0)
  74. return -1;
  75. snprintf(path, sizeof(path), "%s/ramfs", root);
  76. if (mkdir_p(path) != 0)
  77. return -1;
  78. snprintf(fonts, sizeof(fonts), "%s/fonts", host_repo);
  79. if (access(fonts, R_OK) != 0) {
  80. char cwd[HOST_PATH_MAX];
  81. if (getcwd(cwd, sizeof(cwd)))
  82. snprintf(fonts, sizeof(fonts), "%s/fonts", cwd);
  83. }
  84. snprintf(link, sizeof(link), "%s/ramfs/fonts", root);
  85. if (access(fonts, R_OK) == 0) {
  86. struct stat st;
  87. if (lstat(link, &st) != 0)
  88. symlink(fonts, link);
  89. }
  90. return 0;
  91. }
  92. int host_fs_stat(const char *path, unsigned int *size, unsigned int *is_dir)
  93. {
  94. char host[HOST_PATH_MAX];
  95. struct stat st;
  96. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  97. return -1;
  98. if (stat(host, &st) != 0)
  99. return -1;
  100. if (size)
  101. *size = (unsigned int)st.st_size;
  102. if (is_dir)
  103. *is_dir = S_ISDIR(st.st_mode) ? 1u : 0u;
  104. return 0;
  105. }
  106. int host_fs_read(const char *path, void *buf, unsigned int max)
  107. {
  108. char host[HOST_PATH_MAX];
  109. unsigned char *p = (unsigned char *)buf;
  110. unsigned int done = 0;
  111. int fd;
  112. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  113. return -1;
  114. fd = open(host, O_RDONLY);
  115. if (fd < 0)
  116. return -1;
  117. while (done < max) {
  118. ssize_t n = read(fd, p + done, max - done);
  119. if (n < 0) {
  120. if (errno == EINTR)
  121. continue;
  122. close(fd);
  123. return -1;
  124. }
  125. if (n == 0)
  126. break;
  127. done += (unsigned int)n;
  128. }
  129. close(fd);
  130. return (int)done;
  131. }
  132. int host_fs_write(const char *path, const void *buf, unsigned int len)
  133. {
  134. char host[HOST_PATH_MAX];
  135. const unsigned char *p = (const unsigned char *)buf;
  136. unsigned int done = 0;
  137. int fd;
  138. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  139. return -1;
  140. fd = open(host, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  141. if (fd < 0)
  142. return -1;
  143. while (done < len) {
  144. ssize_t n = write(fd, p + done, len - done);
  145. if (n < 0) {
  146. if (errno == EINTR)
  147. continue;
  148. close(fd);
  149. return -1;
  150. }
  151. if (n == 0)
  152. break;
  153. done += (unsigned int)n;
  154. }
  155. close(fd);
  156. return done == len ? 0 : -1;
  157. }
  158. int host_fs_unlink(const char *path)
  159. {
  160. char host[HOST_PATH_MAX];
  161. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  162. return -1;
  163. return unlink(host) == 0 ? 0 : -1;
  164. }
  165. int host_fs_mkdir(const char *path)
  166. {
  167. char host[HOST_PATH_MAX];
  168. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  169. return -1;
  170. return mkdir(host, 0755) == 0 ? 0 : -1;
  171. }
  172. int host_fs_rmdir(const char *path)
  173. {
  174. char host[HOST_PATH_MAX];
  175. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  176. return -1;
  177. return rmdir(host) == 0 ? 0 : -1;
  178. }
  179. int host_fs_list(const char *path, unsigned int index, void *out)
  180. {
  181. char host[HOST_PATH_MAX];
  182. struct sys_dirent *de = (struct sys_dirent *)out;
  183. struct dirent *d;
  184. DIR *dir;
  185. unsigned int n = 0;
  186. if (host_guest_to_host(path, host, sizeof(host)) != 0)
  187. return -1;
  188. dir = opendir(host);
  189. if (!dir)
  190. return -1;
  191. while ((d = readdir(dir)) != 0) {
  192. char full[HOST_PATH_MAX];
  193. struct stat st;
  194. if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  195. continue;
  196. if (n++ != index)
  197. continue;
  198. memset(de, 0, sizeof(*de));
  199. snprintf(de->name, sizeof(de->name), "%s", d->d_name);
  200. snprintf(full, sizeof(full), "%s/%s", host, d->d_name);
  201. if (stat(full, &st) == 0) {
  202. de->size = (unsigned int)st.st_size;
  203. de->is_dir = S_ISDIR(st.st_mode) ? 1u : 0u;
  204. }
  205. closedir(dir);
  206. return 1;
  207. }
  208. closedir(dir);
  209. return 0;
  210. }