Import upstream version 1.28
[debian/tar] / gnu / at-func.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Define at-style functions like fstatat, unlinkat, fchownat, etc.
4    Copyright (C) 2006, 2009-2014 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU 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    This program 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 General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include "dosname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
22
23 #ifdef GNULIB_SUPPORT_ONLY_AT_FDCWD
24 # include <errno.h>
25 # ifndef ENOTSUP
26 #  define ENOTSUP EINVAL
27 # endif
28 #else
29 # include "openat.h"
30 # include "openat-priv.h"
31 # include "save-cwd.h"
32 #endif
33
34 #ifdef AT_FUNC_USE_F1_COND
35 # define CALL_FUNC(F)                           \
36   (flag == AT_FUNC_USE_F1_COND                  \
37     ? AT_FUNC_F1 (F AT_FUNC_POST_FILE_ARGS)     \
38     : AT_FUNC_F2 (F AT_FUNC_POST_FILE_ARGS))
39 # define VALIDATE_FLAG(F)                       \
40   if (flag & ~AT_FUNC_USE_F1_COND)              \
41     {                                           \
42       errno = EINVAL;                           \
43       return FUNC_FAIL;                         \
44     }
45 #else
46 # define CALL_FUNC(F) (AT_FUNC_F1 (F AT_FUNC_POST_FILE_ARGS))
47 # define VALIDATE_FLAG(F) /* empty */
48 #endif
49
50 #ifdef AT_FUNC_RESULT
51 # define FUNC_RESULT AT_FUNC_RESULT
52 #else
53 # define FUNC_RESULT int
54 #endif
55
56 #ifdef AT_FUNC_FAIL
57 # define FUNC_FAIL AT_FUNC_FAIL
58 #else
59 # define FUNC_FAIL -1
60 #endif
61
62 /* Call AT_FUNC_F1 to operate on FILE, which is in the directory
63    open on descriptor FD.  If AT_FUNC_USE_F1_COND is defined to a value,
64    AT_FUNC_POST_FILE_PARAM_DECLS must include a parameter named flag;
65    call AT_FUNC_F2 if FLAG is 0 or fail if FLAG contains more bits than
66    AT_FUNC_USE_F1_COND.  Return int and fail with -1 unless AT_FUNC_RESULT
67    or AT_FUNC_FAIL are defined.  If possible, do it without changing the
68    working directory.  Otherwise, resort to using save_cwd/fchdir,
69    then AT_FUNC_F?/restore_cwd.  If either the save_cwd or the restore_cwd
70    fails, then give a diagnostic and exit nonzero.  */
71 FUNC_RESULT
72 AT_FUNC_NAME (int fd, char const *file AT_FUNC_POST_FILE_PARAM_DECLS)
73 {
74   VALIDATE_FLAG (flag);
75
76   if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
77     return CALL_FUNC (file);
78
79 #ifdef GNULIB_SUPPORT_ONLY_AT_FDCWD
80   errno = ENOTSUP;
81   return FUNC_FAIL;
82 #else
83   {
84   /* Be careful to choose names unlikely to conflict with
85      AT_FUNC_POST_FILE_PARAM_DECLS.  */
86   struct saved_cwd saved_cwd;
87   int saved_errno;
88   FUNC_RESULT err;
89
90   {
91     char proc_buf[OPENAT_BUFFER_SIZE];
92     char *proc_file = openat_proc_name (proc_buf, fd, file);
93     if (proc_file)
94       {
95         FUNC_RESULT proc_result = CALL_FUNC (proc_file);
96         int proc_errno = errno;
97         if (proc_file != proc_buf)
98           free (proc_file);
99         /* If the syscall succeeds, or if it fails with an unexpected
100            errno value, then return right away.  Otherwise, fall through
101            and resort to using save_cwd/restore_cwd.  */
102         if (FUNC_FAIL != proc_result)
103           return proc_result;
104         if (! EXPECTED_ERRNO (proc_errno))
105           {
106             errno = proc_errno;
107             return proc_result;
108           }
109       }
110   }
111
112   if (save_cwd (&saved_cwd) != 0)
113     openat_save_fail (errno);
114   if (0 <= fd && fd == saved_cwd.desc)
115     {
116       /* If saving the working directory collides with the user's
117          requested fd, then the user's fd must have been closed to
118          begin with.  */
119       free_cwd (&saved_cwd);
120       errno = EBADF;
121       return FUNC_FAIL;
122     }
123
124   if (fchdir (fd) != 0)
125     {
126       saved_errno = errno;
127       free_cwd (&saved_cwd);
128       errno = saved_errno;
129       return FUNC_FAIL;
130     }
131
132   err = CALL_FUNC (file);
133   saved_errno = (err == FUNC_FAIL ? errno : 0);
134
135   if (restore_cwd (&saved_cwd) != 0)
136     openat_restore_fail (errno);
137
138   free_cwd (&saved_cwd);
139
140   if (saved_errno)
141     errno = saved_errno;
142   return err;
143   }
144 #endif
145 }
146 #undef CALL_FUNC
147 #undef FUNC_RESULT
148 #undef FUNC_FAIL