vfs_fat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_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) {
  45. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  46. // create new object
  47. fs_user_mount_t *vfs = m_new_obj(fs_user_mount_t);
  48. vfs->base.type = type;
  49. vfs->flags = FSUSER_FREE_OBJ;
  50. vfs->fatfs.drv = vfs;
  51. // load block protocol methods
  52. mp_load_method(args[0], MP_QSTR_readblocks, vfs->readblocks);
  53. mp_load_method_maybe(args[0], MP_QSTR_writeblocks, vfs->writeblocks);
  54. mp_load_method_maybe(args[0], MP_QSTR_ioctl, vfs->u.ioctl);
  55. if (vfs->u.ioctl[0] != MP_OBJ_NULL) {
  56. // device supports new block protocol, so indicate it
  57. vfs->flags |= FSUSER_HAVE_IOCTL;
  58. } else {
  59. // no ioctl method, so assume the device uses the old block protocol
  60. mp_load_method_maybe(args[0], MP_QSTR_sync, vfs->u.old.sync);
  61. mp_load_method(args[0], MP_QSTR_count, vfs->u.old.count);
  62. }
  63. return MP_OBJ_FROM_PTR(vfs);
  64. }
  65. STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
  66. // create new object
  67. fs_user_mount_t *vfs = MP_OBJ_TO_PTR(fat_vfs_make_new(&mp_fat_vfs_type, 1, 0, &bdev_in));
  68. // make the filesystem
  69. uint8_t working_buf[_MAX_SS];
  70. FRESULT res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
  71. if (res != FR_OK) {
  72. mp_raise_OSError(fresult_to_errno_table[res]);
  73. }
  74. return mp_const_none;
  75. }
  76. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
  77. STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
  78. STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_open_obj, fatfs_builtin_open_self);
  79. STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
  80. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]);
  81. bool is_str_type = true;
  82. const char *path;
  83. if (n_args == 2) {
  84. if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
  85. is_str_type = false;
  86. }
  87. path = mp_obj_str_get_str(args[1]);
  88. } else {
  89. path = "";
  90. }
  91. return fat_vfs_ilistdir2(self, path, is_str_type);
  92. }
  93. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func);
  94. STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
  95. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  96. const char *path = mp_obj_str_get_str(path_in);
  97. FILINFO fno;
  98. FRESULT res = f_stat(&self->fatfs, path, &fno);
  99. if (res != FR_OK) {
  100. mp_raise_OSError(fresult_to_errno_table[res]);
  101. }
  102. // check if path is a file or directory
  103. if ((fno.fattrib & AM_DIR) == attr) {
  104. res = f_unlink(&self->fatfs, path);
  105. if (res != FR_OK) {
  106. mp_raise_OSError(fresult_to_errno_table[res]);
  107. }
  108. return mp_const_none;
  109. } else {
  110. mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR);
  111. }
  112. }
  113. STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
  114. return fat_vfs_remove_internal(vfs_in, path_in, 0); // 0 == file attribute
  115. }
  116. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
  117. STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) {
  118. return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR);
  119. }
  120. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
  121. STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) {
  122. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  123. const char *old_path = mp_obj_str_get_str(path_in);
  124. const char *new_path = mp_obj_str_get_str(path_out);
  125. FRESULT res = f_rename(&self->fatfs, old_path, new_path);
  126. if (res == FR_EXIST) {
  127. // if new_path exists then try removing it (but only if it's a file)
  128. fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute
  129. // try to rename again
  130. res = f_rename(&self->fatfs, old_path, new_path);
  131. }
  132. if (res == FR_OK) {
  133. return mp_const_none;
  134. } else {
  135. mp_raise_OSError(fresult_to_errno_table[res]);
  136. }
  137. }
  138. STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
  139. STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
  140. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  141. const char *path = mp_obj_str_get_str(path_o);
  142. FRESULT res = f_mkdir(&self->fatfs, path);
  143. if (res == FR_OK) {
  144. return mp_const_none;
  145. } else {
  146. mp_raise_OSError(fresult_to_errno_table[res]);
  147. }
  148. }
  149. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
  150. /// Change current directory.
  151. STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) {
  152. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  153. const char *path;
  154. path = mp_obj_str_get_str(path_in);
  155. FRESULT res = f_chdir(&self->fatfs, path);
  156. if (res != FR_OK) {
  157. mp_raise_OSError(fresult_to_errno_table[res]);
  158. }
  159. return mp_const_none;
  160. }
  161. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir);
  162. /// Get the current directory.
  163. STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
  164. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  165. char buf[MICROPY_ALLOC_PATH_MAX + 1];
  166. FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf));
  167. if (res != FR_OK) {
  168. mp_raise_OSError(fresult_to_errno_table[res]);
  169. }
  170. return mp_obj_new_str(buf, strlen(buf), false);
  171. }
  172. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd);
  173. /// \function stat(path)
  174. /// Get the status of a file or directory.
  175. STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
  176. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  177. const char *path = mp_obj_str_get_str(path_in);
  178. FILINFO fno;
  179. if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
  180. // stat root directory
  181. fno.fsize = 0;
  182. fno.fdate = 0x2821; // Jan 1, 2000
  183. fno.ftime = 0;
  184. fno.fattrib = AM_DIR;
  185. } else {
  186. FRESULT res = f_stat(&self->fatfs, path, &fno);
  187. if (res != FR_OK) {
  188. mp_raise_OSError(fresult_to_errno_table[res]);
  189. }
  190. }
  191. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  192. mp_int_t mode = 0;
  193. if (fno.fattrib & AM_DIR) {
  194. mode |= MP_S_IFDIR;
  195. } else {
  196. mode |= MP_S_IFREG;
  197. }
  198. mp_int_t seconds = timeutils_seconds_since_2000(
  199. 1980 + ((fno.fdate >> 9) & 0x7f),
  200. (fno.fdate >> 5) & 0x0f,
  201. fno.fdate & 0x1f,
  202. (fno.ftime >> 11) & 0x1f,
  203. (fno.ftime >> 5) & 0x3f,
  204. 2 * (fno.ftime & 0x1f)
  205. );
  206. t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
  207. t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
  208. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
  209. t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
  210. t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
  211. t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
  212. t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
  213. t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
  214. t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
  215. t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
  216. return MP_OBJ_FROM_PTR(t);
  217. }
  218. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat);
  219. // Get the status of a VFS.
  220. STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
  221. mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
  222. (void)path_in;
  223. DWORD nclst;
  224. FATFS *fatfs = &self->fatfs;
  225. FRESULT res = f_getfree(fatfs, &nclst);
  226. if (FR_OK != res) {
  227. mp_raise_OSError(fresult_to_errno_table[res]);
  228. }
  229. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  230. t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * SECSIZE(fatfs)); // f_bsize
  231. t->items[1] = t->items[0]; // f_frsize
  232. t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2)); // f_blocks
  233. t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
  234. t->items[4] = t->items[3]; // f_bavail
  235. t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
  236. t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
  237. t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
  238. t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
  239. t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax
  240. return MP_OBJ_FROM_PTR(t);
  241. }
  242. STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
  243. STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
  244. fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
  245. // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
  246. // User can specify read-only device by:
  247. // 1. readonly=True keyword argument
  248. // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
  249. if (mp_obj_is_true(readonly)) {
  250. self->writeblocks[0] = MP_OBJ_NULL;
  251. }
  252. // mount the block device
  253. FRESULT res = f_mount(&self->fatfs);
  254. // check if we need to make the filesystem
  255. if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) {
  256. uint8_t working_buf[_MAX_SS];
  257. res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
  258. }
  259. if (res != FR_OK) {
  260. mp_raise_OSError(fresult_to_errno_table[res]);
  261. }
  262. return mp_const_none;
  263. }
  264. STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_fat_mount_obj, vfs_fat_mount);
  265. STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
  266. fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
  267. FRESULT res = f_umount(&self->fatfs);
  268. if (res != FR_OK) {
  269. mp_raise_OSError(fresult_to_errno_table[res]);
  270. }
  271. return mp_const_none;
  272. }
  273. STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
  274. STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
  275. { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
  276. { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
  277. { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&fat_vfs_ilistdir_obj) },
  278. { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) },
  279. { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&fat_vfs_rmdir_obj) },
  280. { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&fat_vfs_chdir_obj) },
  281. { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&fat_vfs_getcwd_obj) },
  282. { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
  283. { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) },
  284. { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) },
  285. { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
  286. { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
  287. { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
  288. };
  289. STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
  290. const mp_obj_type_t mp_fat_vfs_type = {
  291. { &mp_type_type },
  292. .name = MP_QSTR_VfsFat,
  293. .make_new = fat_vfs_make_new,
  294. .locals_dict = (mp_obj_dict_t*)&fat_vfs_locals_dict,
  295. };
  296. #endif // MICROPY_VFS_FAT