vfs_fat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 FF_MAX_SS == FF_MIN_SS
  39. #define SECSIZE(fs) (FF_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->fatfs.drv = vfs;
  64. // Initialise underlying block device
  65. vfs->blockdev.flags = MP_BLOCKDEV_FLAG_FREE_OBJ;
  66. vfs->blockdev.block_size = FF_MIN_SS; // default, will be populated by call to MP_BLOCKDEV_IOCTL_BLOCK_SIZE
  67. mp_vfs_blockdev_init(&vfs->blockdev, args[0]);
  68. // mount the block device so the VFS methods can be used
  69. FRESULT res = f_mount(&vfs->fatfs);
  70. if (res == FR_NO_FILESYSTEM) {
  71. // don't error out if no filesystem, to let mkfs()/mount() create one if wanted
  72. vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NO_FILESYSTEM;
  73. } else if (res != FR_OK) {
  74. mp_raise_OSError(fresult_to_errno_table[res]);
  75. }
  76. return MP_OBJ_FROM_PTR(vfs);
  77. }
  78. #if _FS_REENTRANT
  79. STATIC mp_obj_t fat_vfs_del(mp_obj_t self_in) {
  80. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(self_in);
  81. // f_umount only needs to be called to release the sync object
  82. f_umount(&self->fatfs);
  83. return mp_const_none;
  84. }
  85. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_del_obj, fat_vfs_del);
  86. #endif
  87. STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
  88. // create new object
  89. fs_user_mount_t *vfs = MP_OBJ_TO_PTR(fat_vfs_make_new(&mp_fat_vfs_type, 1, 0, &bdev_in));
  90. // make the filesystem
  91. uint8_t working_buf[FF_MAX_SS];
  92. FRESULT res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
  93. if (res == FR_MKFS_ABORTED) { // Probably doesn't support FAT16
  94. res = f_mkfs(&vfs->fatfs, FM_FAT32, 0, working_buf, sizeof(working_buf));
  95. }
  96. if (res != FR_OK) {
  97. mp_raise_OSError(fresult_to_errno_table[res]);
  98. }
  99. return mp_const_none;
  100. }
  101. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
  102. STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
  103. typedef struct _mp_vfs_fat_ilistdir_it_t {
  104. mp_obj_base_t base;
  105. mp_fun_1_t iternext;
  106. bool is_str;
  107. FF_DIR dir;
  108. } mp_vfs_fat_ilistdir_it_t;
  109. STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
  110. mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
  111. for (;;) {
  112. FILINFO fno;
  113. FRESULT res = f_readdir(&self->dir, &fno);
  114. char *fn = fno.fname;
  115. if (res != FR_OK || fn[0] == 0) {
  116. // stop on error or end of dir
  117. break;
  118. }
  119. // Note that FatFS already filters . and .., so we don't need to
  120. // make 4-tuple with info about this entry
  121. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
  122. if (self->is_str) {
  123. t->items[0] = mp_obj_new_str(fn, strlen(fn));
  124. } else {
  125. t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
  126. }
  127. if (fno.fattrib & AM_DIR) {
  128. // dir
  129. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
  130. } else {
  131. // file
  132. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
  133. }
  134. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
  135. t->items[3] = mp_obj_new_int_from_uint(fno.fsize);
  136. return MP_OBJ_FROM_PTR(t);
  137. }
  138. // ignore error because we may be closing a second time
  139. f_closedir(&self->dir);
  140. return MP_OBJ_STOP_ITERATION;
  141. }
  142. STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
  143. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]);
  144. bool is_str_type = true;
  145. const char *path;
  146. if (n_args == 2) {
  147. if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
  148. is_str_type = false;
  149. }
  150. path = mp_obj_str_get_str(args[1]);
  151. } else {
  152. path = "";
  153. }
  154. // Create a new iterator object to list the dir
  155. mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t);
  156. iter->base.type = &mp_type_polymorph_iter;
  157. iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
  158. iter->is_str = is_str_type;
  159. FRESULT res = f_opendir(&self->fatfs, &iter->dir, path);
  160. if (res != FR_OK) {
  161. mp_raise_OSError(fresult_to_errno_table[res]);
  162. }
  163. return MP_OBJ_FROM_PTR(iter);
  164. }
  165. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func);
  166. STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
  167. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  168. const char *path = mp_obj_str_get_str(path_in);
  169. FILINFO fno;
  170. FRESULT res = f_stat(&self->fatfs, path, &fno);
  171. if (res != FR_OK) {
  172. mp_raise_OSError(fresult_to_errno_table[res]);
  173. }
  174. // check if path is a file or directory
  175. if ((fno.fattrib & AM_DIR) == attr) {
  176. res = f_unlink(&self->fatfs, path);
  177. if (res != FR_OK) {
  178. mp_raise_OSError(fresult_to_errno_table[res]);
  179. }
  180. return mp_const_none;
  181. } else {
  182. mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR);
  183. }
  184. }
  185. STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
  186. return fat_vfs_remove_internal(vfs_in, path_in, 0); // 0 == file attribute
  187. }
  188. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
  189. STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) {
  190. return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR);
  191. }
  192. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
  193. STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) {
  194. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  195. const char *old_path = mp_obj_str_get_str(path_in);
  196. const char *new_path = mp_obj_str_get_str(path_out);
  197. FRESULT res = f_rename(&self->fatfs, old_path, new_path);
  198. if (res == FR_EXIST) {
  199. // if new_path exists then try removing it (but only if it's a file)
  200. fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute
  201. // try to rename again
  202. res = f_rename(&self->fatfs, old_path, new_path);
  203. }
  204. if (res == FR_OK) {
  205. return mp_const_none;
  206. } else {
  207. mp_raise_OSError(fresult_to_errno_table[res]);
  208. }
  209. }
  210. STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
  211. STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
  212. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  213. const char *path = mp_obj_str_get_str(path_o);
  214. FRESULT res = f_mkdir(&self->fatfs, path);
  215. if (res == FR_OK) {
  216. return mp_const_none;
  217. } else {
  218. mp_raise_OSError(fresult_to_errno_table[res]);
  219. }
  220. }
  221. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
  222. /// Change current directory.
  223. STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) {
  224. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  225. const char *path;
  226. path = mp_obj_str_get_str(path_in);
  227. FRESULT res = f_chdir(&self->fatfs, path);
  228. if (res != FR_OK) {
  229. mp_raise_OSError(fresult_to_errno_table[res]);
  230. }
  231. return mp_const_none;
  232. }
  233. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir);
  234. /// Get the current directory.
  235. STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
  236. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  237. char buf[MICROPY_ALLOC_PATH_MAX + 1];
  238. FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf));
  239. if (res != FR_OK) {
  240. mp_raise_OSError(fresult_to_errno_table[res]);
  241. }
  242. return mp_obj_new_str(buf, strlen(buf));
  243. }
  244. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd);
  245. /// \function stat(path)
  246. /// Get the status of a file or directory.
  247. STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
  248. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  249. const char *path = mp_obj_str_get_str(path_in);
  250. FILINFO fno;
  251. if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
  252. // stat root directory
  253. fno.fsize = 0;
  254. fno.fdate = 0x2821; // Jan 1, 2000
  255. fno.ftime = 0;
  256. fno.fattrib = AM_DIR;
  257. } else {
  258. FRESULT res = f_stat(&self->fatfs, path, &fno);
  259. if (res != FR_OK) {
  260. mp_raise_OSError(fresult_to_errno_table[res]);
  261. }
  262. }
  263. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  264. mp_int_t mode = 0;
  265. if (fno.fattrib & AM_DIR) {
  266. mode |= MP_S_IFDIR;
  267. } else {
  268. mode |= MP_S_IFREG;
  269. }
  270. mp_int_t seconds = timeutils_seconds_since_2000(
  271. 1980 + ((fno.fdate >> 9) & 0x7f),
  272. (fno.fdate >> 5) & 0x0f,
  273. fno.fdate & 0x1f,
  274. (fno.ftime >> 11) & 0x1f,
  275. (fno.ftime >> 5) & 0x3f,
  276. 2 * (fno.ftime & 0x1f)
  277. );
  278. t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
  279. t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
  280. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
  281. t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
  282. t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
  283. t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
  284. t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
  285. t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
  286. t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
  287. t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
  288. return MP_OBJ_FROM_PTR(t);
  289. }
  290. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat);
  291. // Get the status of a VFS.
  292. STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
  293. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  294. (void)path_in;
  295. DWORD nclst;
  296. FATFS *fatfs = &self->fatfs;
  297. FRESULT res = f_getfree(fatfs, &nclst);
  298. if (FR_OK != res) {
  299. mp_raise_OSError(fresult_to_errno_table[res]);
  300. }
  301. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  302. t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * SECSIZE(fatfs)); // f_bsize
  303. t->items[1] = t->items[0]; // f_frsize
  304. t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2)); // f_blocks
  305. t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
  306. t->items[4] = t->items[3]; // f_bavail
  307. t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
  308. t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
  309. t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
  310. t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
  311. t->items[9] = MP_OBJ_NEW_SMALL_INT(FF_MAX_LFN); // f_namemax
  312. return MP_OBJ_FROM_PTR(t);
  313. }
  314. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
  315. STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
  316. fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
  317. // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
  318. // User can specify read-only device by:
  319. // 1. readonly=True keyword argument
  320. // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
  321. if (mp_obj_is_true(readonly)) {
  322. self->blockdev.writeblocks[0] = MP_OBJ_NULL;
  323. }
  324. // check if we need to make the filesystem
  325. FRESULT res = (self->blockdev.flags & MP_BLOCKDEV_FLAG_NO_FILESYSTEM) ? FR_NO_FILESYSTEM : FR_OK;
  326. if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) {
  327. uint8_t working_buf[FF_MAX_SS];
  328. res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
  329. }
  330. if (res != FR_OK) {
  331. mp_raise_OSError(fresult_to_errno_table[res]);
  332. }
  333. self->blockdev.flags &= ~MP_BLOCKDEV_FLAG_NO_FILESYSTEM;
  334. return mp_const_none;
  335. }
  336. STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_fat_mount_obj, vfs_fat_mount);
  337. STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
  338. (void)self_in;
  339. // keep the FAT filesystem mounted internally so the VFS methods can still be used
  340. return mp_const_none;
  341. }
  342. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
  343. STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
  344. #if _FS_REENTRANT
  345. { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&fat_vfs_del_obj) },
  346. #endif
  347. { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
  348. { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
  349. { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&fat_vfs_ilistdir_obj) },
  350. { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) },
  351. { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&fat_vfs_rmdir_obj) },
  352. { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&fat_vfs_chdir_obj) },
  353. { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&fat_vfs_getcwd_obj) },
  354. { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
  355. { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) },
  356. { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) },
  357. { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
  358. { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
  359. { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
  360. };
  361. STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
  362. STATIC const mp_vfs_proto_t fat_vfs_proto = {
  363. .import_stat = fat_vfs_import_stat,
  364. };
  365. const mp_obj_type_t mp_fat_vfs_type = {
  366. { &mp_type_type },
  367. .name = MP_QSTR_VfsFat,
  368. .make_new = fat_vfs_make_new,
  369. .protocol = &fat_vfs_proto,
  370. .locals_dict = (mp_obj_dict_t*)&fat_vfs_locals_dict,
  371. };
  372. #endif // MICROPY_VFS_FAT