nspkg / tests / hookfixtures / paths.v
34 lines · 28 sloc · 931 bytes · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// directory-relative cleanup reaches the deliberately over-PATH_MAX fixture filename.
2module hookfixtures
3
4import os
5
6#include <fcntl.h>
7#include <unistd.h>
8#include <errno.h>
9
10fn C.unlinkat(i32, &char, i32) i32
11
12pub fn deep_directory(root string) string {
13 return root + ('/' + 'd'.repeat(190)).repeat(20)
14}
15
16pub fn long_filename() string {
17 return 'f'.repeat(232) + '.hook'
18}
19
20pub fn cleanup_long_paths(base string) ! {
21 for backend in ['old', 'native', 'public'] {
22 directory := deep_directory(os.join_path(base, 'long-hook-file', backend, 'root'))
23 fd := C.open(unsafe { &char(directory.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_CLOEXEC)
24 if fd < 0 {
25 if C.errno == C.ENOENT { continue }
26 return os.last_error()
27 }
28 defer { if C.close(fd) != 0 { eprintln(os.last_error()) } }
29 name := long_filename()
30 if C.unlinkat(fd, unsafe { &char(name.str) }, 0) != 0 && C.errno != C.ENOENT {
31 return os.last_error()
32 }
33 }
34}
35