[BZ #1978]
[glibc.git] / sysdeps / unix / sysv / linux / renameat.c
blob849c67b5d6b4258d2b6b3a87dd0108a5d8396041
1 /* Copyright (C) 2005 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 <sysdep.h>
26 void
27 attribute_hidden
28 __atfct_seterrno_2 (int errval, int fd1, const char *buf1, int fd2,
29 const char *buf2)
31 if (errval == ENOTDIR && (buf1 != NULL || buf2 != NULL))
33 /* This can mean either the file descriptor is invalid or
34 /proc is not mounted. */
35 struct stat64 st;
37 if (buf1 != NULL)
39 if (__fxstat64 (_STAT_VER, fd1, &st) != 0)
40 /* errno is already set correctly. */
41 return;
43 /* If /proc is not mounted there is nothing we can do. */
44 if (S_ISDIR (st.st_mode)
45 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
46 || !S_ISDIR (st.st_mode)))
48 errval = ENOSYS;
49 goto out;
53 if (buf2 != NULL)
55 if (__fxstat64 (_STAT_VER, fd2, &st) != 0)
56 /* errno is already set correctly. */
57 return;
59 /* If /proc is not mounted there is nothing we can do. */
60 if (S_ISDIR (st.st_mode)
61 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
62 || !S_ISDIR (st.st_mode)))
63 errval = ENOSYS;
67 out:
68 __set_errno (errval);
72 /* Rename the file OLD relative to OLDFD to NEW relative to NEWFD. */
73 int
74 renameat (oldfd, old, newfd, new)
75 int oldfd;
76 const char *old;
77 int newfd;
78 const char *new;
80 static const char procfd[] = "/proc/self/fd/%d/%s";
81 char *bufold = NULL;
83 if (oldfd != AT_FDCWD && old[0] != '/')
85 size_t filelen = strlen (old);
86 /* Buffer for the path name we are going to use. It consists of
87 - the string /proc/self/fd/
88 - the file descriptor number
89 - the file name provided.
90 The final NUL is included in the sizeof. A bit of overhead
91 due to the format elements compensates for possible negative
92 numbers. */
93 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
94 bufold = alloca (buflen);
96 __snprintf (bufold, buflen, procfd, oldfd, old);
97 old = bufold;
100 char *bufnew = NULL;
102 if (newfd != AT_FDCWD && new[0] != '/')
104 size_t filelen = strlen (new);
105 /* Buffer for the path name we are going to use. It consists of
106 - the string /proc/self/fd/
107 - the file descriptor number
108 - the file name provided.
109 The final NUL is included in the sizeof. A bit of overhead
110 due to the format elements compensates for possible negative
111 numbers. */
112 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
113 bufnew = alloca (buflen);
115 __snprintf (bufnew, buflen, procfd, newfd, new);
116 new = bufnew;
119 INTERNAL_SYSCALL_DECL (err);
121 int result = INTERNAL_SYSCALL (rename, err, 2, old, new);
123 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
125 __atfct_seterrno_2 (INTERNAL_SYSCALL_ERRNO (result, err), newfd, bufnew,
126 oldfd, bufold);
127 result = -1;
130 return result;