]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
affb60b1 LP |
2 | #pragma once |
3 | ||
affb60b1 LP |
4 | #include <stdbool.h> |
5 | #include <sys/stat.h> | |
6 | #include <sys/types.h> | |
7 | ||
8 | #include "macro.h" | |
9 | ||
10 | static inline void umaskp(mode_t *u) { | |
650c7a2e | 11 | umask(*u & 0777); |
affb60b1 LP |
12 | } |
13 | ||
14 | #define _cleanup_umask_ _cleanup_(umaskp) | |
15 | ||
650c7a2e LP |
16 | /* We make use of the fact here that the umask() concept is using only the lower 9 bits of mode_t, although |
17 | * mode_t has space for the file type in the bits further up. We simply OR in the file type mask S_IFMT to | |
18 | * distinguish the first and the second iteration of the RUN_WITH_UMASK() loop, so that we can run the first | |
19 | * one, and exit on the second. */ | |
affb60b1 | 20 | |
650c7a2e | 21 | assert_cc((S_IFMT & 0777) == 0); |
affb60b1 LP |
22 | |
23 | #define RUN_WITH_UMASK(mask) \ | |
650c7a2e LP |
24 | for (_cleanup_umask_ mode_t _saved_umask_ = umask(mask) | S_IFMT; \ |
25 | FLAGS_SET(_saved_umask_, S_IFMT); \ | |
26 | _saved_umask_ &= 0777) |