Skip to content

Commit 5a95d35

Browse files
committed
[DFS] Add fcntl API (same with ioctl in RT-Thread).
1 parent 8246da6 commit 5a95d35

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

components/dfs/include/dfs_posix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* 2009-05-27 Yi.qiu The first version.
2323
* 2010-07-18 Bernard add stat and statfs structure definitions.
2424
* 2011-05-16 Yi.qiu Change parameter name of rename, "new" is C++ key word.
25+
* 2017-12-27 Bernard Add fcntl API.
2526
*/
2627

2728
#ifndef __DFS_POSIX_H__
@@ -66,6 +67,7 @@ int unlink(const char *pathname);
6667
int stat(const char *file, struct stat *buf);
6768
int fstat(int fildes, struct stat *buf);
6869
int fsync(int fildes);
70+
int fcntl(int fildes, int cmd, void *data);
6971
int ioctl(int fildes, int cmd, void *data);
7072

7173
/* directory api*/

components/dfs/src/dfs_file.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,29 @@ int dfs_file_close(struct dfs_fd *fd)
173173
*/
174174
int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
175175
{
176-
if (fd == NULL || fd->type != FT_REGULAR)
176+
if (fd == NULL)
177177
return -EINVAL;
178178

179+
/* regular file system fd */
180+
if (fd->type == FT_REGULAR)
181+
{
182+
switch (cmd)
183+
{
184+
case F_GETFL:
185+
return fd->flags; /* return flags */
186+
case F_SETFL:
187+
{
188+
int flags = (int)args;
189+
int mask = O_NONBLOCK | O_APPEND;
190+
191+
flags &= mask;
192+
fd->flags &= mask;
193+
fd->flags |= flags;
194+
}
195+
return 0;
196+
}
197+
}
198+
179199
if (fd->fops->ioctl != NULL)
180200
return fd->fops->ioctl(fd, cmd, args);
181201

components/dfs/src/dfs_posix.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,28 +428,46 @@ RTM_EXPORT(fsync);
428428
* @return 0 on successful completion. Otherwise, -1 shall be returned and errno
429429
* set to indicate the error.
430430
*/
431-
int ioctl(int fildes, int cmd, void *data)
431+
int fcntl(int fildes, int cmd, void *data)
432432
{
433-
int ret;
433+
int ret = -1;
434434
struct dfs_fd *d;
435435

436436
/* get the fd */
437437
d = fd_get(fildes);
438-
if (d == NULL)
438+
if (d)
439439
{
440-
rt_set_errno(-EBADF);
441-
return -1;
440+
ret = dfs_file_ioctl(d, cmd, data);
441+
fd_put(d);
442442
}
443+
else ret = -EBADF;
443444

444-
ret = dfs_file_ioctl(d, cmd, data);
445445
if (ret != 0)
446446
{
447447
rt_set_errno(ret);
448448
ret = -1;
449449
}
450-
fd_put(d);
451450

452-
return ret;
451+
return 0;
452+
}
453+
RTM_EXPORT(fcntl);
454+
455+
/**
456+
* this function is a POSIX compliant version, which shall perform a variety of
457+
* control functions on devices.
458+
*
459+
* @param fildes the file description
460+
* @param cmd the specified command
461+
* @param data represents the additional information that is needed by this
462+
* specific device to perform the requested function.
463+
*
464+
* @return 0 on successful completion. Otherwise, -1 shall be returned and errno
465+
* set to indicate the error.
466+
*/
467+
int ioctl(int fildes, int cmd, void *data)
468+
{
469+
/* we use fcntl for this API. */
470+
return fcntl(fildes, cmd, data);
453471
}
454472
RTM_EXPORT(ioctl);
455473

include/libc/libc_fcntl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
#define O_ACCMODE (_O_RDONLY | _O_WRONLY | _O_RDWR)
1717
#endif
1818

19+
#ifndef F_GETFL
20+
#define F_GETFL 3
21+
#endif
22+
#ifndef F_SETFL
23+
#define F_SETFL 4
24+
#endif
25+
1926
#else
2027
#define O_RDONLY 00
2128
#define O_WRONLY 01

0 commit comments

Comments
 (0)