pathconf.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Get file-specific information about a file. Linux version.
  2. Copyright (C) 1991-2019 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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <mntent.h>
  17. #include <stdio_ext.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <sys/sysmacros.h>
  21. #include "pathconf.h"
  22. #include "linux_fsinfo.h"
  23. #include <not-cancel.h>
  24. static long int posix_pathconf (const char *file, int name);
  25. /* Define this first, so it can be inlined. */
  26. #define __pathconf static posix_pathconf
  27. #include <sysdeps/posix/pathconf.c>
  28. /* Get file-specific information about FILE. */
  29. long int
  30. __pathconf (const char *file, int name)
  31. {
  32. struct statfs fsbuf;
  33. switch (name)
  34. {
  35. case _PC_LINK_MAX:
  36. return __statfs_link_max (__statfs (file, &fsbuf), &fsbuf, file, -1);
  37. case _PC_FILESIZEBITS:
  38. return __statfs_filesize_max (__statfs (file, &fsbuf), &fsbuf);
  39. case _PC_2_SYMLINKS:
  40. return __statfs_symlinks (__statfs (file, &fsbuf), &fsbuf);
  41. case _PC_CHOWN_RESTRICTED:
  42. return __statfs_chown_restricted (__statfs (file, &fsbuf), &fsbuf);
  43. default:
  44. return posix_pathconf (file, name);
  45. }
  46. }
  47. static long int
  48. distinguish_extX (const struct statfs *fsbuf, const char *file, int fd)
  49. {
  50. char buf[64];
  51. char path[PATH_MAX];
  52. struct stat64 st;
  53. if ((file == NULL ? fstat64 (fd, &st) : stat64 (file, &st)) != 0)
  54. /* Strange. The statfd call worked, but stat fails. Default to
  55. the more pessimistic value. */
  56. return EXT2_LINK_MAX;
  57. __snprintf (buf, sizeof (buf), "/sys/dev/block/%u:%u",
  58. __gnu_dev_major (st.st_dev), __gnu_dev_minor (st.st_dev));
  59. ssize_t n = __readlink (buf, path, sizeof (path));
  60. if (n != -1 && n < sizeof (path))
  61. {
  62. path[n] = '\0';
  63. char *base = strdupa (__basename (path));
  64. __snprintf (path, sizeof (path), "/sys/fs/ext4/%s", base);
  65. return __access (path, F_OK) == 0 ? EXT4_LINK_MAX : EXT2_LINK_MAX;
  66. }
  67. /* XXX Is there a better way to distinguish ext2/3 from ext4 than
  68. iterating over the mounted filesystems and compare the device
  69. numbers? */
  70. FILE *mtab = __setmntent ("/proc/mounts", "r");
  71. if (mtab == NULL)
  72. mtab = __setmntent (_PATH_MOUNTED, "r");
  73. /* By default be conservative. */
  74. long int result = EXT2_LINK_MAX;
  75. if (mtab != NULL)
  76. {
  77. struct mntent mntbuf;
  78. char tmpbuf[1024];
  79. /* No locking needed. */
  80. (void) __fsetlocking (mtab, FSETLOCKING_BYCALLER);
  81. while (__getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
  82. {
  83. if (strcmp (mntbuf.mnt_type, "ext2") != 0
  84. && strcmp (mntbuf.mnt_type, "ext3") != 0
  85. && strcmp (mntbuf.mnt_type, "ext4") != 0)
  86. continue;
  87. struct stat64 fsst;
  88. if (stat64 (mntbuf.mnt_dir, &fsst) >= 0
  89. && st.st_dev == fsst.st_dev)
  90. {
  91. if (strcmp (mntbuf.mnt_type, "ext4") == 0)
  92. result = EXT4_LINK_MAX;
  93. break;
  94. }
  95. }
  96. /* Close the file. */
  97. __endmntent (mtab);
  98. }
  99. return result;
  100. }
  101. /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
  102. long int
  103. __statfs_link_max (int result, const struct statfs *fsbuf, const char *file,
  104. int fd)
  105. {
  106. if (result < 0)
  107. {
  108. if (errno == ENOSYS)
  109. /* Not possible, return the default value. */
  110. return LINUX_LINK_MAX;
  111. /* Some error occured. */
  112. return -1;
  113. }
  114. switch (fsbuf->f_type)
  115. {
  116. case EXT2_SUPER_MAGIC:
  117. /* Unfortunately the kernel does not return a different magic number
  118. for ext4. This would be necessary to easily detect etx4 since it
  119. has a different LINK_MAX value. Therefore we have to find it out
  120. the hard way. */
  121. return distinguish_extX (fsbuf, file, fd);
  122. case F2FS_SUPER_MAGIC:
  123. return F2FS_LINK_MAX;
  124. case MINIX_SUPER_MAGIC:
  125. case MINIX_SUPER_MAGIC2:
  126. return MINIX_LINK_MAX;
  127. case MINIX2_SUPER_MAGIC:
  128. case MINIX2_SUPER_MAGIC2:
  129. return MINIX2_LINK_MAX;
  130. case XENIX_SUPER_MAGIC:
  131. return XENIX_LINK_MAX;
  132. case SYSV4_SUPER_MAGIC:
  133. case SYSV2_SUPER_MAGIC:
  134. return SYSV_LINK_MAX;
  135. case COH_SUPER_MAGIC:
  136. return COH_LINK_MAX;
  137. case UFS_MAGIC:
  138. case UFS_CIGAM:
  139. return UFS_LINK_MAX;
  140. case REISERFS_SUPER_MAGIC:
  141. return REISERFS_LINK_MAX;
  142. case XFS_SUPER_MAGIC:
  143. return XFS_LINK_MAX;
  144. case LUSTRE_SUPER_MAGIC:
  145. return LUSTRE_LINK_MAX;
  146. default:
  147. return LINUX_LINK_MAX;
  148. }
  149. }
  150. /* Used like: return statfs_filesize_max (__statfs (name, &buf), &buf); */
  151. long int
  152. __statfs_filesize_max (int result, const struct statfs *fsbuf)
  153. {
  154. if (result < 0)
  155. {
  156. if (errno == ENOSYS)
  157. /* Not possible, return the default value. */
  158. return 32;
  159. /* Some error occured. */
  160. return -1;
  161. }
  162. switch (fsbuf->f_type)
  163. {
  164. case F2FS_SUPER_MAGIC:
  165. return 256;
  166. case BTRFS_SUPER_MAGIC:
  167. return 255;
  168. case EXT2_SUPER_MAGIC:
  169. case UFS_MAGIC:
  170. case UFS_CIGAM:
  171. case REISERFS_SUPER_MAGIC:
  172. case XFS_SUPER_MAGIC:
  173. case SMB_SUPER_MAGIC:
  174. case NTFS_SUPER_MAGIC:
  175. case UDF_SUPER_MAGIC:
  176. case JFS_SUPER_MAGIC:
  177. case VXFS_SUPER_MAGIC:
  178. case CGROUP_SUPER_MAGIC:
  179. case LUSTRE_SUPER_MAGIC:
  180. return 64;
  181. case MSDOS_SUPER_MAGIC:
  182. case JFFS_SUPER_MAGIC:
  183. case JFFS2_SUPER_MAGIC:
  184. case NCP_SUPER_MAGIC:
  185. case ROMFS_SUPER_MAGIC:
  186. return 32;
  187. default:
  188. return 32;
  189. }
  190. }
  191. /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
  192. long int
  193. __statfs_symlinks (int result, const struct statfs *fsbuf)
  194. {
  195. if (result < 0)
  196. {
  197. if (errno == ENOSYS)
  198. /* Not possible, return the default value. */
  199. return 1;
  200. /* Some error occured. */
  201. return -1;
  202. }
  203. switch (fsbuf->f_type)
  204. {
  205. case ADFS_SUPER_MAGIC:
  206. case BFS_MAGIC:
  207. case CRAMFS_MAGIC:
  208. case DEVPTS_SUPER_MAGIC:
  209. case EFS_SUPER_MAGIC:
  210. case EFS_MAGIC:
  211. case MSDOS_SUPER_MAGIC:
  212. case NTFS_SUPER_MAGIC:
  213. case QNX4_SUPER_MAGIC:
  214. case ROMFS_SUPER_MAGIC:
  215. /* No symlink support. */
  216. return 0;
  217. default:
  218. return 1;
  219. }
  220. }
  221. /* Used like: return __statfs_chown_restricted (__statfs (name, &buf), &buf);*/
  222. long int
  223. __statfs_chown_restricted (int result, const struct statfs *fsbuf)
  224. {
  225. if (result < 0)
  226. {
  227. if (errno == ENOSYS)
  228. /* Not possible, return the default value. */
  229. return 1;
  230. /* Some error occured. */
  231. return -1;
  232. }
  233. return 1;
  234. }