-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.c
163 lines (144 loc) · 5.26 KB
/
fs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* JCUnit - a very simple unit testing framework for C
*
* Copyright (C) 2021-2022 Denis Korchagin <denis.korchagin.1995@gmail.com>
*
* This file is part of JCUnit
*
* For the full license information, please view the LICENSE
* file that was distributed with this source code.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation of version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "headers/fs.h"
#include "headers/object-allocator.h"
#include "headers/bytes-allocator.h"
#include "headers/errors.h"
#include "headers/util.h"
#define GET_ENTRY_PATH_ADD_PATH_SEPARATOR (1)
static char * path_buffer[PATH_MAX] = {0};
static struct stat st_buf;
static char * get_entry_path(const char * path, uint32_t path_len, struct dirent * dirent, unsigned int flags);
bool fs_is_dir(const char * pathname)
{
return stat(pathname, &st_buf) == 0 && (st_buf.st_mode & S_IFDIR) > 0;
}
bool fs_is_file_exists(const char * pathname)
{
return stat(pathname, &st_buf) == 0;
}
bool fs_is_file_executable(const char * pathname)
{
return stat(pathname, &st_buf) == 0 && (st_buf.st_mode & S_IEXEC) > 0;
}
bool fs_check_extension(const char * path, const char * extension)
{
char * point = strrchr(path, '.');
if (point == NULL) {
return extension == NULL;
}
if (extension == NULL) {
return false;
}
return strcmp(extension, point) == 0;
}
void fs_read_dir(const char * path, fs_read_dir_func * read_dir_func, void * context)
{
struct path_list base_path;
base_path.path = path;
struct list queue;
list_init(&queue);
list_append(&queue, &base_path.list_entry);
struct path_list * current_path;
bool request_to_quit = false;
for (;;) {
if (request_to_quit || list_is_empty(&queue)) {
break;
}
struct list *shifted = list_shift(&queue);
assert(shifted != NULL);
current_path = list_get_owner(shifted, struct path_list, list_entry);
DIR * dir = opendir(current_path->path);
if (dir == NULL) {
jcunit_fatal_error("Can't open directory \"%s\"!", current_path->path);
}
uint32_t current_path_len = strlen(current_path->path);
for (;;) {
if (request_to_quit) {
break;
}
struct dirent *dirent = readdir(dir);
if (dirent == NULL)
break;
if (dirent->d_type == DT_DIR) {
if (
strncmp(".", dirent->d_name, dirent->d_namlen) == 0
|| strncmp("..", dirent->d_name, dirent->d_namlen) == 0
) {
continue;
}
struct path_list * new_directory = alloc_path_list();
list_init(&new_directory->list_entry);
new_directory->path = get_entry_path(current_path->path, current_path_len, dirent, GET_ENTRY_PATH_ADD_PATH_SEPARATOR);
list_append(&queue, &new_directory->list_entry);
continue;
}
if (dirent->d_type != DT_REG) {
continue;
}
char * file_entry_path = get_entry_path(current_path->path, current_path_len, dirent, GET_ENTRY_PATH_ADD_PATH_SEPARATOR);
unsigned int fs_flags = 0;
request_to_quit = !read_dir_func(file_entry_path, context, &fs_flags);
if ((fs_flags & FS_FLAG_USE_PATH) == 0) {
free_bytes((void *) file_entry_path);
file_entry_path = NULL;
}
}
if (current_path != &base_path) {
release_path_list(current_path);
current_path = NULL;
}
(void) closedir(dir);
}
}
char * get_entry_path(const char * path, uint32_t path_len, struct dirent * dirent, unsigned int flags)
{
uint32_t entry_len = path_len + dirent->d_namlen + ((flags & GET_ENTRY_PATH_ADD_PATH_SEPARATOR) > 0 ? 1 : 0);
char * entry_path = alloc_bytes(entry_len + 1);
memcpy((void *) entry_path, (const void *) path, path_len);
if ((flags & GET_ENTRY_PATH_ADD_PATH_SEPARATOR) > 0) {
entry_path[path_len] = PATH_SEPARATOR;
memcpy((void *) (entry_path + path_len + 1), (const void *) dirent->d_name, dirent->d_namlen);
}
else {
memcpy((void *) (entry_path + path_len), (const void *) dirent->d_name, dirent->d_namlen);
}
entry_path[entry_len] = '\0';
return entry_path;
}
const char * fs_resolve_path(const char * path)
{
const char * resolved_path = realpath(path, (char *) path_buffer);
if (resolved_path == NULL) {
return NULL;
}
return duplicate_cstring(resolved_path);
}