Back to index

glibc  2.9
fxstatat64.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 <stddef.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <sys/stat.h>
00025 #include <kernel_stat.h>
00026 
00027 #include <sysdep.h>
00028 #include <sys/syscall.h>
00029 #include <bp-checks.h>
00030 
00031 #include <kernel-features.h>
00032 
00033 #if __ASSUME_STAT64_SYSCALL == 0
00034 # include <xstatconv.h>
00035 #endif
00036 
00037 #ifdef __NR_stat64
00038 # if  __ASSUME_STAT64_SYSCALL == 0
00039 /* The variable is shared between all wrappers around *stat64 calls.
00040    This is the definition.  */
00041 extern int __have_no_stat64;
00042 # endif
00043 #endif
00044 
00045 /* Get information about the file NAME in BUF.  */
00046 
00047 int
00048 __fxstatat64 (int vers, int fd, const char *file, struct stat64 *st, int flag)
00049 {
00050   if (__builtin_expect (vers != _STAT_VER_LINUX, 0))
00051     {
00052       __set_errno (EINVAL);
00053       return -1;
00054     }
00055 
00056   int result;
00057   INTERNAL_SYSCALL_DECL (err);
00058 
00059 #ifdef __NR_fstatat64
00060 # ifndef __ASSUME_ATFCTS
00061   if (__have_atfcts >= 0)
00062 # endif
00063     {
00064       result = INTERNAL_SYSCALL (fstatat64, err, 4, fd, file, st, flag);
00065 # ifndef __ASSUME_ATFCTS
00066       if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1)
00067          && INTERNAL_SYSCALL_ERRNO (result, err) == ENOSYS)
00068        __have_atfcts = -1;
00069       else
00070 # endif
00071        if (!__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00072          return 0;
00073        else
00074          {
00075            __set_errno (INTERNAL_SYSCALL_ERRNO (result, err));
00076            return -1;
00077          }
00078     }
00079 #endif
00080 
00081 #ifndef __ASSUME_ATFCTS
00082   if (flag & ~AT_SYMLINK_NOFOLLOW)
00083     {
00084       __set_errno (EINVAL);
00085       return -1;
00086     }
00087 
00088   char *buf = NULL;
00089 
00090   if (fd != AT_FDCWD && file[0] != '/')
00091     {
00092       size_t filelen = strlen (file);
00093       static const char procfd[] = "/proc/self/fd/%d/%s";
00094       /* Buffer for the path name we are going to use.  It consists of
00095         - the string /proc/self/fd/
00096         - the file descriptor number
00097         - the file name provided.
00098         The final NUL is included in the sizeof.   A bit of overhead
00099         due to the format elements compensates for possible negative
00100         numbers.  */
00101       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00102       buf = alloca (buflen);
00103 
00104       __snprintf (buf, buflen, procfd, fd, file);
00105       file = buf;
00106     }
00107 
00108 # if __ASSUME_STAT64_SYSCALL > 0
00109   if (flag & AT_SYMLINK_NOFOLLOW)
00110     result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
00111                             CHECK_1 (st));
00112   else
00113     result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
00114                             CHECK_1 (st));
00115   if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00116     {
00117 #  if defined _HAVE_STAT64___ST_INO && __ASSUME_ST_INO_64_BIT == 0
00118       if (st->__st_ino != (__ino_t) st->st_ino)
00119        st->st_ino = st->__st_ino;
00120 #  endif
00121       return result;
00122     }
00123 # else
00124   struct kernel_stat kst;
00125 #  ifdef __NR_stat64
00126   if (! __have_no_stat64)
00127     {
00128       if (flag & AT_SYMLINK_NOFOLLOW)
00129        result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
00130                                CHECK_1 (st));
00131       else
00132        result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
00133                                CHECK_1 (st));
00134 
00135       if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00136        {
00137 #   if defined _HAVE_STAT64___ST_INO && __ASSUME_ST_INO_64_BIT == 0
00138          if (st->__st_ino != (__ino_t) st->st_ino)
00139            st->st_ino = st->__st_ino;
00140 #   endif
00141          return result;
00142        }
00143       if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
00144        goto fail;
00145 
00146       __have_no_stat64 = 1;
00147     }
00148 #  endif
00149 
00150   if (flag & AT_SYMLINK_NOFOLLOW)
00151     result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
00152                             __ptrvalue (&kst));
00153   else
00154     result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
00155                             __ptrvalue (&kst));
00156 
00157   if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00158     return __xstat64_conv (vers, &kst, st);
00159 
00160  fail:
00161 # endif
00162   __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00163 
00164   return -1;
00165 #endif
00166 }
00167 libc_hidden_def (__fxstatat64)