2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-05-03 08:24:21 -04:00
|
|
|
#include <stdint.h>
|
2014-03-22 10:56:32 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-05-02 11:03:04 -04:00
|
|
|
#include "mpconfig.h"
|
2014-03-22 10:56:32 -04:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
2014-07-19 11:39:13 -04:00
|
|
|
#include "objtuple.h"
|
2014-03-22 10:56:32 -04:00
|
|
|
#include "systick.h"
|
|
|
|
#include "rng.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "ff.h"
|
2014-07-19 11:39:13 -04:00
|
|
|
#include "file.h"
|
2014-07-31 18:44:04 -04:00
|
|
|
#include "sdcard.h"
|
2014-05-03 18:27:38 -04:00
|
|
|
#include "portmodules.h"
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
#if _USE_LFN
|
|
|
|
static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */
|
|
|
|
#endif
|
|
|
|
|
2014-07-31 18:44:04 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-03-22 10:56:32 -04:00
|
|
|
STATIC mp_obj_t os_listdir(uint n_args, const mp_obj_t *args) {
|
2014-07-31 18:44:04 -04:00
|
|
|
bool is_str_type = true;
|
2014-03-22 10:56:32 -04:00
|
|
|
const char *path;
|
|
|
|
if (n_args == 1) {
|
2014-03-29 09:43:38 -04:00
|
|
|
if (mp_obj_get_type(args[0]) == &mp_type_bytes) {
|
2014-07-31 18:44:04 -04:00
|
|
|
is_str_type = false;
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
path = mp_obj_str_get_str(args[0]);
|
|
|
|
} else {
|
2014-07-31 18:44:04 -04:00
|
|
|
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));
|
2014-08-01 11:10:41 -04:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
2014-07-31 18:44:04 -04:00
|
|
|
if (sdcard_is_present()) { // TODO this is not the correct logic to check for /sd
|
|
|
|
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_sd));
|
|
|
|
}
|
2014-08-01 11:10:41 -04:00
|
|
|
#endif
|
2014-07-31 18:44:04 -04:00
|
|
|
return dir_list;
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-07-31 18:44:04 -04:00
|
|
|
/*
|
|
|
|
if (fno.fattrib & AM_DIR) {
|
|
|
|
// dir
|
|
|
|
} else {
|
|
|
|
// file
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
2014-07-31 18:44:04 -04:00
|
|
|
*/
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
// make a string object for this entry
|
2014-07-31 18:44:04 -04:00
|
|
|
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));
|
|
|
|
}
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
// add the entry to the list
|
2014-07-31 18:44:04 -04:00
|
|
|
mp_obj_list_append(dir_list, entry_o);
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
f_closedir(&dir);
|
|
|
|
|
|
|
|
return dir_list;
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
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
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "File exists: '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
default:
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
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:
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
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:
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing directory '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
|
|
|
|
|
|
|
|
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
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-07-31 18:44:04 -04:00
|
|
|
if (path[0] == '/' && path[1] == '\0') {
|
|
|
|
// stat root directory
|
|
|
|
fno.fsize = 0;
|
|
|
|
fno.fdate = 0;
|
|
|
|
fno.ftime = 0;
|
|
|
|
fno.fattrib = AM_DIR;
|
|
|
|
} else {
|
|
|
|
FRESULT res = f_stat(path, &fno);
|
|
|
|
if (res != FR_OK) {
|
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t os_sync(void) {
|
|
|
|
storage_flush();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t os_urandom(mp_obj_t num) {
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t n = mp_obj_get_int(num);
|
2014-03-22 10:56:32 -04:00
|
|
|
byte *data;
|
2014-03-29 09:43:38 -04:00
|
|
|
mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data);
|
2014-03-22 10:56:32 -04:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
data[i] = rng_get();
|
|
|
|
}
|
|
|
|
return mp_obj_str_builder_end(o);
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
|
|
|
STATIC const mp_map_elem_t os_module_globals_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_os) },
|
|
|
|
|
2014-07-31 18:44:04 -04:00
|
|
|
{ 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 },
|
2014-03-22 10:56:32 -04:00
|
|
|
{ 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 },
|
2014-07-19 11:39:13 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
|
2014-03-22 10:56:32 -04:00
|
|
|
{ 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 },
|
|
|
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) },
|
|
|
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj },
|
|
|
|
};
|
|
|
|
|
2014-04-05 16:53:54 -04:00
|
|
|
STATIC const mp_obj_dict_t os_module_globals = {
|
|
|
|
.base = {&mp_type_dict},
|
|
|
|
.map = {
|
|
|
|
.all_keys_are_qstrs = 1,
|
|
|
|
.table_is_fixed_array = 1,
|
2014-06-19 12:54:34 -04:00
|
|
|
.used = MP_ARRAY_SIZE(os_module_globals_table),
|
|
|
|
.alloc = MP_ARRAY_SIZE(os_module_globals_table),
|
2014-04-05 16:53:54 -04:00
|
|
|
.table = (mp_map_elem_t*)os_module_globals_table,
|
|
|
|
},
|
2014-03-22 10:56:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mp_obj_module_t os_module = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.name = MP_QSTR_os,
|
2014-04-05 16:53:54 -04:00
|
|
|
.globals = (mp_obj_dict_t*)&os_module_globals,
|
2014-03-22 10:56:32 -04:00
|
|
|
};
|