| 1 |
|
| /* po_storeio.h - the store's own file layer. |
| 2 |
|
| * |
| 3 |
|
| * Paths, the directory listing, the sidecar index and sealing. The Perl this |
| 4 |
|
| * replaces was orchestration over C that already existed - po_wal.h could |
| 5 |
|
| * replay and seal, po_sgraph.h could build the map, po_tattr.h could |
| 6 |
|
| * summarise a trace - and every one of those calls crossed the XS boundary |
| 7 |
|
| * once per record to do it. |
| 8 |
|
| * |
| 9 |
|
| * THE DIRECTORY LISTING IS WHERE THE PORTABILITY LIVES. |
| 10 |
|
| * |
| 11 |
|
| * `opendir` and `DIR` are POSIX and do not exist on MSVC. Declaring either |
| 12 |
|
| * one in a header that a Windows build also compiles is how a distribution |
| 13 |
|
| * spends three releases on the same mistake, because the failure is a link |
| 14 |
|
| * error a long way from the cause. So the shim below is self-prefixed |
| 15 |
|
| * throughout - po_dir, po_opendir - and never names the ambient types, and |
| 16 |
|
| * the Windows half calls FindFirstFile rather than pretending to be POSIX. |
| 17 |
|
| */ |
| 18 |
|
| #ifndef PO_STOREIO_H |
| 19 |
|
| #define PO_STOREIO_H |
| 20 |
|
| |
| 21 |
|
| #include "punk_observe/po_compat.h" |
| 22 |
|
| #include "punk_observe/po_nsarith.h" |
| 23 |
|
| #include "punk_observe/po_wal.h" |
| 24 |
|
| |
| 25 |
|
| #include <stdio.h> |
| 26 |
|
| #include <sys/stat.h> |
| 27 |
|
| |
| 28 |
|
| #ifdef _WIN32 |
| 29 |
|
| # include <windows.h> |
| 30 |
|
| # include <direct.h> |
| 31 |
|
| # include <process.h> |
| 32 |
|
| # define po_getpid() _getpid() |
| 33 |
|
| #else |
| 34 |
|
| # include <dirent.h> |
| 35 |
|
| # include <unistd.h> |
| 36 |
|
| # define po_getpid() getpid() |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
|
| #define PO_PATHMAX 4096 |
| 40 |
|
| |
| 41 |
|
| /* ---- a directory, without naming POSIX ------------------------------------ */ |
| 42 |
|
| |
| 43 |
|
| typedef struct { |
| 44 |
|
| #ifdef _WIN32 |
| 45 |
|
| HANDLE h; |
| 46 |
|
| WIN32_FIND_DATAA fd; |
| 47 |
|
| int first; |
| 48 |
|
| #else |
| 49 |
|
| DIR *d; |
| 50 |
|
| #endif |
| 51 |
|
| int open; |
| 52 |
|
| } po_dir; |
| 53 |
|
| |
| 54 |
526 |
| static int po_opendir(po_dir *o, const char *path) { |
| 55 |
526 |
| memset(o, 0, sizeof(*o)); |
| 56 |
|
| #ifdef _WIN32 |
| 57 |
|
| { |
| 58 |
|
| char pat[PO_PATHMAX]; |
| 59 |
|
| size_t n = strlen(path); |
| 60 |
|
| if (n + 3 >= sizeof(pat)) return 0; |
| 61 |
|
| memcpy(pat, path, n); |
| 62 |
|
| memcpy(pat + n, "\\*", 3); |
| 63 |
|
| o->h = FindFirstFileA(pat, &o->fd); |
| 64 |
|
| if (o->h == INVALID_HANDLE_VALUE) return 0; |
| 65 |
|
| o->first = 1; |
| 66 |
|
| } |
| 67 |
|
| #else |
| 68 |
526 |
| o->d = opendir(path); |
| 69 |
526 |
▶ | if (!o->d) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o->d) return 0; |
1 |
525 |
526 |
|
| 70 |
|
| #endif |
| 71 |
525 |
| o->open = 1; |
| 72 |
525 |
| return 1; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| /* The next name, or NULL. Borrowed, and valid until the next call. */ |
| 76 |
2863 |
| static const char *po_readdir(po_dir *o) { |
| 77 |
2863 |
▶ | if (!o->open) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o->open) return NULL; |
0 |
2863 |
2863 |
|
| 78 |
|
| #ifdef _WIN32 |
| 79 |
|
| if (o->first) { o->first = 0; return o->fd.cFileName; } |
| 80 |
|
| if (!FindNextFileA(o->h, &o->fd)) return NULL; |
| 81 |
|
| return o->fd.cFileName; |
| 82 |
|
| #else |
| 83 |
|
| { |
| 84 |
2863 |
| struct dirent *e = readdir(o->d); |
| 85 |
2863 |
▶ | return e ? e->d_name : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return e ? e->d_name : NULL; |
2338 |
525 |
2863 |
|
| 86 |
|
| } |
| 87 |
|
| #endif |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
525 |
| static void po_closedir(po_dir *o) { |
| 91 |
525 |
▶ | if (!o->open) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o->open) return; |
0 |
525 |
525 |
|
| 92 |
|
| #ifdef _WIN32 |
| 93 |
|
| FindClose(o->h); |
| 94 |
|
| #else |
| 95 |
525 |
| closedir(o->d); |
| 96 |
|
| #endif |
| 97 |
525 |
| o->open = 0; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| /* ---- paths ---------------------------------------------------------------- */ |
| 101 |
|
| |
| 102 |
|
| /* Joined with a forward slash on every platform, Windows included: it accepts |
| 103 |
|
| * one, and a store written on one machine and read on another must produce |
| 104 |
|
| * the same names. */ |
| 105 |
3133 |
| static size_t po_path_join(char *out, size_t cap, const char *a, const char *b) { |
| 106 |
3133 |
▶ | size_t an = a ? strlen(a) : 0, bn = b ? strlen(b) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| size_t an = a ? strlen(a) : 0, bn = b ? strlen(b) : 0; |
3133 |
0 |
3133 |
| size_t an = a ? strlen(a) : 0, bn = b ? strlen(b) : 0; |
3133 |
0 |
3133 |
|
| 107 |
3133 |
▶ | if (an && (a[an - 1] == '/' || a[an - 1] == '\\')) an--; |
Branch: 3 branches
| Branch | True | False | Total |
| if (an && (a[an - 1] == '/' || a[an - 1] == '\\')) an--; |
3133 |
0 |
3133 |
| if (an && (a[an - 1] == '/' || a[an - 1] == '\\')) an--; |
3133 |
0 |
3133 |
| if (an && (a[an - 1] == '/' || a[an - 1] == '\\')) an--; |
0 |
3133 |
3133 |
|
| 108 |
3133 |
▶ | if (an + 1 + bn + 1 > cap) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (an + 1 + bn + 1 > cap) return 0; |
0 |
3133 |
3133 |
|
| 109 |
3133 |
▶ | if (an) memcpy(out, a, an); |
Branch: 1 branch
| Branch | True | False | Total |
| if (an) memcpy(out, a, an); |
3133 |
0 |
3133 |
|
| 110 |
3133 |
| out[an] = '/'; |
| 111 |
3133 |
▶ | if (bn) memcpy(out + an + 1, b, bn); |
Branch: 1 branch
| Branch | True | False | Total |
| if (bn) memcpy(out + an + 1, b, bn); |
3133 |
0 |
3133 |
|
| 112 |
3133 |
| out[an + 1 + bn] = '\0'; |
| 113 |
3133 |
| return an + 1 + bn; |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
1413 |
| static int po_file_size(const char *path, po_u64 *out) { |
| 117 |
|
| struct stat st; |
| 118 |
1413 |
▶ | if (stat(path, &st) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stat(path, &st) != 0) return 0; |
9 |
1404 |
1413 |
|
| 119 |
1404 |
▶ | if (out) *out = (po_u64)st.st_size; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out) *out = (po_u64)st.st_size; |
879 |
525 |
1404 |
|
| 120 |
1404 |
| return 1; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
644 |
| static int po_is_dir(const char *path) { |
| 124 |
|
| struct stat st; |
| 125 |
644 |
▶ | if (stat(path, &st) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stat(path, &st) != 0) return 0; |
135 |
509 |
644 |
|
| 126 |
|
| #ifdef S_ISDIR |
| 127 |
509 |
| return S_ISDIR(st.st_mode) ? 1 : 0; |
| 128 |
|
| #else |
| 129 |
|
| return (st.st_mode & S_IFDIR) ? 1 : 0; |
| 130 |
|
| #endif |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| /* Every component, like `mkdir -p`. A store root that does not exist yet is |
| 134 |
|
| * the ordinary case on a first run, not an error. */ |
| 135 |
123 |
| static int po_mkpath(const char *path) { |
| 136 |
|
| char buf[PO_PATHMAX]; |
| 137 |
123 |
| size_t n = strlen(path), i; |
| 138 |
123 |
▶ | if (n >= sizeof(buf)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n >= sizeof(buf)) return 0; |
0 |
123 |
123 |
|
| 139 |
123 |
| memcpy(buf, path, n + 1); |
| 140 |
3354 |
▶ | for (i = 1; i <= n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= n; i++) { |
3231 |
123 |
3354 |
|
| 141 |
3231 |
▶ | if (buf[i] != '/' && buf[i] != '\\' && buf[i] != '\0') continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (buf[i] != '/' && buf[i] != '\\' && buf[i] != '\0') continue; |
2857 |
374 |
3231 |
| if (buf[i] != '/' && buf[i] != '\\' && buf[i] != '\0') continue; |
2857 |
0 |
2857 |
| if (buf[i] != '/' && buf[i] != '\\' && buf[i] != '\0') continue; |
2734 |
123 |
2857 |
|
| 142 |
|
| { |
| 143 |
497 |
| char c = buf[i]; |
| 144 |
497 |
| buf[i] = '\0'; |
| 145 |
497 |
▶ | if (!po_is_dir(buf)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_is_dir(buf)) { |
134 |
363 |
497 |
|
| 146 |
|
| #ifdef _WIN32 |
| 147 |
|
| _mkdir(buf); |
| 148 |
|
| #else |
| 149 |
134 |
| mkdir(buf, 0777); |
| 150 |
|
| #endif |
| 151 |
|
| } |
| 152 |
497 |
| buf[i] = c; |
| 153 |
|
| } |
| 154 |
|
| } |
| 155 |
123 |
| return po_is_dir(path); |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
|
| /* ---- the sidecar ---------------------------------------------------------- |
| 159 |
|
| * |
| 160 |
|
| * One key per line, tab separated, and the graph as one edge per line. A |
| 161 |
|
| * summary that needed a parser to read would be a summary nobody read while |
| 162 |
|
| * debugging a store at three in the morning, which is the only time anybody |
| 163 |
|
| * reads one. */ |
| 164 |
171 |
| static size_t po_idx_esc(const char *s, size_t n, char *out, size_t cap) { |
| 165 |
171 |
| size_t i, o = 0; |
| 166 |
771 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
600 |
171 |
771 |
|
| 167 |
600 |
| unsigned char c = (unsigned char)s[i]; |
| 168 |
600 |
▶ | if (c == '\t' || c == '\n' || c == '\\') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '\t' || c == '\n' || c == '\\') { |
600 |
0 |
600 |
| if (c == '\t' || c == '\n' || c == '\\') { |
600 |
0 |
600 |
| if (c == '\t' || c == '\n' || c == '\\') { |
0 |
600 |
600 |
|
| 169 |
|
| static const char hex[] = "0123456789abcdef"; |
| 170 |
0 |
▶ | if (o + 3 >= cap) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (o + 3 >= cap) break; |
0 |
0 |
0 |
|
| 171 |
0 |
| out[o++] = '\\'; |
| 172 |
0 |
| out[o++] = hex[c >> 4]; |
| 173 |
0 |
| out[o++] = hex[c & 15]; |
| 174 |
|
| } |
| 175 |
|
| else { |
| 176 |
600 |
▶ | if (o + 1 >= cap) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (o + 1 >= cap) break; |
0 |
600 |
600 |
|
| 177 |
600 |
| out[o++] = (char)c; |
| 178 |
|
| } |
| 179 |
|
| } |
| 180 |
171 |
| out[o] = '\0'; |
| 181 |
171 |
| return o; |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
166 |
| static size_t po_idx_unesc(const char *s, size_t n, char *out, size_t cap) { |
| 185 |
166 |
| size_t i = 0, o = 0; |
| 186 |
824 |
▶ | while (i < n && o + 1 < cap) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < n && o + 1 < cap) { |
658 |
166 |
824 |
| while (i < n && o + 1 < cap) { |
658 |
0 |
658 |
|
| 187 |
658 |
▶ | if (s[i] == '\\' && i + 2 < n) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[i] == '\\' && i + 2 < n) { |
0 |
658 |
658 |
| if (s[i] == '\\' && i + 2 < n) { |
0 |
0 |
0 |
|
| 188 |
0 |
| int hi = -1, lo = -1; |
| 189 |
0 |
| char a = s[i + 1], b = s[i + 2]; |
| 190 |
0 |
▶ | if (a >= '0' && a <= '9') hi = a - '0'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (a >= '0' && a <= '9') hi = a - '0'; |
0 |
0 |
0 |
| if (a >= '0' && a <= '9') hi = a - '0'; |
0 |
0 |
0 |
|
| 191 |
0 |
▶ | else if (a >= 'a' && a <= 'f') hi = a - 'a' + 10; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (a >= 'a' && a <= 'f') hi = a - 'a' + 10; |
0 |
0 |
0 |
| else if (a >= 'a' && a <= 'f') hi = a - 'a' + 10; |
0 |
0 |
0 |
|
| 192 |
0 |
▶ | if (b >= '0' && b <= '9') lo = b - '0'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (b >= '0' && b <= '9') lo = b - '0'; |
0 |
0 |
0 |
| if (b >= '0' && b <= '9') lo = b - '0'; |
0 |
0 |
0 |
|
| 193 |
0 |
▶ | else if (b >= 'a' && b <= 'f') lo = b - 'a' + 10; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (b >= 'a' && b <= 'f') lo = b - 'a' + 10; |
0 |
0 |
0 |
| else if (b >= 'a' && b <= 'f') lo = b - 'a' + 10; |
0 |
0 |
0 |
|
| 194 |
0 |
▶ | if (hi >= 0 && lo >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (hi >= 0 && lo >= 0) { |
0 |
0 |
0 |
| if (hi >= 0 && lo >= 0) { |
0 |
0 |
0 |
|
| 195 |
0 |
| out[o++] = (char)((hi << 4) | lo); |
| 196 |
0 |
| i += 3; |
| 197 |
0 |
| continue; |
| 198 |
|
| } |
| 199 |
|
| } |
| 200 |
658 |
| out[o++] = s[i++]; |
| 201 |
|
| } |
| 202 |
166 |
| out[o] = '\0'; |
| 203 |
166 |
| return o; |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
|
| /* ---- sealing -------------------------------------------------------------- |
| 207 |
|
| * |
| 208 |
|
| * A SEALED LOG IS THE UNIT OF THE READ SIDE. The live log is being appended |
| 209 |
|
| * to by the worker that owns it, so a reader gets whatever complete frames |
| 210 |
|
| * exist at the moment it looks - correct, but not stable, and not something |
| 211 |
|
| * to build an index over. |
| 212 |
|
| * |
| 213 |
|
| * Sealing writes the trailer, renames the file out of the way, and the caller |
| 214 |
|
| * computes the summary beside it. After that the file never changes again, so |
| 215 |
|
| * its index can never be stale. */ |
| 216 |
91 |
| static int po_seal_rename(const char *from, const char *to) { |
| 217 |
|
| /* rename(2) within a directory is atomic, so a reader either sees the |
| 218 |
|
| * live log or the sealed segment and never a file mid-rename. */ |
| 219 |
91 |
| return rename(from, to) == 0; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| /* The write-then-rename the index uses, for the same reason: a reader must |
| 223 |
|
| * never see half a sidecar. */ |
| 224 |
91 |
| static int po_atomic_write(const char *path, const char *bytes, size_t n) { |
| 225 |
|
| char tmp[PO_PATHMAX]; |
| 226 |
|
| FILE *f; |
| 227 |
|
| size_t w; |
| 228 |
91 |
| int len = snprintf(tmp, sizeof(tmp), "%s.tmp%ld", path, (long)po_getpid()); |
| 229 |
91 |
▶ | if (len <= 0 || (size_t)len >= sizeof(tmp)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len <= 0 || (size_t)len >= sizeof(tmp)) return 0; |
91 |
0 |
91 |
| if (len <= 0 || (size_t)len >= sizeof(tmp)) return 0; |
0 |
91 |
91 |
|
| 230 |
91 |
| f = fopen(tmp, "wb"); |
| 231 |
91 |
▶ | if (!f) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) return 0; |
0 |
91 |
91 |
|
| 232 |
91 |
▶ | w = n ? fwrite(bytes, 1, n, f) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| w = n ? fwrite(bytes, 1, n, f) : 0; |
91 |
0 |
91 |
|
| 233 |
91 |
| fclose(f); |
| 234 |
91 |
▶ | if (w != n) { remove(tmp); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w != n) { remove(tmp); return 0; } |
0 |
91 |
91 |
|
| 235 |
91 |
| remove(path); /* Windows rename refuses an existing target */ |
| 236 |
91 |
▶ | if (rename(tmp, path) != 0) { remove(tmp); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rename(tmp, path) != 0) { remove(tmp); return 0; } |
0 |
91 |
91 |
|
| 237 |
91 |
| return 1; |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| /* Read a whole file. Returns malloc'd bytes, or NULL. */ |
| 241 |
725 |
| static char *po_slurp(const char *path, size_t *out_len) { |
| 242 |
|
| FILE *f; |
| 243 |
725 |
| po_u64 sz = 0; |
| 244 |
|
| char *buf; |
| 245 |
|
| size_t got; |
| 246 |
|
| |
| 247 |
725 |
▶ | if (!po_file_size(path, &sz)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_file_size(path, &sz)) return NULL; |
4 |
721 |
725 |
|
| 248 |
721 |
| f = fopen(path, "rb"); |
| 249 |
721 |
▶ | if (!f) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) return NULL; |
0 |
721 |
721 |
|
| 250 |
721 |
| buf = (char *)malloc((size_t)sz + 1); |
| 251 |
721 |
▶ | if (!buf) { fclose(f); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) { fclose(f); return NULL; } |
0 |
721 |
721 |
|
| 252 |
721 |
▶ | got = sz ? fread(buf, 1, (size_t)sz, f) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| got = sz ? fread(buf, 1, (size_t)sz, f) : 0; |
721 |
0 |
721 |
|
| 253 |
721 |
| fclose(f); |
| 254 |
721 |
| buf[got] = '\0'; |
| 255 |
721 |
▶ | if (out_len) *out_len = got; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_len) *out_len = got; |
721 |
0 |
721 |
|
| 256 |
721 |
| return buf; |
| 257 |
|
| } |
| 258 |
|
| |
| 259 |
|
| #endif /* PO_STOREIO_H */ |