mirror of
https://github.com/git-for-windows/git.git
synced 2026-03-16 11:02:37 -05:00
Emulating the POSIX `dirent` API on Windows via `FindFirstFile()`/`FindNextFile()` is pretty staightforward, however, most of the information provided in the `WIN32_FIND_DATA` structure is thrown away in the process. A more sophisticated implementation may cache this data, e.g. for later reuse in calls to `lstat()`. Make the `dirent` implementation pluggable so that it can be switched at runtime, e.g. based on a config option. Define a base DIR structure with pointers to `readdir()`/`closedir()` that match the `opendir()` implementation (similar to vtable pointers in Object-Oriented Programming). Define `readdir()`/`closedir()` so that they call the function pointers in the `DIR` structure. This allows to choose the `opendir()` implementation on a call-by-call basis. Make the fixed-size `dirent.d_name` buffer a flex array, as `d_name` may be implementation specific (e.g. a caching implementation may allocate a `struct dirent` with _just_ the size needed to hold the `d_name` in question). Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
33 lines
805 B
C
33 lines
805 B
C
#ifndef DIRENT_H
|
|
#define DIRENT_H
|
|
|
|
#define DT_UNKNOWN 0
|
|
#define DT_DIR 1
|
|
#define DT_REG 2
|
|
#define DT_LNK 3
|
|
|
|
struct dirent {
|
|
unsigned char d_type; /* file type to prevent lstat after readdir */
|
|
char d_name[FLEX_ARRAY]; /* file name */
|
|
};
|
|
|
|
/*
|
|
* Base DIR structure, contains pointers to readdir/closedir implementations so
|
|
* that opendir may choose a concrete implementation on a call-by-call basis.
|
|
*/
|
|
typedef struct DIR {
|
|
struct dirent *(*preaddir)(struct DIR *dir);
|
|
int (*pclosedir)(struct DIR *dir);
|
|
} DIR;
|
|
|
|
/* default dirent implementation */
|
|
extern DIR *dirent_opendir(const char *dirname);
|
|
|
|
/* current dirent implementation */
|
|
extern DIR *(*opendir)(const char *dirname);
|
|
|
|
#define readdir(dir) (dir->preaddir(dir))
|
|
#define closedir(dir) (dir->pclosedir(dir))
|
|
|
|
#endif /* DIRENT_H */
|