Skip to content

Commit b6e58d5

Browse files
pfalconcarlescufi
authored andcommitted
lib: posix: fs: Convert to use generic fdtable
All the handling of POSIX file descriptors is now done by fdtable.c. fs.c still manages its own table of file structures of the underlying fs lib. Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent f484bba commit b6e58d5

File tree

2 files changed

+83
-124
lines changed

2 files changed

+83
-124
lines changed

include/posix/dirent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C" {
1616
#ifdef CONFIG_POSIX_FS
1717
#include <fs.h>
1818

19-
typedef struct fs_dir_t DIR;
19+
typedef void DIR;
2020

2121
struct dirent {
2222
unsigned int d_ino;

lib/posix/fs.c

Lines changed: 82 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
#include <posix/unistd.h>
1111
#include <posix/dirent.h>
1212
#include <string.h>
13+
#include <misc/fdtable.h>
1314

1415
BUILD_ASSERT_MSG(PATH_MAX > MAX_FILE_NAME,
1516
"PATH_MAX is less than MAX_FILE_NAME");
1617

17-
union file_desc {
18-
struct fs_file_t file;
19-
struct fs_dir_t dir;
20-
};
21-
2218
struct posix_fs_desc {
23-
union file_desc desc;
19+
union {
20+
struct fs_file_t file;
21+
struct fs_dir_t dir;
22+
};
2423
bool is_dir;
2524
bool used;
2625
};
@@ -30,63 +29,30 @@ static struct posix_fs_desc desc_array[CONFIG_POSIX_MAX_OPEN_FILES];
3029
static struct fs_dirent fdirent;
3130
static struct dirent pdirent;
3231

33-
static int posix_fs_alloc_fd(union file_desc **ptr, bool is_dir)
32+
static struct fd_op_vtable fs_fd_op_vtable;
33+
34+
static struct posix_fs_desc *posix_fs_alloc_obj(bool is_dir)
3435
{
35-
int fd;
36+
int i;
37+
struct posix_fs_desc *ptr = NULL;
3638
unsigned int key = irq_lock();
3739

38-
for (fd = 0; fd < CONFIG_POSIX_MAX_OPEN_FILES; fd++) {
39-
if (desc_array[fd].used == false) {
40-
*ptr = &desc_array[fd].desc;
41-
desc_array[fd].used = true;
42-
desc_array[fd].is_dir = is_dir;
40+
for (i = 0; i < CONFIG_POSIX_MAX_OPEN_FILES; i++) {
41+
if (desc_array[i].used == false) {
42+
ptr = &desc_array[i];
43+
ptr->used = true;
44+
ptr->is_dir = is_dir;
4345
break;
4446
}
4547
}
4648
irq_unlock(key);
4749

48-
if (fd >= CONFIG_POSIX_MAX_OPEN_FILES) {
49-
return -1;
50-
}
51-
52-
return fd;
53-
}
54-
55-
static int posix_fs_get_ptr(int fd, union file_desc **ptr, bool is_dir)
56-
{
57-
int rc = 0;
58-
unsigned int key;
59-
60-
if (fd < 0 || fd >= CONFIG_POSIX_MAX_OPEN_FILES) {
61-
return -1;
62-
}
63-
64-
key = irq_lock();
65-
66-
if ((desc_array[fd].used == true) &&
67-
(desc_array[fd].is_dir == is_dir)) {
68-
*ptr = &desc_array[fd].desc;
69-
} else {
70-
rc = -1;
71-
}
72-
irq_unlock(key);
73-
74-
return rc;
75-
}
76-
77-
static inline void posix_fs_free_ptr(struct posix_fs_desc *ptr)
78-
{
79-
struct posix_fs_desc *desc = ptr;
80-
unsigned int key = irq_lock();
81-
82-
desc->used = false;
83-
desc->is_dir = false;
84-
irq_unlock(key);
50+
return ptr;
8551
}
8652

87-
static inline void posix_fs_free_fd(int fd)
53+
static inline void posix_fs_free_obj(struct posix_fs_desc *ptr)
8854
{
89-
posix_fs_free_ptr(&desc_array[fd]);
55+
ptr->used = false;
9056
}
9157

9258
/**
@@ -97,46 +63,65 @@ static inline void posix_fs_free_fd(int fd)
9763
int open(const char *name, int flags)
9864
{
9965
int rc, fd;
100-
struct fs_file_t *ptr = NULL;
66+
struct posix_fs_desc *ptr = NULL;
10167

10268
ARG_UNUSED(flags);
10369

104-
fd = posix_fs_alloc_fd((union file_desc **)&ptr, false);
105-
if ((fd < 0) || (ptr == NULL)) {
106-
errno = ENFILE;
70+
fd = z_reserve_fd();
71+
if (fd < 0) {
72+
return -1;
73+
}
74+
75+
ptr = posix_fs_alloc_obj(false);
76+
if (ptr == NULL) {
77+
z_free_fd(fd);
78+
errno = EMFILE;
10779
return -1;
10880
}
109-
(void)memset(ptr, 0, sizeof(struct fs_file_t));
11081

111-
rc = fs_open(ptr, name);
82+
(void)memset(&ptr->file, 0, sizeof(ptr->file));
83+
84+
rc = fs_open(&ptr->file, name);
11285
if (rc < 0) {
113-
posix_fs_free_fd(fd);
86+
posix_fs_free_obj(ptr);
87+
z_free_fd(fd);
11488
errno = -rc;
11589
return -1;
11690
}
11791

92+
z_finalize_fd(fd, ptr, &fs_fd_op_vtable);
93+
11894
return fd;
11995
}
12096

121-
/**
122-
* @brief Close a file descriptor.
123-
*
124-
* See IEEE 1003.1
125-
*/
126-
int close(int fd)
97+
static int fs_ioctl_vmeth(void *obj, unsigned int request, ...)
12798
{
12899
int rc;
129-
struct fs_file_t *ptr = NULL;
100+
struct posix_fs_desc *ptr = obj;
130101

131-
if (posix_fs_get_ptr(fd, (union file_desc **)&ptr, false)) {
132-
errno = EBADF;
133-
return -1;
134-
}
102+
switch (request) {
103+
case ZFD_IOCTL_CLOSE:
104+
rc = fs_close(&ptr->file);
105+
break;
106+
107+
case ZFD_IOCTL_LSEEK: {
108+
va_list args;
109+
off_t offset;
110+
int whence;
135111

136-
rc = fs_close(ptr);
112+
va_start(args, request);
113+
offset = va_arg(args, off_t);
114+
whence = va_arg(args, int);
115+
va_end(args);
116+
117+
rc = fs_seek(&ptr->file, offset, whence);
118+
break;
119+
}
137120

138-
/* Free file ptr memory */
139-
posix_fs_free_fd(fd);
121+
default:
122+
errno = EOPNOTSUPP;
123+
return -1;
124+
}
140125

141126
if (rc < 0) {
142127
errno = -rc;
@@ -151,17 +136,12 @@ int close(int fd)
151136
*
152137
* See IEEE 1003.1
153138
*/
154-
ssize_t write(int fd, const void *buffer, size_t count)
139+
static ssize_t fs_write_vmeth(void *obj, const void *buffer, size_t count)
155140
{
156141
ssize_t rc;
157-
struct fs_file_t *ptr = NULL;
142+
struct posix_fs_desc *ptr = obj;
158143

159-
if (posix_fs_get_ptr(fd, (union file_desc **)&ptr, false)) {
160-
errno = EBADF;
161-
return -1;
162-
}
163-
164-
rc = fs_write(ptr, buffer, count);
144+
rc = fs_write(&ptr->file, buffer, count);
165145
if (rc < 0) {
166146
errno = -rc;
167147
return -1;
@@ -175,17 +155,12 @@ ssize_t write(int fd, const void *buffer, size_t count)
175155
*
176156
* See IEEE 1003.1
177157
*/
178-
ssize_t read(int fd, void *buffer, size_t count)
158+
static ssize_t fs_read_vmeth(void *obj, void *buffer, size_t count)
179159
{
180160
ssize_t rc;
181-
struct fs_file_t *ptr = NULL;
182-
183-
if (posix_fs_get_ptr(fd, (union file_desc **)&ptr, false)) {
184-
errno = EBADF;
185-
return -1;
186-
}
161+
struct posix_fs_desc *ptr = obj;
187162

188-
rc = fs_read(ptr, buffer, count);
163+
rc = fs_read(&ptr->file, buffer, count);
189164
if (rc < 0) {
190165
errno = -rc;
191166
return -1;
@@ -194,29 +169,11 @@ ssize_t read(int fd, void *buffer, size_t count)
194169
return rc;
195170
}
196171

197-
/**
198-
* @brief Move read/write file offset.
199-
*
200-
* See IEEE 1003.1
201-
*/
202-
off_t lseek(int fd, off_t offset, int whence)
203-
{
204-
int rc;
205-
struct fs_file_t *ptr = NULL;
206-
207-
if (posix_fs_get_ptr(fd, (union file_desc **)&ptr, false)) {
208-
errno = EBADF;
209-
return -1;
210-
}
211-
212-
rc = fs_seek(ptr, offset, whence);
213-
if (rc < 0) {
214-
errno = -rc;
215-
return -1;
216-
}
217-
218-
return 0;
219-
}
172+
static struct fd_op_vtable fs_fd_op_vtable = {
173+
.read = fs_read_vmeth,
174+
.write = fs_write_vmeth,
175+
.ioctl = fs_ioctl_vmeth,
176+
};
220177

221178
/**
222179
* @brief Open a directory stream.
@@ -225,19 +182,20 @@ off_t lseek(int fd, off_t offset, int whence)
225182
*/
226183
DIR *opendir(const char *dirname)
227184
{
228-
int rc, fd;
229-
struct fs_dir_t *ptr = NULL;
185+
int rc;
186+
struct posix_fs_desc *ptr;
230187

231-
fd = posix_fs_alloc_fd((union file_desc **)&ptr, true);
232-
if ((fd < 0) || (ptr == NULL)) {
188+
ptr = posix_fs_alloc_obj(true);
189+
if (ptr == NULL) {
233190
errno = EMFILE;
234191
return NULL;
235192
}
236-
(void)memset(ptr, 0, sizeof(struct fs_dir_t));
237193

238-
rc = fs_opendir(ptr, dirname);
194+
(void)memset(&ptr->dir, 0, sizeof(ptr->dir));
195+
196+
rc = fs_opendir(&ptr->dir, dirname);
239197
if (rc < 0) {
240-
posix_fs_free_fd(fd);
198+
posix_fs_free_obj(ptr);
241199
errno = -rc;
242200
return NULL;
243201
}
@@ -253,16 +211,16 @@ DIR *opendir(const char *dirname)
253211
int closedir(DIR *dirp)
254212
{
255213
int rc;
214+
struct posix_fs_desc *ptr = dirp;
256215

257216
if (dirp == NULL) {
258217
errno = EBADF;
259218
return -1;
260219
}
261220

262-
rc = fs_closedir(dirp);
221+
rc = fs_closedir(&ptr->dir);
263222

264-
/* Free file ptr memory */
265-
posix_fs_free_ptr((struct posix_fs_desc *)dirp);
223+
posix_fs_free_obj(ptr);
266224

267225
if (rc < 0) {
268226
errno = -rc;
@@ -280,13 +238,14 @@ int closedir(DIR *dirp)
280238
struct dirent *readdir(DIR *dirp)
281239
{
282240
int rc;
241+
struct posix_fs_desc *ptr = dirp;
283242

284243
if (dirp == NULL) {
285244
errno = EBADF;
286245
return NULL;
287246
}
288247

289-
rc = fs_readdir(dirp, &fdirent);
248+
rc = fs_readdir(&ptr->dir, &fdirent);
290249
if (rc < 0) {
291250
errno = -rc;
292251
return NULL;

0 commit comments

Comments
 (0)