-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathsentry_path_windows.c
More file actions
816 lines (735 loc) · 19.9 KB
/
sentry_path_windows.c
File metadata and controls
816 lines (735 loc) · 19.9 KB
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#include "sentry_boot.h"
#include "sentry_alloc.h"
#include "sentry_core.h"
#include "sentry_path.h"
#include "sentry_string.h"
#include "sentry_utils.h"
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <stdlib.h>
#include <sys/locking.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <wchar.h>
// follow the maximum path length documented here:
// https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
#define MAX_PATH_BUFFER_SIZE 32768
// only read this many bytes to memory ever
// Increased to 512MB to support large minidumps from TSAN/ASAN builds
static const size_t MAX_READ_TO_BUFFER = 536870912;
#ifndef __MINGW32__
# define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
# define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
#endif
struct sentry_pathiter_s {
HANDLE dir_handle;
const sentry_path_t *parent;
sentry_path_t *current;
};
static size_t
write_loop(FILE *f, const char *buf, size_t buf_len)
{
while (buf_len > 0) {
size_t n = fwrite(buf, 1, buf_len, f);
if (n == 0 && errno == EINVAL) {
continue;
}
if (n < buf_len) {
break;
}
buf += n;
buf_len -= n;
}
fflush(f);
return buf_len;
}
bool
sentry__filelock_try_lock(sentry_filelock_t *lock)
{
lock->is_locked = false;
int fd = _wopen(lock->path->path_w, _O_RDWR | _O_CREAT | _O_TRUNC,
_S_IREAD | _S_IWRITE);
if (fd < 0) {
return false;
}
if (_locking(fd, _LK_NBLCK, 1) != 0) {
_close(fd);
return false;
}
lock->fd = fd;
lock->is_locked = true;
return true;
}
void
sentry__filelock_unlock(sentry_filelock_t *lock)
{
if (!lock->is_locked) {
return;
}
_locking(lock->fd, LK_UNLCK, 1);
_close(lock->fd);
// the remove function will fail if we, or any other process still has an
// open handle to the file.
sentry__path_remove(lock->path);
lock->is_locked = false;
}
// we assume the `len` parameter to include null-termination
static sentry_path_t *
path_with_len(size_t len)
{
sentry_path_t *rv = SENTRY_MAKE(sentry_path_t);
if (!rv) {
return NULL;
}
rv->path = sentry_malloc(sizeof(char) * len);
if (!rv->path) {
sentry_free(rv);
return NULL;
}
// let call sites allocate, because the narrow UTF-8 length passed in here
// comes from strlen which always returns an encoding-dependent byte length,
// and so we must read the full string to get its wide length.
rv->path_w = NULL;
return rv;
}
sentry_path_t *
sentry__path_absolute(const sentry_path_t *path)
{
wchar_t *path_wstr = path->path_w;
if (!path_wstr) {
return NULL;
}
// _wfullpath allocates a correctly sized buffer for the absolute path when
// passing in a NULL buffer. This limits allocation to a minimum (instead of
// passing in a MAX_PATH_BUFFER_SIZE buffer). But we must clone at the end,
// so we return a `sentry_malloc()` allocated buffer rather than one from
// the system allocator (which allows us to open our allocator).
wchar_t *full = _wfullpath(NULL, path_wstr, 0);
if (!full) {
return NULL;
}
sentry_path_t *rv = SENTRY_MAKE(sentry_path_t);
if (!rv) {
free(full);
return NULL;
}
// we convert the wide-string absolute path back to canonical narrow UTF-8
rv->path = sentry__string_from_wstr(full);
if (!rv->path) {
free(full);
sentry_free(rv);
return NULL;
}
rv->path_w = sentry__string_clone_wstr(full);
free(full);
if (!rv->path_w) {
sentry_free(rv->path);
sentry_free(rv);
return NULL;
}
return rv;
}
sentry_path_t *
sentry__path_current_exe(void)
{
// Let's be safe here and use the "new" 32K character limit for paths.
wchar_t *path_wstr = sentry_malloc(MAX_PATH_BUFFER_SIZE * sizeof(wchar_t));
const size_t len
= GetModuleFileNameW(NULL, path_wstr, MAX_PATH_BUFFER_SIZE);
if (!len) {
SENTRY_WARN("unable to get current exe path");
sentry_free(path_wstr);
return NULL;
}
sentry_path_t *path = SENTRY_MAKE(sentry_path_t);
if (!path) {
sentry_free(path_wstr);
return NULL;
}
// convert the path to our canonical narrow UTF-8...
path->path = sentry__string_from_wstr(path_wstr);
if (!path->path) {
sentry_free(path);
sentry_free(path_wstr);
return NULL;
}
// ...and create a fit-sized buffer for the wide-char so we can free the 64k
path->path_w = sentry__string_clone_wstr(path_wstr);
if (!path->path_w) {
sentry_free(path->path);
sentry_free(path);
sentry_free(path_wstr);
return NULL;
}
sentry_free(path_wstr);
return path;
}
sentry_path_t *
sentry__path_dir(const sentry_path_t *path)
{
sentry_path_t *dir_path = sentry__path_clone(path);
if (!dir_path) {
return NULL;
}
// find the filename part and truncate just in front of it if possible
char *filename = (char *)sentry__path_filename(dir_path);
if (filename > dir_path->path) {
*(filename - 1) = '\0';
}
wchar_t *filename_w = (wchar_t *)sentry__path_filename_w(dir_path);
if (filename_w > dir_path->path_w) {
*(filename_w - 1) = L'\0';
}
return dir_path;
}
sentry_path_t *
sentry__path_from_wstr_n(const wchar_t *s, size_t s_len)
{
if (!s) {
return NULL;
}
sentry_path_t *rv = SENTRY_MAKE(sentry_path_t);
if (!rv) {
return NULL;
}
rv->path = sentry__string_from_wstr_n(s, s_len);
if (!rv->path) {
sentry_free(rv);
return NULL;
}
rv->path_w = sentry_malloc(sizeof(wchar_t) * (s_len + 1));
if (!rv->path_w) {
sentry_free(rv->path);
sentry_free(rv);
return NULL;
}
if (s_len) {
wmemcpy(rv->path_w, s, s_len);
}
rv->path_w[s_len] = L'\0';
return rv;
}
sentry_path_t *
sentry__path_from_wstr(const wchar_t *s)
{
if (!s) {
return NULL;
}
return sentry__path_from_wstr_n(s, wcslen(s));
}
sentry_path_t *
sentry__path_join_wstr(const sentry_path_t *base, const wchar_t *other_w)
{
char *other = sentry__string_from_wstr(other_w);
if (!other) {
return NULL;
}
sentry_path_t *rv = sentry__path_join_str(base, other);
sentry_free(other);
return rv;
}
const char *
sentry__path_filename(const sentry_path_t *path)
{
const char *s = path->path;
const char *ptr = s;
size_t idx = strlen(s);
while (true) {
if (s[idx] == '/' || s[idx] == '\\') {
ptr = s + idx + 1;
break;
}
if (idx > 0) {
idx -= 1;
} else {
break;
}
}
return ptr;
}
const wchar_t *
sentry__path_filename_w(const sentry_path_t *path)
{
const wchar_t *s = path->path_w;
const wchar_t *ptr = s;
size_t idx = wcslen(s);
while (true) {
if (s[idx] == L'/' || s[idx] == L'\\') {
ptr = s + idx + 1;
break;
}
if (idx > 0) {
idx -= 1;
} else {
break;
}
}
return ptr;
}
bool
sentry__path_filename_matches(const sentry_path_t *path, const char *filename)
{
sentry_path_t *fn = sentry__path_from_str(filename);
if (!fn) {
return false;
}
bool matches = _stricmp(sentry__path_filename(path), fn->path) == 0;
sentry__path_free(fn);
return matches;
}
bool
sentry__path_ends_with(const sentry_path_t *path, const char *suffix)
{
sentry_path_t *s = sentry__path_from_str(suffix);
if (!s) {
return false;
}
const size_t pathlen = strlen(path->path);
const size_t suffixlen = strlen(s->path);
if (suffixlen > pathlen) {
sentry__path_free(s);
return false;
}
bool matches = _stricmp(&path->path[pathlen - suffixlen], s->path) == 0;
sentry__path_free(s);
return matches;
}
bool
sentry__path_is_dir(const sentry_path_t *path)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return false;
}
struct _stat buf;
return _wstat(path_w, &buf) == 0 && S_ISDIR(buf.st_mode);
}
bool
sentry__path_is_file(const sentry_path_t *path)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return false;
}
struct _stat buf;
return _wstat(path_w, &buf) == 0 && S_ISREG(buf.st_mode);
}
size_t
sentry__path_get_size(const sentry_path_t *path)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return 0;
}
struct _stat buf;
size_t result = 0;
if (_wstat(path_w, &buf) == 0 && S_ISREG(buf.st_mode)) {
result = (size_t)buf.st_size;
}
return result;
}
time_t
sentry__path_get_mtime(const sentry_path_t *path)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return 0;
}
struct _stat buf;
time_t result = 0;
if (_wstat(path_w, &buf) == 0) {
result = (time_t)buf.st_mtime;
}
return result;
}
sentry_path_t *
sentry__path_append_str(const sentry_path_t *base, const char *suffix)
{
sentry_path_t *suffix_path = sentry__path_from_str(suffix);
if (!suffix_path) {
return NULL;
}
// concat into a new path
const size_t len_base = strlen(base->path);
const size_t len_suffix = strlen(suffix_path->path);
const size_t len = len_base + len_suffix + 1;
sentry_path_t *rv = path_with_len(len);
if (rv) {
memcpy(rv->path, base->path, len_base * sizeof(char));
memcpy(rv->path + len_base, suffix_path->path,
(len_suffix + 1) * sizeof(char));
rv->path_w = sentry__string_to_wstr(rv->path);
if (!rv->path_w) {
sentry__path_free(rv);
return NULL;
}
}
sentry__path_free(suffix_path);
return rv;
}
sentry_path_t *
sentry__path_join_str(const sentry_path_t *base, const char *other)
{
// If `other` is an absolute drive path, we just return `other` as a path
if (isalpha(other[0]) && other[1] == ':') {
return sentry__path_from_str(other);
}
// If `other` is an absolute path without a drive...
if (other[0] == '/' || other[0] == '\\') {
// ...we depend on `base` being a drive path to prepend that drive
if (isalpha(base->path[0]) && base->path[1] == ':') {
const size_t other_len = strlen(other);
sentry_path_t *rv = path_with_len(other_len + 3);
if (!rv) {
return NULL;
}
rv->path[0] = base->path[0];
rv->path[1] = ':';
memcpy(rv->path + 2, other, sizeof(char) * other_len);
rv->path[other_len + 2] = '\0';
rv->path_w = sentry__string_to_wstr(rv->path);
if (!rv->path_w) {
sentry__path_free(rv);
return NULL;
}
return rv;
}
// ...or return `other` as an absolute path without a drive specified
return sentry__path_from_str(other);
}
// in all other cases we join the two paths
const size_t base_len = strlen(base->path);
const size_t other_len = strlen(other);
size_t len = base_len + other_len + 1;
bool need_sep = false;
if (base_len && base->path[base_len - 1] != '/'
&& base->path[base_len - 1] != '\\') {
len += 1;
need_sep = true;
}
sentry_path_t *rv = path_with_len(len);
if (!rv) {
return NULL;
}
memcpy(rv->path, base->path, sizeof(char) * base_len);
if (need_sep) {
rv->path[base_len] = '\\';
rv->path[base_len + 1] = '\0';
}
size_t join_offset = base_len + (need_sep ? 1 : 0);
memcpy(rv->path + join_offset, other, sizeof(char) * (other_len + 1));
rv->path_w = sentry__string_to_wstr(rv->path);
if (!rv->path_w) {
sentry__path_free(rv);
return NULL;
}
return rv;
}
sentry_path_t *
sentry__path_clone(const sentry_path_t *path)
{
sentry_path_t *rv = SENTRY_MAKE(sentry_path_t);
if (!rv) {
return NULL;
}
rv->path = sentry__string_clone(path->path);
rv->path_w = sentry__string_clone_wstr(path->path_w);
if (!rv->path || !rv->path_w) {
sentry__path_free(rv);
return NULL;
}
return rv;
}
static int
is_last_error_path_not_found(void)
{
const DWORD last_error = GetLastError();
return last_error == ERROR_PATH_NOT_FOUND
|| last_error == ERROR_FILE_NOT_FOUND;
}
int
sentry__path_remove(const sentry_path_t *path)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return 1;
}
const BOOL removal_success = sentry__path_is_dir(path)
? RemoveDirectoryW(path_w)
: DeleteFileW(path_w);
return removal_success ? 0 : !is_last_error_path_not_found();
}
int
sentry__path_rename(const sentry_path_t *src, const sentry_path_t *dst)
{
wchar_t *src_w = src->path_w;
wchar_t *dst_w = dst->path_w;
if (!src_w || !dst_w) {
return 1;
}
// MOVEFILE_REPLACE_EXISTING allows overwriting the destination if it exists
return MoveFileExW(src_w, dst_w, MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
}
int
sentry__path_copy(const sentry_path_t *src, const sentry_path_t *dst)
{
if (!src->path_w || !dst->path_w) {
return 1;
}
return CopyFileW(src->path_w, dst->path_w, FALSE) ? 0 : 1;
}
int
sentry__path_create_dir_all(const sentry_path_t *path)
{
char *p = NULL;
char *ptr = NULL;
int rv = 0;
#define _TRY_MAKE_DIR \
do { \
wchar_t *p_w = sentry__string_to_wstr(p); \
if (!p_w) { \
rv = 1; \
goto done; \
} \
BOOL success = CreateDirectoryW(p_w, NULL); \
DWORD err = GetLastError(); \
sentry_free(p_w); \
if (!success && err != ERROR_ALREADY_EXISTS) { \
rv = 1; \
goto done; \
} \
} while (0)
const size_t len = strlen(path->path) + 1;
p = sentry_malloc(sizeof(char) * len);
if (!p) {
return 1;
}
memcpy(p, path->path, len * sizeof(char));
for (ptr = p; *ptr; ptr++) {
if ((*ptr == '\\' || *ptr == '/') && ptr != p && ptr[-1] != ':') {
*ptr = 0;
_TRY_MAKE_DIR;
*ptr = '\\';
}
}
_TRY_MAKE_DIR;
#undef _TRY_MAKE_DIR
done:
sentry_free(p);
return rv;
}
sentry_pathiter_t *
sentry__path_iter_directory(const sentry_path_t *path)
{
sentry_pathiter_t *rv = SENTRY_MAKE(sentry_pathiter_t);
if (!rv) {
return NULL;
}
rv->dir_handle = INVALID_HANDLE_VALUE;
rv->parent = path;
rv->current = NULL;
return rv;
}
const sentry_path_t *
sentry__pathiter_next(sentry_pathiter_t *piter)
{
WIN32_FIND_DATAW data;
while (true) {
if (piter->dir_handle == INVALID_HANDLE_VALUE) {
const size_t path_len_w = wcslen(piter->parent->path_w);
wchar_t *pattern
= sentry_malloc(sizeof(wchar_t) * (path_len_w + 3));
if (!pattern) {
return NULL;
}
memcpy(
pattern, piter->parent->path_w, sizeof(wchar_t) * path_len_w);
pattern[path_len_w] = L'\\';
pattern[path_len_w + 1] = L'*';
pattern[path_len_w + 2] = L'\0';
piter->dir_handle = FindFirstFileW(pattern, &data);
sentry_free(pattern);
if (piter->dir_handle == INVALID_HANDLE_VALUE) {
return NULL;
}
} else {
if (!FindNextFileW(piter->dir_handle, &data)) {
return NULL;
}
}
if (wcscmp(data.cFileName, L".") != 0
&& wcscmp(data.cFileName, L"..") != 0) {
break;
}
}
if (piter->current) {
sentry__path_free(piter->current);
}
piter->current = sentry__path_join_wstr(piter->parent, data.cFileName);
return piter->current;
}
void
sentry__pathiter_free(sentry_pathiter_t *piter)
{
if (!piter) {
return;
}
if (piter->dir_handle != INVALID_HANDLE_VALUE) {
FindClose(piter->dir_handle);
}
sentry__path_free(piter->current);
sentry_free(piter);
}
int
sentry__path_touch(const sentry_path_t *path)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return 1;
}
FILE *f = _wfopen(path_w, L"a");
if (f) {
fclose(f);
return 0;
}
return 1;
}
char *
sentry__path_read_to_buffer(const sentry_path_t *path, size_t *size_out)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return NULL;
}
FILE *f = _wfopen(path_w, L"rb");
if (!f) {
return NULL;
}
const size_t len = sentry__path_get_size(path);
if (len == 0) {
fclose(f);
char *rv = sentry_malloc(1);
if (rv) {
rv[0] = '\0';
}
if (size_out) {
*size_out = 0;
}
return rv;
}
if (len > MAX_READ_TO_BUFFER) {
fclose(f);
return NULL;
}
// this is completely not sane in concurrent situations but hey
char *rv = sentry_malloc(len + 1);
if (!rv) {
fclose(f);
return NULL;
}
size_t remaining = len;
size_t offset = 0;
while (remaining > 0) {
size_t n = fread(rv + offset, 1, remaining, f);
if (n == 0) {
break;
}
offset += n;
remaining -= n;
}
rv[offset] = '\0';
fclose(f);
if (size_out) {
*size_out = offset;
}
return rv;
}
static int
write_buffer_with_mode(const sentry_path_t *path, const char *buf,
size_t buf_len, const wchar_t *mode)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return 1;
}
FILE *f = _wfopen(path_w, mode);
if (!f) {
return 1;
}
size_t remaining = write_loop(f, buf, buf_len);
fclose(f);
return remaining == 0 ? 0 : 1;
}
int
sentry__path_write_buffer(
const sentry_path_t *path, const char *buf, size_t buf_len)
{
return write_buffer_with_mode(path, buf, buf_len, L"wb");
}
int
sentry__path_append_buffer(
const sentry_path_t *path, const char *buf, size_t buf_len)
{
return write_buffer_with_mode(path, buf, buf_len, L"ab");
}
struct sentry_filewriter_s {
size_t byte_count;
FILE *f;
};
MUST_USE sentry_filewriter_t *
sentry__filewriter_new(const sentry_path_t *path)
{
wchar_t *path_w = path->path_w;
if (!path_w) {
return NULL;
}
FILE *f = _wfopen(path_w, L"wb");
if (!f) {
return NULL;
}
sentry_filewriter_t *result = SENTRY_MAKE(sentry_filewriter_t);
if (!result) {
fclose(f);
return NULL;
}
result->f = f;
result->byte_count = 0;
return result;
}
size_t
sentry__filewriter_write(
sentry_filewriter_t *filewriter, const char *buf, size_t buf_len)
{
if (!filewriter) {
return 0;
}
while (buf_len > 0) {
size_t n = fwrite(buf, 1, buf_len, filewriter->f);
if (n == 0 && errno == EINVAL) {
continue;
}
if (n < buf_len) {
break;
}
filewriter->byte_count += n;
buf += n;
buf_len -= n;
}
return buf_len;
}
void
sentry__filewriter_free(sentry_filewriter_t *filewriter)
{
if (!filewriter) {
return;
}
fflush(filewriter->f);
fclose(filewriter->f);
sentry_free(filewriter);
}
size_t
sentry__filewriter_byte_count(const sentry_filewriter_t *filewriter)
{
return filewriter->byte_count;
}