xstatconv.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Convert between the kernel's `struct stat' format, and libc's.
  2. Copyright (C) 1991,1995,1996,1997,2000,2002 Free Software Foundation, Inc.
  3. 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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Modified for uClibc by Erik Andersen <andersen@codepoet.org>
  17. */
  18. #define _GNU_SOURCE
  19. #define _LARGEFILE64_SOURCE
  20. #include <features.h>
  21. #undef __OPTIMIZE__
  22. /* We absolutely do _NOT_ want interfaces silently
  23. * * * renamed under us or very bad things will happen... */
  24. #ifdef __USE_FILE_OFFSET64
  25. # undef __USE_FILE_OFFSET64
  26. #endif
  27. #include <sys/stat.h>
  28. #include "xstatconv.h"
  29. void __xstat_conv(struct kernel_stat *kbuf, struct stat *buf)
  30. {
  31. /* Convert to current kernel version of `struct stat'. */
  32. buf->st_dev = kbuf->st_dev;
  33. buf->st_ino = kbuf->st_ino;
  34. buf->st_mode = kbuf->st_mode;
  35. buf->st_nlink = kbuf->st_nlink;
  36. buf->st_uid = kbuf->st_uid;
  37. buf->st_gid = kbuf->st_gid;
  38. buf->st_rdev = kbuf->st_rdev;
  39. buf->st_size = kbuf->st_size;
  40. buf->st_blksize = kbuf->st_blksize;
  41. buf->st_blocks = kbuf->st_blocks;
  42. buf->st_atime = kbuf->st_atime;
  43. buf->st_mtime = kbuf->st_mtime;
  44. buf->st_ctime = kbuf->st_ctime;
  45. #ifdef STAT_HAVE_NSEC
  46. buf->st_atime_nsec = kbuf->st_atime_nsec;
  47. buf->st_mtime_nsec = kbuf->st_mtime_nsec;
  48. buf->st_ctime_nsec = kbuf->st_ctime_nsec;
  49. #endif
  50. }
  51. #if defined(__UCLIBC_HAS_LFS__)
  52. /* OK, this is ugly, but not much we can do about it.
  53. *
  54. * The issue at hand is that the kernel introduced an ABI change between
  55. * 2.4 and 2.6 for big endian machines on some architectures in stat64 with
  56. * st_dev and st_rdev fields. Older kernels used 'short' but newer kernels
  57. * use a 'long'. As if that wasn't fun enough, the location of st_blocks and
  58. * its padding have swapped. So, when applicable, we need to detect and shift
  59. * bits around and hope for the best. The kernel at least helps us out a bit
  60. * because it will be sure to zero out all padding fields.
  61. *
  62. * Unimportant Note About Mis-detections:
  63. * - if user is running a 2.4 system with the old ABI and they stat a file on
  64. * a system which major 0 / minor 0. We can ignore this though because the
  65. * kernel has major 0 reserved for non mountable devices (wh00t!)
  66. * - if user is running a 2.6 system with the new ABI and they stat a file on
  67. * a device whose st_dev fills up the lower 6 bytes into the upper 2 bytes
  68. * which overlap. Again we should be able to safely ignore this because
  69. * that means it'd be a pretty big ass (read: uncommon) major/minor combo :).
  70. Old [2.4]:
  71. struct stat64 {
  72. unsigned short st_dev;
  73. unsigned char __pad0[10];
  74. ...
  75. unsigned long st_blocks;
  76. unsigned long __pad4;
  77. ...
  78. New [2.6]:
  79. struct stat64 {
  80. unsigned long long st_dev;
  81. unsigned char __pad0[4];
  82. ...
  83. unsigned long __pad4;
  84. unsigned long st_blocks;
  85. ...
  86. * sizeof(unsigned short) = 2
  87. * sizeof(unsigned long long) = 8
  88. */
  89. #include <endian.h>
  90. #include <sys/utsname.h>
  91. #if BYTE_ORDER == BIG_ENDIAN && \
  92. (defined(__ARMEB__)) /* || defined(__sh__) || defined(__sparc__))*/
  93. # define NEED_ABI_CHECK 1
  94. #else
  95. # define NEED_ABI_CHECK 0
  96. #endif
  97. void __xstat64_conv(struct kernel_stat64 *kbuf, struct stat64 *buf)
  98. {
  99. /* Convert to current kernel version of `struct stat64'. */
  100. #if NEED_ABI_CHECK
  101. if (!kbuf->st_dev.old_abi) {
  102. /* new 2.6 ABI */
  103. buf->st_dev = kbuf->st_dev.new_abi;
  104. buf->st_rdev = kbuf->st_rdev.new_abi;
  105. buf->st_blocks = kbuf->st_blocks;
  106. } else {
  107. /* old 2.4 ABI */
  108. buf->st_dev = kbuf->st_dev.old_abi;
  109. buf->st_rdev = kbuf->st_dev.old_abi;
  110. buf->st_blocks = kbuf->__pad_st_blocks;
  111. }
  112. #else
  113. buf->st_dev = kbuf->st_dev;
  114. buf->st_ino = kbuf->st_ino;
  115. buf->st_rdev = kbuf->st_rdev;
  116. #endif
  117. #ifdef _HAVE_STAT64___ST_INO
  118. buf->__st_ino = kbuf->__st_ino;
  119. #endif
  120. buf->st_mode = kbuf->st_mode;
  121. buf->st_nlink = kbuf->st_nlink;
  122. buf->st_uid = kbuf->st_uid;
  123. buf->st_gid = kbuf->st_gid;
  124. buf->st_size = kbuf->st_size;
  125. buf->st_blksize = kbuf->st_blksize;
  126. buf->st_atime = kbuf->st_atime;
  127. buf->st_mtime = kbuf->st_mtime;
  128. buf->st_ctime = kbuf->st_ctime;
  129. #ifdef STAT_HAVE_NSEC
  130. buf->st_atime_nsec = kbuf->st_atime_nsec;
  131. buf->st_mtime_nsec = kbuf->st_mtime_nsec;
  132. buf->st_ctime_nsec = kbuf->st_ctime_nsec;
  133. #endif
  134. }
  135. #endif