GCC Code Coverage Report


Directory: ./
File: tests/unit/tmpdir.h
Date: 2026-11-30 15:03:27
Exec Total Coverage
Lines: 20 25 80.0%
Functions: 4 4 100.0%
Branches: 9 16 56.2%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2026 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #ifndef TMPDIR_H
21 #define TMPDIR_H
22
23 #include <ftw.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <limits.h>
28
29 static char test_tmpdir[PATH_MAX];
30
31 13 static int tmpdir_remove_cb(const char *fpath, const struct stat *sb,
32 int typeflag, struct FTW *ftwbuf) {
33 (void)sb;
34 (void)typeflag;
35 (void)ftwbuf;
36 13 return remove(fpath);
37 }
38
39 2 static void tmpdir_cleanup(void) {
40
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (test_tmpdir[0] != '\0') {
41 2 nftw(test_tmpdir, tmpdir_remove_cb, 16, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
42 2 test_tmpdir[0] = '\0';
43 }
44 2 }
45
46 /* Creates the directory on first call, returns its path or fail.
47 * Cleanup is registered via atexit. */
48 15 static const char *tmpdir_get(void) {
49
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (test_tmpdir[0] == '\0') {
50 2 const char *base = getenv("TMPDIR");
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 int n = snprintf(test_tmpdir, sizeof(test_tmpdir),
52 "%s/dlb_test.XXXXXX", base ? base : "/tmp");
53
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (n < 0
54
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 || (size_t)n >= sizeof(test_tmpdir)
55
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 || !mkdtemp(test_tmpdir)) {
56 ✗ perror("tmpdir_get");
57 ✗ test_tmpdir[0] = '\0';
58 ✗ abort();
59 }
60 2 atexit(tmpdir_cleanup);
61 }
62 15 return test_tmpdir;
63 }
64
65 #if defined(__GNUC__) && !defined(__clang__)
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wformat-truncation"
68 #endif
69 /* Convenience: build a path inside the tmpdir. */
70 2 static int tmpdir_path(char *out, size_t outsz, const char *name) {
71 2 int n = snprintf(out, outsz, "%s/%s", tmpdir_get(), name);
72
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (n < 0 || (size_t)n >= outsz) {
73 ✗ fprintf(stderr, "tmpdir_path: path truncated (name=%s)\n", name);
74 ✗ abort();
75 }
76 2 return n;
77 }
78 #if defined(__GNUC__) && !defined(__clang__)
79 #pragma GCC diagnostic pop
80 #endif
81
82 #endif /* TMPDIR_H */
83