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