FileSystemIndexer v0.1.2-beta
File System Indexer - An API to index files from your system
Loading...
Searching...
No Matches
FSI_dirUtils_posix.h File Reference
#include "../../FSIpredefines.h"
#include "../FSI_dirUtils.h"
#include "cstr/cstr.h"
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
Include dependency graph for FSI_dirUtils_posix.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  FSI_DirData_POSIX
struct  FSI_Visited_POSIX

Macros

#define __FSI_DIRUTILS_POSIX_CPP_CLOSE   }
#define __FSI_DIRUTILS_POSIX_CPP_OPEN   extern "C" {

Typedefs

typedef struct FSI_DirData_POSIX FSI_DirData_POSIX
typedef struct FSI_Visited_POSIX FSI_Visited_POSIX

Functions

static void __fsi_walk (CVEC *out, CVEC *visited, const char *path)
void fsi_closeDir (FSI_DirData *d)
 Get the directory data to deallocate memory.
static FSI_EntryType fsi_getEntryType (int dirfd, struct dirent *e)
 Get the entry type of the dirfd from the FSI_DirData_POSIX.
static FSI_EntryType fsi_getEntryTypeFromDirent (int dirfd, struct dirent *e, struct stat *stOut)
int fsi_openDir (FSI_DirData *d, const char *path)
 Open directory as read only.
int fsi_readDir (FSI_DirData *d, FSI_EntryData *out)
 Read directory from reading the FSI_DirData information.
static void fsi_visitedAdd (CVEC *v, dev_t dev, ino_t ino)
static int fsi_visitedHas (CVEC *v, dev_t dev, ino_t ino)

Macro Definition Documentation

◆ __FSI_DIRUTILS_POSIX_CPP_CLOSE

#define __FSI_DIRUTILS_POSIX_CPP_CLOSE   }

Definition at line 6 of file FSI_dirUtils_posix.h.

◆ __FSI_DIRUTILS_POSIX_CPP_OPEN

#define __FSI_DIRUTILS_POSIX_CPP_OPEN   extern "C" {

Definition at line 5 of file FSI_dirUtils_posix.h.

Typedef Documentation

◆ FSI_DirData_POSIX

typedef struct FSI_DirData_POSIX FSI_DirData_POSIX

◆ FSI_Visited_POSIX

typedef struct FSI_Visited_POSIX FSI_Visited_POSIX

Function Documentation

◆ __fsi_walk()

void __fsi_walk ( CVEC * out,
CVEC * visited,
const char * path )
inlinestatic

Definition at line 145 of file FSI_dirUtils_posix.h.

151{
152 int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
153 if (fd < 0) return;
154
155 DIR *dir = fdopendir(fd);
156 if (!dir)
157 {
158 close(fd);
159 return;
160 }
161
162 struct stat dirSt;
163
164 if (fstat(fd, &dirSt) != 0)
165 {
166 closedir(dir);
167 return;
168 }
169
170 if (fsi_visitedHas(visited, dirSt.st_dev, dirSt.st_ino))
171 {
172 closedir(dir);
173 return;
174 }
175
176 fsi_visitedAdd(visited, dirSt.st_dev, dirSt.st_ino);
177
178 struct dirent *e;
179 CSTR tmp = cstr_init();
180
181 while ((e = readdir(dir)))
182 {
183 if (e->d_name[0] == '.' &&
184 (e->d_name[1] == '\0' || (e->d_name[1] == '.' && e->d_name[2] == '\0')))
185 continue;
186
187 struct stat st;
188
189 int type = fsi_getEntryTypeFromDirent(fd, e, &st);
190
191 cstr_set(&tmp, path);
192 cstr_add(&tmp, "/");
193 cstr_add(&tmp, e->d_name);
194
195 cvec_push(out, const char*, CVEC_sys_strdup(tmp.data));
196
197 if (type == FSI_DIR)
198 __fsi_walk(out, visited, tmp.data);
199 }
200
201 cstr_destroy(&tmp);
202
203 closedir(dir);
204}
@ FSI_DIR
static void __fsi_walk(CVEC *out, CVEC *visited, const char *path)
static FSI_EntryType fsi_getEntryTypeFromDirent(int dirfd, struct dirent *e, struct stat *stOut)
static int fsi_visitedHas(CVEC *v, dev_t dev, ino_t ino)
static void fsi_visitedAdd(CVEC *v, dev_t dev, ino_t ino)

References __fsi_walk(), FSI_DIR, fsi_getEntryTypeFromDirent(), fsi_visitedAdd(), and fsi_visitedHas().

Referenced by __fsi_walk(), and fsi_walk().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fsi_closeDir()

void fsi_closeDir ( FSI_DirData * d)

Get the directory data to deallocate memory.

Parameters
dObject to deallocate

Definition at line 41 of file FSI_dirUtils_posix.c.

42{
43 FSI_DirData_POSIX *p = d->impl;
44
45 cstr_destroy(&p->path);
46
47 // Closes FD variable too
48 closedir(p->dir);
49
50 FSI_FREE(p);
51}
#define FSI_FREE(x)

References FSI_DirData_POSIX::dir, FSI_FREE, FSI_DirData::impl, and FSI_DirData_POSIX::path.

◆ fsi_getEntryType()

FSI_EntryType fsi_getEntryType ( int dirfd,
struct dirent * e )
inlinestatic

Get the entry type of the dirfd from the FSI_DirData_POSIX.

  • Get if: Directory, File, Symlink or other
    Parameters
    dirfdEntry type to get from the syscall
    eGet dirent type quickly from the e ptr with its corresponding dirent data
    Returns
    Return the FSI_EntryType

Definition at line 70 of file FSI_dirUtils_posix.h.

71{
72 switch (e->d_type)
73 {
74 case DT_REG: return FSI_FILE;
75 case DT_DIR: return FSI_DIR;
76 case DT_LNK: return FSI_SYMLINK;
77 case DT_UNKNOWN:
78 default:
79 {
80 struct stat st;
81
82 if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) == 0)
83 {
84 if (S_ISREG(st.st_mode)) return FSI_FILE;
85 if (S_ISDIR(st.st_mode)) return FSI_DIR;
86 if (S_ISDIR(st.st_mode)) return FSI_SYMLINK;
87 }
88
89 return FSI_OTHER;
90 }
91 }
92}
@ FSI_SYMLINK
@ FSI_OTHER
@ FSI_FILE

References FSI_DIR, FSI_FILE, FSI_OTHER, and FSI_SYMLINK.

◆ fsi_getEntryTypeFromDirent()

FSI_EntryType fsi_getEntryTypeFromDirent ( int dirfd,
struct dirent * e,
struct stat * stOut )
inlinestatic

Definition at line 94 of file FSI_dirUtils_posix.h.

95{
96 switch (e->d_type)
97 {
98 case DT_REG:
99 return FSI_FILE;
100
101 case DT_DIR:
102 if (fstatat(dirfd, e->d_name, stOut, AT_SYMLINK_NOFOLLOW) == 0)
103 return FSI_DIR;
104
105 return FSI_OTHER;
106
107 case DT_LNK: return FSI_SYMLINK;
108
109 default:
110 if (fstatat(dirfd, e->d_name, stOut, AT_SYMLINK_NOFOLLOW) == 0)
111 {
112 if (S_ISDIR(stOut->st_mode)) return FSI_DIR;
113 if (S_ISREG(stOut->st_mode)) return FSI_FILE;
114 }
115
116 return FSI_OTHER;
117 }
118}

References FSI_DIR, FSI_FILE, FSI_OTHER, and FSI_SYMLINK.

Referenced by __fsi_walk().

Here is the caller graph for this function:

◆ fsi_openDir()

int fsi_openDir ( FSI_DirData * d,
const char * path )

Open directory as read only.

Parameters
dDirectory data
pathPath to open dir
Returns
Status (1 if worked, 0 if failed)

Definition at line 9 of file FSI_dirUtils_posix.c.

10{
11 const int dirFD = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
12
13 if (dirFD < 0) return 0;
14
15 DIR *dir = fdopendir(dirFD);
16
17 if (!dir)
18 {
19 close(dirFD);
20
21 return 0;
22 }
23
24 FSI_DirData_POSIX *p = malloc(sizeof(*p));
25
26 if (!p)
27 {
28 closedir(dir);
29
30 return 0;
31 }
32
33 p->dir = dir;
34 p->dirfd = dirFD;
35
36 d->impl = p;
37
38 return 1;
39}

References FSI_DirData_POSIX::dir, FSI_DirData_POSIX::dirfd, and FSI_DirData::impl.

◆ fsi_readDir()

int fsi_readDir ( FSI_DirData * d,
FSI_EntryData * out )

Read directory from reading the FSI_DirData information.

Parameters
dDirectory data
outEntry data to set the information
Returns
Status (1 if worked, 0 if failed)

◆ fsi_visitedAdd()

void fsi_visitedAdd ( CVEC * v,
dev_t dev,
ino_t ino )
inlinestatic

Definition at line 135 of file FSI_dirUtils_posix.h.

136{
137 FSI_Visited_POSIX e = {0};
138
139 e.dev = dev;
140 e.ino = ino;
141
142 cvec_push(v, FSI_Visited_POSIX, e);
143}

References FSI_Visited_POSIX::dev, and FSI_Visited_POSIX::ino.

Referenced by __fsi_walk().

Here is the caller graph for this function:

◆ fsi_visitedHas()

int fsi_visitedHas ( CVEC * v,
dev_t dev,
ino_t ino )
inlinestatic

Definition at line 120 of file FSI_dirUtils_posix.h.

121{
122 for (size_t i = 0 ; i < v->size ; i++)
123 {
124 FSI_Visited_POSIX *e = (FSI_Visited_POSIX*)cvec_get(v, i);
125
126 if (!e) continue;
127
128 if (e->dev == dev && e->ino == ino)
129 return 1;
130 }
131
132 return 0;
133}

References FSI_Visited_POSIX::dev, and FSI_Visited_POSIX::ino.

Referenced by __fsi_walk().

Here is the caller graph for this function: