Back to index

glibc  2.9
renameat.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <fcntl.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <kernel-features.h>
00024 #include <sysdep.h>
00025 
00026 
00027 #ifndef __ASSUME_ATFCTS
00028 void
00029 attribute_hidden
00030 __atfct_seterrno_2 (int errval, int fd1, const char *buf1, int fd2,
00031                   const char *buf2)
00032 {
00033   if (buf1 != NULL || buf2 != NULL)
00034     {
00035       struct stat64 st;
00036 
00037       if (errval == ENOTDIR)
00038        {
00039          /* This can mean either the file descriptor is invalid or
00040             /proc is not mounted.  */
00041          if (buf1 != NULL)
00042            {
00043              if (__fxstat64 (_STAT_VER, fd1, &st) != 0)
00044               /* errno is already set correctly.  */
00045               return;
00046 
00047              /* If /proc is not mounted there is nothing we can do.  */
00048              if (S_ISDIR (st.st_mode)
00049                 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
00050                     || !S_ISDIR (st.st_mode)))
00051               {
00052                 errval = ENOSYS;
00053                 goto out;
00054               }
00055            }
00056 
00057          if (buf2 != NULL)
00058            {
00059              if (__fxstat64 (_STAT_VER, fd2, &st) != 0)
00060               /* errno is already set correctly.  */
00061               return;
00062 
00063              /* If /proc is not mounted there is nothing we can do.  */
00064              if (S_ISDIR (st.st_mode)
00065                 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
00066                     || !S_ISDIR (st.st_mode)))
00067               errval = ENOSYS;
00068            }
00069        }
00070       else if (errval == ENOENT)
00071        {
00072          /* This could mean the file descriptor is not valid.  We
00073             reuse BUF for the stat call.  Find the slash after the
00074             file descriptor number.  */
00075          if (buf1 != NULL)
00076            {
00077              *(char *) strchr (buf1 + sizeof "/proc/self/fd", '/') = '\0';
00078 
00079              int e = __lxstat64 (_STAT_VER, buf1, &st);
00080              if ((e == -1 && errno == ENOENT)
00081                 ||(e == 0 && !S_ISLNK (st.st_mode)))
00082               {
00083                 errval = EBADF;
00084                 goto out;
00085               }
00086            }
00087 
00088          if (buf2 != NULL)
00089            {
00090              *(char *) strchr (buf2 + sizeof "/proc/self/fd", '/') = '\0';
00091 
00092              int e = __lxstat64 (_STAT_VER, buf2, &st);
00093              if ((e == -1 && errno == ENOENT)
00094                 ||(e == 0 && !S_ISLNK (st.st_mode)))
00095               errval = EBADF;
00096            }
00097        }
00098     }
00099 
00100  out:
00101   __set_errno (errval);
00102 }
00103 #endif
00104 
00105 
00106 /* Rename the file OLD relative to OLDFD to NEW relative to NEWFD.  */
00107 int
00108 renameat (oldfd, old, newfd, new)
00109      int oldfd;
00110      const char *old;
00111      int newfd;
00112      const char *new;
00113 {
00114   int result;
00115 
00116 #ifdef __NR_renameat
00117 # ifndef __ASSUME_ATFCTS
00118   if (__have_atfcts >= 0)
00119 # endif
00120     {
00121       result = INLINE_SYSCALL (renameat, 4, oldfd, old, newfd, new);
00122 # ifndef __ASSUME_ATFCTS
00123       if (result == -1 && errno == ENOSYS)
00124        __have_atfcts = -1;
00125       else
00126 # endif
00127        return result;
00128     }
00129 #endif
00130 
00131 #ifndef __ASSUME_ATFCTS
00132   static const char procfd[] = "/proc/self/fd/%d/%s";
00133   char *bufold = NULL;
00134 
00135   if (oldfd != AT_FDCWD && old[0] != '/')
00136     {
00137       size_t filelen = strlen (old);
00138       /* Buffer for the path name we are going to use.  It consists of
00139         - the string /proc/self/fd/
00140         - the file descriptor number
00141         - the file name provided.
00142         The final NUL is included in the sizeof.   A bit of overhead
00143         due to the format elements compensates for possible negative
00144         numbers.  */
00145       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00146       bufold = alloca (buflen);
00147 
00148       __snprintf (bufold, buflen, procfd, oldfd, old);
00149       old = bufold;
00150     }
00151 
00152   char *bufnew = NULL;
00153 
00154   if (newfd != AT_FDCWD && new[0] != '/')
00155     {
00156       size_t filelen = strlen (new);
00157       /* Buffer for the path name we are going to use.  It consists of
00158         - the string /proc/self/fd/
00159         - the file descriptor number
00160         - the file name provided.
00161         The final NUL is included in the sizeof.   A bit of overhead
00162         due to the format elements compensates for possible negative
00163         numbers.  */
00164       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00165       bufnew = alloca (buflen);
00166 
00167       __snprintf (bufnew, buflen, procfd, newfd, new);
00168       new = bufnew;
00169     }
00170 
00171   INTERNAL_SYSCALL_DECL (err);
00172 
00173   result = INTERNAL_SYSCALL (rename, err, 2, old,  new);
00174 
00175   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00176     {
00177       __atfct_seterrno_2 (INTERNAL_SYSCALL_ERRNO (result, err), newfd, bufnew,
00178                        oldfd, bufold);
00179       result = -1;
00180     }
00181 
00182   return result;
00183 #endif
00184 }