vfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <sys/errno.h>
  18. #include <sys/fcntl.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/unistd.h>
  21. #include <dirent.h>
  22. #include "esp_vfs.h"
  23. #include "esp_log.h"
  24. /*
  25. * File descriptors visible by the applications are composed of two parts.
  26. * Lower CONFIG_MAX_FD_BITS bits are used for the actual file descriptor.
  27. * Next (16 - CONFIG_MAX_FD_BITS - 1) bits are used to identify the VFS this
  28. * descriptor corresponds to.
  29. * Highest bit is zero.
  30. * We can only use 16 bits because newlib stores file descriptor as short int.
  31. */
  32. #ifndef CONFIG_MAX_FD_BITS
  33. #define CONFIG_MAX_FD_BITS 12
  34. #endif
  35. #define MAX_VFS_ID_BITS (16 - CONFIG_MAX_FD_BITS - 1)
  36. // mask of actual file descriptor (e.g. 0x00000fff)
  37. #define VFS_FD_MASK ((1 << CONFIG_MAX_FD_BITS) - 1)
  38. // max number of VFS entries
  39. #define VFS_MAX_COUNT ((1 << MAX_VFS_ID_BITS) - 1)
  40. // mask of VFS id (e.g. 0x00007000)
  41. #define VFS_INDEX_MASK (VFS_MAX_COUNT << CONFIG_MAX_FD_BITS)
  42. #define VFS_INDEX_S CONFIG_MAX_FD_BITS
  43. #define LEN_PATH_PREFIX_IGNORED SIZE_MAX /* special length value for VFS which is never recognised by open() */
  44. typedef struct vfs_entry_ {
  45. esp_vfs_t vfs; // contains pointers to VFS functions
  46. char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS
  47. size_t path_prefix_len; // micro-optimization to avoid doing extra strlen
  48. void* ctx; // optional pointer which can be passed to VFS
  49. int offset; // index of this structure in s_vfs array
  50. } vfs_entry_t;
  51. static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 };
  52. static size_t s_vfs_count = 0;
  53. static esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_vfs_t* vfs, void* ctx, int *p_minimum_fd, int *p_maximum_fd)
  54. {
  55. if (len != LEN_PATH_PREFIX_IGNORED) {
  56. if ((len != 0 && len < 2) || (len > ESP_VFS_PATH_MAX)) {
  57. return ESP_ERR_INVALID_ARG;
  58. }
  59. if ((len > 0 && base_path[0] != '/') || base_path[len - 1] == '/') {
  60. return ESP_ERR_INVALID_ARG;
  61. }
  62. }
  63. vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t));
  64. if (entry == NULL) {
  65. return ESP_ERR_NO_MEM;
  66. }
  67. size_t index;
  68. for (index = 0; index < s_vfs_count; ++index) {
  69. if (s_vfs[index] == NULL) {
  70. break;
  71. }
  72. }
  73. if (index == s_vfs_count) {
  74. if (s_vfs_count >= VFS_MAX_COUNT) {
  75. free(entry);
  76. return ESP_ERR_NO_MEM;
  77. }
  78. ++s_vfs_count;
  79. }
  80. s_vfs[index] = entry;
  81. if (len != LEN_PATH_PREFIX_IGNORED) {
  82. strcpy(entry->path_prefix, base_path); // we have already verified argument length
  83. } else {
  84. bzero(entry->path_prefix, sizeof(entry->path_prefix));
  85. }
  86. memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t));
  87. entry->path_prefix_len = len;
  88. entry->ctx = ctx;
  89. entry->offset = index;
  90. if (p_minimum_fd != NULL) {
  91. *p_minimum_fd = index << VFS_INDEX_S;
  92. }
  93. if (p_maximum_fd != NULL) {
  94. *p_maximum_fd = (index + 1) << VFS_INDEX_S;
  95. }
  96. return ESP_OK;
  97. }
  98. esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx)
  99. {
  100. return esp_vfs_register_common(base_path, strlen(base_path), vfs, ctx, NULL, NULL);
  101. }
  102. esp_err_t esp_vfs_register_socket_space(const esp_vfs_t *vfs, void *ctx, int *p_min_fd, int *p_max_fd)
  103. {
  104. return esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, p_min_fd, p_max_fd);
  105. }
  106. esp_err_t esp_vfs_unregister(const char* base_path)
  107. {
  108. for (size_t i = 0; i < s_vfs_count; ++i) {
  109. vfs_entry_t* vfs = s_vfs[i];
  110. if (vfs == NULL) {
  111. continue;
  112. }
  113. if (memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) {
  114. free(vfs);
  115. s_vfs[i] = NULL;
  116. return ESP_OK;
  117. }
  118. }
  119. return ESP_ERR_INVALID_STATE;
  120. }
  121. static const vfs_entry_t* get_vfs_for_fd(int fd)
  122. {
  123. int index = ((fd & VFS_INDEX_MASK) >> VFS_INDEX_S);
  124. if (index >= s_vfs_count) {
  125. return NULL;
  126. }
  127. return s_vfs[index];
  128. }
  129. static int translate_fd(const vfs_entry_t* vfs, int fd)
  130. {
  131. if (vfs->vfs.flags & ESP_VFS_FLAG_SHARED_FD_SPACE) {
  132. return fd;
  133. } else {
  134. return fd & VFS_FD_MASK;
  135. }
  136. }
  137. static const char* translate_path(const vfs_entry_t* vfs, const char* src_path)
  138. {
  139. assert(strncmp(src_path, vfs->path_prefix, vfs->path_prefix_len) == 0);
  140. if (strlen(src_path) == vfs->path_prefix_len) {
  141. // special case when src_path matches the path prefix exactly
  142. return "/";
  143. }
  144. return src_path + vfs->path_prefix_len;
  145. }
  146. static const vfs_entry_t* get_vfs_for_path(const char* path)
  147. {
  148. const vfs_entry_t* best_match = NULL;
  149. ssize_t best_match_prefix_len = -1;
  150. size_t len = strlen(path);
  151. for (size_t i = 0; i < s_vfs_count; ++i) {
  152. const vfs_entry_t* vfs = s_vfs[i];
  153. if (!vfs || vfs->path_prefix_len == LEN_PATH_PREFIX_IGNORED) {
  154. continue;
  155. }
  156. // match path prefix
  157. if (len < vfs->path_prefix_len ||
  158. memcmp(path, vfs->path_prefix, vfs->path_prefix_len) != 0) {
  159. continue;
  160. }
  161. // this is the default VFS and we don't have a better match yet.
  162. if (vfs->path_prefix_len == 0 && !best_match) {
  163. best_match = vfs;
  164. continue;
  165. }
  166. // if path is not equal to the prefix, expect to see a path separator
  167. // i.e. don't match "/data" prefix for "/data1/foo.txt" path
  168. if (len > vfs->path_prefix_len &&
  169. path[vfs->path_prefix_len] != '/') {
  170. continue;
  171. }
  172. // Out of all matching path prefixes, select the longest one;
  173. // i.e. if "/dev" and "/dev/uart" both match, for "/dev/uart/1" path,
  174. // choose "/dev/uart",
  175. // This causes all s_vfs_count VFS entries to be scanned when opening
  176. // a file by name. This can be optimized by introducing a table for
  177. // FS search order, sorted so that longer prefixes are checked first.
  178. if (best_match_prefix_len < (ssize_t) vfs->path_prefix_len) {
  179. best_match_prefix_len = (ssize_t) vfs->path_prefix_len;
  180. best_match = vfs;
  181. }
  182. }
  183. return best_match;
  184. }
  185. /*
  186. * Using huge multi-line macros is never nice, but in this case
  187. * the only alternative is to repeat this chunk of code (with different function names)
  188. * for each syscall being implemented. Given that this define is contained within a single
  189. * file, this looks like a good tradeoff.
  190. *
  191. * First we check if syscall is implemented by VFS (corresponding member is not NULL),
  192. * then call the right flavor of the method (e.g. open or open_p) depending on
  193. * ESP_VFS_FLAG_CONTEXT_PTR flag. If ESP_VFS_FLAG_CONTEXT_PTR is set, context is passed
  194. * in as first argument and _p variant is used for the call.
  195. * It is enough to check just one of them for NULL, as both variants are part of a union.
  196. */
  197. #define CHECK_AND_CALL(ret, r, pvfs, func, ...) \
  198. if (pvfs->vfs.func == NULL) { \
  199. __errno_r(r) = ENOSYS; \
  200. return -1; \
  201. } \
  202. if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
  203. ret = (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
  204. } else { \
  205. ret = (*pvfs->vfs.func)(__VA_ARGS__);\
  206. }
  207. #define CHECK_AND_CALLV(r, pvfs, func, ...) \
  208. if (pvfs->vfs.func == NULL) { \
  209. __errno_r(r) = ENOSYS; \
  210. return; \
  211. } \
  212. if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
  213. (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
  214. } else { \
  215. (*pvfs->vfs.func)(__VA_ARGS__);\
  216. }
  217. #define CHECK_AND_CALLP(ret, r, pvfs, func, ...) \
  218. if (pvfs->vfs.func == NULL) { \
  219. __errno_r(r) = ENOSYS; \
  220. return NULL; \
  221. } \
  222. if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
  223. ret = (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
  224. } else { \
  225. ret = (*pvfs->vfs.func)(__VA_ARGS__);\
  226. }
  227. int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode)
  228. {
  229. const vfs_entry_t* vfs = get_vfs_for_path(path);
  230. if (vfs == NULL) {
  231. __errno_r(r) = ENOENT;
  232. return -1;
  233. }
  234. const char* path_within_vfs = translate_path(vfs, path);
  235. int ret;
  236. CHECK_AND_CALL(ret, r, vfs, open, path_within_vfs, flags, mode);
  237. if (ret < 0) {
  238. return ret;
  239. }
  240. return ret + (vfs->offset << VFS_INDEX_S);
  241. }
  242. ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size)
  243. {
  244. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  245. if (vfs == NULL) {
  246. __errno_r(r) = EBADF;
  247. return -1;
  248. }
  249. int local_fd = translate_fd(vfs, fd);
  250. ssize_t ret;
  251. CHECK_AND_CALL(ret, r, vfs, write, local_fd, data, size);
  252. return ret;
  253. }
  254. off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode)
  255. {
  256. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  257. if (vfs == NULL) {
  258. __errno_r(r) = EBADF;
  259. return -1;
  260. }
  261. int local_fd = translate_fd(vfs, fd);
  262. off_t ret;
  263. CHECK_AND_CALL(ret, r, vfs, lseek, local_fd, size, mode);
  264. return ret;
  265. }
  266. ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size)
  267. {
  268. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  269. if (vfs == NULL) {
  270. __errno_r(r) = EBADF;
  271. return -1;
  272. }
  273. int local_fd = translate_fd(vfs, fd);
  274. ssize_t ret;
  275. CHECK_AND_CALL(ret, r, vfs, read, local_fd, dst, size);
  276. return ret;
  277. }
  278. int esp_vfs_close(struct _reent *r, int fd)
  279. {
  280. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  281. if (vfs == NULL) {
  282. __errno_r(r) = EBADF;
  283. return -1;
  284. }
  285. int local_fd = translate_fd(vfs, fd);
  286. int ret;
  287. CHECK_AND_CALL(ret, r, vfs, close, local_fd);
  288. return ret;
  289. }
  290. int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st)
  291. {
  292. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  293. if (vfs == NULL) {
  294. __errno_r(r) = EBADF;
  295. return -1;
  296. }
  297. int local_fd = translate_fd(vfs, fd);
  298. int ret;
  299. CHECK_AND_CALL(ret, r, vfs, fstat, local_fd, st);
  300. return ret;
  301. }
  302. int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st)
  303. {
  304. const vfs_entry_t* vfs = get_vfs_for_path(path);
  305. if (vfs == NULL) {
  306. __errno_r(r) = ENOENT;
  307. return -1;
  308. }
  309. const char* path_within_vfs = translate_path(vfs, path);
  310. int ret;
  311. CHECK_AND_CALL(ret, r, vfs, stat, path_within_vfs, st);
  312. return ret;
  313. }
  314. int esp_vfs_link(struct _reent *r, const char* n1, const char* n2)
  315. {
  316. const vfs_entry_t* vfs = get_vfs_for_path(n1);
  317. if (vfs == NULL) {
  318. __errno_r(r) = ENOENT;
  319. return -1;
  320. }
  321. const vfs_entry_t* vfs2 = get_vfs_for_path(n2);
  322. if (vfs != vfs2) {
  323. __errno_r(r) = EXDEV;
  324. return -1;
  325. }
  326. const char* path1_within_vfs = translate_path(vfs, n1);
  327. const char* path2_within_vfs = translate_path(vfs, n2);
  328. int ret;
  329. CHECK_AND_CALL(ret, r, vfs, link, path1_within_vfs, path2_within_vfs);
  330. return ret;
  331. }
  332. int esp_vfs_unlink(struct _reent *r, const char *path)
  333. {
  334. const vfs_entry_t* vfs = get_vfs_for_path(path);
  335. if (vfs == NULL) {
  336. __errno_r(r) = ENOENT;
  337. return -1;
  338. }
  339. const char* path_within_vfs = translate_path(vfs, path);
  340. int ret;
  341. CHECK_AND_CALL(ret, r, vfs, unlink, path_within_vfs);
  342. return ret;
  343. }
  344. int esp_vfs_rename(struct _reent *r, const char *src, const char *dst)
  345. {
  346. const vfs_entry_t* vfs = get_vfs_for_path(src);
  347. if (vfs == NULL) {
  348. __errno_r(r) = ENOENT;
  349. return -1;
  350. }
  351. const vfs_entry_t* vfs_dst = get_vfs_for_path(dst);
  352. if (vfs != vfs_dst) {
  353. __errno_r(r) = EXDEV;
  354. return -1;
  355. }
  356. const char* src_within_vfs = translate_path(vfs, src);
  357. const char* dst_within_vfs = translate_path(vfs, dst);
  358. int ret;
  359. CHECK_AND_CALL(ret, r, vfs, rename, src_within_vfs, dst_within_vfs);
  360. return ret;
  361. }
  362. DIR* opendir(const char* name)
  363. {
  364. const vfs_entry_t* vfs = get_vfs_for_path(name);
  365. struct _reent* r = __getreent();
  366. if (vfs == NULL) {
  367. __errno_r(r) = ENOENT;
  368. return NULL;
  369. }
  370. const char* path_within_vfs = translate_path(vfs, name);
  371. DIR* ret;
  372. CHECK_AND_CALLP(ret, r, vfs, opendir, path_within_vfs);
  373. if (ret != NULL) {
  374. ret->dd_vfs_idx = vfs->offset << VFS_INDEX_S;
  375. }
  376. return ret;
  377. }
  378. struct dirent* readdir(DIR* pdir)
  379. {
  380. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  381. struct _reent* r = __getreent();
  382. if (vfs == NULL) {
  383. __errno_r(r) = EBADF;
  384. return NULL;
  385. }
  386. struct dirent* ret;
  387. CHECK_AND_CALLP(ret, r, vfs, readdir, pdir);
  388. return ret;
  389. }
  390. int readdir_r(DIR* pdir, struct dirent* entry, struct dirent** out_dirent)
  391. {
  392. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  393. struct _reent* r = __getreent();
  394. if (vfs == NULL) {
  395. errno = EBADF;
  396. return -1;
  397. }
  398. int ret;
  399. CHECK_AND_CALL(ret, r, vfs, readdir_r, pdir, entry, out_dirent);
  400. return ret;
  401. }
  402. long telldir(DIR* pdir)
  403. {
  404. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  405. struct _reent* r = __getreent();
  406. if (vfs == NULL) {
  407. errno = EBADF;
  408. return -1;
  409. }
  410. long ret;
  411. CHECK_AND_CALL(ret, r, vfs, telldir, pdir);
  412. return ret;
  413. }
  414. void seekdir(DIR* pdir, long loc)
  415. {
  416. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  417. struct _reent* r = __getreent();
  418. if (vfs == NULL) {
  419. errno = EBADF;
  420. return;
  421. }
  422. CHECK_AND_CALLV(r, vfs, seekdir, pdir, loc);
  423. }
  424. void rewinddir(DIR* pdir)
  425. {
  426. seekdir(pdir, 0);
  427. }
  428. int closedir(DIR* pdir)
  429. {
  430. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  431. struct _reent* r = __getreent();
  432. if (vfs == NULL) {
  433. errno = EBADF;
  434. return -1;
  435. }
  436. int ret;
  437. CHECK_AND_CALL(ret, r, vfs, closedir, pdir);
  438. return ret;
  439. }
  440. int mkdir(const char* name, mode_t mode)
  441. {
  442. const vfs_entry_t* vfs = get_vfs_for_path(name);
  443. struct _reent* r = __getreent();
  444. if (vfs == NULL) {
  445. __errno_r(r) = ENOENT;
  446. return -1;
  447. }
  448. const char* path_within_vfs = translate_path(vfs, name);
  449. int ret;
  450. CHECK_AND_CALL(ret, r, vfs, mkdir, path_within_vfs, mode);
  451. return ret;
  452. }
  453. int rmdir(const char* name)
  454. {
  455. const vfs_entry_t* vfs = get_vfs_for_path(name);
  456. struct _reent* r = __getreent();
  457. if (vfs == NULL) {
  458. __errno_r(r) = ENOENT;
  459. return -1;
  460. }
  461. const char* path_within_vfs = translate_path(vfs, name);
  462. int ret;
  463. CHECK_AND_CALL(ret, r, vfs, rmdir, path_within_vfs);
  464. return ret;
  465. }
  466. int fcntl(int fd, int cmd, ...)
  467. {
  468. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  469. struct _reent* r = __getreent();
  470. if (vfs == NULL) {
  471. __errno_r(r) = EBADF;
  472. return -1;
  473. }
  474. int local_fd = translate_fd(vfs, fd);
  475. int ret;
  476. va_list args;
  477. va_start(args, cmd);
  478. CHECK_AND_CALL(ret, r, vfs, fcntl, local_fd, cmd, args);
  479. va_end(args);
  480. return ret;
  481. }
  482. int ioctl(int fd, int cmd, ...)
  483. {
  484. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  485. struct _reent* r = __getreent();
  486. if (vfs == NULL) {
  487. __errno_r(r) = EBADF;
  488. return -1;
  489. }
  490. int local_fd = translate_fd(vfs, fd);
  491. int ret;
  492. va_list args;
  493. va_start(args, cmd);
  494. CHECK_AND_CALL(ret, r, vfs, ioctl, local_fd, cmd, args);
  495. va_end(args);
  496. return ret;
  497. }
  498. int fsync(int fd)
  499. {
  500. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  501. struct _reent* r = __getreent();
  502. if (vfs == NULL) {
  503. __errno_r(r) = EBADF;
  504. return -1;
  505. }
  506. int local_fd = translate_fd(vfs, fd);
  507. int ret;
  508. CHECK_AND_CALL(ret, r, vfs, fsync, local_fd);
  509. return ret;
  510. }