| 1 |
/* provide a replacement openat function |
| 2 |
Copyright (C) 2004-2006, 2008-2024 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* written by Jim Meyering */ |
| 18 |
|
| 19 |
#ifndef _GL_HEADER_OPENAT |
| 20 |
#define _GL_HEADER_OPENAT |
| 21 |
|
| 22 |
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _Noreturn, |
| 23 |
_GL_ATTRIBUTE_DEPRECATED, HAVE_OPENAT. */ |
| 24 |
#if !_GL_CONFIG_H_INCLUDED |
| 25 |
#error "Please include config.h first." |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <fcntl.h> |
| 29 |
|
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
_GL_INLINE_HEADER_BEGIN |
| 35 |
|
| 36 |
#ifdef __cplusplus |
| 37 |
extern "C" { |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
#if !HAVE_OPENAT |
| 42 |
|
| 43 |
int openat_permissive (int fd, char const *file, int flags, mode_t mode, |
| 44 |
int *cwd_errno); |
| 45 |
bool openat_needs_fchdir (void); |
| 46 |
|
| 47 |
#else |
| 48 |
|
| 49 |
# define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \ |
| 50 |
openat (Fd, File, Flags, Mode) |
| 51 |
# define openat_needs_fchdir() false |
| 52 |
|
| 53 |
#endif |
| 54 |
|
| 55 |
_Noreturn void openat_restore_fail (int); |
| 56 |
_Noreturn void openat_save_fail (int); |
| 57 |
|
| 58 |
/* Using these function names makes application code |
| 59 |
slightly more readable than it would be with |
| 60 |
fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW). */ |
| 61 |
|
| 62 |
#if GNULIB_CHOWNAT |
| 63 |
|
| 64 |
# ifndef CHOWNAT_INLINE |
| 65 |
# define CHOWNAT_INLINE _GL_INLINE |
| 66 |
# endif |
| 67 |
|
| 68 |
CHOWNAT_INLINE int |
| 69 |
chownat (int fd, char const *file, uid_t owner, gid_t group) |
| 70 |
{ |
| 71 |
return fchownat (fd, file, owner, group, 0); |
| 72 |
} |
| 73 |
|
| 74 |
CHOWNAT_INLINE int |
| 75 |
lchownat (int fd, char const *file, uid_t owner, gid_t group) |
| 76 |
{ |
| 77 |
return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW); |
| 78 |
} |
| 79 |
|
| 80 |
#endif |
| 81 |
|
| 82 |
#if GNULIB_CHMODAT |
| 83 |
|
| 84 |
# ifndef CHMODAT_INLINE |
| 85 |
# define CHMODAT_INLINE _GL_INLINE |
| 86 |
# endif |
| 87 |
|
| 88 |
CHMODAT_INLINE int |
| 89 |
chmodat (int fd, char const *file, mode_t mode) |
| 90 |
{ |
| 91 |
return fchmodat (fd, file, mode, 0); |
| 92 |
} |
| 93 |
|
| 94 |
CHMODAT_INLINE int |
| 95 |
lchmodat (int fd, char const *file, mode_t mode) |
| 96 |
{ |
| 97 |
return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW); |
| 98 |
} |
| 99 |
|
| 100 |
#endif |
| 101 |
|
| 102 |
#if GNULIB_STATAT |
| 103 |
|
| 104 |
# ifndef STATAT_INLINE |
| 105 |
# define STATAT_INLINE _GL_INLINE |
| 106 |
# endif |
| 107 |
|
| 108 |
_GL_ATTRIBUTE_DEPRECATED |
| 109 |
STATAT_INLINE int |
| 110 |
statat (int fd, char const *name, struct stat *st) |
| 111 |
{ |
| 112 |
return fstatat (fd, name, st, 0); |
| 113 |
} |
| 114 |
|
| 115 |
_GL_ATTRIBUTE_DEPRECATED |
| 116 |
STATAT_INLINE int |
| 117 |
lstatat (int fd, char const *name, struct stat *st) |
| 118 |
{ |
| 119 |
return fstatat (fd, name, st, AT_SYMLINK_NOFOLLOW); |
| 120 |
} |
| 121 |
|
| 122 |
#endif |
| 123 |
|
| 124 |
/* For now, there are no wrappers named laccessat or leuidaccessat, |
| 125 |
since gnulib doesn't support faccessat(,AT_SYMLINK_NOFOLLOW) and |
| 126 |
since access rights on symlinks are of limited utility. Likewise, |
| 127 |
wrappers are not provided for accessat or euidaccessat, so as to |
| 128 |
avoid dragging in -lgen on some platforms. */ |
| 129 |
|
| 130 |
|
| 131 |
#ifdef __cplusplus |
| 132 |
} |
| 133 |
#endif |
| 134 |
|
| 135 |
_GL_INLINE_HEADER_END |
| 136 |
|
| 137 |
#endif /* _GL_HEADER_OPENAT */ |