summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorlizezhong233 <[email protected]>2026-11-23 13:53:09 +1100
committerGitHub <[email protected]>2026-11-23 10:53:09 +0800
commitdbdf2d3ef67a300ded90284073c6aafd789ffd9a (patch)
treebbfff6867dd94c9d030a7c6314aa120f607c75a6 /components
parentcde255bc54cbfa3d9eb3eec518953a0aded60e82 (diff)
[dfs][posix] Report open failures with positive errno #12067
Diffstat (limited to 'components')
-rw-r--r--components/dfs/dfs_v1/src/dfs.c34
-rw-r--r--components/dfs/dfs_v1/src/dfs_posix.c6
-rw-r--r--components/dfs/dfs_v2/src/dfs.c55
-rw-r--r--components/dfs/dfs_v2/src/dfs_posix.c18
-rw-r--r--components/dfs/utest/tc_posix_api.c60
5 files changed, 105 insertions, 68 deletions
diff --git a/components/dfs/dfs_v1/src/dfs.c b/components/dfs/dfs_v1/src/dfs.c
index 9a7c98cc33..5d3684d6c0 100644
--- a/components/dfs/dfs_v1/src/dfs.c
+++ b/components/dfs/dfs_v1/src/dfs.c
@@ -180,7 +180,7 @@ void dfs_file_unlock(void)
* @param fd The file descriptor that the table must accommodate.
* @return int
* - The input file descriptor `fd` if it is within the current or newly expanded table's capacity.
- * - `-1` if the requested file descriptor exceeds `DFS_FD_MAX` or memory allocation fails.
+ * - `-EMFILE` if the descriptor limit is reached, or `-ENOMEM` if allocation fails.
*/
static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
{
@@ -197,7 +197,7 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
/* If the file descriptor exceeds the maximum allowable limit, return an error.*/
if (fd >= DFS_FD_MAX)
{
- return -1;
+ return -EMFILE;
}
/* Calculate the new capacity, rounding up to the nearest multiple of 4.*/
@@ -213,7 +213,7 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *));
if (!fds)
{
- return -1;
+ return -ENOMEM;
}
/* clean the new allocated fds */
@@ -236,7 +236,7 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
* @param startfd The starting index for the search for an empty slot.
* @return int
* - The index of the first available slot if successful.
- * - `-1` if no slot is available or if table expansion fails
+ * - `-EMFILE` if the descriptor limit is reached, or `-ENOMEM` if allocation fails.
*/
static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
{
@@ -256,11 +256,7 @@ static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
{
idx = startfd;
}
- if (fd_slot_expand(fdt, idx) < 0)
- {
- return -1;
- }
- return idx;
+ return fd_slot_expand(fdt, idx);
}
/**
@@ -271,7 +267,7 @@ static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
*
* @return
* - The index of the allocated file descriptor if successful.
- * - `-1` if no slot is available or memory allocation fails.
+ * - `-EMFILE` if the descriptor limit is reached, or `-ENOMEM` if allocation fails.
*/
static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
{
@@ -280,15 +276,15 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
idx = fd_slot_alloc(fdt, startfd);
- /* allocate 'struct dfs_file' */
if (idx < 0)
{
- return -1;
+ return idx;
}
+
fd = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
if (!fd)
{
- return -1;
+ return -ENOMEM;
}
fd->ref_count = 1;
fd->magic = DFS_FD_MAGIC;
@@ -302,7 +298,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
* @ingroup group_fs_file_descriptor
* This function will allocate a file descriptor.
*
- * @return -1 on failed or the allocated file descriptor.
+ * @return The allocated descriptor, or -1 with errno set on failure.
*/
int fdt_fd_new(struct dfs_fdtable *fdt)
{
@@ -314,13 +310,13 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
/* find an empty fd entry */
idx = fd_alloc(fdt, DFS_STDIO_OFFSET);
- /* can't find an empty fd entry */
+ dfs_file_unlock();
if (idx < 0)
{
- LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
+ LOG_E("Failed to allocate a file descriptor.");
+ rt_set_errno(-idx);
+ return -1;
}
-
- dfs_file_unlock();
return idx;
}
@@ -479,7 +475,7 @@ rt_err_t sys_dup(int oldfd)
}
exit:
dfs_file_unlock();
- return newfd;
+ return newfd < 0 ? -1 : newfd;
}
/**
diff --git a/components/dfs/dfs_v1/src/dfs_posix.c b/components/dfs/dfs_v1/src/dfs_posix.c
index 6e40a175f6..425b6be267 100644
--- a/components/dfs/dfs_v1/src/dfs_posix.c
+++ b/components/dfs/dfs_v1/src/dfs_posix.c
@@ -40,7 +40,7 @@
* - `O_EXCL`: Ensure that `O_CREAT` creates the file exclusively.
* - Other platform-specific flags
*
- * @return the non-negative integer on successful open, others for failed.
+ * @return A non-negative file descriptor on success, or -1 with errno set on failure.
*/
int open(const char *file, int flags, ...)
{
@@ -51,8 +51,6 @@ int open(const char *file, int flags, ...)
fd = fd_new();
if (fd < 0)
{
- rt_set_errno(-ENOMEM);
-
return -1;
}
d = fd_get(fd);
@@ -63,7 +61,7 @@ int open(const char *file, int flags, ...)
/* release the ref-count of fd */
fd_release(fd);
- rt_set_errno(result);
+ rt_set_errno(-result);
return -1;
}
diff --git a/components/dfs/dfs_v2/src/dfs.c b/components/dfs/dfs_v2/src/dfs.c
index f1601f0003..cf808adec0 100644
--- a/components/dfs/dfs_v2/src/dfs.c
+++ b/components/dfs/dfs_v2/src/dfs.c
@@ -50,7 +50,7 @@ static struct dfs_fdtable _fdtab = {0};
* @param[in] fd The file descriptor that needs to be accommodated
*
* @return int The input file descriptor if successful (fd >= 0),
- * -1 if expansion failed or fd exceeds maximum allowed value (DFS_FD_MAX)
+ * -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
*
* @note Expand table size to next multiple of 4 (but not exceeding DFS_FD_MAX)
*/
@@ -66,7 +66,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
}
if (fd >= DFS_FD_MAX)
{
- return -1;
+ return -EMFILE;
}
nr = ((fd + 4) & ~3);
@@ -77,7 +77,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *));
if (!fds)
{
- return -1;
+ return -ENOMEM;
}
/* clean the new allocated fds */
@@ -98,7 +98,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
* @param[in] startfd The starting file descriptor index to begin searching
*
* @return int The allocated file descriptor index if successful (>= 0),
- * -1 if allocation failed (table expansion failed)
+ * -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
*
* @note If no empty slot found, expand the table and return the new index.
*/
@@ -121,12 +121,7 @@ static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd)
idx = startfd;
}
- if (_fdt_slot_expand(fdt, idx) < 0)
- {
- return -1;
- }
-
- return idx;
+ return _fdt_slot_expand(fdt, idx);
}
/**
@@ -136,7 +131,7 @@ static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd)
* @param[in] startfd The starting file descriptor index to begin searching
*
* @return int The allocated file descriptor index if successful (>= 0),
- * -1 if allocation failed
+ * -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
*
* @note This is a wrapper function that calls _fdt_slot_alloc() to perform
* the actual allocation. It maintains the same behavior as _fdt_slot_alloc().
@@ -294,51 +289,43 @@ void dfs_file_destroy(struct dfs_file *file)
*
* @return int The allocated file descriptor index if successful (>= 0),
* -RT_ENOSYS if filesystem lock failed,
- * -1 if allocation failed (no empty slots or memory allocation failed)
+ * -1 if allocation failed. On failure, errno is set to the cause.
*/
int fdt_fd_new(struct dfs_fdtable *fdt)
{
- int idx = -1;
+ int idx;
/* lock filesystem */
if (dfs_file_lock() != RT_EOK)
{
+ rt_set_errno(ENOSYS);
return -RT_ENOSYS;
}
/* find an empty fd entry */
idx = _fdt_fd_alloc(fdt, (fdt == &_fdtab) ? DFS_STDIO_OFFSET : 0);
- /* can't find an empty fd entry */
- if (idx < 0)
+ if (idx >= 0)
{
- LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
- }
- else if (!fdt->fds[idx])
- {
- struct dfs_file *file;
-
- file = dfs_file_create();
+ struct dfs_file *file = dfs_file_create();
if (file)
{
fdt->fds[idx] = file;
-
LOG_D("allocate a new fd @ %d", idx);
}
else
{
- fdt->fds[idx] = RT_NULL;
- idx = -1;
+ idx = -ENOMEM;
}
}
- else
- {
- LOG_E("DFS not found an empty fds entry.");
- idx = -1;
- }
dfs_file_unlock();
-
+ if (idx < 0)
+ {
+ LOG_E("Failed to allocate a file descriptor.");
+ rt_set_errno(-idx);
+ return -1;
+ }
return idx;
}
@@ -459,7 +446,7 @@ exit:
* @brief Allocate a new file descriptor in current process's file descriptor table
*
* @return int The allocated file descriptor index if successful (>= 0),
- * -1 if allocation failed
+ * a negative value with errno set on failure
*/
int fd_new(void)
{
@@ -814,7 +801,7 @@ int dfs_dup(int oldfd, int startfd)
}
exit:
dfs_file_unlock();
- return newfd;
+ return newfd < 0 ? -1 : newfd;
}
/**
@@ -862,7 +849,7 @@ int dfs_dup_to(int oldfd, struct dfs_fdtable *fdtab)
exit:
dfs_file_unlock();
- return newfd;
+ return newfd < 0 ? -1 : newfd;
}
/**
diff --git a/components/dfs/dfs_v2/src/dfs_posix.c b/components/dfs/dfs_v2/src/dfs_posix.c
index 5044bec748..692d026c24 100644
--- a/components/dfs/dfs_v2/src/dfs_posix.c
+++ b/components/dfs/dfs_v2/src/dfs_posix.c
@@ -43,17 +43,17 @@
* - `O_EXCL`: Ensure that `O_CREAT` creates the file exclusively.
* - Other platform-specific flags
*
- * @return the non-negative integer on successful open, others for failed.
+ * @return A non-negative file descriptor on success, or -1 with errno set on failure.
*/
int open(const char *file, int flags, ...)
{
int fd, result;
- struct dfs_file *df = RT_NULL;
+ struct dfs_file *df;
mode_t mode = 0;
if (file == NULL)
{
- rt_set_errno(-EBADF);
+ rt_set_errno(EBADF);
return -1;
}
@@ -66,21 +66,17 @@ int open(const char *file, int flags, ...)
}
fd = fd_new();
- if (fd >= 0)
- {
- df = fd_get(fd);
- }
- else
+ if (fd < 0)
{
- rt_set_errno(-RT_ERROR);
- return RT_NULL;
+ return -1;
}
+ df = fd_get(fd);
result = dfs_file_open(df, file, flags, mode);
if (result < 0)
{
fd_release(fd);
- rt_set_errno(result);
+ rt_set_errno(-result);
return -1;
}
diff --git a/components/dfs/utest/tc_posix_api.c b/components/dfs/utest/tc_posix_api.c
index ded1eef780..9c6155b9d6 100644
--- a/components/dfs/utest/tc_posix_api.c
+++ b/components/dfs/utest/tc_posix_api.c
@@ -151,6 +151,64 @@ static void test_posix_close(void)
fd = -1;
}
+static void test_posix_open_full(void)
+{
+ int *held = rt_calloc(DFS_FD_MAX, sizeof(*held));
+ int count = 0;
+ int result, error;
+
+ uassert_not_null(held);
+ if (!held)
+ {
+ return;
+ }
+
+ /* Reserve slots without opening additional files on the test filesystem. */
+ while (count < DFS_FD_MAX)
+ {
+ result = fd_new();
+ if (result < 0)
+ {
+ break;
+ }
+ held[count++] = result;
+ }
+
+ errno = 0;
+ result = open(TEST_FILE, O_RDONLY);
+ error = errno;
+ while (count > 0)
+ {
+ fd_release(held[--count]);
+ }
+ rt_free(held);
+
+ uassert_int_equal(result, -1);
+ uassert_int_equal(error, EMFILE);
+
+ result = open(TEST_FILE, O_RDONLY);
+ uassert_true(result >= 0);
+ if (result >= 0)
+ {
+ uassert_int_equal(close(result), 0);
+ }
+}
+
+static void test_posix_open_missing(void)
+{
+ int result, error;
+
+ errno = 0;
+ result = open(TEST_FILE, O_RDONLY);
+ error = errno;
+ uassert_int_equal(result, -1);
+ uassert_int_equal(error, ENOENT);
+ if (result >= 0)
+ {
+ close(result);
+ }
+}
+
static void test_posix_stat(void)
{
struct stat stat_buf;
@@ -514,8 +572,10 @@ static void testcase(void)
UTEST_UNIT_RUN(test_posix_write);
UTEST_UNIT_RUN(test_posix_read);
UTEST_UNIT_RUN(test_posix_close);
+ UTEST_UNIT_RUN(test_posix_open_full);
UTEST_UNIT_RUN(test_posix_stat);
UTEST_UNIT_RUN(test_posix_unlink);
+ UTEST_UNIT_RUN(test_posix_open_missing);
UTEST_UNIT_RUN(test_posix_mkdir);
UTEST_UNIT_RUN(test_posix_rmdir);
UTEST_UNIT_RUN(test_posix_rename);