Skip to content

Commit 499a633

Browse files
cfriedtnashif
authored andcommitted
posix: device_io: use mode argument correctly in open()
Previously, we had only used the flags field and ignored mode with the open() function. Signed-off-by: Chris Friedt <[email protected]>
1 parent 48dff55 commit 499a633

File tree

7 files changed

+67
-50
lines changed

7 files changed

+67
-50
lines changed

include/zephyr/posix/fcntl.h

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

1010
#ifdef CONFIG_PICOLIBC
11-
#define O_CREAT 0x0040
11+
#define O_CREAT 0x0040
12+
#define O_TRUNC 0x0200
13+
#define O_APPEND 0x0400
1214
#else
13-
#define O_CREAT 0x0200
15+
#define O_APPEND 0x0008
16+
#define O_CREAT 0x0200
17+
#define O_TRUNC 0x0400
1418
#endif
1519

1620
#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
@@ -19,7 +23,6 @@
1923
#define O_WRONLY 01
2024
#define O_RDWR 02
2125

22-
#define O_APPEND 0x0400
2326
#define O_EXCL 0x0800
2427
#define O_NONBLOCK 0x4000
2528

lib/posix/options/device_io.c

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

11+
#include <zephyr/posix/fcntl.h>
1112
#include <zephyr/posix/poll.h>
1213
#include <zephyr/posix/unistd.h>
1314
#include <zephyr/posix/sys/select.h>
@@ -16,28 +17,28 @@
1617
int zvfs_close(int fd);
1718
FILE *zvfs_fdopen(int fd, const char *mode);
1819
int zvfs_fileno(FILE *file);
19-
int zvfs_open(const char *name, int flags);
20+
int zvfs_open(const char *name, int flags, int mode);
2021
ssize_t zvfs_read(int fd, void *buf, size_t sz, size_t *from_offset);
2122
ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset);
2223

2324
void FD_CLR(int fd, struct zvfs_fd_set *fdset)
2425
{
25-
return ZVFS_FD_CLR(fd, (struct zvfs_fd_set *)fdset);
26+
return ZVFS_FD_CLR(fd, fdset);
2627
}
2728

2829
int FD_ISSET(int fd, struct zvfs_fd_set *fdset)
2930
{
30-
return ZVFS_FD_ISSET(fd, (struct zvfs_fd_set *)fdset);
31+
return ZVFS_FD_ISSET(fd, fdset);
3132
}
3233

3334
void FD_SET(int fd, struct zvfs_fd_set *fdset)
3435
{
35-
ZVFS_FD_SET(fd, (struct zvfs_fd_set *)fdset);
36+
ZVFS_FD_SET(fd, fdset);
3637
}
3738

3839
void FD_ZERO(fd_set *fdset)
3940
{
40-
ZVFS_FD_ZERO((struct zvfs_fd_set *)fdset);
41+
ZVFS_FD_ZERO(fdset);
4142
}
4243

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

6162
int open(const char *name, int flags, ...)
6263
{
63-
/* FIXME: necessarily need to check for O_CREAT and unpack ... if set */
64-
return zvfs_open(name, flags);
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);
6574
}
6675
#ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_OPEN
6776
FUNC_ALIAS(open, _open, int);

lib/posix/options/fs.c

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

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)
62+
int zvfs_open(const char *name, int flags, int mode)
8663
{
8764
int rc, fd;
8865
struct posix_fs_desc *ptr = NULL;
89-
int zmode = posix_mode_to_zephyr(flags);
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+
}
9075

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

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

10085
ptr = posix_fs_alloc_obj(false);
10186
if (ptr == NULL) {
102-
zvfs_free_fd(fd);
103-
errno = EMFILE;
104-
return -1;
87+
rc = -EMFILE;
88+
goto out_err;
10589
}
10690

10791
fs_file_t_init(&ptr->file);
10892

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

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);
111107
if (rc < 0) {
112-
posix_fs_free_obj(ptr);
113-
zvfs_free_fd(fd);
114-
errno = -rc;
115-
return -1;
108+
goto out_err;
116109
}
117110

118111
zvfs_finalize_fd(fd, ptr, &fs_fd_op_vtable);
119112

113+
goto out;
114+
115+
out_err:
116+
if (ptr != NULL) {
117+
posix_fs_free_obj(ptr);
118+
}
119+
120+
zvfs_free_fd(fd);
121+
errno = -rc;
122+
return -1;
123+
124+
out:
120125
return fd;
121126
}
122127

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);
30+
res = open(TEST_DIR_FILE, O_CREAT | O_RDWR, 0770);
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);
19+
res = open(TEST_FILE, O_CREAT | O_RDWR, 0660);
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);
63+
fd = open(THE_FILE, O_CREAT | O_WRONLY, 0440);
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);
239+
fd = open(THE_FILE, O_CREAT | O_WRONLY, 0440);
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);
24+
fh = open(filename, O_CREAT | O_WRONLY, 0440);
2525
zassert(fh >= 0, "Failed creating test file");
2626

2727
uint8_t filling[FILL_SIZE];

0 commit comments

Comments
 (0)