blob: 7ff3fbbb89f8a27de0fd02a2dc2ee39a068458c3 [file] [log] [blame]
Paul Sokolovsky0c124c32014-05-14 22:08:45 +03001/*
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 Sokolovsky0c124c32014-05-14 22:08:45 +030033#include "mpconfig.h"
stijn5478ed12014-06-28 20:49:39 +020034#include "misc.h"
Paul Sokolovsky0c124c32014-05-14 22:08:45 +030035#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 George40f3c022014-07-03 13:25:24 +010043 { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT((mp_int_t)error_val))); } }
Paul Sokolovsky0c124c32014-05-14 22:08:45 +030044
45STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) {
46 struct stat sb;
Paul Sokolovsky0405b222014-05-26 02:00:51 +030047 uint len;
48 const char *path = mp_obj_str_get_data(path_in, &len);
Paul Sokolovsky0c124c32014-05-14 22:08:45 +030049
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 George40f3c022014-07-03 13:25:24 +010054 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 Sokolovsky0c124c32014-05-14 22:08:45 +030060 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}
66STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_stat_obj, mod_os_stat);
67
68STATIC 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
73STATIC 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 Blotf6932d62014-06-19 18:54:34 +020078 .used = MP_ARRAY_SIZE(mp_module_os_globals_table),
79 .alloc = MP_ARRAY_SIZE(mp_module_os_globals_table),
Paul Sokolovsky0c124c32014-05-14 22:08:45 +030080 .table = (mp_map_elem_t*)mp_module_os_globals_table,
81 },
82};
83
84const 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};