Paul Sokolovsky | 0c124c3 | 2014-05-14 22:08:45 +0300 | [diff] [blame] | 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 | |
Paul Sokolovsky | 0c124c3 | 2014-05-14 22:08:45 +0300 | [diff] [blame] | 33 | #include "mpconfig.h" |
stijn | 5478ed1 | 2014-06-28 20:49:39 +0200 | [diff] [blame] | 34 | #include "misc.h" |
Paul Sokolovsky | 0c124c3 | 2014-05-14 22:08:45 +0300 | [diff] [blame] | 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) \ |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame^] | 43 | { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT((mp_int_t)error_val))); } } |
Paul Sokolovsky | 0c124c3 | 2014-05-14 22:08:45 +0300 | [diff] [blame] | 44 | |
| 45 | STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) { |
| 46 | struct stat sb; |
Paul Sokolovsky | 0405b22 | 2014-05-26 02:00:51 +0300 | [diff] [blame] | 47 | uint len; |
| 48 | const char *path = mp_obj_str_get_data(path_in, &len); |
Paul Sokolovsky | 0c124c3 | 2014-05-14 22:08:45 +0300 | [diff] [blame] | 49 | |
| 50 | int res = stat(path, &sb); |
| 51 | RAISE_ERRNO(res, errno); |
| 52 | |
| 53 | mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame^] | 54 | t->items[0] = MP_OBJ_NEW_SMALL_INT((mp_int_t)sb.st_mode); |
| 55 | t->items[1] = MP_OBJ_NEW_SMALL_INT((mp_int_t)sb.st_ino); |
| 56 | t->items[2] = MP_OBJ_NEW_SMALL_INT((mp_int_t)sb.st_dev); |
| 57 | t->items[3] = MP_OBJ_NEW_SMALL_INT((mp_int_t)sb.st_nlink); |
| 58 | t->items[4] = MP_OBJ_NEW_SMALL_INT((mp_int_t)sb.st_uid); |
| 59 | t->items[5] = MP_OBJ_NEW_SMALL_INT((mp_int_t)sb.st_gid); |
Paul Sokolovsky | 0c124c3 | 2014-05-14 22:08:45 +0300 | [diff] [blame] | 60 | t->items[6] = MP_OBJ_NEW_SMALL_INT(sb.st_size); |
| 61 | t->items[7] = MP_OBJ_NEW_SMALL_INT(sb.st_atime); |
| 62 | t->items[8] = MP_OBJ_NEW_SMALL_INT(sb.st_mtime); |
| 63 | t->items[9] = MP_OBJ_NEW_SMALL_INT(sb.st_ctime); |
| 64 | return t; |
| 65 | } |
| 66 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_stat_obj, mod_os_stat); |
| 67 | |
| 68 | STATIC const mp_map_elem_t mp_module_os_globals_table[] = { |
| 69 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR__os) }, |
| 70 | { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&mod_os_stat_obj }, |
| 71 | }; |
| 72 | |
| 73 | STATIC const mp_obj_dict_t mp_module_os_globals = { |
| 74 | .base = {&mp_type_dict}, |
| 75 | .map = { |
| 76 | .all_keys_are_qstrs = 1, |
| 77 | .table_is_fixed_array = 1, |
Emmanuel Blot | f6932d6 | 2014-06-19 18:54:34 +0200 | [diff] [blame] | 78 | .used = MP_ARRAY_SIZE(mp_module_os_globals_table), |
| 79 | .alloc = MP_ARRAY_SIZE(mp_module_os_globals_table), |
Paul Sokolovsky | 0c124c3 | 2014-05-14 22:08:45 +0300 | [diff] [blame] | 80 | .table = (mp_map_elem_t*)mp_module_os_globals_table, |
| 81 | }, |
| 82 | }; |
| 83 | |
| 84 | const mp_obj_module_t mp_module_os = { |
| 85 | .base = { &mp_type_module }, |
| 86 | .name = MP_QSTR__os, |
| 87 | .globals = (mp_obj_dict_t*)&mp_module_os_globals, |
| 88 | }; |