11a0314d2426d820ae0c39bfe9e58e8f7eeb6b7d
[debian/gzip] / lib / openat.h
1 /* provide a replacement openat function
2    Copyright (C) 2004-2006, 2008-2010 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 <http://www.gnu.org/licenses/>.  */
16
17 /* written by Jim Meyering */
18
19 #ifndef _GL_HEADER_OPENAT
20 #define _GL_HEADER_OPENAT
21
22 #include <fcntl.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <dirent.h>
27 #include <unistd.h>
28 #include <stdbool.h>
29
30 #ifndef __attribute__
31 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
32 #  define __attribute__(x) /* empty */
33 # endif
34 #endif
35
36 #ifndef ATTRIBUTE_NORETURN
37 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
38 #endif
39
40 #if !HAVE_OPENAT
41
42 int openat_permissive (int fd, char const *file, int flags, mode_t mode,
43                        int *cwd_errno);
44 bool openat_needs_fchdir (void);
45
46 #else
47
48 # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
49     openat (Fd, File, Flags, Mode)
50 # define openat_needs_fchdir() false
51
52 #endif
53
54 void openat_restore_fail (int) ATTRIBUTE_NORETURN;
55 void openat_save_fail (int) ATTRIBUTE_NORETURN;
56
57 /* Using these function names makes application code
58    slightly more readable than it would be with
59    fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW).  */
60 static inline int
61 chownat (int fd, char const *file, uid_t owner, gid_t group)
62 {
63   return fchownat (fd, file, owner, group, 0);
64 }
65
66 static inline int
67 lchownat (int fd, char const *file, uid_t owner, gid_t group)
68 {
69   return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
70 }
71
72 static inline int
73 chmodat (int fd, char const *file, mode_t mode)
74 {
75   return fchmodat (fd, file, mode, 0);
76 }
77
78 static inline int
79 lchmodat (int fd, char const *file, mode_t mode)
80 {
81   return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
82 }
83
84 static inline int
85 statat (int fd, char const *name, struct stat *st)
86 {
87   return fstatat (fd, name, st, 0);
88 }
89
90 static inline int
91 lstatat (int fd, char const *name, struct stat *st)
92 {
93   return fstatat (fd, name, st, AT_SYMLINK_NOFOLLOW);
94 }
95
96 #if GNULIB_FACCESSAT
97 /* For now, there are no wrappers named laccessat or leuidaccessat,
98    since gnulib doesn't support faccessat(,AT_SYMLINK_NOFOLLOW) and
99    since access rights on symlinks are of limited utility.  */
100
101 static inline int
102 accessat (int fd, char const *file, int mode)
103 {
104   return faccessat (fd, file, mode, 0);
105 }
106
107 static inline int
108 euidaccessat (int fd, char const *file, int mode)
109 {
110   return faccessat (fd, file, mode, AT_EACCESS);
111 }
112 #endif
113
114 #endif /* _GL_HEADER_OPENAT */