root/maint/gnulib/lib/same.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. same_name
  2. same_nameat

   1 /* Determine whether two file names refer to the same file.
   2 
   3    Copyright (C) 1997-2000, 2002-2006, 2009-2021 Free Software Foundation, Inc.
   4 
   5    This program is free software: you can redistribute it and/or modify
   6    it under the terms of the GNU General Public License as published by
   7    the Free Software Foundation; either version 3 of the License, or
   8    (at your option) any later version.
   9 
  10    This program 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
  13    GNU General Public License for more details.
  14 
  15    You should have received a copy of the GNU General Public License
  16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 /* written by Jim Meyering */
  19 
  20 #include <config.h>
  21 
  22 #include <fcntl.h>
  23 #include <stdbool.h>
  24 #include <stdio.h>
  25 #include <unistd.h>
  26 #include <stdlib.h>
  27 #include <sys/types.h>
  28 #include <sys/stat.h>
  29 #include <ctype.h>
  30 #include <errno.h>
  31 
  32 #include <string.h>
  33 
  34 #include <limits.h>
  35 #ifndef _POSIX_NAME_MAX
  36 # define _POSIX_NAME_MAX 14
  37 #endif
  38 
  39 #include "same.h"
  40 #include "dirname.h"
  41 #include "error.h"
  42 #include "same-inode.h"
  43 
  44 #ifndef MIN
  45 # define MIN(a, b) ((a) < (b) ? (a) : (b))
  46 #endif
  47 
  48 /* Whether file name components are silently truncated (behavior that
  49    POSIX stopped allowing in 2008).  This enables checks whether
  50    truncated base names are the same, while checking the directories.  */
  51 #if !_POSIX_NO_TRUNC && HAVE_FPATHCONF && defined _PC_NAME_MAX
  52 # define CHECK_TRUNCATION true
  53 #else
  54 # define CHECK_TRUNCATION false
  55 #endif
  56 
  57 /* Return nonzero if SOURCE and DEST point to the same name in the same
  58    directory.  */
  59 
  60 bool
  61 same_name (const char *source, const char *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63   return same_nameat (AT_FDCWD, source, AT_FDCWD, dest);
  64 }
  65 
  66 /* Likewise, but interpret the file names relative to SOURCE_FD and DEST_FD,
  67    in the style of openat.  */
  68 
  69 bool
  70 same_nameat (int source_dfd, char const *source,
     /* [previous][next][first][last][top][bottom][index][help] */
  71              int dest_dfd, char const *dest)
  72 {
  73   /* Compare the basenames.  */
  74   char const *source_basename = last_component (source);
  75   char const *dest_basename = last_component (dest);
  76   size_t source_baselen = base_len (source_basename);
  77   size_t dest_baselen = base_len (dest_basename);
  78   bool identical_basenames =
  79     (source_baselen == dest_baselen
  80      && memcmp (source_basename, dest_basename, dest_baselen) == 0);
  81   bool compare_dirs = identical_basenames;
  82   bool same = false;
  83 
  84 #if CHECK_TRUNCATION
  85   size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX;
  86   size_t min_baselen = MIN (source_baselen, dest_baselen);
  87   if (slen_max <= min_baselen
  88       && memcmp (source_basename, dest_basename, slen_max) == 0)
  89     compare_dirs = true;
  90 #endif
  91 
  92   if (compare_dirs)
  93     {
  94       struct stat source_dir_stats;
  95       struct stat dest_dir_stats;
  96 
  97       /* Compare the parent directories (via the device and inode numbers).  */
  98       char *source_dirname = dir_name (source);
  99       int flags = AT_SYMLINK_NOFOLLOW;
 100       if (fstatat (source_dfd, source_dirname, &source_dir_stats, flags) != 0)
 101         {
 102           /* Shouldn't happen.  */
 103           error (1, errno, "%s", source_dirname);
 104         }
 105       free (source_dirname);
 106 
 107       char *dest_dirname = dir_name (dest);
 108 
 109 #if CHECK_TRUNCATION
 110       int destdir_errno = 0;
 111       int open_flags = O_SEARCH | O_CLOEXEC | O_DIRECTORY;
 112       int destdir_fd = openat (dest_dfd, dest_dirname, open_flags);
 113       if (destdir_fd < 0 || fstat (destdir_fd, &dest_dir_stats) != 0)
 114         destdir_errno = errno;
 115       else if (SAME_INODE (source_dir_stats, dest_dir_stats))
 116         {
 117           same = identical_basenames;
 118           if (! same)
 119             {
 120               errno = 0;
 121               long name_max = fpathconf (destdir_fd, _PC_NAME_MAX);
 122               if (name_max < 0)
 123                 destdir_errno = errno;
 124               else
 125                 same = (name_max <= min_baselen
 126                         && (memcmp (source_basename, dest_basename, name_max)
 127                             == 0));
 128             }
 129         }
 130       close (destdir_fd);
 131       if (destdir_errno != 0)
 132         {
 133           /* Shouldn't happen.  */
 134           error (1, destdir_errno, "%s", dest_dirname);
 135         }
 136 #else
 137       if (fstatat (dest_dfd, dest_dirname, &dest_dir_stats, flags) != 0)
 138         {
 139           /* Shouldn't happen.  */
 140           error (1, errno, "%s", dest_dirname);
 141         }
 142       same = SAME_INODE (source_dir_stats, dest_dir_stats);
 143 #endif
 144 
 145       free (dest_dirname);
 146     }
 147 
 148   return same;
 149 }

/* [previous][next][first][last][top][bottom][index][help] */