Skip to content

Commit dbdf2d3

Browse files
authored
[dfs][posix] Report open failures with positive errno #12067
1 parent cde255b commit dbdf2d3

6 files changed

Lines changed: 108 additions & 71 deletions

File tree

‎.github/utest/configs/components/dfs.cfg‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ CONFIG_RT_CONSOLEBUF_SIZE=1024
33
CONFIG_RT_NAME_MAX=24
44
CONFIG_RT_USING_CI_ACTION=y
55

6-
CONFIG_RT_UTEST_DFS_API_TC=y
7-
CONFIG_RT_UTEST_POSIX_API_TC=y
6+
CONFIG_RT_UTEST_TC_USING_DFS_API=y
7+
CONFIG_RT_UTEST_TC_USING_POSIX_API=y
88
CONFIG_RT_UTEST_DFS_MNT_PATH=""
99
CONFIG_RT_UTEST_DFS_FS_TYPE="elm"
10-
CONFIG_RT_UTEST_DFS_BLOCK_DEV="sd0"
10+
CONFIG_RT_UTEST_DFS_BLOCK_DEV="sd"

‎components/dfs/dfs_v1/src/dfs.c‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void dfs_file_unlock(void)
180180
* @param fd The file descriptor that the table must accommodate.
181181
* @return int
182182
* - The input file descriptor `fd` if it is within the current or newly expanded table's capacity.
183-
* - `-1` if the requested file descriptor exceeds `DFS_FD_MAX` or memory allocation fails.
183+
* - `-EMFILE` if the descriptor limit is reached, or `-ENOMEM` if allocation fails.
184184
*/
185185
static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
186186
{
@@ -197,7 +197,7 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
197197
/* If the file descriptor exceeds the maximum allowable limit, return an error.*/
198198
if (fd >= DFS_FD_MAX)
199199
{
200-
return -1;
200+
return -EMFILE;
201201
}
202202

203203
/* 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)
213213
fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *));
214214
if (!fds)
215215
{
216-
return -1;
216+
return -ENOMEM;
217217
}
218218

219219
/* clean the new allocated fds */
@@ -236,7 +236,7 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
236236
* @param startfd The starting index for the search for an empty slot.
237237
* @return int
238238
* - The index of the first available slot if successful.
239-
* - `-1` if no slot is available or if table expansion fails
239+
* - `-EMFILE` if the descriptor limit is reached, or `-ENOMEM` if allocation fails.
240240
*/
241241
static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
242242
{
@@ -256,11 +256,7 @@ static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
256256
{
257257
idx = startfd;
258258
}
259-
if (fd_slot_expand(fdt, idx) < 0)
260-
{
261-
return -1;
262-
}
263-
return idx;
259+
return fd_slot_expand(fdt, idx);
264260
}
265261

266262
/**
@@ -271,7 +267,7 @@ static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
271267
*
272268
* @return
273269
* - The index of the allocated file descriptor if successful.
274-
* - `-1` if no slot is available or memory allocation fails.
270+
* - `-EMFILE` if the descriptor limit is reached, or `-ENOMEM` if allocation fails.
275271
*/
276272
static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
277273
{
@@ -280,15 +276,15 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
280276

281277
idx = fd_slot_alloc(fdt, startfd);
282278

283-
/* allocate 'struct dfs_file' */
284279
if (idx < 0)
285280
{
286-
return -1;
281+
return idx;
287282
}
283+
288284
fd = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
289285
if (!fd)
290286
{
291-
return -1;
287+
return -ENOMEM;
292288
}
293289
fd->ref_count = 1;
294290
fd->magic = DFS_FD_MAGIC;
@@ -302,7 +298,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
302298
* @ingroup group_fs_file_descriptor
303299
* This function will allocate a file descriptor.
304300
*
305-
* @return -1 on failed or the allocated file descriptor.
301+
* @return The allocated descriptor, or -1 with errno set on failure.
306302
*/
307303
int fdt_fd_new(struct dfs_fdtable *fdt)
308304
{
@@ -314,13 +310,13 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
314310
/* find an empty fd entry */
315311
idx = fd_alloc(fdt, DFS_STDIO_OFFSET);
316312

317-
/* can't find an empty fd entry */
313+
dfs_file_unlock();
318314
if (idx < 0)
319315
{
320-
LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
316+
LOG_E("Failed to allocate a file descriptor.");
317+
rt_set_errno(-idx);
318+
return -1;
321319
}
322-
323-
dfs_file_unlock();
324320
return idx;
325321
}
326322

@@ -479,7 +475,7 @@ rt_err_t sys_dup(int oldfd)
479475
}
480476
exit:
481477
dfs_file_unlock();
482-
return newfd;
478+
return newfd < 0 ? -1 : newfd;
483479
}
484480

485481
/**

‎components/dfs/dfs_v1/src/dfs_posix.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* - `O_EXCL`: Ensure that `O_CREAT` creates the file exclusively.
4141
* - Other platform-specific flags
4242
*
43-
* @return the non-negative integer on successful open, others for failed.
43+
* @return A non-negative file descriptor on success, or -1 with errno set on failure.
4444
*/
4545
int open(const char *file, int flags, ...)
4646
{
@@ -51,8 +51,6 @@ int open(const char *file, int flags, ...)
5151
fd = fd_new();
5252
if (fd < 0)
5353
{
54-
rt_set_errno(-ENOMEM);
55-
5654
return -1;
5755
}
5856
d = fd_get(fd);
@@ -63,7 +61,7 @@ int open(const char *file, int flags, ...)
6361
/* release the ref-count of fd */
6462
fd_release(fd);
6563

66-
rt_set_errno(result);
64+
rt_set_errno(-result);
6765

6866
return -1;
6967
}

‎components/dfs/dfs_v2/src/dfs.c‎

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static struct dfs_fdtable _fdtab = {0};
5050
* @param[in] fd The file descriptor that needs to be accommodated
5151
*
5252
* @return int The input file descriptor if successful (fd >= 0),
53-
* -1 if expansion failed or fd exceeds maximum allowed value (DFS_FD_MAX)
53+
* -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
5454
*
5555
* @note Expand table size to next multiple of 4 (but not exceeding DFS_FD_MAX)
5656
*/
@@ -66,7 +66,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
6666
}
6767
if (fd >= DFS_FD_MAX)
6868
{
69-
return -1;
69+
return -EMFILE;
7070
}
7171

7272
nr = ((fd + 4) & ~3);
@@ -77,7 +77,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
7777
fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *));
7878
if (!fds)
7979
{
80-
return -1;
80+
return -ENOMEM;
8181
}
8282

8383
/* clean the new allocated fds */
@@ -98,7 +98,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
9898
* @param[in] startfd The starting file descriptor index to begin searching
9999
*
100100
* @return int The allocated file descriptor index if successful (>= 0),
101-
* -1 if allocation failed (table expansion failed)
101+
* -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
102102
*
103103
* @note If no empty slot found, expand the table and return the new index.
104104
*/
@@ -121,12 +121,7 @@ static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd)
121121
idx = startfd;
122122
}
123123

124-
if (_fdt_slot_expand(fdt, idx) < 0)
125-
{
126-
return -1;
127-
}
128-
129-
return idx;
124+
return _fdt_slot_expand(fdt, idx);
130125
}
131126

132127
/**
@@ -136,7 +131,7 @@ static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd)
136131
* @param[in] startfd The starting file descriptor index to begin searching
137132
*
138133
* @return int The allocated file descriptor index if successful (>= 0),
139-
* -1 if allocation failed
134+
* -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
140135
*
141136
* @note This is a wrapper function that calls _fdt_slot_alloc() to perform
142137
* the actual allocation. It maintains the same behavior as _fdt_slot_alloc().
@@ -294,51 +289,43 @@ void dfs_file_destroy(struct dfs_file *file)
294289
*
295290
* @return int The allocated file descriptor index if successful (>= 0),
296291
* -RT_ENOSYS if filesystem lock failed,
297-
* -1 if allocation failed (no empty slots or memory allocation failed)
292+
* -1 if allocation failed. On failure, errno is set to the cause.
298293
*/
299294
int fdt_fd_new(struct dfs_fdtable *fdt)
300295
{
301-
int idx = -1;
296+
int idx;
302297

303298
/* lock filesystem */
304299
if (dfs_file_lock() != RT_EOK)
305300
{
301+
rt_set_errno(ENOSYS);
306302
return -RT_ENOSYS;
307303
}
308304

309305
/* find an empty fd entry */
310306
idx = _fdt_fd_alloc(fdt, (fdt == &_fdtab) ? DFS_STDIO_OFFSET : 0);
311-
/* can't find an empty fd entry */
312-
if (idx < 0)
307+
if (idx >= 0)
313308
{
314-
LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
315-
}
316-
else if (!fdt->fds[idx])
317-
{
318-
struct dfs_file *file;
319-
320-
file = dfs_file_create();
309+
struct dfs_file *file = dfs_file_create();
321310

322311
if (file)
323312
{
324313
fdt->fds[idx] = file;
325-
326314
LOG_D("allocate a new fd @ %d", idx);
327315
}
328316
else
329317
{
330-
fdt->fds[idx] = RT_NULL;
331-
idx = -1;
318+
idx = -ENOMEM;
332319
}
333320
}
334-
else
335-
{
336-
LOG_E("DFS not found an empty fds entry.");
337-
idx = -1;
338-
}
339321

340322
dfs_file_unlock();
341-
323+
if (idx < 0)
324+
{
325+
LOG_E("Failed to allocate a file descriptor.");
326+
rt_set_errno(-idx);
327+
return -1;
328+
}
342329
return idx;
343330
}
344331

@@ -459,7 +446,7 @@ int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file
459446
* @brief Allocate a new file descriptor in current process's file descriptor table
460447
*
461448
* @return int The allocated file descriptor index if successful (>= 0),
462-
* -1 if allocation failed
449+
* a negative value with errno set on failure
463450
*/
464451
int fd_new(void)
465452
{
@@ -814,7 +801,7 @@ int dfs_dup(int oldfd, int startfd)
814801
}
815802
exit:
816803
dfs_file_unlock();
817-
return newfd;
804+
return newfd < 0 ? -1 : newfd;
818805
}
819806

820807
/**
@@ -862,7 +849,7 @@ int dfs_dup_to(int oldfd, struct dfs_fdtable *fdtab)
862849
exit:
863850
dfs_file_unlock();
864851

865-
return newfd;
852+
return newfd < 0 ? -1 : newfd;
866853
}
867854

868855
/**

‎components/dfs/dfs_v2/src/dfs_posix.c‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@
4343
* - `O_EXCL`: Ensure that `O_CREAT` creates the file exclusively.
4444
* - Other platform-specific flags
4545
*
46-
* @return the non-negative integer on successful open, others for failed.
46+
* @return A non-negative file descriptor on success, or -1 with errno set on failure.
4747
*/
4848
int open(const char *file, int flags, ...)
4949
{
5050
int fd, result;
51-
struct dfs_file *df = RT_NULL;
51+
struct dfs_file *df;
5252
mode_t mode = 0;
5353

5454
if (file == NULL)
5555
{
56-
rt_set_errno(-EBADF);
56+
rt_set_errno(EBADF);
5757
return -1;
5858
}
5959

@@ -66,21 +66,17 @@ int open(const char *file, int flags, ...)
6666
}
6767

6868
fd = fd_new();
69-
if (fd >= 0)
70-
{
71-
df = fd_get(fd);
72-
}
73-
else
69+
if (fd < 0)
7470
{
75-
rt_set_errno(-RT_ERROR);
76-
return RT_NULL;
71+
return -1;
7772
}
73+
df = fd_get(fd);
7874

7975
result = dfs_file_open(df, file, flags, mode);
8076
if (result < 0)
8177
{
8278
fd_release(fd);
83-
rt_set_errno(result);
79+
rt_set_errno(-result);
8480
return -1;
8581
}
8682

0 commit comments

Comments
 (0)