Skip to content

Commit eb0aaca

Browse files
Ramakrishna Pallalanashif
Ramakrishna Pallala
authored andcommitted
lib: posix: Add Posix Style File System API support
Add IEEE 1003.1 Posix Style file system API support. These API's will internally use corresponding Zephyr File System API's. Signed-off-by: Ramakrishna Pallala <[email protected]>
1 parent 0b76b12 commit eb0aaca

File tree

9 files changed

+550
-19
lines changed

9 files changed

+550
-19
lines changed

arch/posix/include/posix_cheats.h

+16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#define mqueue_desc zap_mqueue_desc
4646
#define mqd_t zap_mqd_t
4747
#define mq_attr zap_mq_attr
48+
#define dirent zap_dirent
49+
#define DIR zap_DIR
4850

4951
/* Condition variables */
5052

@@ -163,6 +165,20 @@
163165
#define mq_timedreceive(...) zap_mq_timedreceive(__VA_ARGS__)
164166
#define mq_timedsend(...) zap_mq_timedsend(__VA_ARGS__)
165167

168+
/* File system */
169+
#define open zap_open
170+
#define close zap_close
171+
#define write zap_write
172+
#define read zap_read
173+
#define lseek zap_lseek
174+
#define opendir zap_opendir
175+
#define closedir zap_closedir
176+
#define readdir zap_readdir
177+
#define rename zap_rename
178+
#define unlink zap_unlink
179+
#define stat zap_stat
180+
#define mkdir zap_mkdir
181+
166182
#endif /* CONFIG_PTHREAD_IPC */
167183

168184
#endif /* CONFIG_ARCH_POSIX */

include/fs.h

+18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,25 @@
77
#ifndef _FS_H_
88
#define _FS_H_
99

10+
#ifdef CONFIG_ARCH_POSIX
11+
#ifndef __ssize_t_defined
12+
typedef __SIZE_TYPE__ ssize_t;
13+
#define __ssize_t_defined
14+
#endif
15+
16+
#ifndef __off_t_defined
17+
#ifndef __USE_FILE_OFFSET64
18+
typedef long int off_t;
19+
#else
20+
typedef long long int off_t;
21+
#endif
22+
#define __off_t_defined
23+
#endif
24+
25+
#else
1026
#include <sys/types.h>
27+
#endif
28+
1129
#include <misc/dlist.h>
1230
#include <fs/fs_interface.h>
1331

include/posix/sys/stat.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __POSIX_STAT_H__
8+
#define __POSIX_STAT_H__
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
#ifdef CONFIG_PTHREAD_IPC
15+
#include <kernel.h>
16+
17+
#ifdef CONFIG_POSIX_FS
18+
#define S_IRWXU 00700
19+
#define S_IRUSR 00400
20+
#define S_IWUSR 00200
21+
#define S_IXUSR 00100
22+
23+
#define S_IRWXG 00070
24+
#define S_IRGRP 00040
25+
#define S_IWGRP 00020
26+
#define S_IXGRP 00010
27+
28+
#define S_IRWXO 00007
29+
#define S_IROTH 00004
30+
#define S_IWOTH 00002
31+
#define S_IXOTH 00001
32+
33+
/* File open modes */
34+
#define O_ACCMODE 0003
35+
#define O_RDONLY 00
36+
#define O_WRONLY 01
37+
#define O_RDWR 02
38+
39+
#define SEEK_SET 0 /* Seek from beginning of file. */
40+
#define SEEK_CUR 1 /* Seek from current position. */
41+
#define SEEK_END 2 /* Seek from end of file. */
42+
43+
struct stat {
44+
unsigned long st_size;
45+
unsigned long st_blksize;
46+
unsigned long st_blocks;
47+
};
48+
49+
#endif /* CONFIG_POSIX_FS */
50+
51+
#endif /* CONFIG_PTHREAD_IPC */
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
57+
#endif /* __POSIX_STAT_H__ */

include/posix/sys/types.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
extern "C" {
1212
#endif
1313

14+
#ifndef CONFIG_ARCH_POSIX
1415
#include_next <sys/types.h>
16+
#endif
1517

1618
#ifdef CONFIG_PTHREAD_IPC
1719
#include <kernel.h>

include/posix/unistd.h

+36-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,46 @@ extern "C" {
1111
#endif
1212

1313
#include "sys/types.h"
14+
#include "sys/stat.h"
15+
16+
#ifdef CONFIG_POSIX_FS
17+
#include <fs.h>
18+
19+
#undef PATH_MAX
20+
#define PATH_MAX 256
21+
22+
typedef struct fs_dir_t DIR;
23+
typedef unsigned int mode_t;
24+
25+
struct dirent {
26+
unsigned int d_ino;
27+
char d_name[PATH_MAX + 1];
28+
};
29+
30+
/* File related operations */
31+
extern int open(const char *name, int flags);
32+
extern int close(int file);
33+
extern ssize_t write(int file, char *buffer, unsigned int count);
34+
extern ssize_t read(int file, char *buffer, unsigned int count);
35+
extern int lseek(int file, int offset, int whence);
36+
37+
/* Directory related operations */
38+
extern DIR *opendir(const char *dirname);
39+
extern int closedir(DIR *dirp);
40+
extern struct dirent *readdir(DIR *dirp);
41+
42+
/* File System related operations */
43+
extern int rename(const char *old, const char *newp);
44+
extern int unlink(const char *path);
45+
extern int stat(const char *path, struct stat *buf);
46+
extern int mkdir(const char *path, mode_t mode);
47+
#endif
1448

1549
unsigned sleep(unsigned int seconds);
1650
int usleep(useconds_t useconds);
1751

18-
#endif
1952
#ifdef __cplusplus
2053
}
54+
#endif
55+
2156
#endif /* __POSIX_UNISTD_H__ */

lib/libc/newlib/libc-hooks.c

+23-18
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void __stdin_hook_install(unsigned char (*hook)(void))
7575
_stdin_hook = hook;
7676
}
7777

78+
#ifndef CONFIG_POSIX_FS
7879
int _read(int fd, char *buf, int nbytes)
7980
{
8081
int i = 0;
@@ -104,6 +105,28 @@ int _write(int fd, char *buf, int nbytes)
104105
}
105106
FUNC_ALIAS(_write, write, int);
106107

108+
int _open(const char *name, int mode)
109+
{
110+
return -1;
111+
}
112+
FUNC_ALIAS(_open, open, int);
113+
114+
int _close(int file)
115+
{
116+
return -1;
117+
}
118+
FUNC_ALIAS(_close, close, int);
119+
120+
int _lseek(int file, int ptr, int dir)
121+
{
122+
return 0;
123+
}
124+
FUNC_ALIAS(_lseek, lseek, int);
125+
#else
126+
extern ssize_t write(int file, char *buffer, unsigned int count);
127+
#define _write write
128+
#endif
129+
107130
int _isatty(int file)
108131
{
109132
return 1;
@@ -137,24 +160,6 @@ void _exit(int status)
137160
}
138161
}
139162

140-
int _open(const char *name, int mode)
141-
{
142-
return -1;
143-
}
144-
FUNC_ALIAS(_open, open, int);
145-
146-
int _close(int file)
147-
{
148-
return -1;
149-
}
150-
FUNC_ALIAS(_close, close, int);
151-
152-
int _lseek(int file, int ptr, int dir)
153-
{
154-
return 0;
155-
}
156-
FUNC_ALIAS(_lseek, lseek, int);
157-
158163
void *_sbrk(int count)
159164
{
160165
void *ptr = heap_base + heap_sz;

lib/posix/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ zephyr_library_sources(timer.c)
1616
zephyr_library_sources(pthread_rwlock.c)
1717
zephyr_library_sources(semaphore.c)
1818
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
19+
zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
1920

2021
zephyr_library_link_libraries(PTHREAD)
2122
target_link_libraries(PTHREAD INTERFACE zephyr_interface)

lib/posix/Kconfig

+19
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,23 @@ config MQUEUE_NAMELEN_MAX
7070
Mention length of message queue name in number of characters.
7171

7272
endif
73+
74+
if FILE_SYSTEM
75+
config POSIX_FS
76+
bool
77+
prompt "Enable POSIX file system API support"
78+
default n
79+
help
80+
This enabled POSIX style file system related APIs.
81+
82+
if POSIX_FS
83+
config POSIX_MAX_OPEN_FILES
84+
int
85+
prompt "Maximum number of open file descriptors"
86+
default 16
87+
help
88+
Mention maximum number of open file descriptors.
89+
endif
90+
endif
91+
7392
endif

0 commit comments

Comments
 (0)