hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sysdeps/unix/sysv/linux/pathconf.c

Packit 6c4009
/* Get file-specific information about a file.  Linux version.
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <mntent.h>
Packit 6c4009
#include <stdio_ext.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/sysmacros.h>
Packit 6c4009
Packit 6c4009
#include "pathconf.h"
Packit 6c4009
#include "linux_fsinfo.h"
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
static long int posix_pathconf (const char *file, int name);
Packit 6c4009
Packit 6c4009
/* Define this first, so it can be inlined.  */
Packit 6c4009
#define __pathconf static posix_pathconf
Packit 6c4009
#include <sysdeps/posix/pathconf.c>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Get file-specific information about FILE.  */
Packit 6c4009
long int
Packit 6c4009
__pathconf (const char *file, int name)
Packit 6c4009
{
Packit 6c4009
  struct statfs fsbuf;
Packit 6c4009
Packit 6c4009
  switch (name)
Packit 6c4009
    {
Packit 6c4009
    case _PC_LINK_MAX:
Packit 6c4009
      return __statfs_link_max (__statfs (file, &fsbuf), &fsbuf, file, -1);
Packit 6c4009
Packit 6c4009
    case _PC_FILESIZEBITS:
Packit 6c4009
      return __statfs_filesize_max (__statfs (file, &fsbuf), &fsbuf);
Packit 6c4009
Packit 6c4009
    case _PC_2_SYMLINKS:
Packit 6c4009
      return __statfs_symlinks (__statfs (file, &fsbuf), &fsbuf);
Packit 6c4009
Packit 6c4009
    case _PC_CHOWN_RESTRICTED:
Packit 6c4009
      return __statfs_chown_restricted (__statfs (file, &fsbuf), &fsbuf);
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return posix_pathconf (file, name);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static long int
Packit 6c4009
distinguish_extX (const struct statfs *fsbuf, const char *file, int fd)
Packit 6c4009
{
Packit 6c4009
  char buf[64];
Packit 6c4009
  char path[PATH_MAX];
Packit 6c4009
  struct stat64 st;
Packit 6c4009
Packit 6c4009
  if ((file == NULL ? fstat64 (fd, &st) : stat64 (file, &st)) != 0)
Packit 6c4009
    /* Strange.  The statfd call worked, but stat fails.  Default to
Packit 6c4009
       the more pessimistic value.  */
Packit 6c4009
    return EXT2_LINK_MAX;
Packit 6c4009
Packit 6c4009
  __snprintf (buf, sizeof (buf), "/sys/dev/block/%u:%u",
Packit 6c4009
	      __gnu_dev_major (st.st_dev), __gnu_dev_minor (st.st_dev));
Packit 6c4009
Packit 6c4009
  ssize_t n = __readlink (buf, path, sizeof (path));
Packit 6c4009
  if (n != -1 && n < sizeof (path))
Packit 6c4009
    {
Packit 6c4009
      path[n] = '\0';
Packit 6c4009
      char *base = strdupa (__basename (path));
Packit 6c4009
      __snprintf (path, sizeof (path), "/sys/fs/ext4/%s", base);
Packit 6c4009
Packit 6c4009
      return __access (path, F_OK) == 0 ? EXT4_LINK_MAX : EXT2_LINK_MAX;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* XXX Is there a better way to distinguish ext2/3 from ext4 than
Packit 6c4009
     iterating over the mounted filesystems and compare the device
Packit 6c4009
     numbers?  */
Packit 6c4009
  FILE *mtab = __setmntent ("/proc/mounts", "r");
Packit 6c4009
  if (mtab == NULL)
Packit 6c4009
    mtab = __setmntent (_PATH_MOUNTED, "r");
Packit 6c4009
Packit 6c4009
  /* By default be conservative.  */
Packit 6c4009
  long int result = EXT2_LINK_MAX;
Packit 6c4009
  if (mtab != NULL)
Packit 6c4009
    {
Packit 6c4009
      struct mntent mntbuf;
Packit 6c4009
      char tmpbuf[1024];
Packit 6c4009
Packit 6c4009
      /* No locking needed.  */
Packit 6c4009
      (void) __fsetlocking (mtab, FSETLOCKING_BYCALLER);
Packit 6c4009
Packit 6c4009
      while (__getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
Packit 6c4009
	{
Packit 6c4009
	  if (strcmp (mntbuf.mnt_type, "ext2") != 0
Packit 6c4009
	      && strcmp (mntbuf.mnt_type, "ext3") != 0
Packit 6c4009
	      && strcmp (mntbuf.mnt_type, "ext4") != 0)
Packit 6c4009
	    continue;
Packit 6c4009
Packit 6c4009
	  struct stat64 fsst;
Packit 6c4009
	  if (stat64 (mntbuf.mnt_dir, &fsst) >= 0
Packit 6c4009
	      && st.st_dev == fsst.st_dev)
Packit 6c4009
	    {
Packit 6c4009
	      if (strcmp (mntbuf.mnt_type, "ext4") == 0)
Packit 6c4009
		result = EXT4_LINK_MAX;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Close the file.  */
Packit 6c4009
      __endmntent (mtab);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Used like: return statfs_link_max (__statfs (name, &buf), &buf;; */
Packit 6c4009
long int
Packit 6c4009
__statfs_link_max (int result, const struct statfs *fsbuf, const char *file,
Packit 6c4009
		   int fd)
Packit 6c4009
{
Packit 6c4009
  if (result < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	/* Not possible, return the default value.  */
Packit 6c4009
	return LINUX_LINK_MAX;
Packit 6c4009
Packit 6c4009
      /* Some error occured.  */
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  switch (fsbuf->f_type)
Packit 6c4009
    {
Packit 6c4009
    case EXT2_SUPER_MAGIC:
Packit 6c4009
      /* Unfortunately the kernel does not return a different magic number
Packit 6c4009
	 for ext4.  This would be necessary to easily detect etx4 since it
Packit 6c4009
	 has a different LINK_MAX value.  Therefore we have to find it out
Packit 6c4009
	 the hard way.  */
Packit 6c4009
      return distinguish_extX (fsbuf, file, fd);
Packit 6c4009
Packit 6c4009
    case F2FS_SUPER_MAGIC:
Packit 6c4009
      return F2FS_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case MINIX_SUPER_MAGIC:
Packit 6c4009
    case MINIX_SUPER_MAGIC2:
Packit 6c4009
      return MINIX_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case MINIX2_SUPER_MAGIC:
Packit 6c4009
    case MINIX2_SUPER_MAGIC2:
Packit 6c4009
      return MINIX2_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case XENIX_SUPER_MAGIC:
Packit 6c4009
      return XENIX_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case SYSV4_SUPER_MAGIC:
Packit 6c4009
    case SYSV2_SUPER_MAGIC:
Packit 6c4009
      return SYSV_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case COH_SUPER_MAGIC:
Packit 6c4009
      return COH_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case UFS_MAGIC:
Packit 6c4009
    case UFS_CIGAM:
Packit 6c4009
      return UFS_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case REISERFS_SUPER_MAGIC:
Packit 6c4009
      return REISERFS_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case XFS_SUPER_MAGIC:
Packit 6c4009
      return XFS_LINK_MAX;
Packit 6c4009
Packit 6c4009
    case LUSTRE_SUPER_MAGIC:
Packit 6c4009
      return LUSTRE_LINK_MAX;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return LINUX_LINK_MAX;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Used like: return statfs_filesize_max (__statfs (name, &buf), &buf;; */
Packit 6c4009
long int
Packit 6c4009
__statfs_filesize_max (int result, const struct statfs *fsbuf)
Packit 6c4009
{
Packit 6c4009
  if (result < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	/* Not possible, return the default value.  */
Packit 6c4009
	return 32;
Packit 6c4009
Packit 6c4009
      /* Some error occured.  */
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  switch (fsbuf->f_type)
Packit 6c4009
    {
Packit 6c4009
    case F2FS_SUPER_MAGIC:
Packit 6c4009
      return 256;
Packit 6c4009
Packit 6c4009
    case BTRFS_SUPER_MAGIC:
Packit 6c4009
      return 255;
Packit 6c4009
Packit 6c4009
    case EXT2_SUPER_MAGIC:
Packit 6c4009
    case UFS_MAGIC:
Packit 6c4009
    case UFS_CIGAM:
Packit 6c4009
    case REISERFS_SUPER_MAGIC:
Packit 6c4009
    case XFS_SUPER_MAGIC:
Packit 6c4009
    case SMB_SUPER_MAGIC:
Packit 6c4009
    case NTFS_SUPER_MAGIC:
Packit 6c4009
    case UDF_SUPER_MAGIC:
Packit 6c4009
    case JFS_SUPER_MAGIC:
Packit 6c4009
    case VXFS_SUPER_MAGIC:
Packit 6c4009
    case CGROUP_SUPER_MAGIC:
Packit 6c4009
    case LUSTRE_SUPER_MAGIC:
Packit 6c4009
      return 64;
Packit 6c4009
Packit 6c4009
    case MSDOS_SUPER_MAGIC:
Packit 6c4009
    case JFFS_SUPER_MAGIC:
Packit 6c4009
    case JFFS2_SUPER_MAGIC:
Packit 6c4009
    case NCP_SUPER_MAGIC:
Packit 6c4009
    case ROMFS_SUPER_MAGIC:
Packit 6c4009
      return 32;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return 32;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Used like: return statfs_link_max (__statfs (name, &buf), &buf;; */
Packit 6c4009
long int
Packit 6c4009
__statfs_symlinks (int result, const struct statfs *fsbuf)
Packit 6c4009
{
Packit 6c4009
  if (result < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	/* Not possible, return the default value.  */
Packit 6c4009
	return 1;
Packit 6c4009
Packit 6c4009
      /* Some error occured.  */
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  switch (fsbuf->f_type)
Packit 6c4009
    {
Packit 6c4009
    case ADFS_SUPER_MAGIC:
Packit 6c4009
    case BFS_MAGIC:
Packit 6c4009
    case CRAMFS_MAGIC:
Packit 6c4009
    case DEVPTS_SUPER_MAGIC:
Packit 6c4009
    case EFS_SUPER_MAGIC:
Packit 6c4009
    case EFS_MAGIC:
Packit 6c4009
    case MSDOS_SUPER_MAGIC:
Packit 6c4009
    case NTFS_SUPER_MAGIC:
Packit 6c4009
    case QNX4_SUPER_MAGIC:
Packit 6c4009
    case ROMFS_SUPER_MAGIC:
Packit 6c4009
      /* No symlink support.  */
Packit 6c4009
      return 0;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Used like: return __statfs_chown_restricted (__statfs (name, &buf), &buf;;*/
Packit 6c4009
long int
Packit 6c4009
__statfs_chown_restricted (int result, const struct statfs *fsbuf)
Packit 6c4009
{
Packit 6c4009
  if (result < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	/* Not possible, return the default value.  */
Packit 6c4009
	return 1;
Packit 6c4009
Packit 6c4009
      /* Some error occured.  */
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 1;
Packit 6c4009
}