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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
// A lang for filesystem paths
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <gc.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistr.h>
#include "arrays.h"
#include "files.h"
#include "integers.h"
#include "optionals.h"
#include "paths.h"
#include "patterns.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("/")))
fail("Path interpolations cannot contain slashes: %k", &text);
else if (Text$has(text, Pattern(";")))
fail("Path interpolations cannot contain semicolons: %k", &text);
else if (Text$equal_values(text, Path(".")) || Text$equal_values(text, Path("..")))
fail("Path interpolation is \"%k\" which is disallowed to prevent security vulnerabilities", &text);
return (Path_t)text;
}
PUREFUNC public Path_t Path$escape_path(Path_t path)
{
if (Text$starts_with(path, Path("~/")) || Text$starts_with(path, Path("/")))
fail("Invalid path component: %k", &path);
return path;
}
public Path_t Path$cleanup(Path_t path)
{
if (!Text$starts_with(path, Path("/")) && !Text$starts_with(path, Path("./"))
&& !Text$starts_with(path, Path("../")) && !Text$starts_with(path, Path("~/")))
path = Text$concat(Text("./"), path);
// Not fully resolved, but at least get rid of some of the cruft like "/./"
// and "/foo/../" and "//"
bool trailing_slash = Text$ends_with(path, Path("/"));
Array_t components = Text$split(path, Pattern("/"));
if (components.length == 0) return Path("/");
Path_t root = *(Path_t*)components.data;
Array$remove_at(&components, I(1), I(1), sizeof(Path_t));
for (int64_t i = 0; i < components.length; ) {
Path_t component = *(Path_t*)(components.data + i*components.stride);
if (component.length == 0 || Text$equal_values(component, Path("."))) { // Skip (//) and (/./)
Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t));
} else if (Text$equal_values(component, Path(".."))) {
if (i == 0) {
if (root.length == 0) { // (/..) -> (/)
Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t));
i += 1;
} else if (Text$equal_values(root, Path("."))) { // (./..) -> (..)
root = Path("..");
Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t));
i += 1;
} else if (Text$equal_values(root, Path("~"))) {
root = Path(""); // Convert $HOME to absolute path:
Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t));
// `i` is pointing to where the `..` lived
const char *home = getenv("HOME");
if (!home) fail("Could not get $HOME directory!");
// Insert all but the last component:
for (const char *p = home + 1; *p; ) {
const char *next_slash = strchr(p, '/');
if (!next_slash) break; // Skip last component
Path_t home_component = Text$format("%.*s", (int)(next_slash - p), p);
Array$insert(&components, &home_component, I(i+1), sizeof(Path_t));
i += 1;
p = next_slash + 1;
}
} else { // (../..) -> (../..)
i += 1;
}
} else if (Text$equal(&component, (Path_t*)(components.data + (i-1)*components.stride))) { // (___/../..) -> (____/../..)
i += 1;
} else { // (___/foo/..) -> (___)
Array$remove_at(&components, I(i), I(2), sizeof(Path_t));
i -= 1;
}
} else { // (___/foo/baz) -> (___/foo/baz)
i++;
}
}
Text_t cleaned_up = Text$concat(root, Text("/"), Text$join(Text("/"), components));
if (trailing_slash && !Text$ends_with(cleaned_up, Text("/")))
cleaned_up = Text$concat(cleaned_up, Text("/"));
return cleaned_up;
}
static inline Path_t Path$_expand_home(Path_t path)
{
if (Text$starts_with(path, Path("~/"))) {
Path_t after_tilde = Text$slice(path, I(2), I(-1));
return Text$format("%s%k", getenv("HOME"), &after_tilde);
} else {
return path;
}
}
public Path_t Path$_concat(int n, Path_t items[n])
{
Path_t cleaned_up = Path$cleanup(Text$_concat(n, items));
if (cleaned_up.length > PATH_MAX)
fail("Path exceeds the maximum path length: %k", &cleaned_up);
return cleaned_up;
}
public Text_t Path$resolved(Path_t path, Path_t relative_to)
{
path = Path$cleanup(path);
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 Text$concat(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)
{
path = Path$_expand_home(path);
struct stat sb;
return (stat(Text$as_c_string(path), &sb) == 0);
}
public bool Path$is_file(Path_t path, bool follow_symlinks)
{
path = Path$_expand_home(path);
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)
{
path = Path$_expand_home(path);
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)
{
path = Path$_expand_home(path);
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)
{
path = Path$_expand_home(path);
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)
{
path = Path$_expand_home(path);
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, Array_t bytes, int mode, int permissions)
{
path = Path$_expand_home(path);
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));
if (bytes.stride != 1)
Array$compact(&bytes, 1);
ssize_t written = write(fd, bytes.data, (size_t)bytes.length);
if (written != (ssize_t)bytes.length)
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)
{
Array_t bytes = Text$utf8_bytes(text);
_write(path, bytes, O_WRONLY | O_CREAT, permissions);
}
public void Path$write_bytes(Path_t path, Array_t bytes, int permissions)
{
_write(path, bytes, O_WRONLY | O_CREAT, permissions);
}
public void Path$append(Path_t path, Text_t text, int permissions)
{
Array_t bytes = Text$utf8_bytes(text);
_write(path, bytes, O_WRONLY | O_APPEND | O_CREAT, permissions);
}
public void Path$append_bytes(Path_t path, Array_t bytes, int permissions)
{
_write(path, bytes, O_WRONLY | O_APPEND | O_CREAT, permissions);
}
public Array_t Path$read_bytes(Path_t path)
{
path = Path$_expand_home(path);
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 *content = GC_MALLOC_ATOMIC((size_t)sb.st_size+1);
memcpy(content, mem, (size_t)sb.st_size);
content[sb.st_size] = '\0';
close(fd);
return (Array_t){.data=content, .atomic=1, .stride=1, .length=(int64_t)sb.st_size};
} else {
size_t capacity = 256, len = 0;
char *content = GC_MALLOC_ATOMIC(capacity);
for (;;) {
char chunk[256];
ssize_t just_read = read(fd, chunk, sizeof(chunk));
if (just_read < 0)
fail("Failed while reading file: %k (%s)", &path, strerror(errno));
else if (just_read == 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
break;
}
if (len + (size_t)just_read >= capacity) {
content = GC_REALLOC(content, (capacity *= 2));
}
memcpy(&content[len], chunk, (size_t)just_read);
len += (size_t)just_read;
if ((size_t)just_read < sizeof(chunk))
break;
}
close(fd);
if (u8_check((uint8_t*)content, len) != NULL)
fail("File does not contain valid UTF8 data!");
return (Array_t){.data=content, .atomic=1, .stride=1, .length=len};
}
}
public Text_t Path$read(Path_t path)
{
Array_t bytes = Path$read_bytes(path);
return Text$from_bytes(bytes);
}
public void Path$remove(Path_t path, bool ignore_missing)
{
path = Path$_expand_home(path);
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)
{
path = Path$_expand_home(path);
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)
{
path = Path$_expand_home(path);
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)
{
path = Path$_expand_home(path);
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_bytes(Path_t path, Array_t bytes)
{
path = Path$_expand_home(path);
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));
if (bytes.stride != 1)
Array$compact(&bytes, 1);
ssize_t written = write(fd, bytes.data, (size_t)bytes.length);
if (written != (ssize_t)bytes.length)
fail("Could not write to file: %s\n%s", buf, strerror(errno));
return Text$format("%s", buf);
}
public Text_t Path$write_unique(Path_t path, Text_t text)
{
return Path$write_unique_bytes(path, Text$utf8_bytes(text));
}
public Path_t Path$parent(Path_t path)
{
return Path$cleanup(Text$concat(path, Path("/../")));
}
public Text_t Path$base_name(Path_t path)
{
path = Path$cleanup(path);
if (Text$ends_with(path, Path("/")))
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("");
}
static void _line_reader_cleanup(FILE **f)
{
if (f && *f) {
fclose(*f);
*f = NULL;
}
}
static Text_t _next_line(FILE **f)
{
if (!f || !*f) return NULL_TEXT;
char *line = NULL;
size_t size = 0;
ssize_t len = getline(&line, &size, *f);
if (len <= 0) {
_line_reader_cleanup(f);
return NULL_TEXT;
}
while (len > 0 && (line[len-1] == '\r' || line[len-1] == '\n'))
--len;
if (u8_check((uint8_t*)line, (size_t)len) != NULL)
fail("Invalid UTF8!");
Text_t line_text = Text$format("%.*s", len, line);
free(line);
return line_text;
}
public Closure_t Path$by_line(Path_t path)
{
path = Path$_expand_home(path);
FILE *f = fopen(Text$as_c_string(path), "r");
if (f == NULL)
fail("Could not read file: %k (%s)", &path, strerror(errno));
FILE **wrapper = GC_MALLOC(sizeof(FILE*));
*wrapper = f;
GC_register_finalizer(wrapper, (void*)_line_reader_cleanup, NULL, NULL, NULL);
return (Closure_t){.fn=(void*)_next_line, .userdata=wrapper};
}
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
|