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