2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / renameat.c
blob86bb75a7b01549b2fa4e76966a89fcec67fe3d61
1 /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <kernel-features.h>
24 #include <sysdep.h>
27 #ifndef __ASSUME_ATFCTS
28 void
29 attribute_hidden
30 __atfct_seterrno_2 (int errval, int fd1, const char *buf1, int fd2,
31 const char *buf2)
33 if (buf1 != NULL || buf2 != NULL)
35 struct stat64 st;
37 if (errval == ENOTDIR)
39 /* This can mean either the file descriptor is invalid or
40 /proc is not mounted. */
41 if (buf1 != NULL)
43 if (__fxstat64 (_STAT_VER, fd1, &st) != 0)
44 /* errno is already set correctly. */
45 return;
47 /* If /proc is not mounted there is nothing we can do. */
48 if (S_ISDIR (st.st_mode)
49 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
50 || !S_ISDIR (st.st_mode)))
52 errval = ENOSYS;
53 goto out;
57 if (buf2 != NULL)
59 if (__fxstat64 (_STAT_VER, fd2, &st) != 0)
60 /* errno is already set correctly. */
61 return;
63 /* If /proc is not mounted there is nothing we can do. */
64 if (S_ISDIR (st.st_mode)
65 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
66 || !S_ISDIR (st.st_mode)))
67 errval = ENOSYS;
70 else if (errval == ENOENT)
72 /* This could mean the file descriptor is not valid. We
73 reuse BUF for the stat call. Find the slash after the
74 file descriptor number. */
75 if (buf1 != NULL)
77 *(char *) strchr (buf1 + sizeof "/proc/self/fd", '/') = '\0';
79 int e = __lxstat64 (_STAT_VER, buf1, &st);
80 if ((e == -1 && errno == ENOENT)
81 ||(e == 0 && !S_ISLNK (st.st_mode)))
83 errval = EBADF;
84 goto out;
88 if (buf2 != NULL)
90 *(char *) strchr (buf2 + sizeof "/proc/self/fd", '/') = '\0';
92 int e = __lxstat64 (_STAT_VER, buf2, &st);
93 if ((e == -1 && errno == ENOENT)
94 ||(e == 0 && !S_ISLNK (st.st_mode)))
95 errval = EBADF;
100 out:
101 __set_errno (errval);
103 #endif
106 /* Rename the file OLD relative to OLDFD to NEW relative to NEWFD. */
108 renameat (oldfd, old, newfd, new)
109 int oldfd;
110 const char *old;
111 int newfd;
112 const char *new;
114 int result;
116 #ifdef __NR_renameat
117 # ifndef __ASSUME_ATFCTS
118 if (__have_atfcts >= 0)
119 # endif
121 result = INLINE_SYSCALL (renameat, 4, oldfd, old, newfd, new);
122 # ifndef __ASSUME_ATFCTS
123 if (result == -1 && errno == ENOSYS)
124 __have_atfcts = -1;
125 else
126 # endif
127 return result;
129 #endif
131 #ifndef __ASSUME_ATFCTS
132 static const char procfd[] = "/proc/self/fd/%d/%s";
133 char *bufold = NULL;
135 if (oldfd != AT_FDCWD && old[0] != '/')
137 size_t filelen = strlen (old);
138 /* Buffer for the path name we are going to use. It consists of
139 - the string /proc/self/fd/
140 - the file descriptor number
141 - the file name provided.
142 The final NUL is included in the sizeof. A bit of overhead
143 due to the format elements compensates for possible negative
144 numbers. */
145 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
146 bufold = alloca (buflen);
148 __snprintf (bufold, buflen, procfd, oldfd, old);
149 old = bufold;
152 char *bufnew = NULL;
154 if (newfd != AT_FDCWD && new[0] != '/')
156 size_t filelen = strlen (new);
157 /* Buffer for the path name we are going to use. It consists of
158 - the string /proc/self/fd/
159 - the file descriptor number
160 - the file name provided.
161 The final NUL is included in the sizeof. A bit of overhead
162 due to the format elements compensates for possible negative
163 numbers. */
164 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
165 bufnew = alloca (buflen);
167 __snprintf (bufnew, buflen, procfd, newfd, new);
168 new = bufnew;
171 INTERNAL_SYSCALL_DECL (err);
173 result = INTERNAL_SYSCALL (rename, err, 2, old, new);
175 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
177 __atfct_seterrno_2 (INTERNAL_SYSCALL_ERRNO (result, err), newfd, bufnew,
178 oldfd, bufold);
179 result = -1;
182 return result;
183 #endif