|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2013, 2014 Damien P. George |
| 7 | + * Copyright (c) 2014 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 <sys/types.h> |
| 29 | +#include <sys/stat.h> |
| 30 | +#include <unistd.h> |
| 31 | +#include <errno.h> |
| 32 | + |
| 33 | +#include "misc.h" |
| 34 | +#include "mpconfig.h" |
| 35 | +#include "nlr.h" |
| 36 | +#include "qstr.h" |
| 37 | +#include "obj.h" |
| 38 | +#include "runtime.h" |
| 39 | +#include "objtuple.h" |
| 40 | + |
| 41 | +#define RAISE_ERRNO(err_flag, error_val) \ |
| 42 | + { if (err_flag == -1) \ |
| 43 | + { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT((machine_int_t)error_val))); } } |
| 44 | + |
| 45 | +STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) { |
| 46 | + struct stat sb; |
| 47 | + const char *path = mp_obj_str_get_str(path_in); |
| 48 | + |
| 49 | + int res = stat(path, &sb); |
| 50 | + RAISE_ERRNO(res, errno); |
| 51 | + |
| 52 | + mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL); |
| 53 | + t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.st_mode); |
| 54 | + t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.st_ino); |
| 55 | + t->items[2] = MP_OBJ_NEW_SMALL_INT((machine_int_t)sb.st_dev); |
| 56 | + t->items[3] = MP_OBJ_NEW_SMALL_INT(sb.st_nlink); |
| 57 | + t->items[4] = MP_OBJ_NEW_SMALL_INT(sb.st_uid); |
| 58 | + t->items[5] = MP_OBJ_NEW_SMALL_INT(sb.st_gid); |
| 59 | + t->items[6] = MP_OBJ_NEW_SMALL_INT(sb.st_size); |
| 60 | + t->items[7] = MP_OBJ_NEW_SMALL_INT(sb.st_atime); |
| 61 | + t->items[8] = MP_OBJ_NEW_SMALL_INT(sb.st_mtime); |
| 62 | + t->items[9] = MP_OBJ_NEW_SMALL_INT(sb.st_ctime); |
| 63 | + return t; |
| 64 | +} |
| 65 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_stat_obj, mod_os_stat); |
| 66 | + |
| 67 | +STATIC const mp_map_elem_t mp_module_os_globals_table[] = { |
| 68 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR__os) }, |
| 69 | + { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&mod_os_stat_obj }, |
| 70 | +}; |
| 71 | + |
| 72 | +STATIC const mp_obj_dict_t mp_module_os_globals = { |
| 73 | + .base = {&mp_type_dict}, |
| 74 | + .map = { |
| 75 | + .all_keys_are_qstrs = 1, |
| 76 | + .table_is_fixed_array = 1, |
| 77 | + .used = ARRAY_SIZE(mp_module_os_globals_table), |
| 78 | + .alloc = ARRAY_SIZE(mp_module_os_globals_table), |
| 79 | + .table = (mp_map_elem_t*)mp_module_os_globals_table, |
| 80 | + }, |
| 81 | +}; |
| 82 | + |
| 83 | +const mp_obj_module_t mp_module_os = { |
| 84 | + .base = { &mp_type_module }, |
| 85 | + .name = MP_QSTR__os, |
| 86 | + .globals = (mp_obj_dict_t*)&mp_module_os_globals, |
| 87 | +}; |
0 commit comments