symlinkat: new module
[gnulib.git] / lib / at-func.c
1 /* Define an at-style functions like fstatat, unlinkat, fchownat, etc.
2    Copyright (C) 2006, 2009 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 #ifdef AT_FUNC_USE_F1_COND
20 # define CALL_FUNC(F)                           \
21   (flag == AT_FUNC_USE_F1_COND                  \
22     ? AT_FUNC_F1 (F AT_FUNC_POST_FILE_ARGS)     \
23     : AT_FUNC_F2 (F AT_FUNC_POST_FILE_ARGS))
24 # define VALIDATE_FLAG(F)                       \
25   if (flag & ~AT_FUNC_USE_F1_COND)              \
26     {                                           \
27       errno = EINVAL;                           \
28       return -1;                                \
29     }
30 #else
31 # define CALL_FUNC(F) (AT_FUNC_F1 (F AT_FUNC_POST_FILE_ARGS))
32 # define VALIDATE_FLAG(F) /* empty */
33 #endif
34
35 #ifdef AT_FUNC_RESULT
36 # define FUNC_RESULT AT_FUNC_RESULT
37 #else
38 # define FUNC_RESULT int
39 #endif
40
41 /* Call AT_FUNC_F1 to operate on FILE, which is in the directory
42    open on descriptor FD.  If AT_FUNC_USE_F1_COND is defined to a value,
43    AT_FUNC_POST_FILE_PARAM_DECLS must inlude a parameter named flag;
44    call AT_FUNC_F2 if FLAG is 0 or fail if FLAG contains more bits than
45    AT_FUNC_USE_F1_COND.  If possible, do it without changing the
46    working directory.  Otherwise, resort to using save_cwd/fchdir,
47    then AT_FUNC_F?/restore_cwd.  If either the save_cwd or the restore_cwd
48    fails, then give a diagnostic and exit nonzero.  */
49 FUNC_RESULT
50 AT_FUNC_NAME (int fd, char const *file AT_FUNC_POST_FILE_PARAM_DECLS)
51 {
52   /* Be careful to choose names unlikely to conflict with
53      AT_FUNC_POST_FILE_PARAM_DECLS.  */
54   struct saved_cwd saved_cwd;
55   int saved_errno;
56   FUNC_RESULT err;
57
58   VALIDATE_FLAG (flag);
59
60   if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
61     return CALL_FUNC (file);
62
63   {
64     char proc_buf[OPENAT_BUFFER_SIZE];
65     char *proc_file = openat_proc_name (proc_buf, fd, file);
66     if (proc_file)
67       {
68         FUNC_RESULT proc_result = CALL_FUNC (proc_file);
69         int proc_errno = errno;
70         if (proc_file != proc_buf)
71           free (proc_file);
72         /* If the syscall succeeds, or if it fails with an unexpected
73            errno value, then return right away.  Otherwise, fall through
74            and resort to using save_cwd/restore_cwd.  */
75         if (0 <= proc_result)
76           return proc_result;
77         if (! EXPECTED_ERRNO (proc_errno))
78           {
79             errno = proc_errno;
80             return proc_result;
81           }
82       }
83   }
84
85   if (save_cwd (&saved_cwd) != 0)
86     openat_save_fail (errno);
87
88   if (fchdir (fd) != 0)
89     {
90       saved_errno = errno;
91       free_cwd (&saved_cwd);
92       errno = saved_errno;
93       return -1;
94     }
95
96   err = CALL_FUNC (file);
97   saved_errno = (err < 0 ? errno : 0);
98
99   if (restore_cwd (&saved_cwd) != 0)
100     openat_restore_fail (errno);
101
102   free_cwd (&saved_cwd);
103
104   if (saved_errno)
105     errno = saved_errno;
106   return err;
107 }
108 #undef CALL_FUNC
109 #undef FUNC_RESULT