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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
// A lang for filesystem paths
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistr.h>
#include "array.h"
#include "files.h"
#include "functions.h"
#include "integers.h"
#include "path.h"
#include "text.h"
#include "types.h"
#include "util.h"
PUREFUNC public Path_t Path$escape_text(Text_t text)
{
if (Text$has(text, Pattern("/")) || Text$has(text, Pattern(";")))
fail("Invalid path component: %k", &text);
else if (Text$matches(text, Pattern(".")) || Text$matches(text, Pattern("..")))
fail("Invalid path component: %k", &text);
return (Path_t)text;
}
PUREFUNC public Path_t Path$concat(Path_t a, Path_t b)
{
Path_t path = Text$concat(a, b);
while (Text$has(path, Pattern("/../")))
path = Text$replace(path, Pattern("{!/}/../"), Text(""), Text(""), false);
while (Text$has(path, Pattern("/./")))
path = Text$replace(path, Pattern("/./"), Text("/"), Text(""), false);
return path;
}
public Text_t Path$resolved(Path_t path, Path_t relative_to)
{
while (Text$has(path, Pattern("/../")))
path = Text$replace(path, Pattern("{!/}/../"), Text(""), Text(""), false);
while (Text$has(path, Pattern("/./")))
path = Text$replace(path, Pattern("/./"), Text("/"), Text(""), false);
const char *path_str = Text$as_c_string(path);
const char *relative_to_str = Text$as_c_string(relative_to);
const char *resolved_path = resolve_path(path_str, relative_to_str, relative_to_str);
if (resolved_path) {
return (Path_t)(Text$from_str(resolved_path));
} else if (path_str[0] == '/') {
return path;
} else if (path_str[0] == '~' && path_str[1] == '/') {
return (Path_t)Text$format("%s%s", getenv("HOME"), path_str + 1);
} else {
return Paths(Path$resolved(relative_to, Path(".")), Path("/"), path);
}
}
public Text_t Path$relative(Path_t path, Path_t relative_to)
{
path = Path$resolved(path, relative_to);
relative_to = Path$resolved(relative_to, Path("."));
if (Text$matches(path, Patterns(Pattern("{start}"), relative_to, Pattern("{0+..}"))))
return Text$slice(path, I(relative_to.length + 2), I(-1));
return path;
}
public bool Path$exists(Path_t path)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
printf("Path: %k\n", &path);
struct stat sb;
return (stat(Text$as_c_string(path), &sb) == 0);
}
public bool Path$is_file(Path_t path, bool follow_symlinks)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
struct stat sb;
const char *path_str = Text$as_c_string(path);
int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFREG;
}
public bool Path$is_directory(Path_t path, bool follow_symlinks)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
struct stat sb;
const char *path_str = Text$as_c_string(path);
int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFDIR;
}
public bool Path$is_pipe(Path_t path, bool follow_symlinks)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
struct stat sb;
const char *path_str = Text$as_c_string(path);
int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFIFO;
}
public bool Path$is_socket(Path_t path, bool follow_symlinks)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
struct stat sb;
const char *path_str = Text$as_c_string(path);
int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFSOCK;
}
public bool Path$is_symlink(Path_t path)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
struct stat sb;
const char *path_str = Text$as_c_string(path);
int status = stat(path_str, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFLNK;
}
static void _write(Path_t path, Text_t text, int mode, int permissions)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
const char *path_str = Text$as_c_string(path);
int fd = open(path_str, mode, permissions);
if (fd == -1)
fail("Could not write to file: %s\n%s", path_str, strerror(errno));
const char *str = Text$as_c_string(text);
size_t len = strlen(str);
ssize_t written = write(fd, str, len);
if (written != (ssize_t)len)
fail("Could not write to file: %s\n%s", path_str, strerror(errno));
}
public void Path$write(Path_t path, Text_t text, int permissions)
{
_write(path, text, O_WRONLY | O_CREAT, permissions);
}
public void Path$append(Path_t path, Text_t text, int permissions)
{
_write(path, text, O_WRONLY | O_APPEND | O_CREAT, permissions);
}
public Text_t Path$read(Path_t path)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
int fd = open(Text$as_c_string(path), O_RDONLY);
if (fd == -1)
fail("Could not read file: %k (%s)", &path, strerror(errno));
struct stat sb;
if (fstat(fd, &sb) != 0)
fail("Could not read file: %k (%s)", &path, strerror(errno));
if ((sb.st_mode & S_IFMT) == S_IFREG) { // Use memory mapping if it's a real file:
const char *mem = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
char *gc_mem = GC_MALLOC_ATOMIC((size_t)sb.st_size+1);
memcpy(gc_mem, mem, (size_t)sb.st_size);
gc_mem[sb.st_size] = '\0';
close(fd);
return Text$from_strn(gc_mem, (size_t)sb.st_size);
} else {
const size_t chunk_size = 256;
char *buf = GC_MALLOC_ATOMIC(chunk_size);
Text_t contents = Text("");
ssize_t just_read;
do {
just_read = read(fd, buf, chunk_size);
if (just_read < 0)
fail("Failed while reading file: %k (%s)", &path, strerror(errno));
else if (just_read == 0)
break;
if (u8_check((uint8_t*)buf, (size_t)just_read) != NULL)
fail("File does not contain valid UTF8 data!");
contents = Texts(contents, Text$from_strn(buf, (size_t)just_read));
buf = GC_MALLOC_ATOMIC(chunk_size);
} while (just_read > 0);
close(fd);
return contents;
}
}
public void Path$remove(Path_t path, bool ignore_missing)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
const char *path_str = Text$as_c_string(path);
struct stat sb;
if (lstat(path_str, &sb) != 0) {
if (!ignore_missing)
fail("Could not remove file: %s (%s)", path_str, strerror(errno));
}
if ((sb.st_mode & S_IFMT) == S_IFREG || (sb.st_mode & S_IFMT) == S_IFLNK) {
if (unlink(path_str) != 0 && !ignore_missing)
fail("Could not remove file: %s (%s)", path_str, strerror(errno));
} else if ((sb.st_mode & S_IFMT) == S_IFDIR) {
if (rmdir(path_str) != 0 && !ignore_missing)
fail("Could not remove directory: %s (%s)", path_str, strerror(errno));
} else {
fail("Could not remove path: %s (not a file or directory)", path_str, strerror(errno));
}
}
public void Path$create_directory(Path_t path, int permissions)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
if (mkdir(Text$as_c_string(path), (mode_t)permissions) != 0)
fail("Could not create directory: %k (%s)", &path, strerror(errno));
}
static Array_t _filtered_children(Path_t path, bool include_hidden, mode_t filter)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
struct dirent *dir;
Array_t children = {};
const char *path_str = Text$as_c_string(path);
size_t path_len = strlen(path_str);
DIR *d = opendir(path_str);
if (!d)
fail("Could not open directory: %k (%s)", &path, strerror(errno));
if (path_str[path_len-1] == '/')
--path_len;
while ((dir = readdir(d)) != NULL) {
if (!include_hidden && dir->d_name[0] == '.')
continue;
if (streq(dir->d_name, ".") || streq(dir->d_name, ".."))
continue;
const char *child_str = heap_strf("%.*s/%s", path_len, path_str, dir->d_name);
struct stat sb;
if (stat(child_str, &sb) != 0)
continue;
if (!((sb.st_mode & S_IFMT) & filter))
continue;
Path_t child = Text$format("%s%s", child_str, ((sb.st_mode & S_IFMT) == S_IFDIR) ? "/" : ""); // Trailing slash for dirs
Array$insert(&children, &child, I(0), sizeof(Path_t));
}
closedir(d);
return children;
}
public Array_t Path$children(Path_t path, bool include_hidden)
{
return _filtered_children(path, include_hidden, (mode_t)-1);
}
public Array_t Path$files(Path_t path, bool include_hidden)
{
return _filtered_children(path, include_hidden, S_IFREG);
}
public Array_t Path$subdirectories(Path_t path, bool include_hidden)
{
return _filtered_children(path, include_hidden, S_IFDIR);
}
public Path_t Path$unique_directory(Path_t path)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
const char *path_str = Text$as_c_string(path);
size_t len = strlen(path_str);
if (len >= PATH_MAX) fail("Path is too long: %s", path_str);
char buf[PATH_MAX] = {};
strcpy(buf, path_str);
if (buf[len-1] == '/')
buf[--len] = '\0';
char *created = mkdtemp(buf);
if (!created) fail("Failed to create temporary directory: %s (%s)", path_str, strerror(errno));
return Text$format("%s/", created);
}
public Text_t Path$write_unique(Path_t path, Text_t text)
{
if (Text$matches(path, Pattern("~/{0+..}")))
path = Paths(Text$format("%s", getenv("HOME")), Text$slice(path, I(2), I(-1)));
const char *path_str = Text$as_c_string(path);
size_t len = strlen(path_str);
if (len >= PATH_MAX) fail("Path is too long: %s", path_str);
char buf[PATH_MAX] = {};
strcpy(buf, path_str);
int64_t suffixlen = 0;
(void)Text$find(path, Pattern("{0+!X}{end}"), I(1), &suffixlen);
if (suffixlen < 0) suffixlen = 0;
int fd = mkstemps(buf, suffixlen);
if (fd == -1)
fail("Could not write to unique file: %s\n%s", buf, strerror(errno));
const char *str = Text$as_c_string(text);
size_t write_len = strlen(str);
ssize_t written = write(fd, str, write_len);
if (written != (ssize_t)write_len)
fail("Could not write to file: %s\n%s", buf, strerror(errno));
return Text$format("%s", buf);
}
public Path_t Path$parent(Path_t path)
{
while (Text$has(path, Pattern("/../")))
path = Text$replace(path, Pattern("{!/}/../"), Text(""), Text(""), false);
while (Text$has(path, Pattern("/./")))
path = Text$replace(path, Pattern("/./"), Text("/"), Text(""), false);
path = Text$trim(path, Pattern("/."), false, true);
if (Text$equal_values(path, Pattern("~/")))
path = Text$format("%s", getenv("HOME"));
if (Text$equal_values(path, Path("/")))
return path;
else if (Text$equal_values(path, Path("./")))
return Path("../");
else if (Text$has(path, Pattern("/{..}")))
return Text$replace(path, Pattern("{0+..}/{!/}{end}"), Text("@1/"), Text("@"), false);
else
return Texts(path, Text$matches(path, Pattern("{..}/")) ? Path("../") : Path("/../"));
}
public Text_t Path$base_name(Path_t path)
{
if (Text$matches(path, Pattern("/{end}")))
return Text$replace(path, Pattern("{0+..}/{!/}/{end}"), Text("@2"), Text("@"), false);
else
return Text$replace(path, Pattern("{0+..}/{!/}{end}"), Text("@2"), Text("@"), false);
}
public Text_t Path$extension(Path_t path, bool full)
{
Text_t base = Path$base_name(path);
if (Text$matches(base, Pattern(".{!.}.{..}")))
return Text$replace(base, full ? Pattern(".{!.}.{..}") : Pattern(".{..}.{!.}{end}"), Text("@2"), Text("@"), false);
else if (Text$matches(base, Pattern("{!.}.{..}")))
return Text$replace(base, full ? Pattern("{!.}.{..}") : Pattern("{..}.{!.}{end}"), Text("@2"), Text("@"), false);
else
return Text("");
}
public const TypeInfo Path$info = {
.size=sizeof(Path_t),
.align=__alignof__(Path_t),
.tag=TextInfo,
.TextInfo={.lang="Path"},
};
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|