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

aremmell / libsir / 501

07 Sep 2023 03:38PM UTC coverage: 94.721% (-0.06%) from 94.782%
501

push

gitlab-ci

web-flow
fix logic error in _sir_andeql, rename to _sir_eqland (#267)

307 of 307 new or added lines in 4 files covered. (100.0%)

3104 of 3277 relevant lines covered (94.72%)

616870.35 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) {
36,976✔
32
    (void)_sir_seterror(_SIR_E_NOERROR);
36,976✔
33

34
    if (!_sir_sanity())
36,975✔
35
        return 0U;
×
36

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

39
    _sir_defaultlevels(&levels, sir_file_def_lvls);
36,976✔
40
    _sir_defaultopts(&opts, sir_file_def_opts);
36,976✔
41

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

45
    return retval;
36,976✔
46
}
47

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

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

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

58
    return retval;
36,197✔
59
}
60

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

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

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

71
    return retval;
36,725✔
72
}
73

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

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

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

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

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

99
    return sf;
36,741✔
100
}
101

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

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

111
    _sirfile_close(sf);
36,777✔
112

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

116
    return true;
36,777✔
117
}
118

119
void _sirfile_close(sirfile* sf) {
73,589✔
120
    if (!_sir_validptrnofail(sf) || !_sir_validptrnofail(sf->f))
73,589✔
121
        return;
36,812✔
122

123
    _sir_safefclose(&sf->f);
36,777✔
124
}
125

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

130
    if (_sirfile_needsroll(sf)) {
1,321,919✔
131
        bool rolled   = false;
185✔
132
        char* newpath = NULL;
188✔
133

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

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

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

145
        _sir_safefree(&newpath);
188✔
146
        if (!rolled) /* write anyway; don't want to lose data. */
187✔
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,321,919✔
152
    size_t write    = fwrite(output, sizeof(char), writeLen, sf->f);
1,321,919✔
153

154
    SIR_ASSERT(write == writeLen);
1,304,233✔
155

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

161
    return write == writeLen;
1,321,919✔
162
}
163

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

168
    time_t now = -1;
36,721✔
169
    time(&now);
36,721✔
170

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

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

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

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

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

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

195
    return st.st_size + BUFSIZ >= SIR_FROLLSIZE ||
2,643,673✔
196
        SIR_FROLLSIZE - (st.st_size + BUFSIZ) <= BUFSIZ;
1,321,751✔
197
}
198

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

203
    bool retval = false;
185✔
204
    char* name = NULL;
188✔
205
    char* ext  = NULL;
188✔
206

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

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

213
        time(&now);
188✔
214
        SIR_ASSERT(-1 != now);
187✔
215

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

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

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

230
                    do {
231
                        (void)snprintf(*newpath, SIR_MAXPATH, SIR_FNAMEFORMAT, name,
108✔
232
                            timestamp, (sequence > 0U ? seqbuf : ""),
233
                            _sir_validstrnofail(ext) ? ext : "");
105✔
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)) {
105✔
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) {
105✔
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
69✔
249
                            sequence++;
69✔
250
                        } else {
251
                            _sir_selflog("found good path: '%s'", *newpath);
35✔
252
                            resolved = true;
33✔
253
                            break;
33✔
254
                        }
255

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

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

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

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

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

274
    return retval;
188✔
275

276
}
277

278
bool _sirfile_archive(sirfile* sf, const char* newpath) {
36✔
279
    if (!_sirfile_validate(sf) || !_sir_validstr(newpath))
36✔
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))
36✔
288
        return _sir_handleerr(errno);
×
289

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

295
    return false;
×
296
}
297

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

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

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

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

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

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

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

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

341
bool _sirfile_validate(const sirfile* sf) {
4,150,545✔
342
    return _sir_validptrnofail(sf) && _sir_validptrnofail(sf->f) &&
12,451,361✔
343
           _sir_validstrnofail(sf->path) && _sir_validfileid(sf->id);
12,451,361✔
344
}
345

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

350
    if (_sir_bittest(data->fields, SIRU_LEVELS)) {
36,200✔
351
        if (sf->levels != *data->levels) {
200✔
352
            _sir_selflog("updating file (id: %"PRIx32") levels from %04"PRIx16
188✔
353
                         " to %04"PRIx16, sf->id, sf->levels, *data->levels);
354
            sf->levels = *data->levels;
199✔
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;
200✔
361
    }
362

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

373
        return true;
36,000✔
374
    }
375

376
    return false;
×
377
}
378

379
sirfileid _sir_fcache_add(sirfcache* sfc, const char* path, sir_levels levels,
36,976✔
380
    sir_options opts) {
381
    if (!_sir_validptr(sfc) || !_sir_validstr(path) || !_sir_validlevels(levels) ||
43,150✔
382
        !_sir_validopts(opts))
36,954✔
383
        return 0U;
22✔
384

385
    if (sfc->count >= SIR_MAXFILES)
36,954✔
386
        return _sir_seterror(_SIR_E_NOROOM);
19✔
387

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

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

400
        sfc->files[sfc->count++] = sf;
36,741✔
401

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

405
        return sf->id;
36,741✔
406
    }
407

408
    _sir_safefree(&sf);
137✔
409

410
    return 0U;
137✔
411
}
412

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

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

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

425
    for (size_t n = 0; n < sfc->count; n++) {
91,050✔
426
        SIR_ASSERT(_sirfile_validate(sfc->files[n]));
85,911✔
427

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

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

434
            for (size_t i = n; i < sfc->count - 1; i++) {
90,203✔
435
                sfc->files[i] = sfc->files[i + 1];
53,500✔
436
                sfc->files[i + 1] = NULL;
53,500✔
437
            }
438

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

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

447
bool _sir_fcache_pred_path(const void* match, const sirfile* iter) {
107,920✔
448
    const char* path = (const char*)match;
89,968✔
449
    bool equal       = false;
89,968✔
450

451
    _sir_selflog("comparing '%s' == '%s'", path, iter->path);
102,024✔
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};
107,920✔
458
    char resolved2[SIR_MAXPATH] = {0};
107,920✔
459
    struct stat st1             = {0};
107,920✔
460
    struct stat st2             = {0};
107,920✔
461

462
    const char* real1 = realpath(path, resolved1);
89,968✔
463
    const char* real2 = realpath(iter->path, resolved2);
107,920✔
464

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

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

478
    return equal;
107,920✔
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) {
80,460✔
518
    const sirfileid* id = (const sirfileid*)match;
66,758✔
519
    return iter->id == *id;
80,460✔
520
}
521

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

526
    for (size_t n = 0; n < sfc->count; n++) {
225,258✔
527
        if (pred(match, sfc->files[n]))
188,380✔
528
            return sfc->files[n];
36,257✔
529
    }
530

531
    return NULL;
30,716✔
532
}
533

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

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

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

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

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

559
    *dispatched = 0;
2,125,937✔
560
    *wanted     = 0;
2,125,937✔
561

562
    for (size_t n = 0; n < sfc->count; n++) {
3,415,500✔
563
        SIR_ASSERT(_sirfile_validate(sfc->files[n]));
1,273,808✔
564

565
        if (!_sir_bittest(sfc->files[n]->levels, level)) {
1,289,563✔
566
            _sir_selflog("level %04"PRIx16" not set in level mask (%04"PRIx16
2,173✔
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,302✔
571
        }
572

573
        (*wanted)++;
1,287,261✔
574

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

581
        if (write && _sirfile_write(sfc->files[n], write)) {
1,287,261✔
582
            (*dispatched)++;
1,287,261✔
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,125,937✔
590
}
591

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