From 8ea92e06baf73d87a90ca197581cfc31d4674257 Mon Sep 17 00:00:00 2001
From: Andreas Dilger <adilger@whamcloud.com>
Date: Thu, 27 Jun 2019 02:25:55 -0600
Subject: [PATCH] stat: don't explicitly request file size for filenames

When calling 'stat -c %N' to print the filename, don't explicitly
request the size of the file via statx(), as it may add overhead on
some filesystems.  The size is only needed to optimize an allocation
for the relatively rare case of reading a symlink name, and the worst
effect is a somewhat-too-large temporary buffer may be allocated for
areadlink_with_size(), or internal retries if buffer is too small.

The file size will be returned by statx() on most filesystems, even
if not requested, unless the filesystem considers this to be too
expensive for that file, in which case the tradeoff is worthwhile.

* src/stat.c: Don't explicitly request STATX_SIZE for filenames.
Start with a 1KB buffer for areadlink_with_size() if st_size unset.
---
 bootstrap.conf | 0
 src/stat.c     | 8 +++++---
 2 files changed, 5 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 bootstrap.conf

diff --git a/bootstrap.conf b/bootstrap.conf
old mode 100644
new mode 100755
diff --git a/src/stat.c b/src/stat.c
index ec0bb7de4..56e114893 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -1282,7 +1282,7 @@ fmt_to_mask (char fmt)
   switch (fmt)
     {
     case 'N':
-      return STATX_MODE|STATX_SIZE;
+      return STATX_MODE;
     case 'd':
     case 'D':
       return STATX_MODE;
@@ -1354,7 +1354,7 @@ do_stat (char const *filename, char const *format, char const *format2)
   int fd = STREQ (filename, "-") ? 0 : AT_FDCWD;
   int flags = 0;
   struct stat st;
-  struct statx stx;
+  struct statx stx = { 0 };
   const char *pathname = filename;
   struct print_args pa;
   pa.st = &st;
@@ -1491,7 +1491,9 @@ print_stat (char *pformat, size_t prefix_len, unsigned int m,
       out_string (pformat, prefix_len, quoteN (filename));
       if (S_ISLNK (statbuf->st_mode))
         {
-          char *linkname = areadlink_with_size (filename, statbuf->st_size);
+          /* if statx() didn't set size, almost all symlinks are under 1KB */
+          char *linkname = areadlink_with_size (filename, statbuf->st_size ?
+						statbuf->st_size : 1023);
           if (linkname == NULL)
             {
               error (0, errno, _("cannot read symbolic link %s"),
-- 
2.20.1 (Apple Git-117)

