Back to index

glibc  2.9
xstatconv.c
Go to the documentation of this file.
00001 /* Convert between the kernel's `struct stat' format, and libc's.
00002    Copyright (C) 1991, 1995, 1996, 1997, 2000, 2002, 2003, 2006
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <assert.h>
00022 #include <errno.h>
00023 #include <sys/stat.h>
00024 #include <kernel_stat.h>
00025 #include <string.h>
00026 #include <kernel-features.h>
00027 
00028 int
00029 __xstat_conv (int vers, struct kernel_stat *kbuf, void *ubuf)
00030 {
00031   switch (vers)
00032     {
00033     case _STAT_VER_KERNEL:
00034       /* Nothing to do.  The struct is in the form the kernel expects.
00035          We should have short-circuted before we got here, but for
00036          completeness... */
00037       *(struct kernel_stat *) ubuf = *kbuf;
00038       break;
00039 
00040     case _STAT_VER_LINUX:
00041       {
00042        struct stat *buf = ubuf;
00043 
00044        /* Convert to current kernel version of `struct stat'.  */
00045        buf->st_dev = kbuf->st_dev;
00046        buf->__pad1 = 0;
00047        buf->st_ino = kbuf->st_ino;
00048        buf->st_mode = kbuf->st_mode;
00049        buf->st_nlink = kbuf->st_nlink;
00050        buf->st_uid = kbuf->st_uid;
00051        buf->st_gid = kbuf->st_gid;
00052        buf->st_rdev = kbuf->st_rdev;
00053        buf->__pad2 = 0;
00054        buf->st_size = kbuf->st_size;
00055        buf->st_blksize = kbuf->st_blksize;
00056        buf->st_blocks = kbuf->st_blocks;
00057        buf->st_atim.tv_sec = kbuf->st_atime_sec;
00058        buf->st_atim.tv_nsec = 0;
00059        buf->st_mtim.tv_sec = kbuf->st_mtime_sec;
00060        buf->st_mtim.tv_nsec = 0;
00061        buf->st_ctim.tv_sec = kbuf->st_ctime_sec;
00062        buf->st_ctim.tv_nsec = 0;
00063        buf->__unused4 = 0;
00064        buf->__unused5 = 0;
00065       }
00066       break;
00067 
00068     default:
00069       __set_errno (EINVAL);
00070       return -1;
00071     }
00072 
00073   return 0;
00074 }
00075 
00076 int
00077 __xstat32_conv (int vers, struct stat64 *sbuf, struct stat *buf)
00078 {
00079   struct kernel_stat64 *kbuf;
00080 
00081   /* *stat64 syscalls on sparc64 really fill in struct kernel_stat64,
00082      rather than struct stat64.  But it is the same size as
00083      struct kernel_stat64, so use this hack so that we can reuse
00084      i386 {,f,l}xstat{,at}.c routines.  */
00085   __asm ("" : "=r" (kbuf) : "0" (sbuf));
00086   assert (sizeof (struct stat) == sizeof (struct stat64));
00087   assert (sizeof (struct stat64) >= sizeof (struct kernel_stat64));
00088 
00089   switch (vers)
00090     {
00091     case _STAT_VER_LINUX:
00092       {
00093        /* Convert current kernel version of `struct stat64' to
00094            `struct stat'.  */
00095        buf->st_dev = kbuf->st_dev;
00096        buf->__pad1 = 0;
00097        buf->st_ino = kbuf->st_ino;
00098        buf->st_mode = kbuf->st_mode;
00099        buf->st_nlink = kbuf->st_nlink;
00100        buf->st_uid = kbuf->st_uid;
00101        buf->st_gid = kbuf->st_gid;
00102        buf->st_rdev = kbuf->st_rdev;
00103        buf->__pad2 = 0;
00104        buf->st_size = kbuf->st_size;
00105        buf->st_blksize = kbuf->st_blksize;
00106        buf->st_blocks = kbuf->st_blocks;
00107        buf->st_atim.tv_sec = kbuf->st_atime_sec;
00108        buf->st_atim.tv_nsec = kbuf->st_atime_nsec;
00109        buf->st_mtim.tv_sec = kbuf->st_mtime_sec;
00110        buf->st_mtim.tv_nsec = kbuf->st_mtime_nsec;
00111        buf->st_ctim.tv_sec = kbuf->st_ctime_sec;
00112        buf->st_ctim.tv_nsec = kbuf->st_ctime_nsec;
00113        buf->__unused4 = 0;
00114        buf->__unused5 = 0;
00115       }
00116       break;
00117 
00118       /* If struct stat64 is different from struct stat then
00119         _STAT_VER_KERNEL does not make sense.  */
00120     case _STAT_VER_KERNEL:
00121     default:
00122       __set_errno (EINVAL);
00123       return -1;
00124     }
00125 
00126   return 0;
00127 }