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
23 #include <kernel-features.h>
27 #ifndef __ASSUME_ATFCTS
30 __atfct_seterrno_2 (int errval
, int fd1
, const char *buf1
, int fd2
,
33 if (buf1
!= NULL
|| buf2
!= NULL
)
37 if (errval
== ENOTDIR
)
39 /* This can mean either the file descriptor is invalid or
40 /proc is not mounted. */
43 if (__fxstat64 (_STAT_VER
, fd1
, &st
) != 0)
44 /* errno is already set correctly. */
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
)))
59 if (__fxstat64 (_STAT_VER
, fd2
, &st
) != 0)
60 /* errno is already set correctly. */
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
)))
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. */
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
)))
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
)))
101 __set_errno (errval
);
106 /* Rename the file OLD relative to OLDFD to NEW relative to NEWFD. */
108 renameat (oldfd
, old
, newfd
, new)
117 # ifndef __ASSUME_ATFCTS
118 if (__have_atfcts
>= 0)
121 result
= INLINE_SYSCALL (renameat
, 4, oldfd
, old
, newfd
, new);
122 # ifndef __ASSUME_ATFCTS
123 if (result
== -1 && errno
== ENOSYS
)
131 #ifndef __ASSUME_ATFCTS
132 static const char procfd
[] = "/proc/self/fd/%d/%s";
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
145 size_t buflen
= sizeof (procfd
) + sizeof (int) * 3 + filelen
;
146 bufold
= alloca (buflen
);
148 __snprintf (bufold
, buflen
, procfd
, oldfd
, old
);
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
164 size_t buflen
= sizeof (procfd
) + sizeof (int) * 3 + filelen
;
165 bufnew
= alloca (buflen
);
167 __snprintf (bufnew
, buflen
, procfd
, newfd
, new);
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
,