• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aremmell / libsir / 489

07 Sep 2023 07:23AM UTC coverage: 94.735% (-0.05%) from 94.782%
489

Pull #268

gitlab-ci

aremmell
set perf test lines back to 100k
Pull Request #268: On Windows, use native Win32 API for file I/O

18 of 18 new or added lines in 2 files covered. (100.0%)

3113 of 3286 relevant lines covered (94.74%)

614543.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.65
/src/sirfilecache.c
1
/*
2
 * sirfilecache.c
3
 *
4
 * Author:    Ryan M. Lederman <lederman@gmail.com>
5
 * Copyright: Copyright (c) 2018-2023
6
 * Version:   2.2.3
7
 * License:   The MIT License (MIT)
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
10
 * this software and associated documentation files (the "Software"), to deal in
11
 * the Software without restriction, including without limitation the rights to
12
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13
 * the Software, and to permit persons to whom the Software is furnished to do so,
14
 * subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 */
26
#include "sir/filecache.h"
27
#include "sir/filesystem.h"
28
#include "sir/internal.h"
29
#include "sir/defaults.h"
30

31
sirfileid _sir_addfile(const char* path, sir_levels levels, sir_options opts) {
37,149✔
32
    (void)_sir_seterror(_SIR_E_NOERROR);
37,149✔
33

34
    if (!_sir_sanity())
37,151✔
35
        return 0U;
2✔
36

37
    _SIR_LOCK_SECTION(sirfcache, sfc, SIRMI_FILECACHE, 0U);
37,149✔
38

39
    _sir_defaultlevels(&levels, sir_file_def_lvls);
37,149✔
40
    _sir_defaultopts(&opts, sir_file_def_opts);
37,149✔
41

42
    sirfileid retval = _sir_fcache_add(sfc, path, levels, opts);
37,149✔
43
    _SIR_UNLOCK_SECTION(SIRMI_FILECACHE);
37,149✔
44

45
    return retval;
37,149✔
46
}
47

48
bool _sir_updatefile(sirfileid id, const sir_update_config_data* data) {
36,319✔
49
    (void)_sir_seterror(_SIR_E_NOERROR);
36,319✔
50

51
    if (!_sir_sanity() || !_sir_validfileid(id) || !_sir_validupdatedata(data))
36,319✔
52
        return false;
×
53

54
    _SIR_LOCK_SECTION(sirfcache, sfc, SIRMI_FILECACHE, false);
36,319✔
55
    bool retval = _sir_fcache_update(sfc, id, data);
36,319✔
56
    _SIR_UNLOCK_SECTION(SIRMI_FILECACHE);
36,319✔
57

58
    return retval;
36,319✔
59
}
60

61
bool _sir_remfile(sirfileid id) {
36,884✔
62
    (void)_sir_seterror(_SIR_E_NOERROR);
36,884✔
63

64
    if (!_sir_sanity() || !_sir_validfileid(id))
36,884✔
65
        return false;
16✔
66

67
    _SIR_LOCK_SECTION(sirfcache, sfc, SIRMI_FILECACHE, false);
36,865✔
68
    bool retval = _sir_fcache_rem(sfc, id);
36,865✔
69
    _SIR_UNLOCK_SECTION(SIRMI_FILECACHE);
36,865✔
70

71
    return retval;
36,865✔
72
}
73

74
sirfile* _sirfile_create(const char* path, sir_levels levels, sir_options opts) {
36,928✔
75
    if (!_sir_validstr(path) || !_sir_validlevels(levels) || !_sir_validopts(opts))
36,928✔
76
        return NULL;
×
77

78
    sirfile* sf = (sirfile*)calloc(1, sizeof(sirfile));
36,928✔
79
    if (!sf) {
36,928✔
80
        (void)_sir_handleerr(errno);
24✔
81
        return NULL;
24✔
82
    }
83

84
    sf->path = strndup(path, strnlen(path, SIR_MAXPATH));
36,904✔
85
    if (!sf->path) {
36,904✔
86
        (void)_sir_handleerr(errno);
29✔
87
        _sir_safefree(&sf);
29✔
88
        return NULL;
29✔
89
    }
90

91
    sf->levels = levels;
36,875✔
92
    sf->opts   = opts;
36,875✔
93

94
    if (!_sirfile_open(sf) || !_sirfile_validate(sf)) {
36,875✔
95
        _sirfile_destroy(&sf);
69✔
96
        return NULL;
69✔
97
    }
98

99
    return sf;
36,806✔
100
}
101

102
bool _sirfile_open(sirfile* sf) {
36,909✔
103
    if (!_sir_validptr(sf) && !_sir_validstr(sf->path))
36,909✔
104
        return false;
×
105

106
#if !defined(__WIN__)
107
    FILE* f  = NULL;
36,909✔
108
    bool open = _sir_openfile(&f, sf->path, SIR_FOPENMODE, SIR_PATH_REL_TO_CWD);
36,909✔
109
    if (!open || !f)
36,909✔
110
        return false;
63✔
111
#else /* __WIN__ */
112
    HANDLE h = NULL;
113
    bool open = _sir_openfilewin32(&h, sf->path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
114
        OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, SIR_PATH_REL_TO_CWD);
115
    if (!open || INVALID_HANDLE_VALUE == h)
116
        return false;
117
#endif
118

119
    _sirfile_close(sf);
36,840✔
120

121
#if !defined(__WIN__)
122
    sf->f = f;
36,840✔
123
#else
124
    sf->h = h;
125
#endif
126

127
    sf->id = FNV32_1a((const uint8_t*)sf->path, strnlen(sf->path, SIR_MAXPATH));
36,840✔
128

129
    return true;
36,840✔
130
}
131

132
void _sirfile_close(sirfile* sf) {
73,711✔
133
    if (!_sir_validptrnofail(sf))
73,711✔
134
        return;
×
135

136
#if !defined(__WIN__)
137
    _sir_safefclose(&sf->f);
73,711✔
138
#else /* __WIN__ */
139
    _sir_safeclosehandle(&sf->h);
140
#endif
141
}
142

143
bool _sirfile_write(sirfile* sf, const char* output) {
1,291,454✔
144
    if (!_sirfile_validate(sf) || !_sir_validstr(output))
1,291,454✔
145
        return false;
×
146

147
    if (_sirfile_needsroll(sf)) {
1,291,454✔
148
        bool rolled   = false;
183✔
149
        char* newpath = NULL;
186✔
150

151
        _sir_selflog("file (path: '%s', id: %"PRIx32") reached ~%d bytes in size;"
185✔
152
                     " rolling...", sf->path, sf->id, SIR_FROLLSIZE);
153

154
#if !defined(__WIN__)
155
        _sir_fflush(sf->f);
186✔
156
#endif
157

158
        if (_sirfile_roll(sf, &newpath)) {
186✔
159
            char header[SIR_MAXFHEADER] = {0};
34✔
160
            (void)snprintf(header, SIR_MAXFHEADER, SIR_FHROLLED, newpath);
34✔
161
            rolled = _sirfile_writeheader(sf, header);
34✔
162
        }
163

164
        _sir_safefree(&newpath);
186✔
165
        if (!rolled) /* write anyway; don't want to lose data. */
185✔
166
            _sir_selflog("error: failed to roll file (path: '%s', id: %" PRIx32")!",
152✔
167
                sf->path, sf->id);
168
    }
169

170
    size_t writeLen = strnlen(output, SIR_MAXOUTPUT);
1,291,454✔
171
#if !defined(__WIN__)
172
    size_t write = fwrite(output, sizeof(char), writeLen, sf->f);
1,291,454✔
173
    if (write < writeLen) {
1,291,454✔
174
        (void)_sir_handleerr(errno);
×
175
        clearerr(sf->f);
×
176
    }
177
#else /* __WIN__ */
178
    DWORD from_write_file = 0UL;
179
    if (!WriteFile(sf->h, output, (DWORD)writeLen, &from_write_file, NULL))
180
        (void)_sir_handlewin32err(GetLastError());
181
    size_t write = (size_t)from_write_file;
182
#endif
183

184
    SIR_ASSERT(write == writeLen);
1,274,897✔
185
    return write == writeLen;
1,291,454✔
186
}
187

188
bool _sirfile_writeheader(sirfile* sf, const char* msg) {
36,784✔
189
    if (!_sirfile_validate(sf) || !_sir_validstr(msg))
36,784✔
190
        return false;
×
191

192
    time_t now = -1;
36,784✔
193
    time(&now);
36,784✔
194

195
    char timestamp[SIR_MAXTIME] = {0};
36,784✔
196
    bool fmttime = _sir_formattime(now, timestamp, SIR_FHTIMEFORMAT);
36,784✔
197
    if (!fmttime)
36,784✔
198
        return false;
2,083✔
199

200
    char header[SIR_MAXFHEADER] = {0};
34,701✔
201
    (void)snprintf(header, SIR_MAXFHEADER, SIR_FHFORMAT, msg, timestamp);
28,622✔
202

203
    return _sirfile_write(sf, header);
34,701✔
204
}
205

206
bool _sirfile_needsroll(sirfile* sf) {
1,291,454✔
207
    if (!_sirfile_validate(sf))
1,291,454✔
208
        return false;
×
209

210
    off_t size = 0;
1,241,778✔
211
#if !defined (__WIN__)
212
    struct stat st = {0};
1,291,454✔
213
    int getstat    = fstat(fileno(sf->f), &st);
1,291,454✔
214

215
    if (0 != getstat) { /* if fstat fails, try stat on the path. */
1,291,454✔
216
        getstat = stat(sf->path, &st);
17,216✔
217
        if (0 != getstat)
17,216✔
218
            return _sir_handleerr(errno);
×
219
    }
220

221
    size = st.st_size;
1,291,454✔
222
#else /* __WIN__ */
223
    LARGE_INTEGER li = {0};
224
    if (!GetFileSizeEx(sf->h, &li))
225
        (void)_sir_handlewin32err(GetLastError());
226
    size = (off_t)li.QuadPart;
227
#endif
228
    return size + BUFSIZ >= SIR_FROLLSIZE ||
2,582,745✔
229
        SIR_FROLLSIZE - (size + BUFSIZ) <= BUFSIZ;
1,291,288✔
230
}
231

232
bool _sirfile_roll(sirfile* sf, char** newpath) {
186✔
233
    if (!_sirfile_validate(sf) || !_sir_validptrptr(newpath))
186✔
234
        return false;
×
235

236
    bool retval = false;
183✔
237
    char* name = NULL;
186✔
238
    char* ext  = NULL;
186✔
239

240
    bool split = _sirfile_splitpath(sf, &name, &ext);
186✔
241
    SIR_ASSERT(split);
185✔
242

243
    if (split) {
184✔
244
        time_t now = -1;
186✔
245

246
        time(&now);
186✔
247
        SIR_ASSERT(-1 != now);
185✔
248

249
        if (-1 != now) {
186✔
250
            char timestamp[SIR_MAXTIME] = {0};
110✔
251
            bool fmttime = _sir_formattime(now, timestamp, SIR_FNAMETIMEFORMAT);
110✔
252
            SIR_ASSERT(fmttime);
109✔
253

254
            if (fmttime) {
108✔
255
                *newpath = (char*)calloc(SIR_MAXPATH, sizeof(char));
34✔
256

257
                if (_sir_validptr(*newpath)) {
34✔
258
                    char seqbuf[7] = {0};
34✔
259
                    bool exists = false;
34✔
260
                    bool resolved = false;
31✔
261
                    uint16_t sequence = 0U;
31✔
262

263
                    do {
264
                        (void)snprintf(*newpath, SIR_MAXPATH, SIR_FNAMEFORMAT, name,
78✔
265
                            timestamp, (sequence > 0U ? seqbuf : ""),
266
                            _sir_validstrnofail(ext) ? ext : "");
75✔
267

268
                        /* if less than one second has elapsed since the last roll
269
                         * operation, then we'll overwrite the last rolled log file,
270
                         * and that = data loss. make sure the target path does not
271
                         * already exist. */
272
                        if (!_sir_pathexists(*newpath, &exists, SIR_PATH_REL_TO_CWD)) {
75✔
273
                            /* failed to determine if the file already exists; it is better
274
                             * to continue logging to the same file than to possibly overwrite
275
                             * another (if it failed this time, it will again, so there's no
276
                             * way to definitively choose a good new path). */
277
                            break;
×
278
                        } else if (exists) {
75✔
279
                            /* the file already exists; add a number to the file name
280
                             * until one that does not exist is found. */
281
                            _sir_selflog("path: '%s' already exists; incrementing sequence", *newpath); //-V576
41✔
282
                            sequence++;
41✔
283
                        } else {
284
                            _sir_selflog("found good path: '%s'", *newpath);
33✔
285
                            resolved = true;
31✔
286
                            break;
31✔
287
                        }
288

289
                        if (sequence > 0)
41✔
290
                            (void)snprintf(seqbuf, 7, SIR_FNAMESEQFORMAT, sequence);
41✔
291

292
                    } while (sequence <= 999U);
41✔
293

294
                    if (!resolved)
31✔
295
                        _sir_selflog("error: unable to determine suitable path for '%s';"
×
296
                                        " not rolling!", sf->path);
297

298
                    retval = resolved && _sirfile_archive(sf, *newpath);
34✔
299
                }
300
            }
301
        }
302
    }
303

304
    _sir_safefree(&name);
186✔
305
    _sir_safefree(&ext);
186✔
306

307
    return retval;
186✔
308

309
}
310

311
bool _sirfile_archive(sirfile* sf, const char* newpath) {
34✔
312
    if (!_sirfile_validate(sf) || !_sir_validstr(newpath))
34✔
313
        return false;
×
314

315
#if defined(__WIN__)
316
    /* apparently, need to close the old file first on windows. */
317
    _sirfile_close(sf);
318
#endif
319

320
    if (0 != rename(sf->path, newpath))
34✔
321
        return _sir_handleerr(errno);
×
322

323
    if (_sirfile_open(sf)) {
34✔
324
        _sir_selflog("archived '%s' " SIR_R_ARROW " '%s'", sf->path, newpath);
33✔
325
        return true;
33✔
326
    }
327

328
    return false;
×
329
}
330

331
bool _sirfile_splitpath(const sirfile* sf, char** name, char** ext) {
186✔
332
    if (_sir_validptrptr(name))
186✔
333
        *name = NULL;
186✔
334
    if (_sir_validptrptr(ext))
186✔
335
        *ext = NULL;
186✔
336

337
    if (!_sirfile_validate(sf) || !_sir_validptrptr(name) || !_sir_validptrptr(ext))
186✔
338
        return false;
×
339

340
    char* tmp = strndup(sf->path, strnlen(sf->path, SIR_MAXPATH));
186✔
341
    if (!tmp)
186✔
342
        return _sir_handleerr(errno);
×
343

344
    const char* lastfullstop = strrchr(tmp, '.');
186✔
345
    if (lastfullstop) {
186✔
346
        uintptr_t namesize = lastfullstop - tmp;
166✔
347
        SIR_ASSERT(namesize < SIR_MAXPATH);
165✔
348

349
        tmp[namesize] = '\0';
166✔
350
        *name = (char*)calloc(namesize + 1, sizeof(char));
166✔
351
        if (!*name) {
166✔
352
            _sir_safefree(&tmp);
×
353
            return _sir_handleerr(errno);
×
354
        }
355

356
        _sir_strncpy(*name, namesize + 1, tmp, namesize);
166✔
357
        *ext = strndup(sf->path + namesize, strnlen(sf->path + namesize, SIR_MAXPATH));
166✔
358
    } else {
359
        *name = strndup(sf->path, strnlen(sf->path, SIR_MAXPATH));
20✔
360
    }
361

362
    _sir_safefree(&tmp);
186✔
363
    return _sir_validstr(*name) && (!lastfullstop || _sir_validstr(*ext));
189✔
364
}
365

366
void _sirfile_destroy(sirfile** sf) {
36,871✔
367
    if (sf && *sf) {
36,871✔
368
        _sirfile_close(*sf);
36,871✔
369
        _sir_safefree(&(*sf)->path);
36,871✔
370
        _sir_safefree(sf);
36,871✔
371
    }
372
}
36,871✔
373

374
bool _sirfile_validate(const sirfile* sf) {
4,083,003✔
375
    return _sir_validptrnofail(sf) &&
8,165,884✔
376
#if !defined(__WIN__)
377
        _sir_validptrnofail(sf->f) &&
8,165,762✔
378
#else /* __WIN__ */
379
        (_sir_validptrnofail(sf->h) && INVALID_HANDLE_VALUE != sf->h) &&
380
#endif
381
        _sir_validstrnofail(sf->path) && _sir_validfileid(sf->id);
12,248,765✔
382
}
383

384
bool _sirfile_update(sirfile* sf, const sir_update_config_data* data) {
36,171✔
385
    if (!_sirfile_validate(sf))
36,171✔
386
        return false;
×
387

388
    if (_sir_bittest(data->fields, SIRU_LEVELS)) {
36,171✔
389
        if (sf->levels != *data->levels) {
207✔
390
            _sir_selflog("updating file (id: %"PRIx32") levels from %04"PRIx16
194✔
391
                         " to %04"PRIx16, sf->id, sf->levels, *data->levels);
392
            sf->levels = *data->levels;
205✔
393
        } else {
394
            _sir_selflog("skipped superfluous update of file (id: %"PRIx32")"
2✔
395
                         " levels: %04"PRIx16, sf->id, sf->levels);
396
        }
397

398
        return true;
207✔
399
    }
400

401
    if (_sir_bittest(data->fields, SIRU_OPTIONS)) {
35,964✔
402
        if (sf->opts != *data->opts) {
35,964✔
403
            _sir_selflog("updating file (id: %"PRIx32") options from %08"PRIx32
17,284✔
404
                         " to %08"PRIx32, sf->id, sf->opts, *data->opts);
405
            sf->opts = *data->opts;
18,294✔
406
        } else {
407
            _sir_selflog("skipped superfluous update of file (id: %"PRIx32")"
16,683✔
408
                         " options: %08"PRIx32, sf->id, sf->opts);
409
        }
410

411
        return true;
35,964✔
412
    }
413

414
    return false;
×
415
}
416

417
sirfileid _sir_fcache_add(sirfcache* sfc, const char* path, sir_levels levels,
37,149✔
418
    sir_options opts) {
419
    if (!_sir_validptr(sfc) || !_sir_validstr(path) || !_sir_validlevels(levels) ||
43,252✔
420
        !_sir_validopts(opts))
37,132✔
421
        return 0U;
17✔
422

423
    if (sfc->count >= SIR_MAXFILES)
37,132✔
424
        return _sir_seterror(_SIR_E_NOROOM);
35✔
425

426
    const sirfile* existing = _sir_fcache_find(sfc, (const void*)path, _sir_fcache_pred_path);
37,097✔
427
    if (NULL != existing) {
37,097✔
428
        _sir_selflog("error: already have file (path: '%s', id: %"PRIx32")",
166✔
429
            path, existing->id);
430
        return _sir_seterror(_SIR_E_DUPITEM);
169✔
431
    }
432

433
    sirfile* sf = _sirfile_create(path, levels, opts);
36,928✔
434
    if (_sirfile_validate(sf)) {
36,928✔
435
        _sir_selflog("adding file (path: '%s', id: %"PRIx32"); count = %zu", //-V522
36,806✔
436
            sf->path, sf->id, sfc->count + 1);
437

438
        sfc->files[sfc->count++] = sf;
36,806✔
439

440
        if (!_sir_bittest(sf->opts, SIRO_NOHDR)) //-V522
36,806✔
441
            _sirfile_writeheader(sf, SIR_FHBEGIN);
36,750✔
442

443
        return sf->id;
36,806✔
444
    }
445

446
    _sir_safefree(&sf);
122✔
447

448
    return 0U;
122✔
449
}
450

451
bool _sir_fcache_update(const sirfcache* sfc, sirfileid id, const sir_update_config_data* data) {
36,319✔
452
    if (!_sir_validptr(sfc) || !_sir_validfileid(id) || !_sir_validupdatedata(data))
36,319✔
453
        return false;
×
454

455
    sirfile* found = _sir_fcache_find(sfc, (const void*)&id, _sir_fcache_pred_id);
36,319✔
456
    return found ? _sirfile_update(found, data) : _sir_seterror(_SIR_E_NOITEM);
36,319✔
457
}
458

459
bool _sir_fcache_rem(sirfcache* sfc, sirfileid id) {
36,865✔
460
    if (!_sir_validptr(sfc) || !_sir_validfileid(id))
36,865✔
461
        return false;
×
462

463
    for (size_t n = 0; n < sfc->count; n++) {
115,104✔
464
        SIR_ASSERT(_sirfile_validate(sfc->files[n]));
108,367✔
465

466
        if (sfc->files[n]->id == id) {
114,952✔
467
            _sir_selflog("removing file (path: '%s', id: %"PRIx32"); count = %zu",
34,673✔
468
                sfc->files[n]->path, sfc->files[n]->id, sfc->count - 1);
469

470
            _sirfile_destroy(&sfc->files[n]);
36,713✔
471

472
            for (size_t i = n; i < sfc->count - 1; i++) {
58,334✔
473
                sfc->files[i] = sfc->files[i + 1];
21,621✔
474
                sfc->files[i + 1] = NULL;
21,621✔
475
            }
476

477
            sfc->count--;
36,713✔
478
            return true;
36,713✔
479
        }
480
    }
481

482
    return _sir_seterror(_SIR_E_NOITEM);
152✔
483
}
484

485
bool _sir_fcache_pred_path(const void* match, const sirfile* iter) {
100,233✔
486
    const char* path = (const char*)match;
84,455✔
487
    bool equal       = false;
84,455✔
488

489
    _sir_selflog("comparing '%s' == '%s'", path, iter->path);
94,952✔
490

491
#if !defined(__WIN__)
492
    /* if we're able to stat both files then we can use that information for the
493
     * comparison. otherwise, fall back on comparing the canonical path strings
494
     * returned by realpath. */
495
    char resolved1[SIR_MAXPATH] = {0};
100,233✔
496
    char resolved2[SIR_MAXPATH] = {0};
100,233✔
497
    struct stat st1             = {0};
100,233✔
498
    struct stat st2             = {0};
100,233✔
499

500
    const char* real1 = realpath(path, resolved1);
84,455✔
501
    const char* real2 = realpath(iter->path, resolved2);
100,233✔
502

503
    SIR_UNUSED(real1);
504
    SIR_UNUSED(real2);
505

506
    if ((!stat(resolved1, &st1) && !stat(resolved2, &st2)) ||
108,536✔
507
        (!stat(path, &st1) && !stat(iter->path, &st2))) {
8,303✔
508
        equal = st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino;
91,930✔
509
        _sir_selflog("returning %d for '%s' == '%s'", equal, path, iter->path);
91,930✔
510
    } else {
511
        _sir_selflog("falling back to canonical path string compare");
8,174✔
512
        equal = 0 == strncmp(resolved1, resolved2, SIR_MAXPATH);
8,303✔
513
        _sir_selflog("returning %d for '%s' == '%s'", equal, resolved1, resolved2);
8,174✔
514
    }
515

516
    return equal;
100,233✔
517
#else /* __WIN__ */
518
    /* open both files (only if they already exist) and compare their
519
     * filesystem info. failing that, fall back on conversion to canonical path
520
     * and string comparison. */
521
    DWORD sh_flags   = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
522
    DWORD open_type  = OPEN_EXISTING;
523
    DWORD attr_flags = FILE_ATTRIBUTE_NORMAL;
524
    BY_HANDLE_FILE_INFORMATION fi1 = {0};
525
    BY_HANDLE_FILE_INFORMATION fi2 = {0};
526

527
    HANDLE h1 = CreateFileA(path, 0, sh_flags, NULL, open_type, attr_flags, NULL);
528
    HANDLE h2 = CreateFileA(iter->path,0, sh_flags, NULL, open_type, attr_flags, NULL);
529

530
    if (INVALID_HANDLE_VALUE != h1 && INVALID_HANDLE_VALUE != h2 &&
531
        GetFileInformationByHandle(h1, &fi1) && GetFileInformationByHandle(h2, &fi2)) {
532
        equal = fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber &&
533
                fi1.nFileIndexLow        == fi2.nFileIndexLow        &&
534
                fi1.nFileIndexHigh       == fi2.nFileIndexHigh;
535
        _sir_selflog("returning %d for '%s' == '%s'", equal, path, iter->path);
536
    } else {
537
        _sir_selflog("falling back to canonical path string compare");
538
        char resolved1[SIR_MAXPATH] = {0}, resolved2[SIR_MAXPATH] = {0};
539
        if (GetFullPathNameA(path, SIR_MAXPATH, resolved1, NULL) &&
540
            GetFullPathNameA(iter->path, SIR_MAXPATH, resolved2, NULL))
541
            equal = 0 == StrCmpIA(resolved1, resolved2);
542
        _sir_selflog("returning %d for '%s' == '%s'", equal, resolved1, resolved2);
543
    }
544

545
    if (INVALID_HANDLE_VALUE != h1)
546
        CloseHandle(h1);
547

548
    if (INVALID_HANDLE_VALUE != h2)
549
        CloseHandle(h2);
550

551
    return equal;
552
#endif
553
}
554

555
bool _sir_fcache_pred_id(const void* match, const sirfile* iter) {
108,856✔
556
    const sirfileid* id = (const sirfileid*)match;
91,079✔
557
    return iter->id == *id;
108,856✔
558
}
559

560
sirfile* _sir_fcache_find(const sirfcache* sfc, const void* match, sir_fcache_pred pred) {
73,416✔
561
    if (!_sir_validptr(sfc) || !_sir_validptr(match) || !_sir_validfnptr(pred))
73,416✔
562
        return NULL;
×
563

564
    for (size_t n = 0; n < sfc->count; n++) {
246,165✔
565
        if (pred(match, sfc->files[n]))
209,089✔
566
            return sfc->files[n];
36,340✔
567
    }
568

569
    return NULL;
30,985✔
570
}
571

572
bool _sir_fcache_destroy(sirfcache* sfc) {
654✔
573
    if (!_sir_validptr(sfc))
654✔
574
        return false;
×
575

576
    while (sfc->count > 0) {
743✔
577
        size_t idx = sfc->count - 1;
89✔
578
        SIR_ASSERT(_sirfile_validate(sfc->files[idx]));
87✔
579
        _sirfile_destroy(&sfc->files[idx]);
89✔
580
        sfc->files[idx] = NULL;
89✔
581
        sfc->count--;
89✔
582
    }
583

584
    memset(sfc, 0, sizeof(sirfcache));
549✔
585
    return true;
654✔
586
}
587

588
bool _sir_fcache_dispatch(const sirfcache* sfc, sir_level level, sirbuf* buf,
2,111,510✔
589
    size_t* dispatched, size_t* wanted) {
590
    if (!_sir_validptr(sfc) || !_sir_validlevel(level) || !_sir_validptr(buf) ||
2,131,022✔
591
        !_sir_validptr(dispatched) || !_sir_validptr(wanted))
2,131,022✔
592
        return false;
×
593

594
    const char* write    = NULL;
2,091,998✔
595
    sir_options lastopts = 0U;
2,091,998✔
596

597
    *dispatched = 0;
2,111,510✔
598
    *wanted     = 0;
2,111,510✔
599

600
    for (size_t n = 0; n < sfc->count; n++) {
3,370,702✔
601
        SIR_ASSERT(_sirfile_validate(sfc->files[n]));
1,244,546✔
602

603
        if (!_sir_bittest(sfc->files[n]->levels, level)) {
1,259,192✔
604
            _sir_selflog("level %04"PRIx16" not set in level mask (%04"PRIx16
2,310✔
605
                         ") for file (path: '%s', id: %"PRIx32"); skipping",
606
                         level, sfc->files[n]->levels, sfc->files[n]->path,
607
                         sfc->files[n]->id);
608
            continue;
2,439✔
609
        }
610

611
        (*wanted)++;
1,256,753✔
612

613
        if (!write || sfc->files[n]->opts != lastopts) {
1,256,753✔
614
            write = _sir_format(false, sfc->files[n]->opts, buf);
1,154,101✔
615
            SIR_ASSERT(write);
1,145,107✔
616
            lastopts = sfc->files[n]->opts;
1,154,101✔
617
        }
618

619
        if (write && _sirfile_write(sfc->files[n], write)) {
1,256,753✔
620
            (*dispatched)++;
1,256,753✔
621
        } else {
622
            _sir_selflog("error: write to file (path: '%s', id: %"PRIx32") failed!",
×
623
                sfc->files[n]->path, sfc->files[n]->id);
624
        }
625
    }
626

627
    return (*dispatched == *wanted);
2,111,510✔
628
}
629

630
#if !defined(__WIN__)
631
void _sir_fflush(FILE* f) {
186✔
632
    if (_sir_validptr(f) && 0 != fflush(f))
186✔
633
        (void)_sir_handleerr(errno);
×
634
}
186✔
635
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc