Merge pull request #13549 from kjbracey-arm/readdir_r

Make readdir reentrant
pull/13768/head
Martin Kojtal 2020-10-13 11:11:45 +01:00 committed by GitHub
commit 2cb64f5bce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 13 deletions

View File

@ -184,11 +184,8 @@ FileHandle *mbed_override_console(int fd);
*/ */
FileHandle *mbed_file_handle(int fd); FileHandle *mbed_file_handle(int fd);
} }
typedef mbed::DirHandle DIR;
#else
typedef struct Dir DIR;
#endif #endif
typedef struct DIR_impl DIR;
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
/* The intent of this section is to unify the errno error values to match /* The intent of this section is to unify the errno error values to match

View File

@ -46,6 +46,12 @@
static SingletonPtr<PlatformMutex> _mutex; static SingletonPtr<PlatformMutex> _mutex;
/* DIR is typedeffed to struct DIR_impl in header */
struct DIR_impl {
mbed::DirHandle *handle;
struct dirent entry;
};
#if defined(__ARMCC_VERSION) #if defined(__ARMCC_VERSION)
# include <arm_compat.h> # include <arm_compat.h>
# include <rt_sys.h> # include <rt_sys.h>
@ -1297,9 +1303,15 @@ extern "C" DIR *opendir(const char *path)
return NULL; return NULL;
} }
DirHandle *dir; DIR *dir = new (std::nothrow) DIR;
int err = fs->open(&dir, fp.fileName()); if (!dir) {
errno = ENOMEM;
return NULL;
}
int err = fs->open(&dir->handle, fp.fileName());
if (err < 0) { if (err < 0) {
delete dir;
errno = -err; errno = -err;
return NULL; return NULL;
} }
@ -1309,8 +1321,7 @@ extern "C" DIR *opendir(const char *path)
extern "C" struct dirent *readdir(DIR *dir) extern "C" struct dirent *readdir(DIR *dir)
{ {
static struct dirent ent; int err = dir->handle->read(&dir->entry);
int err = dir->read(&ent);
if (err < 1) { if (err < 1) {
if (err < 0) { if (err < 0) {
errno = -err; errno = -err;
@ -1318,12 +1329,13 @@ extern "C" struct dirent *readdir(DIR *dir)
return NULL; return NULL;
} }
return &ent; return &dir->entry;
} }
extern "C" int closedir(DIR *dir) extern "C" int closedir(DIR *dir)
{ {
int err = dir->close(); int err = dir->handle->close();
delete dir;
if (err < 0) { if (err < 0) {
errno = -err; errno = -err;
return -1; return -1;
@ -1334,17 +1346,17 @@ extern "C" int closedir(DIR *dir)
extern "C" void rewinddir(DIR *dir) extern "C" void rewinddir(DIR *dir)
{ {
dir->rewind(); dir->handle->rewind();
} }
extern "C" off_t telldir(DIR *dir) extern "C" off_t telldir(DIR *dir)
{ {
return dir->tell(); return dir->handle->tell();
} }
extern "C" void seekdir(DIR *dir, off_t off) extern "C" void seekdir(DIR *dir, off_t off)
{ {
dir->seek(off); dir->handle->seek(off);
} }
extern "C" int mkdir(const char *path, mode_t mode) extern "C" int mkdir(const char *path, mode_t mode)