Skip to content

Commit 0584df3

Browse files
aescolarDashingR
authored andcommitted
Revert "posix: device_io: use mode argument correctly in open()"
This reverts commit 499a633. PR zephyrproject-rtos#73978 introduced a regression. Unfortunately this PR cannot be reverted without reverting also Let's revert both PRs to stabilize main again towards the 3.7 release. For more details on the issue see zephyrproject-rtos#75205 Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent e4788dd commit 0584df3

File tree

7 files changed

+50
-67
lines changed

7 files changed

+50
-67
lines changed

include/zephyr/posix/fcntl.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
#define ZEPHYR_POSIX_FCNTL_H_
99

1010
#ifdef CONFIG_PICOLIBC
11-
#define O_CREAT 0x0040
12-
#define O_TRUNC 0x0200
13-
#define O_APPEND 0x0400
11+
#define O_CREAT 0x0040
1412
#else
15-
#define O_APPEND 0x0008
16-
#define O_CREAT 0x0200
17-
#define O_TRUNC 0x0400
13+
#define O_CREAT 0x0200
1814
#endif
1915

2016
#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
@@ -23,6 +19,7 @@
2319
#define O_WRONLY 01
2420
#define O_RDWR 02
2521

22+
#define O_APPEND 0x0400
2623
#define O_EXCL 0x0800
2724
#define O_NONBLOCK 0x4000
2825

lib/posix/options/device_io.c

+7-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <stdio.h>
99
#include <stdint.h>
1010

11-
#include <zephyr/posix/fcntl.h>
1211
#include <zephyr/posix/poll.h>
1312
#include <zephyr/posix/unistd.h>
1413
#include <zephyr/posix/sys/select.h>
@@ -17,28 +16,28 @@
1716
int zvfs_close(int fd);
1817
FILE *zvfs_fdopen(int fd, const char *mode);
1918
int zvfs_fileno(FILE *file);
20-
int zvfs_open(const char *name, int flags, int mode);
19+
int zvfs_open(const char *name, int flags);
2120
ssize_t zvfs_read(int fd, void *buf, size_t sz, size_t *from_offset);
2221
ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset);
2322

2423
void FD_CLR(int fd, struct zvfs_fd_set *fdset)
2524
{
26-
return ZVFS_FD_CLR(fd, fdset);
25+
return ZVFS_FD_CLR(fd, (struct zvfs_fd_set *)fdset);
2726
}
2827

2928
int FD_ISSET(int fd, struct zvfs_fd_set *fdset)
3029
{
31-
return ZVFS_FD_ISSET(fd, fdset);
30+
return ZVFS_FD_ISSET(fd, (struct zvfs_fd_set *)fdset);
3231
}
3332

3433
void FD_SET(int fd, struct zvfs_fd_set *fdset)
3534
{
36-
ZVFS_FD_SET(fd, fdset);
35+
ZVFS_FD_SET(fd, (struct zvfs_fd_set *)fdset);
3736
}
3837

3938
void FD_ZERO(fd_set *fdset)
4039
{
41-
ZVFS_FD_ZERO(fdset);
40+
ZVFS_FD_ZERO((struct zvfs_fd_set *)fdset);
4241
}
4342

4443
int close(int fd)
@@ -61,16 +60,8 @@ int fileno(FILE *file)
6160

6261
int open(const char *name, int flags, ...)
6362
{
64-
int mode = 0;
65-
va_list args;
66-
67-
if ((flags & O_CREAT) != 0) {
68-
va_start(args, flags);
69-
mode = va_arg(args, int);
70-
va_end(args);
71-
}
72-
73-
return zvfs_open(name, flags, mode);
63+
/* FIXME: necessarily need to check for O_CREAT and unpack ... if set */
64+
return zvfs_open(name, flags);
7465
}
7566
#ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_OPEN
7667
FUNC_ALIAS(open, _open, int);

lib/posix/options/fs.c

+35-40
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,37 @@ static inline void posix_fs_free_obj(struct posix_fs_desc *ptr)
5959
ptr->used = false;
6060
}
6161

62-
int zvfs_open(const char *name, int flags, int mode)
62+
static int posix_mode_to_zephyr(int mf)
63+
{
64+
int mode = (mf & O_CREAT) ? FS_O_CREATE : 0;
65+
66+
mode |= (mf & O_APPEND) ? FS_O_APPEND : 0;
67+
68+
switch (mf & O_ACCMODE) {
69+
case O_RDONLY:
70+
mode |= FS_O_READ;
71+
break;
72+
case O_WRONLY:
73+
mode |= FS_O_WRITE;
74+
break;
75+
case O_RDWR:
76+
mode |= FS_O_RDWR;
77+
break;
78+
default:
79+
break;
80+
}
81+
82+
return mode;
83+
}
84+
85+
int zvfs_open(const char *name, int flags)
6386
{
6487
int rc, fd;
6588
struct posix_fs_desc *ptr = NULL;
66-
int zflags = 0;
67-
68-
if ((flags & O_ACCMODE) == O_RDONLY) {
69-
zflags |= FS_O_READ;
70-
} else if ((flags & O_ACCMODE) == O_WRONLY) {
71-
zflags |= FS_O_WRITE;
72-
} else if ((flags & O_ACCMODE) == O_RDWR) {
73-
zflags |= FS_O_RDWR;
74-
}
89+
int zmode = posix_mode_to_zephyr(flags);
7590

76-
if ((flags & O_APPEND) != 0) {
77-
zflags |= FS_O_APPEND;
91+
if (zmode < 0) {
92+
return zmode;
7893
}
7994

8095
fd = zvfs_reserve_fd();
@@ -84,44 +99,24 @@ int zvfs_open(const char *name, int flags, int mode)
8499

85100
ptr = posix_fs_alloc_obj(false);
86101
if (ptr == NULL) {
87-
rc = -EMFILE;
88-
goto out_err;
102+
zvfs_free_fd(fd);
103+
errno = EMFILE;
104+
return -1;
89105
}
90106

91107
fs_file_t_init(&ptr->file);
92108

93-
if (flags & O_CREAT) {
94-
flags &= ~O_CREAT;
109+
rc = fs_open(&ptr->file, name, zmode);
95110

96-
rc = fs_open(&ptr->file, name, FS_O_CREATE | (mode & O_ACCMODE));
97-
if (rc < 0) {
98-
goto out_err;
99-
}
100-
rc = fs_close(&ptr->file);
101-
if (rc < 0) {
102-
goto out_err;
103-
}
104-
}
105-
106-
rc = fs_open(&ptr->file, name, zflags);
107111
if (rc < 0) {
108-
goto out_err;
109-
}
110-
111-
zvfs_finalize_fd(fd, ptr, &fs_fd_op_vtable);
112-
113-
goto out;
114-
115-
out_err:
116-
if (ptr != NULL) {
117112
posix_fs_free_obj(ptr);
113+
zvfs_free_fd(fd);
114+
errno = -rc;
115+
return -1;
118116
}
119117

120-
zvfs_free_fd(fd);
121-
errno = -rc;
122-
return -1;
118+
zvfs_finalize_fd(fd, ptr, &fs_fd_op_vtable);
123119

124-
out:
125120
return fd;
126121
}
127122

tests/posix/fs/src/test_fs_dir.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static int test_mkdir(void)
2727
return res;
2828
}
2929

30-
res = open(TEST_DIR_FILE, O_CREAT | O_RDWR, 0770);
30+
res = open(TEST_DIR_FILE, O_CREAT | O_RDWR);
3131

3232
if (res < 0) {
3333
TC_PRINT("Failed opening file [%d]\n", res);

tests/posix/fs/src/test_fs_file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static int test_file_open(void)
1616
{
1717
int res;
1818

19-
res = open(TEST_FILE, O_CREAT | O_RDWR, 0660);
19+
res = open(TEST_FILE, O_CREAT | O_RDWR);
2020
if (res < 0) {
2121
TC_ERROR("Failed opening file: %d, errno=%d\n", res, errno);
2222
/* FIXME: restructure tests as per #46897 */

tests/posix/fs/src/test_fs_open_flags.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static int test_file_open_flags(void)
6060

6161
/* 2 Create file for read only, attempt to read, attempt to write */
6262
TC_PRINT("Open on non-existent file, flags = O_CREAT | O_WRONLY\n");
63-
fd = open(THE_FILE, O_CREAT | O_WRONLY, 0440);
63+
fd = open(THE_FILE, O_CREAT | O_WRONLY);
6464
if (fd < 0) {
6565
TC_PRINT("Expected success; fd = %d, errno = %d\n", fd, errno);
6666
return TC_FAIL;
@@ -236,7 +236,7 @@ static int test_file_open_flags(void)
236236
TC_PRINT("Attempt write to file opened with O_APPEND | O_RDWR\n");
237237
/* Clean start */
238238
unlink(THE_FILE);
239-
fd = open(THE_FILE, O_CREAT | O_WRONLY, 0440);
239+
fd = open(THE_FILE, O_CREAT | O_WRONLY);
240240
if (fd < 0) {
241241
TC_PRINT("Expected success, fd = %d, errno = %d\n", fd, errno);
242242
return TC_FAIL;

tests/posix/fs/src/test_fs_stat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void create_file(const char *filename, uint32_t size)
2121
{
2222
int fh;
2323

24-
fh = open(filename, O_CREAT | O_WRONLY, 0440);
24+
fh = open(filename, O_CREAT | O_WRONLY);
2525
zassert(fh >= 0, "Failed creating test file");
2626

2727
uint8_t filling[FILL_SIZE];

0 commit comments

Comments
 (0)