Skip to content
Snippets Groups Projects
Commit a63a4761 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

unix/file: Stop assuming that O_RDWR == O_RDONLY | O_WRONLY.

That's not true e.g. on Linux.
parent 71206f02
No related branches found
No related tags found
No related merge requests found
...@@ -156,21 +156,22 @@ STATIC mp_obj_t fdfile_open(const mp_obj_type_t *type, mp_arg_val_t *args) { ...@@ -156,21 +156,22 @@ STATIC mp_obj_t fdfile_open(const mp_obj_type_t *type, mp_arg_val_t *args) {
mp_obj_fdfile_t *o = m_new_obj(mp_obj_fdfile_t); mp_obj_fdfile_t *o = m_new_obj(mp_obj_fdfile_t);
const char *mode_s = mp_obj_str_get_str(args[1].u_obj); const char *mode_s = mp_obj_str_get_str(args[1].u_obj);
int mode = 0; int mode_rw = 0, mode_x = 0;
while (*mode_s) { while (*mode_s) {
switch (*mode_s++) { switch (*mode_s++) {
// Note: these assume O_RDWR = O_RDONLY | O_WRONLY
case 'r': case 'r':
mode |= O_RDONLY; mode_rw = O_RDONLY;
break; break;
case 'w': case 'w':
mode |= O_WRONLY | O_CREAT | O_TRUNC; mode_rw = O_WRONLY;
mode_x = O_CREAT | O_TRUNC;
break; break;
case 'a': case 'a':
mode |= O_WRONLY | O_CREAT | O_APPEND; mode_rw = O_WRONLY;
mode_x = O_CREAT | O_APPEND;
break; break;
case '+': case '+':
mode |= O_RDWR; mode_rw = O_RDWR;
break; break;
#if MICROPY_PY_IO_FILEIO #if MICROPY_PY_IO_FILEIO
// If we don't have io.FileIO, then files are in text mode implicitly // If we don't have io.FileIO, then files are in text mode implicitly
...@@ -194,7 +195,7 @@ STATIC mp_obj_t fdfile_open(const mp_obj_type_t *type, mp_arg_val_t *args) { ...@@ -194,7 +195,7 @@ STATIC mp_obj_t fdfile_open(const mp_obj_type_t *type, mp_arg_val_t *args) {
} }
const char *fname = mp_obj_str_get_str(fid); const char *fname = mp_obj_str_get_str(fid);
int fd = open(fname, mode, 0644); int fd = open(fname, mode_x | mode_rw, 0644);
if (fd == -1) { if (fd == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno))); nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment