From: Jim Meyering Date: Mon, 18 Aug 2003 07:47:40 +0000 (+0000) Subject: Include , . X-Git-Tag: COREUTILS-5_0_91~167 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0fc380c89d65822d8f72f4946269e77e92ece67b;p=platform%2Fupstream%2Fcoreutils.git Include , . (_POSIX_NAME_MAX): Define if not defined. (MIN): New macro. (same_name): If file names are silently truncated, report that the file names are the same if they are the same after the silent truncation. --- diff --git a/lib/same.c b/lib/same.c index d41cccc..c0b9244 100644 --- a/lib/same.c +++ b/lib/same.c @@ -21,6 +21,7 @@ # include #endif +#include #include #ifdef HAVE_UNISTD_H # include @@ -42,6 +43,11 @@ extern int errno; # include #endif +#include +#ifndef _POSIX_NAME_MAX +# define _POSIX_NAME_MAX 14 +#endif + #include "same.h" #include "dirname.h" #include "error.h" @@ -54,6 +60,8 @@ extern int errno; void free (); #endif +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + #define SAME_INODE(Stat_buf_1, Stat_buf_2) \ ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \ && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev) @@ -69,9 +77,24 @@ same_name (const char *source, const char *dest) char const *dest_basename = base_name (dest); size_t source_baselen = base_len (source_basename); size_t dest_baselen = base_len (dest_basename); + bool identical_basenames = + (source_baselen == dest_baselen + && memcmp (source_basename, dest_basename, dest_baselen) == 0); + bool compare_dirs = identical_basenames; + bool same = false; + +#if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX + /* This implementation silently truncates pathname components. If + the base names might be truncated, check whether the truncated + base names are the same, while checking the directories. */ + size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX; + size_t min_baselen = MIN (source_baselen, dest_baselen); + if (slen_max <= min_baselen + && memcmp (source_basename, dest_basename, slen_max) == 0) + compare_dirs = true; +#endif - if (source_baselen == dest_baselen - && memcmp (source_basename, dest_basename, dest_baselen) == 0) + if (compare_dirs) { struct stat source_dir_stats; struct stat dest_dir_stats; @@ -93,12 +116,30 @@ same_name (const char *source, const char *dest) error (1, errno, "%s", dest_dirname); } + same = SAME_INODE (source_dir_stats, dest_dir_stats); + +#if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX + if (same && ! identical_basenames) + { + long name_max = (errno = 0, pathconf (source_dirname, _PC_NAME_MAX)); + if (name_max < 0) + { + if (errno) + { + /* Shouldn't happen. */ + error (1, errno, "%s", source_dirname); + } + same = false; + } + else + same = (name_max <= min_baselen + && memcmp (source_basename, dest_basename, name_max) == 0); + } +#endif + free (source_dirname); free (dest_dirname); - - if (SAME_INODE (source_dir_stats, dest_dir_stats)) - return 1; } - return 0; + return same; }