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

aremmell / libsir / 473

06 Sep 2023 02:24AM UTC coverage: 94.751% (+0.1%) from 94.608%
473

Pull #258

gitlab-ci

johnsonjh
Merge branch 'set-thread-names' of github.com:/aremmell/libsir into set-thread-names
Pull Request #258: Bug fix, new tests, portable set thread name functionality

473 of 473 new or added lines in 10 files covered. (100.0%)

3105 of 3277 relevant lines covered (94.75%)

614386.54 hits per line

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

90.88
/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,390✔
32
    (void)_sir_seterror(_SIR_E_NOERROR);
37,390✔
33

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

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

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

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

45
    return retval;
37,388✔
46
}
47

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

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

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

58
    return retval;
36,053✔
59
}
60

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

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

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

71
    return retval;
37,104✔
72
}
73

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

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

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

91
    sf->levels = levels;
37,120✔
92
    sf->opts   = opts;
37,120✔
93

94
    if (!_sirfile_open(sf) || !_sirfile_validate(sf)) {
37,120✔
95
        _sirfile_destroy(&sf);
65✔
96
        return NULL;
65✔
97
    }
98

99
    return sf;
37,055✔
100
}
101

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

106
    FILE* f  = NULL;
37,154✔
107
    bool open = _sir_openfile(&f, sf->path, SIR_FOPENMODE, SIR_PATH_REL_TO_CWD);
37,154✔
108
    if (!open || !f)
37,154✔
109
        return false;
59✔
110

111
    _sirfile_close(sf);
37,089✔
112

113
    sf->f  = f;
37,089✔
114
    sf->id = FNV32_1a((const uint8_t*)sf->path, strnlen(sf->path, SIR_MAXPATH));
37,089✔
115

116
    return true;
37,089✔
117
}
118

119
void _sirfile_close(sirfile* sf) {
74,205✔
120
    if (!_sir_validptrnofail(sf) || !_sir_validptrnofail(sf->f))
74,205✔
121
        return;
37,120✔
122

123
    _sir_safefclose(&sf->f);
37,085✔
124
}
125

126
bool _sirfile_write(sirfile* sf, const char* output) {
1,293,146✔
127
    if (!_sirfile_validate(sf) || !_sir_validstr(output))
1,293,146✔
128
        return false;
×
129

130
    if (_sirfile_needsroll(sf)) {
1,293,146✔
131
        bool rolled   = false;
183✔
132
        char* newpath = NULL;
186✔
133

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

137
        _sir_fflush(sf->f);
186✔
138

139
        if (_sirfile_roll(sf, &newpath)) {
186✔
140
            char header[SIR_MAXFHEADER] = {0};
34✔
141
            (void)snprintf(header, SIR_MAXFHEADER, SIR_FHROLLED, newpath);
34✔
142
            rolled = _sirfile_writeheader(sf, header);
34✔
143
        }
144

145
        _sir_safefree(&newpath);
186✔
146
        if (!rolled) /* write anyway; don't want to lose data. */
185✔
147
            _sir_selflog("error: failed to roll file (path: '%s', id: %" PRIx32")!",
152✔
148
                sf->path, sf->id);
149
    }
150

151
    size_t writeLen = strnlen(output, SIR_MAXOUTPUT);
1,293,146✔
152
    size_t write    = fwrite(output, sizeof(char), writeLen, sf->f);
1,293,146✔
153

154
    SIR_ASSERT(write == writeLen);
1,276,980✔
155

156
    if (write < writeLen) {
1,293,146✔
157
        (void)_sir_handleerr(errno);
×
158
        clearerr(sf->f);
×
159
    }
160

161
    return write == writeLen;
1,293,146✔
162
}
163

164
bool _sirfile_writeheader(sirfile* sf, const char* msg) {
37,033✔
165
    if (!_sirfile_validate(sf) || !_sir_validstr(msg))
37,033✔
166
        return false;
×
167

168
    time_t now = -1;
37,033✔
169
    time(&now);
37,033✔
170

171
    char timestamp[SIR_MAXTIME] = {0};
37,033✔
172
    bool fmttime = _sir_formattime(now, timestamp, SIR_FHTIMEFORMAT);
37,033✔
173
    if (!fmttime)
37,033✔
174
        return false;
2,082✔
175

176
    char header[SIR_MAXFHEADER] = {0};
34,951✔
177
    (void)snprintf(header, SIR_MAXFHEADER, SIR_FHFORMAT, msg, timestamp);
28,793✔
178

179
    return _sirfile_write(sf, header);
34,951✔
180
}
181

182
bool _sirfile_needsroll(sirfile* sf) {
1,293,146✔
183
    if (!_sirfile_validate(sf))
1,293,146✔
184
        return false;
×
185

186
    struct stat st = {0};
1,293,146✔
187
    int getstat    = fstat(fileno(sf->f), &st);
1,293,146✔
188

189
    if (0 != getstat) { /* if fstat fails, try stat on the path. */
1,293,146✔
190
        getstat = stat(sf->path, &st);
17,032✔
191
        if (0 != getstat)
17,032✔
192
            return _sir_handleerr(errno);
×
193
    }
194

195
    return st.st_size + BUFSIZ >= SIR_FROLLSIZE ||
2,586,129✔
196
        SIR_FROLLSIZE - (st.st_size + BUFSIZ) <= BUFSIZ;
1,292,980✔
197
}
198

199
bool _sirfile_roll(sirfile* sf, char** newpath) {
186✔
200
    if (!_sirfile_validate(sf) || !_sir_validptrptr(newpath))
186✔
201
        return false;
×
202

203
    bool retval = false;
183✔
204
    char* name = NULL;
186✔
205
    char* ext  = NULL;
186✔
206

207
    bool split = _sirfile_splitpath(sf, &name, &ext);
186✔
208
    SIR_ASSERT(split);
185✔
209

210
    if (split) {
184✔
211
        time_t now = -1;
186✔
212

213
        time(&now);
186✔
214
        SIR_ASSERT(-1 != now);
185✔
215

216
        if (-1 != now) {
186✔
217
            char timestamp[SIR_MAXTIME] = {0};
110✔
218
            bool fmttime = _sir_formattime(now, timestamp, SIR_FNAMETIMEFORMAT);
110✔
219
            SIR_ASSERT(fmttime);
109✔
220

221
            if (fmttime) {
108✔
222
                *newpath = (char*)calloc(SIR_MAXPATH, sizeof(char));
34✔
223

224
                if (_sir_validptr(*newpath)) {
34✔
225
                    char seqbuf[7] = {0};
34✔
226
                    bool exists = false;
34✔
227
                    bool resolved = false;
31✔
228
                    uint16_t sequence = 0U;
31✔
229

230
                    do {
231
                        (void)snprintf(*newpath, SIR_MAXPATH, SIR_FNAMEFORMAT, name,
80✔
232
                            timestamp, (sequence > 0U ? seqbuf : ""),
233
                            _sir_validstrnofail(ext) ? ext : "");
77✔
234

235
                        /* if less than one second has elapsed since the last roll
236
                         * operation, then we'll overwrite the last rolled log file,
237
                         * and that = data loss. make sure the target path does not
238
                         * already exist. */
239
                        if (!_sir_pathexists(*newpath, &exists, SIR_PATH_REL_TO_CWD)) {
77✔
240
                            /* failed to determine if the file already exists; it is better
241
                             * to continue logging to the same file than to possibly overwrite
242
                             * another (if it failed this time, it will again, so there's no
243
                             * way to definitively choose a good new path). */
244
                            break;
×
245
                        } else if (exists) {
77✔
246
                            /* the file already exists; add a number to the file name
247
                             * until one that does not exist is found. */
248
                            _sir_selflog("path: '%s' already exists; incrementing sequence", *newpath); //-V576
43✔
249
                            sequence++;
43✔
250
                        } else {
251
                            _sir_selflog("found good path: '%s'", *newpath);
33✔
252
                            resolved = true;
31✔
253
                            break;
31✔
254
                        }
255

256
                        if (sequence > 0)
43✔
257
                            (void)snprintf(seqbuf, 7, SIR_FNAMESEQFORMAT, sequence);
43✔
258

259
                    } while (sequence <= 999U);
43✔
260

261
                    if (!resolved)
31✔
262
                        _sir_selflog("error: unable to determine suitable path for '%s';"
×
263
                                        " not rolling!", sf->path);
264

265
                    retval = resolved && _sirfile_archive(sf, *newpath);
34✔
266
                }
267
            }
268
        }
269
    }
270

271
    _sir_safefree(&name);
186✔
272
    _sir_safefree(&ext);
186✔
273

274
    return retval;
186✔
275

276
}
277

278
bool _sirfile_archive(sirfile* sf, const char* newpath) {
34✔
279
    if (!_sirfile_validate(sf) || !_sir_validstr(newpath))
34✔
280
        return false;
×
281

282
#if defined(__WIN__)
283
    /* apparently, need to close the old file first on windows. */
284
    _sirfile_close(sf);
285
#endif
286

287
    if (0 != rename(sf->path, newpath))
34✔
288
        return _sir_handleerr(errno);
×
289

290
    if (_sirfile_open(sf)) {
34✔
291
        _sir_selflog("archived '%s' " SIR_R_ARROW " '%s'", sf->path, newpath);
33✔
292
        return true;
33✔
293
    }
294

295
    return false;
×
296
}
297

298
bool _sirfile_splitpath(const sirfile* sf, char** name, char** ext) {
186✔
299
    if (_sir_validptrptr(name))
186✔
300
        *name = NULL;
186✔
301
    if (_sir_validptrptr(ext))
186✔
302
        *ext = NULL;
186✔
303

304
    if (!_sirfile_validate(sf) || !_sir_validptrptr(name) || !_sir_validptrptr(ext))
186✔
305
        return false;
×
306

307
    char* tmp = strndup(sf->path, strnlen(sf->path, SIR_MAXPATH));
186✔
308
    if (!tmp)
186✔
309
        return _sir_handleerr(errno);
×
310

311
    const char* lastfullstop = strrchr(tmp, '.');
186✔
312
    if (lastfullstop) {
186✔
313
        uintptr_t namesize = lastfullstop - tmp;
166✔
314
        SIR_ASSERT(namesize < SIR_MAXPATH);
165✔
315

316
        tmp[namesize] = '\0';
166✔
317
        *name = (char*)calloc(namesize + 1, sizeof(char));
166✔
318
        if (!*name) {
166✔
319
            _sir_safefree(&tmp);
×
320
            return _sir_handleerr(errno);
×
321
        }
322

323
        _sir_strncpy(*name, namesize + 1, tmp, namesize);
166✔
324
        *ext = strndup(sf->path + namesize, strnlen(sf->path + namesize, SIR_MAXPATH));
166✔
325
    } else {
326
        *name = strndup(sf->path, strnlen(sf->path, SIR_MAXPATH));
20✔
327
    }
328

329
    _sir_safefree(&tmp);
186✔
330
    return _sir_validstr(*name) && (!lastfullstop || _sir_validstr(*ext));
189✔
331
}
332

333
void _sirfile_destroy(sirfile** sf) {
37,116✔
334
    if (sf && *sf) {
37,116✔
335
        _sirfile_close(*sf);
37,116✔
336
        _sir_safefree(&(*sf)->path);
37,116✔
337
        _sir_safefree(sf);
37,116✔
338
    }
339
}
37,116✔
340

341
bool _sirfile_validate(const sirfile* sf) {
4,091,420✔
342
    return _sir_validptrnofail(sf) && _sir_validptrnofail(sf->f) &&
12,274,024✔
343
           _sir_validstrnofail(sf->path) && _sir_validfileid(sf->id);
12,274,024✔
344
}
345

346
bool _sirfile_update(sirfile* sf, const sir_update_config_data* data) {
35,926✔
347
    if (!_sirfile_validate(sf))
35,926✔
348
        return false;
×
349

350
    if (_sir_bittest(data->fields, SIRU_LEVELS)) {
35,926✔
351
        if (sf->levels != *data->levels) {
207✔
352
            _sir_selflog("updating file (id: %"PRIx32") levels from %04"PRIx16
195✔
353
                         " to %04"PRIx16, sf->id, sf->levels, *data->levels);
354
            sf->levels = *data->levels;
206✔
355
        } else {
356
            _sir_selflog("skipped superfluous update of file (id: %"PRIx32")"
1✔
357
                         " levels: %04"PRIx16, sf->id, sf->levels);
358
        }
359

360
        return true;
207✔
361
    }
362

363
    if (_sir_bittest(data->fields, SIRU_OPTIONS)) {
35,719✔
364
        if (sf->opts != *data->opts) {
35,719✔
365
            _sir_selflog("updating file (id: %"PRIx32") options from %08"PRIx32
17,148✔
366
                         " to %08"PRIx32, sf->id, sf->opts, *data->opts);
367
            sf->opts = *data->opts;
18,142✔
368
        } else {
369
            _sir_selflog("skipped superfluous update of file (id: %"PRIx32")"
16,595✔
370
                         " options: %08"PRIx32, sf->id, sf->opts);
371
        }
372

373
        return true;
35,719✔
374
    }
375

376
    return false;
×
377
}
378

379
sirfileid _sir_fcache_add(sirfcache* sfc, const char* path, sir_levels levels,
37,388✔
380
    sir_options opts) {
381
    if (!_sir_validptr(sfc) || !_sir_validstr(path) || !_sir_validlevels(levels) ||
43,570✔
382
        !_sir_validopts(opts))
37,371✔
383
        return 0U;
17✔
384

385
    if (sfc->count >= SIR_MAXFILES)
37,371✔
386
        return _sir_seterror(_SIR_E_NOROOM);
33✔
387

388
    const sirfile* existing = _sir_fcache_find(sfc, (const void*)path, _sir_fcache_pred_path);
37,338✔
389
    if (NULL != existing) {
37,338✔
390
        _sir_selflog("error: already have file (path: '%s', id: %"PRIx32")",
162✔
391
            path, existing->id);
392
        return _sir_seterror(_SIR_E_DUPITEM);
165✔
393
    }
394

395
    sirfile* sf = _sirfile_create(path, levels, opts);
37,173✔
396
    if (_sirfile_validate(sf)) {
37,173✔
397
        _sir_selflog("adding file (path: '%s', id: %"PRIx32"); count = %zu", //-V522
37,055✔
398
            sf->path, sf->id, sfc->count + 1);
399

400
        sfc->files[sfc->count++] = sf;
37,055✔
401

402
        if (!_sir_bittest(sf->opts, SIRO_NOHDR)) //-V522
37,055✔
403
            _sirfile_writeheader(sf, SIR_FHBEGIN);
36,999✔
404

405
        return sf->id;
37,055✔
406
    }
407

408
    _sir_safefree(&sf);
118✔
409

410
    return 0U;
118✔
411
}
412

413
bool _sir_fcache_update(const sirfcache* sfc, sirfileid id, const sir_update_config_data* data) {
36,054✔
414
    if (!_sir_validptr(sfc) || !_sir_validfileid(id) || !_sir_validupdatedata(data))
36,054✔
415
        return false;
×
416

417
    sirfile* found = _sir_fcache_find(sfc, (const void*)&id, _sir_fcache_pred_id);
36,054✔
418
    return found ? _sirfile_update(found, data) : _sir_seterror(_SIR_E_NOITEM);
36,054✔
419
}
420

421
bool _sir_fcache_rem(sirfcache* sfc, sirfileid id) {
37,104✔
422
    if (!_sir_validptr(sfc) || !_sir_validfileid(id))
37,104✔
423
        return false;
×
424

425
    for (size_t n = 0; n < sfc->count; n++) {
117,629✔
426
        SIR_ASSERT(_sirfile_validate(sfc->files[n]));
111,063✔
427

428
        if (sfc->files[n]->id == id) {
117,488✔
429
            _sir_selflog("removing file (path: '%s', id: %"PRIx32"); count = %zu",
34,902✔
430
                sfc->files[n]->path, sfc->files[n]->id, sfc->count - 1);
431

432
            _sirfile_destroy(&sfc->files[n]);
36,963✔
433

434
            for (size_t i = n; i < sfc->count - 1; i++) {
57,778✔
435
                sfc->files[i] = sfc->files[i + 1];
20,815✔
436
                sfc->files[i + 1] = NULL;
20,815✔
437
            }
438

439
            sfc->count--;
36,963✔
440
            return true;
36,963✔
441
        }
442
    }
443

444
    return _sir_seterror(_SIR_E_NOITEM);
141✔
445
}
446

447
bool _sir_fcache_pred_path(const void* match, const sirfile* iter) {
101,747✔
448
    const char* path = (const char*)match;
85,451✔
449
    bool equal       = false;
85,451✔
450

451
    _sir_selflog("comparing '%s' == '%s'", path, iter->path);
96,592✔
452

453
#if !defined(__WIN__)
454
    /* if we're able to stat both files then we can use that information for the
455
     * comparison. otherwise, fall back on comparing the canonical path strings
456
     * returned by realpath. */
457
    char resolved1[SIR_MAXPATH] = {0};
101,747✔
458
    char resolved2[SIR_MAXPATH] = {0};
101,747✔
459
    struct stat st1             = {0};
101,747✔
460
    struct stat st2             = {0};
101,747✔
461

462
    const char* real1 = realpath(path, resolved1);
85,451✔
463
    const char* real2 = realpath(iter->path, resolved2);
101,747✔
464

465
    SIR_UNUSED(real1);
466
    SIR_UNUSED(real2);
467

468
    if ((!stat(resolved1, &st1) && !stat(resolved2, &st2)) ||
109,916✔
469
        (!stat(path, &st1) && !stat(iter->path, &st2))) {
8,184✔
470
        equal = st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino;
93,578✔
471
        _sir_selflog("returning %d for '%s' == '%s'", equal, path, iter->path);
93,578✔
472
    } else {
473
        _sir_selflog("falling back to canonical path string compare");
8,040✔
474
        equal = 0 == strncmp(resolved1, resolved2, SIR_MAXPATH);
8,169✔
475
        _sir_selflog("returning %d for '%s' == '%s'", equal, resolved1, resolved2);
8,040✔
476
    }
477

478
    return equal;
101,747✔
479
#else /* __WIN__ */
480
    /* open both files (only if they already exist) and compare their
481
     * filesystem info. failing that, fall back on conversion to canonical path
482
     * and string comparison. */
483
    DWORD sh_flags   = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
484
    DWORD open_type  = OPEN_EXISTING;
485
    DWORD attr_flags = FILE_ATTRIBUTE_NORMAL;
486
    BY_HANDLE_FILE_INFORMATION fi1 = {0};
487
    BY_HANDLE_FILE_INFORMATION fi2 = {0};
488

489
    HANDLE h1 = CreateFileA(path, 0, sh_flags, NULL, open_type, attr_flags, NULL);
490
    HANDLE h2 = CreateFileA(iter->path,0, sh_flags, NULL, open_type, attr_flags, NULL);
491

492
    if (INVALID_HANDLE_VALUE != h1 && INVALID_HANDLE_VALUE != h2 &&
493
        GetFileInformationByHandle(h1, &fi1) && GetFileInformationByHandle(h2, &fi2)) {
494
        equal = fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber &&
495
                fi1.nFileIndexLow        == fi2.nFileIndexLow        &&
496
                fi1.nFileIndexHigh       == fi2.nFileIndexHigh;
497
        _sir_selflog("returning %d for '%s' == '%s'", equal, path, iter->path);
498
    } else {
499
        _sir_selflog("falling back to canonical path string compare");
500
        char resolved1[SIR_MAXPATH] = {0}, resolved2[SIR_MAXPATH] = {0};
501
        if (GetFullPathNameA(path, SIR_MAXPATH, resolved1, NULL) &&
502
            GetFullPathNameA(iter->path, SIR_MAXPATH, resolved2, NULL))
503
            equal = 0 == StrCmpIA(resolved1, resolved2);
504
        _sir_selflog("returning %d for '%s' == '%s'", equal, resolved1, resolved2);
505
    }
506

507
    if (INVALID_HANDLE_VALUE != h1)
508
        CloseHandle(h1);
509

510
    if (INVALID_HANDLE_VALUE != h2)
511
        CloseHandle(h2);
512

513
    return equal;
514
#endif
515
}
516

517
bool _sir_fcache_pred_id(const void* match, const sirfile* iter) {
109,697✔
518
    const sirfileid* id = (const sirfileid*)match;
90,911✔
519
    return iter->id == *id;
109,697✔
520
}
521

522
sirfile* _sir_fcache_find(const sirfcache* sfc, const void* match, sir_fcache_pred pred) {
73,392✔
523
    if (!_sir_validptr(sfc) || !_sir_validptr(match) || !_sir_validfnptr(pred))
73,392✔
524
        return NULL;
×
525

526
    for (size_t n = 0; n < sfc->count; n++) {
248,745✔
527
        if (pred(match, sfc->files[n]))
211,444✔
528
            return sfc->files[n];
36,091✔
529
    }
530

531
    return NULL;
31,131✔
532
}
533

534
bool _sir_fcache_destroy(sirfcache* sfc) {
654✔
535
    if (!_sir_validptr(sfc))
654✔
536
        return false;
×
537

538
    while (sfc->count > 0) {
742✔
539
        size_t idx = sfc->count - 1;
88✔
540
        SIR_ASSERT(_sirfile_validate(sfc->files[idx]));
86✔
541
        _sirfile_destroy(&sfc->files[idx]);
88✔
542
        sfc->files[idx] = NULL;
88✔
543
        sfc->count--;
88✔
544
    }
545

546
    memset(sfc, 0, sizeof(sirfcache));
549✔
547
    return true;
654✔
548
}
549

550
bool _sir_fcache_dispatch(const sirfcache* sfc, sir_level level, sirbuf* buf,
2,111,489✔
551
    size_t* dispatched, size_t* wanted) {
552
    if (!_sir_validptr(sfc) || !_sir_validlevel(level) || !_sir_validptr(buf) ||
2,131,001✔
553
        !_sir_validptr(dispatched) || !_sir_validptr(wanted))
2,131,001✔
554
        return false;
×
555

556
    const char* write    = NULL;
2,091,977✔
557
    sir_options lastopts = 0U;
2,091,977✔
558

559
    *dispatched = 0;
2,111,489✔
560
    *wanted     = 0;
2,111,489✔
561

562
    for (size_t n = 0; n < sfc->count; n++) {
3,372,109✔
563
        SIR_ASSERT(_sirfile_validate(sfc->files[n]));
1,246,386✔
564

565
        if (!_sir_bittest(sfc->files[n]->levels, level)) {
1,260,620✔
566
            _sir_selflog("level %04"PRIx16" not set in level mask (%04"PRIx16
2,296✔
567
                         ") for file (path: '%s', id: %"PRIx32"); skipping",
568
                         level, sfc->files[n]->levels, sfc->files[n]->path,
569
                         sfc->files[n]->id);
570
            continue;
2,425✔
571
        }
572

573
        (*wanted)++;
1,258,195✔
574

575
        if (!write || sfc->files[n]->opts != lastopts) {
1,258,195✔
576
            write = _sir_format(false, sfc->files[n]->opts, buf);
1,154,228✔
577
            SIR_ASSERT(write);
1,145,644✔
578
            lastopts = sfc->files[n]->opts;
1,154,228✔
579
        }
580

581
        if (write && _sirfile_write(sfc->files[n], write)) {
1,258,195✔
582
            (*dispatched)++;
1,258,195✔
583
        } else {
584
            _sir_selflog("error: write to file (path: '%s', id: %"PRIx32") failed!",
×
585
                sfc->files[n]->path, sfc->files[n]->id);
586
        }
587
    }
588

589
    return (*dispatched == *wanted);
2,111,489✔
590
}
591

592
void _sir_fflush(FILE* f) {
186✔
593
    if (_sir_validptr(f) && 0 != fflush(f))
186✔
594
        (void)_sir_handleerr(errno);
×
595
}
186✔
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