]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/pathconf.c
1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 1991,1995,1996,1998-2003,2008,2010,2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include <stdio_ext.h>
24 #include <sys/sysmacros.h>
27 #include "linux_fsinfo.h"
28 #include <not-cancel.h>
30 static long int posix_pathconf (const char *file
, int name
);
32 /* Define this first, so it can be inlined. */
33 #define __pathconf static posix_pathconf
34 #include <sysdeps/posix/pathconf.c>
37 /* Get file-specific information about FILE. */
39 __pathconf (const char *file
, int name
)
48 return __statfs_link_max (__statfs (file
, &fsbuf
), &fsbuf
, file
, -1);
50 case _PC_FILESIZEBITS
:
51 return __statfs_filesize_max (__statfs (file
, &fsbuf
), &fsbuf
);
54 return __statfs_symlinks (__statfs (file
, &fsbuf
), &fsbuf
);
56 case _PC_CHOWN_RESTRICTED
:
57 return __statfs_chown_restricted (__statfs (file
, &fsbuf
), &fsbuf
);
60 flags
= O_RDONLY
|O_NONBLOCK
|O_NOCTTY
;
64 fd
= open_not_cancel_2 (file
, flags
);
67 long int r
= __fcntl (fd
, F_GETPIPE_SZ
);
68 close_not_cancel_no_status (fd
);
75 return posix_pathconf (file
, name
);
81 distinguish_extX (const struct statfs
*fsbuf
, const char *file
, int fd
)
87 if ((file
== NULL
? fstat64 (fd
, &st
) : stat64 (file
, &st
)) != 0)
88 /* Strange. The statfd call worked, but stat fails. Default to
89 the more pessimistic value. */
92 __snprintf (buf
, sizeof (buf
), "/sys/dev/block/%u:%u",
93 gnu_dev_major (st
.st_dev
), gnu_dev_minor (st
.st_dev
));
95 ssize_t n
= __readlink (buf
, path
, sizeof (path
));
96 if (n
!= -1 && n
< sizeof (path
))
99 char *base
= strdupa (basename (path
));
100 __snprintf (path
, sizeof (path
), "/sys/fs/ext4/%s", base
);
102 return __access (path
, F_OK
) == 0 ? EXT4_LINK_MAX
: EXT2_LINK_MAX
;
105 /* XXX Is there a better way to distinguish ext2/3 from ext4 than
106 iterating over the mounted filesystems and compare the device
108 FILE *mtab
= __setmntent ("/proc/mounts", "r");
110 mtab
= __setmntent (_PATH_MOUNTED
, "r");
112 /* By default be conservative. */
113 long int result
= EXT2_LINK_MAX
;
116 struct mntent mntbuf
;
119 /* No locking needed. */
120 (void) __fsetlocking (mtab
, FSETLOCKING_BYCALLER
);
122 while (__getmntent_r (mtab
, &mntbuf
, tmpbuf
, sizeof (tmpbuf
)))
124 if (strcmp (mntbuf
.mnt_type
, "ext2") != 0
125 && strcmp (mntbuf
.mnt_type
, "ext3") != 0
126 && strcmp (mntbuf
.mnt_type
, "ext4") != 0)
130 if (stat64 (mntbuf
.mnt_dir
, &fsst
) >= 0
131 && st
.st_dev
== fsst
.st_dev
)
133 if (strcmp (mntbuf
.mnt_type
, "ext4") == 0)
134 result
= EXT4_LINK_MAX
;
139 /* Close the file. */
147 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
149 __statfs_link_max (int result
, const struct statfs
*fsbuf
, const char *file
,
155 /* Not possible, return the default value. */
156 return LINUX_LINK_MAX
;
158 /* Some error occured. */
162 switch (fsbuf
->f_type
)
164 case EXT2_SUPER_MAGIC
:
165 /* Unfortunately the kernel does not return a different magic number
166 for ext4. This would be necessary to easily detect etx4 since it
167 has a different LINK_MAX value. Therefore we have to find it out
169 return distinguish_extX (fsbuf
, file
, fd
);
171 case MINIX_SUPER_MAGIC
:
172 case MINIX_SUPER_MAGIC2
:
173 return MINIX_LINK_MAX
;
175 case MINIX2_SUPER_MAGIC
:
176 case MINIX2_SUPER_MAGIC2
:
177 return MINIX2_LINK_MAX
;
179 case XENIX_SUPER_MAGIC
:
180 return XENIX_LINK_MAX
;
182 case SYSV4_SUPER_MAGIC
:
183 case SYSV2_SUPER_MAGIC
:
184 return SYSV_LINK_MAX
;
186 case COH_SUPER_MAGIC
:
193 case REISERFS_SUPER_MAGIC
:
194 return REISERFS_LINK_MAX
;
196 case XFS_SUPER_MAGIC
:
199 case LUSTRE_SUPER_MAGIC
:
200 return LUSTRE_LINK_MAX
;
203 return LINUX_LINK_MAX
;
208 /* Used like: return statfs_filesize_max (__statfs (name, &buf), &buf); */
210 __statfs_filesize_max (int result
, const struct statfs
*fsbuf
)
215 /* Not possible, return the default value. */
218 /* Some error occured. */
222 switch (fsbuf
->f_type
)
224 case BTRFS_SUPER_MAGIC
:
227 case EXT2_SUPER_MAGIC
:
230 case REISERFS_SUPER_MAGIC
:
231 case XFS_SUPER_MAGIC
:
232 case SMB_SUPER_MAGIC
:
233 case NTFS_SUPER_MAGIC
:
234 case UDF_SUPER_MAGIC
:
235 case JFS_SUPER_MAGIC
:
236 case VXFS_SUPER_MAGIC
:
237 case CGROUP_SUPER_MAGIC
:
238 case LUSTRE_SUPER_MAGIC
:
241 case MSDOS_SUPER_MAGIC
:
242 case JFFS_SUPER_MAGIC
:
243 case JFFS2_SUPER_MAGIC
:
244 case NCP_SUPER_MAGIC
:
245 case ROMFS_SUPER_MAGIC
:
254 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
256 __statfs_symlinks (int result
, const struct statfs
*fsbuf
)
261 /* Not possible, return the default value. */
264 /* Some error occured. */
268 switch (fsbuf
->f_type
)
270 case ADFS_SUPER_MAGIC
:
273 case DEVPTS_SUPER_MAGIC
:
274 case EFS_SUPER_MAGIC
:
276 case MSDOS_SUPER_MAGIC
:
277 case NTFS_SUPER_MAGIC
:
278 case QNX4_SUPER_MAGIC
:
279 case ROMFS_SUPER_MAGIC
:
280 /* No symlink support. */
289 /* Used like: return __statfs_chown_restricted (__statfs (name, &buf), &buf);*/
291 __statfs_chown_restricted (int result
, const struct statfs
*fsbuf
)
296 /* Not possible, return the default value. */
299 /* Some error occured. */
305 switch (fsbuf
->f_type
)
307 case XFS_SUPER_MAGIC
:
308 /* Read the value from /proc/sys/fs/xfs/restrict_chown. If we cannot
309 read it default to assume the restriction is in place. */
310 fd
= open_not_cancel_2 ("/proc/sys/fs/xfs/restrict_chown", O_RDONLY
);
314 if (TEMP_FAILURE_RETRY (read_not_cancel (fd
, buf
, 2)) == 2
315 && buf
[0] >= '0' && buf
[0] <= '1')
316 retval
= buf
[0] - '0';
318 close_not_cancel_no_status (fd
);