]> git.cworth.org Git - tar/blob - gnu/renameat.c
Imported Upstream version 1.24
[tar] / gnu / renameat.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Rename a file relative to open directories.
4    Copyright (C) 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 Eric Blake */
20
21 #include <config.h>
22
23 #include <stdio.h>
24
25 #if HAVE_RENAMEAT
26
27 # include <errno.h>
28 # include <stdbool.h>
29 # include <stdlib.h>
30 # include <string.h>
31 # include <sys/stat.h>
32
33 # include "dirname.h"
34 # include "openat.h"
35
36 # undef renameat
37
38 /* renameat does not honor trailing / on Solaris 10.  Solve it in a
39    similar manner to rename.  No need to worry about bugs not present
40    on Solaris, since all other systems either lack renameat or honor
41    trailing slash correctly.  */
42
43 int
44 rpl_renameat (int fd1, char const *src, int fd2, char const *dst)
45 {
46   size_t src_len = strlen (src);
47   size_t dst_len = strlen (dst);
48   char *src_temp = (char *) src;
49   char *dst_temp = (char *) dst;
50   bool src_slash;
51   bool dst_slash;
52   int ret_val = -1;
53   int rename_errno = ENOTDIR;
54   struct stat src_st;
55   struct stat dst_st;
56
57   /* Let strace see any ENOENT failure.  */
58   if (!src_len || !dst_len)
59     return renameat (fd1, src, fd2, dst);
60
61   src_slash = src[src_len - 1] == '/';
62   dst_slash = dst[dst_len - 1] == '/';
63   if (!src_slash && !dst_slash)
64     return renameat (fd1, src, fd2, dst);
65
66   /* Presence of a trailing slash requires directory semantics.  If
67      the source does not exist, or if the destination cannot be turned
68      into a directory, give up now.  Otherwise, strip trailing slashes
69      before calling rename.  */
70   if (lstatat (fd1, src, &src_st))
71     return -1;
72   if (lstatat (fd2, dst, &dst_st))
73     {
74       if (errno != ENOENT || !S_ISDIR (src_st.st_mode))
75         return -1;
76     }
77   else if (!S_ISDIR (dst_st.st_mode))
78     {
79       errno = ENOTDIR;
80       return -1;
81     }
82   else if (!S_ISDIR (src_st.st_mode))
83     {
84       errno = EISDIR;
85       return -1;
86     }
87
88 # if RENAME_TRAILING_SLASH_SOURCE_BUG
89   /* See the lengthy comment in rename.c why Solaris 9 is forced to
90      GNU behavior, while Solaris 10 is left with POSIX behavior,
91      regarding symlinks with trailing slash.  */
92   if (src_slash)
93     {
94       src_temp = strdup (src);
95       if (!src_temp)
96         {
97           /* Rather than rely on strdup-posix, we set errno ourselves.  */
98           rename_errno = ENOMEM;
99           goto out;
100         }
101       strip_trailing_slashes (src_temp);
102       if (lstatat (fd1, src_temp, &src_st))
103         {
104           rename_errno = errno;
105           goto out;
106         }
107       if (S_ISLNK (src_st.st_mode))
108         goto out;
109     }
110   if (dst_slash)
111     {
112       dst_temp = strdup (dst);
113       if (!dst_temp)
114         {
115           rename_errno = ENOMEM;
116           goto out;
117         }
118       strip_trailing_slashes (dst_temp);
119       if (lstatat (fd2, dst_temp, &dst_st))
120         {
121           if (errno != ENOENT)
122             {
123               rename_errno = errno;
124               goto out;
125             }
126         }
127       else if (S_ISLNK (dst_st.st_mode))
128         goto out;
129     }
130 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG */
131
132   ret_val = renameat (fd1, src_temp, fd2, dst_temp);
133   rename_errno = errno;
134  out:
135   if (src_temp != src)
136     free (src_temp);
137   if (dst_temp != dst)
138     free (dst_temp);
139   errno = rename_errno;
140   return ret_val;
141 }
142
143 #else /* !HAVE_RENAMEAT */
144
145 # include "openat-priv.h"
146
147 /* Rename FILE1, in the directory open on descriptor FD1, to FILE2, in
148    the directory open on descriptor FD2.  If possible, do it without
149    changing the working directory.  Otherwise, resort to using
150    save_cwd/fchdir, then rename/restore_cwd.  If either the save_cwd or
151    the restore_cwd fails, then give a diagnostic and exit nonzero.  */
152
153 int
154 renameat (int fd1, char const *file1, int fd2, char const *file2)
155 {
156   return at_func2 (fd1, file1, fd2, file2, rename);
157 }
158
159 #endif /* !HAVE_RENAMEAT */