Skip to content

Commit 0c124c3

Browse files
committed
unix: Add "_os" module with stat().
stat() is bad function to use using FFI, because its ABI is largely private. To start with, Glibc .so doesn't even have "stat" symbol. Then, layout of struct stat is too implementation-dependent. So, introduce _os to deal with stat() and other similar cases.
1 parent 2a27365 commit 0c124c3

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

unix/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ SRC_C = \
6868
input.c \
6969
file.c \
7070
modsocket.c \
71+
modos.c \
7172
$(SRC_MOD)
7273

7374
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))

unix/modos.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
};

unix/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
// names in exception messages (may require more RAM).
5050
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
5151

52+
extern const struct _mp_obj_module_t mp_module_os;
5253
extern const struct _mp_obj_module_t mp_module_time;
5354
extern const struct _mp_obj_module_t mp_module_socket;
5455
extern const struct _mp_obj_module_t mp_module_ffi;
@@ -68,6 +69,7 @@ extern const struct _mp_obj_module_t mp_module_ffi;
6869
MICROPY_MOD_FFI_DEF \
6970
MICROPY_MOD_TIME_DEF \
7071
{ MP_OBJ_NEW_QSTR(MP_QSTR_microsocket), (mp_obj_t)&mp_module_socket }, \
72+
{ MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&mp_module_os }, \
7173

7274
// type definitions for the specific machine
7375

unix/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Q(makefile)
3333

3434
Q(FileIO)
3535

36+
Q(_os)
37+
Q(stat)
38+
3639
Q(ffi)
3740
Q(ffimod)
3841
Q(ffifunc)

0 commit comments

Comments
 (0)