Back to index

glibc  2.9
pathconf.c
Go to the documentation of this file.
00001 /* Get file-specific information about a file.  Linux version.
00002    Copyright (C) 1991,1995,1996,1998-2003,2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <unistd.h>
00021 #include <errno.h>
00022 
00023 #include "pathconf.h"
00024 #include "linux_fsinfo.h"
00025 #include <not-cancel.h>
00026 
00027 static long int posix_pathconf (const char *file, int name);
00028 
00029 /* Define this first, so it can be inlined.  */
00030 #define __pathconf static posix_pathconf
00031 #include <sysdeps/posix/pathconf.c>
00032 
00033 
00034 /* Get file-specific information about FILE.  */
00035 long int
00036 __pathconf (const char *file, int name)
00037 {
00038   struct statfs fsbuf;
00039 
00040   switch (name)
00041     {
00042     case _PC_LINK_MAX:
00043       return __statfs_link_max (__statfs (file, &fsbuf), &fsbuf);
00044 
00045     case _PC_FILESIZEBITS:
00046       return __statfs_filesize_max (__statfs (file, &fsbuf), &fsbuf);
00047 
00048     case _PC_2_SYMLINKS:
00049       return __statfs_symlinks (__statfs (file, &fsbuf), &fsbuf);
00050 
00051     case _PC_CHOWN_RESTRICTED:
00052       return __statfs_chown_restricted (__statfs (file, &fsbuf), &fsbuf);
00053 
00054     default:
00055       return posix_pathconf (file, name);
00056     }
00057 }
00058 
00059 
00060 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
00061 long int
00062 __statfs_link_max (int result, const struct statfs *fsbuf)
00063 {
00064   if (result < 0)
00065     {
00066       if (errno == ENOSYS)
00067        /* Not possible, return the default value.  */
00068        return LINUX_LINK_MAX;
00069 
00070       /* Some error occured.  */
00071       return -1;
00072     }
00073 
00074   switch (fsbuf->f_type)
00075     {
00076     case EXT2_SUPER_MAGIC:
00077       return EXT2_LINK_MAX;
00078 
00079     case MINIX_SUPER_MAGIC:
00080     case MINIX_SUPER_MAGIC2:
00081       return MINIX_LINK_MAX;
00082 
00083     case MINIX2_SUPER_MAGIC:
00084     case MINIX2_SUPER_MAGIC2:
00085       return MINIX2_LINK_MAX;
00086 
00087     case XENIX_SUPER_MAGIC:
00088       return XENIX_LINK_MAX;
00089 
00090     case SYSV4_SUPER_MAGIC:
00091     case SYSV2_SUPER_MAGIC:
00092       return SYSV_LINK_MAX;
00093 
00094     case COH_SUPER_MAGIC:
00095       return COH_LINK_MAX;
00096 
00097     case UFS_MAGIC:
00098     case UFS_CIGAM:
00099       return UFS_LINK_MAX;
00100 
00101     case REISERFS_SUPER_MAGIC:
00102       return REISERFS_LINK_MAX;
00103 
00104     case XFS_SUPER_MAGIC:
00105       return XFS_LINK_MAX;
00106 
00107     default:
00108       return LINUX_LINK_MAX;
00109     }
00110 }
00111 
00112 
00113 /* Used like: return statfs_filesize_max (__statfs (name, &buf), &buf); */
00114 long int
00115 __statfs_filesize_max (int result, const struct statfs *fsbuf)
00116 {
00117   if (result < 0)
00118     {
00119       if (errno == ENOSYS)
00120        /* Not possible, return the default value.  */
00121        return 32;
00122 
00123       /* Some error occured.  */
00124       return -1;
00125     }
00126 
00127   switch (fsbuf->f_type)
00128     {
00129     case EXT2_SUPER_MAGIC:
00130     case UFS_MAGIC:
00131     case UFS_CIGAM:
00132     case REISERFS_SUPER_MAGIC:
00133     case XFS_SUPER_MAGIC:
00134     case SMB_SUPER_MAGIC:
00135     case NTFS_SUPER_MAGIC:
00136     case UDF_SUPER_MAGIC:
00137     case JFS_SUPER_MAGIC:
00138     case VXFS_SUPER_MAGIC:
00139       return 64;
00140 
00141     case MSDOS_SUPER_MAGIC:
00142     case JFFS_SUPER_MAGIC:
00143     case JFFS2_SUPER_MAGIC:
00144     case NCP_SUPER_MAGIC:
00145     case ROMFS_SUPER_MAGIC:
00146       return 32;
00147 
00148     default:
00149       return 32;
00150     }
00151 }
00152 
00153 
00154 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
00155 long int
00156 __statfs_symlinks (int result, const struct statfs *fsbuf)
00157 {
00158   if (result < 0)
00159     {
00160       if (errno == ENOSYS)
00161        /* Not possible, return the default value.  */
00162        return 1;
00163 
00164       /* Some error occured.  */
00165       return -1;
00166     }
00167 
00168   switch (fsbuf->f_type)
00169     {
00170     case ADFS_SUPER_MAGIC:
00171     case BFS_MAGIC:
00172     case CRAMFS_MAGIC:
00173     case DEVPTS_SUPER_MAGIC:
00174     case EFS_SUPER_MAGIC:
00175     case EFS_MAGIC:
00176     case MSDOS_SUPER_MAGIC:
00177     case NTFS_SUPER_MAGIC:
00178     case QNX4_SUPER_MAGIC:
00179     case ROMFS_SUPER_MAGIC:
00180       /* No symlink support.  */
00181       return 0;
00182 
00183     default:
00184       return 1;
00185     }
00186 }
00187 
00188 
00189 /* Used like: return __statfs_chown_restricted (__statfs (name, &buf), &buf);*/
00190 long int
00191 __statfs_chown_restricted (int result, const struct statfs *fsbuf)
00192 {
00193   if (result < 0)
00194     {
00195       if (errno == ENOSYS)
00196        /* Not possible, return the default value.  */
00197        return 1;
00198 
00199       /* Some error occured.  */
00200       return -1;
00201     }
00202 
00203   int fd;
00204   long int retval = 1;
00205   switch (fsbuf->f_type)
00206     {
00207     case XFS_SUPER_MAGIC:
00208       /* Read the value from /proc/sys/fs/xfs/restrict_chown.  If we cannot
00209         read it default to assume the restriction is in place.  */
00210       fd = open_not_cancel_2 ("/proc/sys/fs/xfs/restrict_chown", O_RDONLY);
00211       if (fd != -1)
00212        {
00213          char buf[2];
00214          if (TEMP_FAILURE_RETRY (read_not_cancel (fd, buf, 2)) == 2
00215              && buf[0] >= '0' && buf[0] <= '1')
00216            retval = buf[0] - '0';
00217 
00218          close_not_cancel_no_status (fd);
00219        }
00220       break;
00221 
00222     default:
00223       break;
00224     }
00225 
00226   return retval;
00227 }