blob: d21f4812e9f0fed12213d39107c1fa2fa33efa27 [file] [log] [blame]
Paul Sokolovskye9be6a32016-02-13 22:51:21 +02001/*
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
28#include "py/mpconfig.h"
29#if MICROPY_VFS_FAT
30
31#include "py/nlr.h"
32#include "py/runtime.h"
33#include "lib/fatfs/ff.h"
34#include "lib/fatfs/diskio.h"
Paul Sokolovsky6b0c8822016-02-15 00:16:46 +020035#include "extmod/vfs_fat_file.h"
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020036#include "fsusermount.h"
37
38#define mp_obj_fat_vfs_t fs_user_mount_t
39
40STATIC 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) {
41 mp_arg_check_num(n_args, n_kw, 2, 2, false);
42 mp_obj_fat_vfs_t *vfs = fatfs_mount_mkfs(n_args, args, (mp_map_t*)&mp_const_empty_map, false);
43 vfs->base.type = type;
Paul Sokolovsky1bb15ca2016-02-14 16:21:27 +020044 return MP_OBJ_FROM_PTR(vfs);
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020045}
46
47STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
48 mp_obj_t args[] = {bdev_in, MP_OBJ_NEW_QSTR(MP_QSTR_mkfs)};
49 fatfs_mount_mkfs(2, args, (mp_map_t*)&mp_const_empty_map, true);
50 return mp_const_none;
51}
52STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
53STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
54
Paul Sokolovsky1bb15ca2016-02-14 16:21:27 +020055STATIC mp_obj_t fat_vfs_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020056 // Skip self
57 return fatfs_builtin_open(n_args - 1, args + 1, kwargs);
58}
59MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fat_vfs_open);
60
Paul Sokolovskycd6d1892016-02-28 17:17:24 +020061STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) {
62 bool is_str_type = true;
63 const char *path;
64 if (n_args == 2) {
65 if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
66 is_str_type = false;
67 }
68 path = mp_obj_str_get_str(args[1]);
69 } else {
70 path = "";
71 }
72
73 return fat_vfs_listdir(path, is_str_type);
74}
75STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_listdir_obj, 1, 2, fat_vfs_listdir_func);
76
Paul Sokolovsky19749db2016-02-28 20:30:07 +020077STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
Paul Sokolovsky6f469202016-02-28 20:45:51 +020078 (void)vfs_in;
Paul Sokolovsky19749db2016-02-28 20:30:07 +020079 const char *path = mp_obj_str_get_str(path_in);
80 // TODO check that path is actually a file before trying to unlink it
81 FRESULT res = f_unlink(path);
82 switch (res) {
83 case FR_OK:
84 return mp_const_none;
85 default:
86 // TODO: standard errno's
87 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
88 }
89}
90STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
91
Paul Sokolovskybbe832a2016-02-29 00:02:50 +020092STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
93 (void)vfs_in;
94 const char *path = mp_obj_str_get_str(path_o);
95 FRESULT res = f_mkdir(path);
96 switch (res) {
97 case FR_OK:
98 return mp_const_none;
99 case FR_EXIST:
100 // TODO should be FileExistsError
101 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "File exists: '%s'", path));
102 default:
103 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
104 }
105}
106STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
107
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200108STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
109 { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
110 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200111 { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&fat_vfs_listdir_obj) },
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200112 { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) },
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200113 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200114};
115STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
116
117const mp_obj_type_t mp_fat_vfs_type = {
118 { &mp_type_type },
119 .name = MP_QSTR_VfsFat,
120 .make_new = fat_vfs_make_new,
121 .locals_dict = (mp_obj_dict_t*)&fat_vfs_locals_dict,
122};
123
124#endif // MICROPY_VFS_FAT