68 lines
1.0 KiB
C
68 lines
1.0 KiB
C
#ifndef H_DIRINFO
|
|
#define H_DIRINFO
|
|
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <dirent.h>
|
|
|
|
struct dirinfo
|
|
{
|
|
int fd;
|
|
DIR* dp;
|
|
struct stat stat;
|
|
};
|
|
|
|
static inline void
|
|
close_dirinfo(struct dirinfo* dirinfo)
|
|
{
|
|
if (dirinfo->fd > 0)
|
|
close(dirinfo->fd);
|
|
|
|
if (dirinfo->dp)
|
|
closedir(dirinfo->dp);
|
|
}
|
|
|
|
static inline int
|
|
openat_dirinfo(int dirfd, const char* path, struct dirinfo* dirinfo)
|
|
{
|
|
if ((dirinfo->fd = openat(dirfd, path, O_DIRECTORY|O_RDONLY)) == -1)
|
|
goto err;
|
|
|
|
if ((dirinfo->dp = fdopendir(dirinfo->fd)) == NULL)
|
|
goto err;
|
|
|
|
if (fstat(dirinfo->fd, &dirinfo->stat) == -1)
|
|
goto err;
|
|
|
|
return 1;
|
|
|
|
err:
|
|
close_dirinfo(dirinfo);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static inline int
|
|
open_dirinfo(const char* path, struct dirinfo* dirinfo)
|
|
{
|
|
if ((dirinfo->fd = open(path, O_DIRECTORY|O_RDONLY)) == -1)
|
|
goto err;
|
|
|
|
if ((dirinfo->dp = fdopendir(dirinfo->fd)) == NULL)
|
|
goto err;
|
|
|
|
if (fstat(dirinfo->fd, &dirinfo->stat) == -1)
|
|
goto err;
|
|
|
|
return 1;
|
|
|
|
err:
|
|
close_dirinfo(dirinfo);
|
|
|
|
return 0;
|
|
}
|
|
|
|
#endif
|
|
|