vfs_fat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Damien P. George
  7. * Copyright (c) 2016 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "py/mpconfig.h"
  28. #if MICROPY_VFS_FAT
  29. #if !MICROPY_VFS
  30. #error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_VFS"
  31. #endif
  32. #include <string.h>
  33. #include "py/runtime.h"
  34. #include "py/mperrno.h"
  35. #include "lib/oofatfs/ff.h"
  36. #include "extmod/vfs_fat.h"
  37. #include "lib/timeutils/timeutils.h"
  38. #if _MAX_SS == _MIN_SS
  39. #define SECSIZE(fs) (_MIN_SS)
  40. #else
  41. #define SECSIZE(fs) ((fs)->ssize)
  42. #endif
  43. #define mp_obj_fat_vfs_t fs_user_mount_t
  44. STATIC mp_import_stat_t fat_vfs_import_stat(void *vfs_in, const char *path) {
  45. fs_user_mount_t *vfs = vfs_in;
  46. FILINFO fno;
  47. assert(vfs != NULL);
  48. FRESULT res = f_stat(&vfs->fatfs, path, &fno);
  49. if (res == FR_OK) {
  50. if ((fno.fattrib & AM_DIR) != 0) {
  51. return MP_IMPORT_STAT_DIR;
  52. } else {
  53. return MP_IMPORT_STAT_FILE;
  54. }
  55. }
  56. return MP_IMPORT_STAT_NO_EXIST;
  57. }
  58. STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  59. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  60. // create new object
  61. fs_user_mount_t *vfs = m_new_obj(fs_user_mount_t);
  62. vfs->base.type = type;
  63. vfs->flags = FSUSER_FREE_OBJ;
  64. vfs->fatfs.drv = vfs;
  65. // load block protocol methods
  66. mp_load_method(args[0], MP_QSTR_readblocks, vfs->readblocks);
  67. mp_load_method_maybe(args[0], MP_QSTR_writeblocks, vfs->writeblocks);
  68. mp_load_method_maybe(args[0], MP_QSTR_ioctl, vfs->u.ioctl);
  69. if (vfs->u.ioctl[0] != MP_OBJ_NULL) {
  70. // device supports new block protocol, so indicate it
  71. vfs->flags |= FSUSER_HAVE_IOCTL;
  72. } else {
  73. // no ioctl method, so assume the device uses the old block protocol
  74. mp_load_method_maybe(args[0], MP_QSTR_sync, vfs->u.old.sync);
  75. mp_load_method(args[0], MP_QSTR_count, vfs->u.old.count);
  76. }
  77. // mount the block device so the VFS methods can be used
  78. FRESULT res = f_mount(&vfs->fatfs);
  79. if (res == FR_NO_FILESYSTEM) {
  80. // don't error out if no filesystem, to let mkfs()/mount() create one if wanted
  81. vfs->flags |= FSUSER_NO_FILESYSTEM;
  82. } else if (res != FR_OK) {
  83. mp_raise_OSError(fresult_to_errno_table[res]);
  84. }
  85. return MP_OBJ_FROM_PTR(vfs);
  86. }
  87. #if _FS_REENTRANT
  88. STATIC mp_obj_t fat_vfs_del(mp_obj_t self_in) {
  89. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(self_in);
  90. // f_umount only needs to be called to release the sync object
  91. f_umount(&self->fatfs);
  92. return mp_const_none;
  93. }
  94. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_del_obj, fat_vfs_del);
  95. #endif
  96. STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
  97. // create new object
  98. fs_user_mount_t *vfs = MP_OBJ_TO_PTR(fat_vfs_make_new(&mp_fat_vfs_type, 1, 0, &bdev_in));
  99. // make the filesystem
  100. uint8_t working_buf[_MAX_SS];
  101. FRESULT res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
  102. if (res != FR_OK) {
  103. mp_raise_OSError(fresult_to_errno_table[res]);
  104. }
  105. return mp_const_none;
  106. }
  107. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
  108. STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
  109. typedef struct _mp_vfs_fat_ilistdir_it_t {
  110. mp_obj_base_t base;
  111. mp_fun_1_t iternext;
  112. bool is_str;
  113. FF_DIR dir;
  114. } mp_vfs_fat_ilistdir_it_t;
  115. STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
  116. mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
  117. for (;;) {
  118. FILINFO fno;
  119. FRESULT res = f_readdir(&self->dir, &fno);
  120. char *fn = fno.fname;
  121. if (res != FR_OK || fn[0] == 0) {
  122. // stop on error or end of dir
  123. break;
  124. }
  125. // Note that FatFS already filters . and .., so we don't need to
  126. // make 4-tuple with info about this entry
  127. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
  128. if (self->is_str) {
  129. t->items[0] = mp_obj_new_str(fn, strlen(fn));
  130. } else {
  131. t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
  132. }
  133. if (fno.fattrib & AM_DIR) {
  134. // dir
  135. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
  136. } else {
  137. // file
  138. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
  139. }
  140. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
  141. t->items[3] = mp_obj_new_int_from_uint(fno.fsize);
  142. return MP_OBJ_FROM_PTR(t);
  143. }
  144. // ignore error because we may be closing a second time
  145. f_closedir(&self->dir);
  146. return MP_OBJ_STOP_ITERATION;
  147. }
  148. STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
  149. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]);
  150. bool is_str_type = true;
  151. const char *path;
  152. if (n_args == 2) {
  153. if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
  154. is_str_type = false;
  155. }
  156. path = mp_obj_str_get_str(args[1]);
  157. } else {
  158. path = "";
  159. }
  160. // Create a new iterator object to list the dir
  161. mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t);
  162. iter->base.type = &mp_type_polymorph_iter;
  163. iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
  164. iter->is_str = is_str_type;
  165. FRESULT res = f_opendir(&self->fatfs, &iter->dir, path);
  166. if (res != FR_OK) {
  167. mp_raise_OSError(fresult_to_errno_table[res]);
  168. }
  169. return MP_OBJ_FROM_PTR(iter);
  170. }
  171. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func);
  172. STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
  173. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  174. const char *path = mp_obj_str_get_str(path_in);
  175. FILINFO fno;
  176. FRESULT res = f_stat(&self->fatfs, path, &fno);
  177. if (res != FR_OK) {
  178. mp_raise_OSError(fresult_to_errno_table[res]);
  179. }
  180. // check if path is a file or directory
  181. if ((fno.fattrib & AM_DIR) == attr) {
  182. res = f_unlink(&self->fatfs, path);
  183. if (res != FR_OK) {
  184. mp_raise_OSError(fresult_to_errno_table[res]);
  185. }
  186. return mp_const_none;
  187. } else {
  188. mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR);
  189. }
  190. }
  191. STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
  192. return fat_vfs_remove_internal(vfs_in, path_in, 0); // 0 == file attribute
  193. }
  194. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
  195. STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) {
  196. return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR);
  197. }
  198. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
  199. STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) {
  200. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  201. const char *old_path = mp_obj_str_get_str(path_in);
  202. const char *new_path = mp_obj_str_get_str(path_out);
  203. FRESULT res = f_rename(&self->fatfs, old_path, new_path);
  204. if (res == FR_EXIST) {
  205. // if new_path exists then try removing it (but only if it's a file)
  206. fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute
  207. // try to rename again
  208. res = f_rename(&self->fatfs, old_path, new_path);
  209. }
  210. if (res == FR_OK) {
  211. return mp_const_none;
  212. } else {
  213. mp_raise_OSError(fresult_to_errno_table[res]);
  214. }
  215. }
  216. STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
  217. STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
  218. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  219. const char *path = mp_obj_str_get_str(path_o);
  220. FRESULT res = f_mkdir(&self->fatfs, path);
  221. if (res == FR_OK) {
  222. return mp_const_none;
  223. } else {
  224. mp_raise_OSError(fresult_to_errno_table[res]);
  225. }
  226. }
  227. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
  228. /// Change current directory.
  229. STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) {
  230. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  231. const char *path;
  232. path = mp_obj_str_get_str(path_in);
  233. FRESULT res = f_chdir(&self->fatfs, path);
  234. if (res != FR_OK) {
  235. mp_raise_OSError(fresult_to_errno_table[res]);
  236. }
  237. return mp_const_none;
  238. }
  239. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir);
  240. /// Get the current directory.
  241. STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
  242. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  243. char buf[MICROPY_ALLOC_PATH_MAX + 1];
  244. FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf));
  245. if (res != FR_OK) {
  246. mp_raise_OSError(fresult_to_errno_table[res]);
  247. }
  248. return mp_obj_new_str(buf, strlen(buf));
  249. }
  250. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd);
  251. /// \function stat(path)
  252. /// Get the status of a file or directory.
  253. STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
  254. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  255. const char *path = mp_obj_str_get_str(path_in);
  256. FILINFO fno;
  257. if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
  258. // stat root directory
  259. fno.fsize = 0;
  260. fno.fdate = 0x2821; // Jan 1, 2000
  261. fno.ftime = 0;
  262. fno.fattrib = AM_DIR;
  263. } else {
  264. FRESULT res = f_stat(&self->fatfs, path, &fno);
  265. if (res != FR_OK) {
  266. mp_raise_OSError(fresult_to_errno_table[res]);
  267. }
  268. }
  269. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  270. mp_int_t mode = 0;
  271. if (fno.fattrib & AM_DIR) {
  272. mode |= MP_S_IFDIR;
  273. } else {
  274. mode |= MP_S_IFREG;
  275. }
  276. mp_int_t seconds = timeutils_seconds_since_2000(
  277. 1980 + ((fno.fdate >> 9) & 0x7f),
  278. (fno.fdate >> 5) & 0x0f,
  279. fno.fdate & 0x1f,
  280. (fno.ftime >> 11) & 0x1f,
  281. (fno.ftime >> 5) & 0x3f,
  282. 2 * (fno.ftime & 0x1f)
  283. );
  284. t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
  285. t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
  286. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
  287. t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
  288. t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
  289. t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
  290. t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
  291. t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
  292. t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
  293. t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
  294. return MP_OBJ_FROM_PTR(t);
  295. }
  296. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat);
  297. // Get the status of a VFS.
  298. STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
  299. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  300. (void)path_in;
  301. DWORD nclst;
  302. FATFS *fatfs = &self->fatfs;
  303. FRESULT res = f_getfree(fatfs, &nclst);
  304. if (FR_OK != res) {
  305. mp_raise_OSError(fresult_to_errno_table[res]);
  306. }
  307. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  308. t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * SECSIZE(fatfs)); // f_bsize
  309. t->items[1] = t->items[0]; // f_frsize
  310. t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2)); // f_blocks
  311. t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
  312. t->items[4] = t->items[3]; // f_bavail
  313. t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
  314. t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
  315. t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
  316. t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
  317. t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax
  318. return MP_OBJ_FROM_PTR(t);
  319. }
  320. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
  321. STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
  322. fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
  323. // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
  324. // User can specify read-only device by:
  325. // 1. readonly=True keyword argument
  326. // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
  327. if (mp_obj_is_true(readonly)) {
  328. self->writeblocks[0] = MP_OBJ_NULL;
  329. }
  330. // check if we need to make the filesystem
  331. FRESULT res = (self->flags & FSUSER_NO_FILESYSTEM) ? FR_NO_FILESYSTEM : FR_OK;
  332. if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) {
  333. uint8_t working_buf[_MAX_SS];
  334. res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
  335. }
  336. if (res != FR_OK) {
  337. mp_raise_OSError(fresult_to_errno_table[res]);
  338. }
  339. self->flags &= ~FSUSER_NO_FILESYSTEM;
  340. return mp_const_none;
  341. }
  342. STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_fat_mount_obj, vfs_fat_mount);
  343. STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
  344. (void)self_in;
  345. // keep the FAT filesystem mounted internally so the VFS methods can still be used
  346. return mp_const_none;
  347. }
  348. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
  349. STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
  350. #if _FS_REENTRANT
  351. { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&fat_vfs_del_obj) },
  352. #endif
  353. { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
  354. { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
  355. { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&fat_vfs_ilistdir_obj) },
  356. { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) },
  357. { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&fat_vfs_rmdir_obj) },
  358. { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&fat_vfs_chdir_obj) },
  359. { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&fat_vfs_getcwd_obj) },
  360. { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
  361. { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) },
  362. { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) },
  363. { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
  364. { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
  365. { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
  366. };
  367. STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
  368. STATIC const mp_vfs_proto_t fat_vfs_proto = {
  369. .import_stat = fat_vfs_import_stat,
  370. };
  371. const mp_obj_type_t mp_fat_vfs_type = {
  372. { &mp_type_type },
  373. .name = MP_QSTR_VfsFat,
  374. .make_new = fat_vfs_make_new,
  375. .protocol = &fat_vfs_proto,
  376. .locals_dict = (mp_obj_dict_t*)&fat_vfs_locals_dict,
  377. };
  378. #endif // MICROPY_VFS_FAT