]> git.cworth.org Git - tar/blob - lib/openat.h
Imported Upstream version 1.20
[tar] / lib / openat.h
1 /* provide a replacement openat function
2    Copyright (C) 2004, 2005, 2006, 2008 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 #include <fcntl.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <dirent.h>
24 #include <unistd.h>
25 #include <stdbool.h>
26
27 #ifndef __attribute__
28 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
29 #  define __attribute__(x) /* empty */
30 # endif
31 #endif
32
33 #ifndef ATTRIBUTE_NORETURN
34 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
35 #endif
36
37 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive.  Its
38    value exceeds INT_MAX, so its use as an int doesn't conform to the
39    C standard, and GCC and Sun C complain in some cases.  If the bug
40    is present, undef AT_FDCWD here, so it can be redefined below.  */
41 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
42 # undef AT_FDCWD
43 #endif
44
45 /* Use the same bit pattern as Solaris 9, but with the proper
46    signedness.  The bit pattern is important, in case this actually is
47    Solaris with the above workaround.  */
48 #ifndef AT_FDCWD
49 # define AT_FDCWD (-3041965)
50 #endif
51
52 /* Use the same values as Solaris 9.  This shouldn't matter, but
53    there's no real reason to differ.  */
54 #ifndef AT_SYMLINK_NOFOLLOW
55 # define AT_SYMLINK_NOFOLLOW 4096
56 # define AT_REMOVEDIR 1
57 #endif
58
59 #ifdef __OPENAT_PREFIX
60
61 # undef openat
62 # define __OPENAT_CONCAT(x, y) x ## y
63 # define __OPENAT_XCONCAT(x, y) __OPENAT_CONCAT (x, y)
64 # define __OPENAT_ID(y) __OPENAT_XCONCAT (__OPENAT_PREFIX, y)
65 # define openat __OPENAT_ID (openat)
66 int openat (int fd, char const *file, int flags, /* mode_t mode */ ...);
67 int openat_permissive (int fd, char const *file, int flags, mode_t mode,
68                        int *cwd_errno);
69 # if ! HAVE_FDOPENDIR
70 #  define fdopendir __OPENAT_ID (fdopendir)
71 # endif
72 DIR *fdopendir (int fd);
73 # define fstatat __OPENAT_ID (fstatat)
74 int fstatat (int fd, char const *file, struct stat *st, int flag);
75 # define unlinkat __OPENAT_ID (unlinkat)
76 int unlinkat (int fd, char const *file, int flag);
77 bool openat_needs_fchdir (void);
78
79 #else
80
81 # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
82     openat (Fd, File, Flags, Mode)
83 # define openat_needs_fchdir() false
84
85 #endif
86
87 #if HAVE_OPENAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
88 int rpl_fstatat (int fd, char const *file, struct stat *st, int flag);
89 # if !COMPILING_FSTATAT
90 #  undef fstatat
91 #  define fstatat rpl_fstatat
92 # endif
93 #endif
94
95 int mkdirat (int fd, char const *file, mode_t mode);
96 void openat_restore_fail (int) ATTRIBUTE_NORETURN;
97 void openat_save_fail (int) ATTRIBUTE_NORETURN;
98 int fchmodat (int fd, char const *file, mode_t mode, int flag);
99 int fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag);
100
101 /* Using these function names makes application code
102    slightly more readable than it would be with
103    fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW).  */
104 static inline int
105 chownat (int fd, char const *file, uid_t owner, gid_t group)
106 {
107   return fchownat (fd, file, owner, group, 0);
108 }
109
110 static inline int
111 lchownat (int fd, char const *file, uid_t owner, gid_t group)
112 {
113   return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
114 }
115
116 static inline int
117 chmodat (int fd, char const *file, mode_t mode)
118 {
119   return fchmodat (fd, file, mode, 0);
120 }
121
122 static inline int
123 lchmodat (int fd, char const *file, mode_t mode)
124 {
125   return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
126 }