Move remaining files out of nptl/sysdeps/unix/sysv/linux/x86/.
[glibc.git] / sysdeps / unix / sysv / linux / renameat.c
blobdbc4c75c01d67227d8256d9dd368b7bf14b03816
1 /* Copyright (C) 2005-2014 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <kernel-features.h>
23 #include <sysdep.h>
26 #ifndef __ASSUME_ATFCTS
27 void
28 attribute_hidden
29 __atfct_seterrno_2 (int errval, int fd1, const char *buf1, int fd2,
30 const char *buf2)
32 if (buf1 != NULL || buf2 != NULL)
34 struct stat64 st;
36 if (errval == ENOTDIR)
38 /* This can mean either the file descriptor is invalid or
39 /proc is not mounted. */
40 if (buf1 != NULL)
42 if (__fxstat64 (_STAT_VER, fd1, &st) != 0)
43 /* errno is already set correctly. */
44 return;
46 /* If /proc is not mounted there is nothing we can do. */
47 if (S_ISDIR (st.st_mode)
48 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
49 || !S_ISDIR (st.st_mode)))
51 errval = ENOSYS;
52 goto out;
56 if (buf2 != NULL)
58 if (__fxstat64 (_STAT_VER, fd2, &st) != 0)
59 /* errno is already set correctly. */
60 return;
62 /* If /proc is not mounted there is nothing we can do. */
63 if (S_ISDIR (st.st_mode)
64 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
65 || !S_ISDIR (st.st_mode)))
66 errval = ENOSYS;
69 else if (errval == ENOENT)
71 /* This could mean the file descriptor is not valid. We
72 reuse BUF for the stat call. Find the slash after the
73 file descriptor number. */
74 if (buf1 != NULL)
76 *(char *) strchr (buf1 + sizeof "/proc/self/fd", '/') = '\0';
78 int e = __lxstat64 (_STAT_VER, buf1, &st);
79 if ((e == -1 && errno == ENOENT)
80 ||(e == 0 && !S_ISLNK (st.st_mode)))
82 errval = EBADF;
83 goto out;
87 if (buf2 != NULL)
89 *(char *) strchr (buf2 + sizeof "/proc/self/fd", '/') = '\0';
91 int e = __lxstat64 (_STAT_VER, buf2, &st);
92 if ((e == -1 && errno == ENOENT)
93 ||(e == 0 && !S_ISLNK (st.st_mode)))
94 errval = EBADF;
99 out:
100 __set_errno (errval);
102 #endif
105 /* Rename the file OLD relative to OLDFD to NEW relative to NEWFD. */
107 renameat (oldfd, old, newfd, new)
108 int oldfd;
109 const char *old;
110 int newfd;
111 const char *new;
113 int result;
115 #ifdef __NR_renameat
116 # ifndef __ASSUME_ATFCTS
117 if (__have_atfcts >= 0)
118 # endif
120 result = INLINE_SYSCALL (renameat, 4, oldfd, old, newfd, new);
121 # ifndef __ASSUME_ATFCTS
122 if (result == -1 && errno == ENOSYS)
123 __have_atfcts = -1;
124 else
125 # endif
126 return result;
128 #endif
130 #ifndef __ASSUME_ATFCTS
131 static const char procfd[] = "/proc/self/fd/%d/%s";
132 char *bufold = NULL;
134 if (oldfd != AT_FDCWD && old[0] != '/')
136 size_t filelen = strlen (old);
137 if (__glibc_unlikely (filelen == 0))
139 __set_errno (ENOENT);
140 return -1;
143 /* Buffer for the path name we are going to use. It consists of
144 - the string /proc/self/fd/
145 - the file descriptor number
146 - the file name provided.
147 The final NUL is included in the sizeof. A bit of overhead
148 due to the format elements compensates for possible negative
149 numbers. */
150 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
151 bufold = alloca (buflen);
153 __snprintf (bufold, buflen, procfd, oldfd, old);
154 old = bufold;
157 char *bufnew = NULL;
159 if (newfd != AT_FDCWD && new[0] != '/')
161 size_t filelen = strlen (new);
162 if (__glibc_unlikely (filelen == 0))
164 __set_errno (ENOENT);
165 return -1;
168 /* Buffer for the path name we are going to use. It consists of
169 - the string /proc/self/fd/
170 - the file descriptor number
171 - the file name provided.
172 The final NUL is included in the sizeof. A bit of overhead
173 due to the format elements compensates for possible negative
174 numbers. */
175 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
176 bufnew = alloca (buflen);
178 __snprintf (bufnew, buflen, procfd, newfd, new);
179 new = bufnew;
182 INTERNAL_SYSCALL_DECL (err);
184 result = INTERNAL_SYSCALL (rename, err, 2, old, new);
186 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (result, err)))
188 __atfct_seterrno_2 (INTERNAL_SYSCALL_ERRNO (result, err), newfd, bufnew,
189 oldfd, bufold);
190 result = -1;
193 return result;
194 #endif