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 <cfriedt@tenstorrent.com>
This commit is contained in:
parent
ab8b28ed7b
commit
748252aa76
6 changed files with 50 additions and 21 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <zephyr/posix/fcntl.h>
|
||||||
#include <zephyr/posix/poll.h>
|
#include <zephyr/posix/poll.h>
|
||||||
#include <zephyr/posix/unistd.h>
|
#include <zephyr/posix/unistd.h>
|
||||||
#include <zephyr/posix/sys/select.h>
|
#include <zephyr/posix/sys/select.h>
|
||||||
|
@ -16,28 +17,28 @@
|
||||||
int zvfs_close(int fd);
|
int zvfs_close(int fd);
|
||||||
FILE *zvfs_fdopen(int fd, const char *mode);
|
FILE *zvfs_fdopen(int fd, const char *mode);
|
||||||
int zvfs_fileno(FILE *file);
|
int zvfs_fileno(FILE *file);
|
||||||
int zvfs_open(const char *name, int flags);
|
int zvfs_open(const char *name, int flags, int mode);
|
||||||
ssize_t zvfs_read(int fd, void *buf, size_t sz, size_t *from_offset);
|
ssize_t zvfs_read(int fd, void *buf, size_t sz, size_t *from_offset);
|
||||||
ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset);
|
ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset);
|
||||||
|
|
||||||
void FD_CLR(int fd, struct zvfs_fd_set *fdset)
|
void FD_CLR(int fd, struct zvfs_fd_set *fdset)
|
||||||
{
|
{
|
||||||
return ZVFS_FD_CLR(fd, (struct zvfs_fd_set *)fdset);
|
return ZVFS_FD_CLR(fd, fdset);
|
||||||
}
|
}
|
||||||
|
|
||||||
int FD_ISSET(int fd, struct zvfs_fd_set *fdset)
|
int FD_ISSET(int fd, struct zvfs_fd_set *fdset)
|
||||||
{
|
{
|
||||||
return ZVFS_FD_ISSET(fd, (struct zvfs_fd_set *)fdset);
|
return ZVFS_FD_ISSET(fd, fdset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FD_SET(int fd, struct zvfs_fd_set *fdset)
|
void FD_SET(int fd, struct zvfs_fd_set *fdset)
|
||||||
{
|
{
|
||||||
ZVFS_FD_SET(fd, (struct zvfs_fd_set *)fdset);
|
ZVFS_FD_SET(fd, fdset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FD_ZERO(fd_set *fdset)
|
void FD_ZERO(fd_set *fdset)
|
||||||
{
|
{
|
||||||
ZVFS_FD_ZERO((struct zvfs_fd_set *)fdset);
|
ZVFS_FD_ZERO(fdset);
|
||||||
}
|
}
|
||||||
|
|
||||||
int close(int fd)
|
int close(int fd)
|
||||||
|
@ -60,8 +61,16 @@ int fileno(FILE *file)
|
||||||
|
|
||||||
int open(const char *name, int flags, ...)
|
int open(const char *name, int flags, ...)
|
||||||
{
|
{
|
||||||
/* FIXME: necessarily need to check for O_CREAT and unpack ... if set */
|
int mode = 0;
|
||||||
return zvfs_open(name, flags);
|
va_list args;
|
||||||
|
|
||||||
|
if ((flags & O_CREAT) != 0) {
|
||||||
|
va_start(args, flags);
|
||||||
|
mode = va_arg(args, int);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
return zvfs_open(name, flags, mode);
|
||||||
}
|
}
|
||||||
#ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_OPEN
|
#ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_OPEN
|
||||||
FUNC_ALIAS(open, _open, int);
|
FUNC_ALIAS(open, _open, int);
|
||||||
|
|
|
@ -85,7 +85,7 @@ static int posix_mode_to_zephyr(int mf)
|
||||||
return mode;
|
return mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int zvfs_open(const char *name, int flags)
|
int zvfs_open(const char *name, int flags, int mode)
|
||||||
{
|
{
|
||||||
int rc, fd;
|
int rc, fd;
|
||||||
struct posix_fs_desc *ptr = NULL;
|
struct posix_fs_desc *ptr = NULL;
|
||||||
|
@ -102,24 +102,44 @@ int zvfs_open(const char *name, int flags)
|
||||||
|
|
||||||
ptr = posix_fs_alloc_obj(false);
|
ptr = posix_fs_alloc_obj(false);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
zvfs_free_fd(fd);
|
rc = -EMFILE;
|
||||||
errno = EMFILE;
|
goto out_err;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs_file_t_init(&ptr->file);
|
fs_file_t_init(&ptr->file);
|
||||||
|
|
||||||
rc = fs_open(&ptr->file, name, zmode);
|
if (flags & O_CREAT) {
|
||||||
|
flags &= ~O_CREAT;
|
||||||
|
|
||||||
|
rc = fs_open(&ptr->file, name, FS_O_CREATE | (mode & O_ACCMODE));
|
||||||
|
if (rc < 0) {
|
||||||
|
goto out_err;
|
||||||
|
}
|
||||||
|
rc = fs_close(&ptr->file);
|
||||||
|
if (rc < 0) {
|
||||||
|
goto out_err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = fs_open(&ptr->file, name, zmode);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
posix_fs_free_obj(ptr);
|
goto out_err;
|
||||||
zvfs_free_fd(fd);
|
|
||||||
errno = -rc;
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zvfs_finalize_fd(fd, ptr, &fs_fd_op_vtable);
|
zvfs_finalize_fd(fd, ptr, &fs_fd_op_vtable);
|
||||||
|
|
||||||
|
goto out;
|
||||||
|
|
||||||
|
out_err:
|
||||||
|
if (ptr != NULL) {
|
||||||
|
posix_fs_free_obj(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
zvfs_free_fd(fd);
|
||||||
|
errno = -rc;
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
out:
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ static int test_mkdir(void)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = open(TEST_DIR_FILE, O_CREAT | O_RDWR);
|
res = open(TEST_DIR_FILE, O_CREAT | O_RDWR, 0770);
|
||||||
|
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
TC_PRINT("Failed opening file [%d]\n", res);
|
TC_PRINT("Failed opening file [%d]\n", res);
|
||||||
|
|
|
@ -16,7 +16,7 @@ static int test_file_open(void)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
res = open(TEST_FILE, O_CREAT | O_RDWR);
|
res = open(TEST_FILE, O_CREAT | O_RDWR, 0660);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
TC_ERROR("Failed opening file: %d, errno=%d\n", res, errno);
|
TC_ERROR("Failed opening file: %d, errno=%d\n", res, errno);
|
||||||
/* FIXME: restructure tests as per #46897 */
|
/* FIXME: restructure tests as per #46897 */
|
||||||
|
|
|
@ -60,7 +60,7 @@ static int test_file_open_flags(void)
|
||||||
|
|
||||||
/* 2 Create file for read only, attempt to read, attempt to write */
|
/* 2 Create file for read only, attempt to read, attempt to write */
|
||||||
TC_PRINT("Open on non-existent file, flags = O_CREAT | O_WRONLY\n");
|
TC_PRINT("Open on non-existent file, flags = O_CREAT | O_WRONLY\n");
|
||||||
fd = open(THE_FILE, O_CREAT | O_WRONLY);
|
fd = open(THE_FILE, O_CREAT | O_WRONLY, 0440);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
TC_PRINT("Expected success; fd = %d, errno = %d\n", fd, errno);
|
TC_PRINT("Expected success; fd = %d, errno = %d\n", fd, errno);
|
||||||
return TC_FAIL;
|
return TC_FAIL;
|
||||||
|
@ -236,7 +236,7 @@ static int test_file_open_flags(void)
|
||||||
TC_PRINT("Attempt write to file opened with O_APPEND | O_RDWR\n");
|
TC_PRINT("Attempt write to file opened with O_APPEND | O_RDWR\n");
|
||||||
/* Clean start */
|
/* Clean start */
|
||||||
unlink(THE_FILE);
|
unlink(THE_FILE);
|
||||||
fd = open(THE_FILE, O_CREAT | O_WRONLY);
|
fd = open(THE_FILE, O_CREAT | O_WRONLY, 0440);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
TC_PRINT("Expected success, fd = %d, errno = %d\n", fd, errno);
|
TC_PRINT("Expected success, fd = %d, errno = %d\n", fd, errno);
|
||||||
return TC_FAIL;
|
return TC_FAIL;
|
||||||
|
|
|
@ -24,7 +24,7 @@ static void create_file(const char *filename, uint32_t size)
|
||||||
{
|
{
|
||||||
int fh;
|
int fh;
|
||||||
|
|
||||||
fh = open(filename, O_CREAT | O_WRONLY);
|
fh = open(filename, O_CREAT | O_WRONLY, 0440);
|
||||||
zassert(fh >= 0, "Failed creating test file");
|
zassert(fh >= 0, "Failed creating test file");
|
||||||
|
|
||||||
uint8_t filling[FILL_SIZE];
|
uint8_t filling[FILL_SIZE];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue