vfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // Copyright 2015-2016 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 "esp_vfs.h"
  19. #include "esp_log.h"
  20. /*
  21. * File descriptors visible by the applications are composed of two parts.
  22. * Lower CONFIG_MAX_FD_BITS bits are used for the actual file descriptor.
  23. * Next (16 - CONFIG_MAX_FD_BITS - 1) bits are used to identify the VFS this
  24. * descriptor corresponds to.
  25. * Highest bit is zero.
  26. * We can only use 16 bits because newlib stores file descriptor as short int.
  27. */
  28. #ifndef CONFIG_MAX_FD_BITS
  29. #define CONFIG_MAX_FD_BITS 12
  30. #endif
  31. #define MAX_VFS_ID_BITS (16 - CONFIG_MAX_FD_BITS - 1)
  32. // mask of actual file descriptor (e.g. 0x00000fff)
  33. #define VFS_FD_MASK ((1 << CONFIG_MAX_FD_BITS) - 1)
  34. // max number of VFS entries
  35. #define VFS_MAX_COUNT ((1 << MAX_VFS_ID_BITS) - 1)
  36. // mask of VFS id (e.g. 0x00007000)
  37. #define VFS_INDEX_MASK (VFS_MAX_COUNT << CONFIG_MAX_FD_BITS)
  38. #define VFS_INDEX_S CONFIG_MAX_FD_BITS
  39. typedef struct vfs_entry_ {
  40. esp_vfs_t vfs; // contains pointers to VFS functions
  41. char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS
  42. size_t path_prefix_len; // micro-optimization to avoid doing extra strlen
  43. void* ctx; // optional pointer which can be passed to VFS
  44. int offset; // index of this structure in s_vfs array
  45. } vfs_entry_t;
  46. static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 };
  47. static size_t s_vfs_count = 0;
  48. esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx)
  49. {
  50. size_t len = strlen(base_path);
  51. if (len < 2 || len > ESP_VFS_PATH_MAX) {
  52. return ESP_ERR_INVALID_ARG;
  53. }
  54. if (base_path[0] != '/' || base_path[len - 1] == '/') {
  55. return ESP_ERR_INVALID_ARG;
  56. }
  57. vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t));
  58. if (entry == NULL) {
  59. return ESP_ERR_NO_MEM;
  60. }
  61. size_t index;
  62. for (index = 0; index < s_vfs_count; ++index) {
  63. if (s_vfs[index] == NULL) {
  64. break;
  65. }
  66. }
  67. if (index == s_vfs_count) {
  68. if (s_vfs_count >= VFS_MAX_COUNT) {
  69. free(entry);
  70. return ESP_ERR_NO_MEM;
  71. }
  72. ++s_vfs_count;
  73. }
  74. s_vfs[index] = entry;
  75. strcpy(entry->path_prefix, base_path); // we have already verified argument length
  76. memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t));
  77. entry->path_prefix_len = len;
  78. entry->ctx = ctx;
  79. entry->offset = index;
  80. return ESP_OK;
  81. }
  82. esp_err_t esp_vfs_unregister(const char* base_path)
  83. {
  84. for (size_t i = 0; i < s_vfs_count; ++i) {
  85. vfs_entry_t* vfs = s_vfs[i];
  86. if (memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) {
  87. free(vfs);
  88. s_vfs[i] = NULL;
  89. return ESP_OK;
  90. }
  91. }
  92. return ESP_ERR_INVALID_STATE;
  93. }
  94. static const vfs_entry_t* get_vfs_for_fd(int fd)
  95. {
  96. int index = ((fd & VFS_INDEX_MASK) >> VFS_INDEX_S);
  97. if (index >= s_vfs_count) {
  98. return NULL;
  99. }
  100. return s_vfs[index];
  101. }
  102. static int translate_fd(const vfs_entry_t* vfs, int fd)
  103. {
  104. return (fd & VFS_FD_MASK) + vfs->vfs.fd_offset;
  105. }
  106. static const char* translate_path(const vfs_entry_t* vfs, const char* src_path)
  107. {
  108. assert(strncmp(src_path, vfs->path_prefix, vfs->path_prefix_len) == 0);
  109. return src_path + vfs->path_prefix_len;
  110. }
  111. static const vfs_entry_t* get_vfs_for_path(const char* path)
  112. {
  113. size_t len = strlen(path);
  114. for (size_t i = 0; i < s_vfs_count; ++i) {
  115. const vfs_entry_t* vfs = s_vfs[i];
  116. if (len < vfs->path_prefix_len + 1) { // +1 is for the trailing slash after base path
  117. continue;
  118. }
  119. if (memcmp(path, vfs->path_prefix, vfs->path_prefix_len) != 0) { // match prefix
  120. continue;
  121. }
  122. if (path[vfs->path_prefix_len] != '/') { // don't match "/data" prefix for "/data1/foo.txt"
  123. continue;
  124. }
  125. return vfs;
  126. }
  127. return NULL;
  128. }
  129. /*
  130. * Using huge multi-line macros is never nice, but in this case
  131. * the only alternative is to repeat this chunk of code (with different function names)
  132. * for each syscall being implemented. Given that this define is contained within a single
  133. * file, this looks like a good tradeoff.
  134. *
  135. * First we check if syscall is implemented by VFS (corresponding member is not NULL),
  136. * then call the right flavor of the method (e.g. open or open_p) depending on
  137. * ESP_VFS_FLAG_CONTEXT_PTR flag. If ESP_VFS_FLAG_CONTEXT_PTR is set, context is passed
  138. * in as first argument and _p variant is used for the call.
  139. * It is enough to check just one of them for NULL, as both variants are part of a union.
  140. */
  141. #define CHECK_AND_CALL(ret, r, pvfs, func, ...) \
  142. if (pvfs->vfs.func == NULL) { \
  143. __errno_r(r) = ENOSYS; \
  144. return -1; \
  145. } \
  146. if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
  147. ret = (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
  148. } else { \
  149. ret = (*pvfs->vfs.func)(__VA_ARGS__);\
  150. }
  151. #define CHECK_AND_CALLV(r, pvfs, func, ...) \
  152. if (pvfs->vfs.func == NULL) { \
  153. __errno_r(r) = ENOSYS; \
  154. return; \
  155. } \
  156. if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
  157. (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
  158. } else { \
  159. (*pvfs->vfs.func)(__VA_ARGS__);\
  160. }
  161. #define CHECK_AND_CALLP(ret, r, pvfs, func, ...) \
  162. if (pvfs->vfs.func == NULL) { \
  163. __errno_r(r) = ENOSYS; \
  164. return NULL; \
  165. } \
  166. if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
  167. ret = (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
  168. } else { \
  169. ret = (*pvfs->vfs.func)(__VA_ARGS__);\
  170. }
  171. int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode)
  172. {
  173. const vfs_entry_t* vfs = get_vfs_for_path(path);
  174. if (vfs == NULL) {
  175. __errno_r(r) = ENOENT;
  176. return -1;
  177. }
  178. const char* path_within_vfs = translate_path(vfs, path);
  179. int ret;
  180. CHECK_AND_CALL(ret, r, vfs, open, path_within_vfs, flags, mode);
  181. if (ret < 0) {
  182. return ret;
  183. }
  184. assert(ret >= vfs->vfs.fd_offset);
  185. return ret - vfs->vfs.fd_offset + (vfs->offset << VFS_INDEX_S);
  186. }
  187. ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size)
  188. {
  189. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  190. if (vfs == NULL) {
  191. __errno_r(r) = EBADF;
  192. return -1;
  193. }
  194. int local_fd = translate_fd(vfs, fd);
  195. int ret;
  196. CHECK_AND_CALL(ret, r, vfs, write, local_fd, data, size);
  197. return ret;
  198. }
  199. off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode)
  200. {
  201. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  202. if (vfs == NULL) {
  203. __errno_r(r) = EBADF;
  204. return -1;
  205. }
  206. int local_fd = translate_fd(vfs, fd);
  207. int ret;
  208. CHECK_AND_CALL(ret, r, vfs, lseek, local_fd, size, mode);
  209. return ret;
  210. }
  211. ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size)
  212. {
  213. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  214. if (vfs == NULL) {
  215. __errno_r(r) = EBADF;
  216. return -1;
  217. }
  218. int local_fd = translate_fd(vfs, fd);
  219. int ret;
  220. CHECK_AND_CALL(ret, r, vfs, read, local_fd, dst, size);
  221. return ret;
  222. }
  223. int esp_vfs_close(struct _reent *r, int fd)
  224. {
  225. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  226. if (vfs == NULL) {
  227. __errno_r(r) = EBADF;
  228. return -1;
  229. }
  230. int local_fd = translate_fd(vfs, fd);
  231. int ret;
  232. CHECK_AND_CALL(ret, r, vfs, close, local_fd);
  233. return ret;
  234. }
  235. int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st)
  236. {
  237. const vfs_entry_t* vfs = get_vfs_for_fd(fd);
  238. if (vfs == NULL) {
  239. __errno_r(r) = EBADF;
  240. return -1;
  241. }
  242. int local_fd = translate_fd(vfs, fd);
  243. int ret;
  244. CHECK_AND_CALL(ret, r, vfs, fstat, local_fd, st);
  245. return ret;
  246. }
  247. int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st)
  248. {
  249. const vfs_entry_t* vfs = get_vfs_for_path(path);
  250. if (vfs == NULL) {
  251. __errno_r(r) = ENOENT;
  252. return -1;
  253. }
  254. const char* path_within_vfs = translate_path(vfs, path);
  255. int ret;
  256. CHECK_AND_CALL(ret, r, vfs, stat, path_within_vfs, st);
  257. return ret;
  258. }
  259. int esp_vfs_link(struct _reent *r, const char* n1, const char* n2)
  260. {
  261. const vfs_entry_t* vfs = get_vfs_for_path(n1);
  262. if (vfs == NULL) {
  263. __errno_r(r) = ENOENT;
  264. return -1;
  265. }
  266. const vfs_entry_t* vfs2 = get_vfs_for_path(n2);
  267. if (vfs != vfs2) {
  268. __errno_r(r) = EXDEV;
  269. return -1;
  270. }
  271. const char* path1_within_vfs = translate_path(vfs, n1);
  272. const char* path2_within_vfs = translate_path(vfs, n2);
  273. int ret;
  274. CHECK_AND_CALL(ret, r, vfs, link, path1_within_vfs, path2_within_vfs);
  275. return ret;
  276. }
  277. int esp_vfs_unlink(struct _reent *r, const char *path)
  278. {
  279. const vfs_entry_t* vfs = get_vfs_for_path(path);
  280. if (vfs == NULL) {
  281. __errno_r(r) = ENOENT;
  282. return -1;
  283. }
  284. const char* path_within_vfs = translate_path(vfs, path);
  285. int ret;
  286. CHECK_AND_CALL(ret, r, vfs, unlink, path_within_vfs);
  287. return ret;
  288. }
  289. int esp_vfs_rename(struct _reent *r, const char *src, const char *dst)
  290. {
  291. const vfs_entry_t* vfs = get_vfs_for_path(src);
  292. if (vfs == NULL) {
  293. __errno_r(r) = ENOENT;
  294. return -1;
  295. }
  296. const vfs_entry_t* vfs_dst = get_vfs_for_path(dst);
  297. if (vfs != vfs_dst) {
  298. __errno_r(r) = EXDEV;
  299. return -1;
  300. }
  301. const char* src_within_vfs = translate_path(vfs, src);
  302. const char* dst_within_vfs = translate_path(vfs, dst);
  303. int ret;
  304. CHECK_AND_CALL(ret, r, vfs, rename, src_within_vfs, dst_within_vfs);
  305. return ret;
  306. }
  307. DIR* opendir(const char* name)
  308. {
  309. const vfs_entry_t* vfs = get_vfs_for_path(name);
  310. struct _reent* r = __getreent();
  311. if (vfs == NULL) {
  312. __errno_r(r) = ENOENT;
  313. return NULL;
  314. }
  315. const char* path_within_vfs = translate_path(vfs, name);
  316. DIR* ret;
  317. CHECK_AND_CALLP(ret, r, vfs, opendir, path_within_vfs);
  318. if (ret != NULL) {
  319. ret->dd_vfs_idx = vfs->offset << VFS_INDEX_S;
  320. }
  321. return ret;
  322. }
  323. struct dirent* readdir(DIR* pdir)
  324. {
  325. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  326. struct _reent* r = __getreent();
  327. if (vfs == NULL) {
  328. __errno_r(r) = EBADF;
  329. return NULL;
  330. }
  331. struct dirent* ret;
  332. CHECK_AND_CALLP(ret, r, vfs, readdir, pdir);
  333. return ret;
  334. }
  335. int readdir_r(DIR* pdir, struct dirent* entry, struct dirent** out_dirent)
  336. {
  337. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  338. struct _reent* r = __getreent();
  339. if (vfs == NULL) {
  340. errno = EBADF;
  341. return -1;
  342. }
  343. int ret;
  344. CHECK_AND_CALL(ret, r, vfs, readdir_r, pdir, entry, out_dirent);
  345. return ret;
  346. }
  347. long telldir(DIR* pdir)
  348. {
  349. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  350. struct _reent* r = __getreent();
  351. if (vfs == NULL) {
  352. errno = EBADF;
  353. return -1;
  354. }
  355. long ret;
  356. CHECK_AND_CALL(ret, r, vfs, telldir, pdir);
  357. return ret;
  358. }
  359. void seekdir(DIR* pdir, long loc)
  360. {
  361. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  362. struct _reent* r = __getreent();
  363. if (vfs == NULL) {
  364. errno = EBADF;
  365. return;
  366. }
  367. CHECK_AND_CALLV(r, vfs, seekdir, pdir, loc);
  368. }
  369. void rewinddir(DIR* pdir)
  370. {
  371. seekdir(pdir, 0);
  372. }
  373. int closedir(DIR* pdir)
  374. {
  375. const vfs_entry_t* vfs = get_vfs_for_fd(pdir->dd_vfs_idx);
  376. struct _reent* r = __getreent();
  377. if (vfs == NULL) {
  378. errno = EBADF;
  379. return -1;
  380. }
  381. int ret;
  382. CHECK_AND_CALL(ret, r, vfs, closedir, pdir);
  383. return ret;
  384. }
  385. int mkdir(const char* name, mode_t mode)
  386. {
  387. const vfs_entry_t* vfs = get_vfs_for_path(name);
  388. struct _reent* r = __getreent();
  389. if (vfs == NULL) {
  390. __errno_r(r) = ENOENT;
  391. return -1;
  392. }
  393. const char* path_within_vfs = translate_path(vfs, name);
  394. int ret;
  395. CHECK_AND_CALL(ret, r, vfs, mkdir, path_within_vfs, mode);
  396. return ret;
  397. }
  398. int rmdir(const char* name)
  399. {
  400. const vfs_entry_t* vfs = get_vfs_for_path(name);
  401. struct _reent* r = __getreent();
  402. if (vfs == NULL) {
  403. __errno_r(r) = ENOENT;
  404. return -1;
  405. }
  406. const char* path_within_vfs = translate_path(vfs, name);
  407. int ret;
  408. CHECK_AND_CALL(ret, r, vfs, rmdir, path_within_vfs);
  409. return ret;
  410. }