moduos.c 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <stdint.h>
28
29
#include <string.h>

30
31
32
#include "py/nlr.h"
#include "py/obj.h"
#include "py/objtuple.h"
33
34
35
36
#include "systick.h"
#include "rng.h"
#include "storage.h"
#include "ff.h"
37
#include "diskio.h"
38
#include "file.h"
39
#include "sdcard.h"
40
#include "fsusermount.h"
41
#include "portmodules.h"
42

43
44
45
46
47
48
49
50
51
52
53
54
55
/// \module os - basic "operating system" services
///
/// The `os` module contains functions for filesystem access and `urandom`.
///
/// The filesystem has `/` as the root directory, and the available physical
/// drives are accessible from here.  They are currently:
///
///     /flash      -- the internal flash filesystem
///     /sd         -- the SD card (if it exists)
///
/// On boot up, the current directory is `/flash` if no SD card is inserted,
/// otherwise it is `/sd`.

56
57
58
59
#if _USE_LFN
static char lfn[_MAX_LFN + 1];   /* Buffer to store the LFN */
#endif

60
61
62
63
64
65
66
67
68
STATIC bool sd_in_root(void) {
#if MICROPY_HW_HAS_SDCARD
    // TODO this is not the correct logic to check for /sd
    return sdcard_is_present();
#else
    return false;
#endif
}

69
70
/// \function chdir(path)
/// Change current directory.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
STATIC mp_obj_t os_chdir(mp_obj_t path_in) {
    const char *path;
    path = mp_obj_str_get_str(path_in);

    FRESULT res = f_chdrive(path);

    if (res == FR_OK) {
        res = f_chdir(path);
    }

    if (res != FR_OK) {
        // TODO should be mp_type_FileNotFoundError
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
    }

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_chdir_obj, os_chdir);

90
91
/// \function getcwd()
/// Get the current directory.
92
93
94
95
96
97
98
99
100
101
102
103
STATIC mp_obj_t os_getcwd(void) {
    char buf[MICROPY_ALLOC_PATH_MAX + 1];
    FRESULT res = f_getcwd(buf, sizeof buf);

    if (res != FR_OK) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
    }

    return mp_obj_new_str(buf, strlen(buf), false);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_getcwd_obj, os_getcwd);

104
105
/// \function listdir([dir])
/// With no argument, list the current directory.  Otherwise list the given directory.
106
STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) {
107
    bool is_str_type = true;
108
109
    const char *path;
    if (n_args == 1) {
110
        if (mp_obj_get_type(args[0]) == &mp_type_bytes) {
111
            is_str_type = false;
112
113
114
        }
        path = mp_obj_str_get_str(args[0]);
    } else {
115
116
117
118
119
120
121
        path = "";
    }

    // "hack" to list root directory
    if (path[0] == '/' && path[1] == '\0') {
        mp_obj_t dir_list = mp_obj_new_list(0, NULL);
        mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_flash));
122
        if (sd_in_root()) {
123
124
            mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_sd));
        }
125
126
127
        if (fs_user_mount != NULL) {
            mp_obj_list_append(dir_list, mp_obj_new_str(fs_user_mount->str + 1, fs_user_mount->len - 1, false));
        }
128
        return dir_list;
129
130
131
132
133
134
135
136
137
138
139
140
141
    }

    FRESULT res;
    FILINFO fno;
    DIR dir;
#if _USE_LFN
    fno.lfname = lfn;
    fno.lfsize = sizeof lfn;
#endif

    res = f_opendir(&dir, path);                       /* Open the directory */
    if (res != FR_OK) {
        // TODO should be mp_type_FileNotFoundError
142
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    }

    mp_obj_t dir_list = mp_obj_new_list(0, NULL);

    for (;;) {
        res = f_readdir(&dir, &fno);                   /* Read a directory item */
        if (res != FR_OK || fno.fname[0] == 0) break;  /* Break on error or end of dir */
        if (fno.fname[0] == '.' && fno.fname[1] == 0) continue;             /* Ignore . entry */
        if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue;             /* Ignore .. entry */

#if _USE_LFN
        char *fn = *fno.lfname ? fno.lfname : fno.fname;
#else
        char *fn = fno.fname;
#endif

159
160
161
162
163
        /*
        if (fno.fattrib & AM_DIR) {
            // dir
        } else {
            // file
164
        }
165
        */
166
167

        // make a string object for this entry
168
169
170
171
172
173
        mp_obj_t entry_o;
        if (is_str_type) {
            entry_o = mp_obj_new_str(fn, strlen(fn), false);
        } else {
            entry_o = mp_obj_new_bytes((const byte*)fn, strlen(fn));
        }
174
175

        // add the entry to the list
176
        mp_obj_list_append(dir_list, entry_o);
177
178
179
180
181
182
    }

    f_closedir(&dir);

    return dir_list;
}
183
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
184

185
186
/// \function mkdir(path)
/// Create a new directory.
187
188
189
190
191
192
193
194
STATIC mp_obj_t os_mkdir(mp_obj_t path_o) {
    const char *path = mp_obj_str_get_str(path_o);
    FRESULT res = f_mkdir(path);
    switch (res) {
        case FR_OK:
            return mp_const_none;
        case FR_EXIST:
            // TODO should be FileExistsError
195
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "File exists: '%s'", path));
196
        default:
197
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
198
199
    }
}
200
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
201

202
203
/// \function remove(path)
/// Remove a file.
204
205
206
207
208
209
210
211
STATIC mp_obj_t os_remove(mp_obj_t path_o) {
    const char *path = mp_obj_str_get_str(path_o);
    // TODO check that path is actually a file before trying to unlink it
    FRESULT res = f_unlink(path);
    switch (res) {
        case FR_OK:
            return mp_const_none;
        default:
212
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
213
214
    }
}
215
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
216

217
218
/// \function rmdir(path)
/// Remove a directory.
219
220
221
222
223
224
225
226
STATIC mp_obj_t os_rmdir(mp_obj_t path_o) {
    const char *path = mp_obj_str_get_str(path_o);
    // TODO check that path is actually a directory before trying to unlink it
    FRESULT res = f_unlink(path);
    switch (res) {
        case FR_OK:
            return mp_const_none;
        default:
227
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing directory '%s'", path));
228
229
    }
}
230
231
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Checks for path equality, ignoring trailing slashes:
//   path_equal(/, /) -> true
//   path_equal(/flash//, /flash) -> true
// second argument must be in canonical form (meaning no trailing slash, unless it's just /)
STATIC bool path_equal(const char *path, const char *path_canonical) {
    for (; *path_canonical != '\0' && *path == *path_canonical; ++path, ++path_canonical) {
    }
    if (*path_canonical != '\0') {
        return false;
    }
    for (; *path == '/'; ++path) {
    }
    return *path == '\0';
}

247
248
/// \function stat(path)
/// Get the status of a file or directory.
249
250
251
252
253
254
255
256
STATIC mp_obj_t os_stat(mp_obj_t path_in) {
    const char *path = mp_obj_str_get_str(path_in);

    FILINFO fno;
#if _USE_LFN
    fno.lfname = NULL;
    fno.lfsize = 0;
#endif
257

258
259
260
261
262
263
264
265
    FRESULT res;
    if (path_equal(path, "/") || path_equal(path, "/flash") || path_equal(path, "/sd")) {
        // stat built-in directory
        if (path[1] == 's' && !sd_in_root()) {
            // no /sd directory
            res = FR_NO_PATH;
            goto error;
        }
266
267
268
269
270
	fno.fsize = 0;
	fno.fdate = 0;
	fno.ftime = 0;
	fno.fattrib = AM_DIR;
    } else {
271
        res = f_stat(path, &fno);
272
        if (res != FR_OK) {
273
            goto error;
274
        }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    }

    mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
    mp_int_t mode = 0;
    if (fno.fattrib & AM_DIR) {
        mode |= 0x4000; // stat.S_IFDIR
    } else {
        mode |= 0x8000; // stat.S_IFREG
    }
    mp_int_t seconds = mod_time_seconds_since_2000(
        1980 + ((fno.fdate >> 9) & 0x7f),
        (fno.fdate >> 5) & 0x0f,
        fno.fdate & 0x1f,
        (fno.ftime >> 11) & 0x1f,
        (fno.ftime >> 5) & 0x3f,
        2 * (fno.ftime & 0x1f)
    );
    t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
    t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
    t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
    t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
    t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
    t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
    t->items[6] = MP_OBJ_NEW_SMALL_INT(fno.fsize); // st_size
    t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
    t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
    t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime

    return t;
304
305
306

error:
    nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
307
308
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
309

310
311
/// \function sync()
/// Sync all filesystems.
312
313
STATIC mp_obj_t os_sync(void) {
    storage_flush();
314
    disk_ioctl(2, CTRL_SYNC, NULL);
315
316
    return mp_const_none;
}
317
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
318

319
#if MICROPY_HW_ENABLE_RNG
320
321
322
/// \function urandom(n)
/// Return a bytes object with n random bytes, generated by the hardware
/// random number generator.
323
STATIC mp_obj_t os_urandom(mp_obj_t num) {
324
    mp_int_t n = mp_obj_get_int(num);
325
326
    vstr_t vstr;
    vstr_init_len(&vstr, n);
327
    for (int i = 0; i < n; i++) {
328
        vstr.buf[i] = rng_get();
329
    }
330
    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
331
}
332
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
333
#endif
334
335

STATIC const mp_map_elem_t os_module_globals_table[] = {
336
    { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) },
337

338
339
    { MP_OBJ_NEW_QSTR(MP_QSTR_chdir), (mp_obj_t)&os_chdir_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), (mp_obj_t)&os_getcwd_obj },
340
341
342
343
    { MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&os_listdir_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj },
344
    { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
345
346
347
348
    { MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove

    { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&os_sync_obj },

349
    /// \constant sep - separation character used in paths
350
351
    { MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) },

352
#if MICROPY_HW_ENABLE_RNG
353
    { MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj },
354
#endif
355
356
};

357
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
358

359
const mp_obj_module_t mp_module_uos = {
360
    .base = { &mp_type_module },
361
    .name = MP_QSTR_uos,
362
    .globals = (mp_obj_dict_t*)&os_module_globals,
363
};