vfs_fat.c 15 KB

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