Re: [PATCH 2/4] dirent->d_type not available on Soalris
authorAustin Clements <amdragon@MIT.EDU>
Wed, 11 Apr 2012 19:36:09 +0000 (15:36 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:46:15 +0000 (09:46 -0800)
11/87f65d438d88f7079d5e368971639abc88c33e [new file with mode: 0644]

diff --git a/11/87f65d438d88f7079d5e368971639abc88c33e b/11/87f65d438d88f7079d5e368971639abc88c33e
new file mode 100644 (file)
index 0000000..6e0fc7f
--- /dev/null
@@ -0,0 +1,213 @@
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 199B4431FAF\r
+       for <notmuch@notmuchmail.org>; Wed, 11 Apr 2012 12:36:14 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id 0yjZUp1Ugt2q for <notmuch@notmuchmail.org>;\r
+       Wed, 11 Apr 2012 12:36:13 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU\r
+       [18.7.68.37])\r
+       by olra.theworths.org (Postfix) with ESMTP id 3F5CC431FAE\r
+       for <notmuch@notmuchmail.org>; Wed, 11 Apr 2012 12:36:13 -0700 (PDT)\r
+X-AuditID: 12074425-b7f4a6d0000008e0-04-4f85dd2c59a8\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+       by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id A1.33.02272.C2DD58F4; Wed, 11 Apr 2012 15:36:12 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id q3BJaB68025613; \r
+       Wed, 11 Apr 2012 15:36:11 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q3BJa97I025836\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Wed, 11 Apr 2012 15:36:10 -0400 (EDT)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1SI3Kr-0000q7-Kj; Wed, 11 Apr 2012 15:36:09 -0400\r
+Date: Wed, 11 Apr 2012 15:36:09 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Vladimir.Marek@oracle.com\r
+Subject: Re: [PATCH 2/4] dirent->d_type not available on Soalris\r
+Message-ID: <20120411193609.GC13549@mit.edu>\r
+References:\r
+ <CAMoJFUvHbSfXLTGKf2ZZnaLpLzZiHJ7W_ZUYZzm70n5mw4r1Kg@mail.gmail.com>\r
+       <1333989127-21523-1-git-send-email-Vladimir.Marek@oracle.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <1333989127-21523-1-git-send-email-Vladimir.Marek@oracle.com>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFprFKsWRmVeSWpSXmKPExsUixG6noqtzt9XfoPONicX1mzOZLTpu72az\r
+       mHF+F4sDs8ezVbeYPT4+vcXisezoT8YA5igum5TUnMyy1CJ9uwSujKtz1jEVTFCqmLTrAXMD\r
+       4zGpLkZODgkBE4lzk3YzQdhiEhfurWfrYuTiEBLYxyhxdf1bJghnA6PEr/7JjBDOSSaJee2f\r
+       WCGcJYwSf//cBirj4GARUJWYdUwFZBSbgIbEtv3LGUFsEQFZif9TPoGtYBawk5j24hiYLSzg\r
+       IPHv0Sx2EJtXQEfixcdHUNsmMEqcPnQbKiEocXLmExaIZi2JG/9egu1iFpCWWP6PAyTMKeAl\r
+       8WX7cjYQW1RARWLKyW1sExiFZiHpnoWkexZC9wJG5lWMsim5Vbq5iZk5xanJusXJiXl5qUW6\r
+       Fnq5mSV6qSmlmxjBwe6iuoNxwiGlQ4wCHIxKPLy7JrT6C7EmlhVX5h5ilORgUhLlXXcVKMSX\r
+       lJ9SmZFYnBFfVJqTWnyIUYKDWUmE12V2i78Qb0piZVVqUT5MSpqDRUmcV1PrnZ+QQHpiSWp2\r
+       ampBahFMVoaDQ0mC98QdoKGCRanpqRVpmTklCGkmDk6Q4TxAw5eD1PAWFyTmFmemQ+RPMepy\r
+       PD/Ue4VRiCUvPy9VSpx3OkiRAEhRRmke3BxYknrFKA70ljDvCpAqHmCCg5v0CmgJE9CSz5NB\r
+       PiguSURISTUwng9XyfqRwr/u1qE7n+8sr7v0Zq/SwkUBkjqZ8YrlD5Xmd7/jdvtUvtxnbvt3\r
+       95ORvDdvTFixWJrJeWdCnejMjnDGBfs/bL3EtDRuLZfQbIeUr/+zvTT+F3uuYl/IV8fdlma4\r
+       sGKq9IW28MIbDD1friudDVmlk78v13nm9S/24vcLmjzEl+8+rMRSnJFoqMVcVJwIADzy/Iwt        AwAA\r
+Cc: notmuch@notmuchmail.org, Vladimir Marek <vlmarek@volny.cz>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 11 Apr 2012 19:36:14 -0000\r
+\r
+Correct me if I'm mistaken, but d_name will only be a basename, so\r
+your calls to stat will fail for files that are not in the current\r
+directory.  I think in all of the situations you had to call stat, we\r
+already construct the absolute path of the file (sometimes a little\r
+later in the code, but it can easily be moved), so this should be easy\r
+to fix.\r
+\r
+Rather than sprinkling portability code throughout notmuch-new, it\r
+seems like it would be simpler if this logic were wrapped in a\r
+separate function.  For example, something along the (completely\r
+untested) lines of,\r
+\r
+static mode_t\r
+dirent_type (const struct *entry, const char *abspath)\r
+{\r
+    struct stat statbuf;\r
+#ifdef _DIRENT_HAVE_D_TYPE\r
+    static const mode_t modes[] = {\r
+       [DT_BLK]  = S_IFBLK,\r
+       [DT_CHR]  = S_IFCHR,\r
+       [DT_DIR]  = S_IFDIR,\r
+       [DT_FIFO] = S_IFIFO,\r
+       [DT_LNK]  = S_IFLNK,\r
+       [DT_REG]  = S_IFREG,\r
+       [DT_SOCK] = S_IFSOCK\r
+    };\r
+    if (entry->d_type >= 0 && entry->d_type < sizeof(modes)/sizeof(modes[0]) &&\r
+       modes[entry->d_type])\r
+       return modes[entry->d_type];\r
+#endif\r
+\r
+    if (stat(abspath, &statbuf) == -1)\r
+       return -1;\r
+    return statbuf.st_mode & S_IFMT;\r
+}\r
+\r
+This has the added benefit of correctly handling DT_UNKNOWN, which we\r
+currently don't.\r
+\r
+Instead of taking the absolute path of the file, this could take the\r
+absolute path of the containing directory and construct the full path\r
+from that and d_name; that would probably be a nicer interface, but it\r
+would be redundant computation.\r
+\r
+Quoth Vladimir.Marek@oracle.com on Apr 09 at  6:32 pm:\r
+> From: Vladimir Marek <vlmarek@volny.cz>\r
+> \r
+> The inspiration was taken from similar issue in mutt:\r
+> http://does-not-exist.org/mail-archives/mutt-dev/msg11290.html\r
+> \r
+> Signed-off-by: Vladimir Marek <vlmarek@volny.cz>\r
+> ---\r
+>  notmuch-new.c |   28 ++++++++++++++++++++++++++++\r
+>  1 files changed, 28 insertions(+), 0 deletions(-)\r
+> \r
+> diff --git a/notmuch-new.c b/notmuch-new.c\r
+> index 4f13535..3d265bd 100644\r
+> --- a/notmuch-new.c\r
+> +++ b/notmuch-new.c\r
+> @@ -21,6 +21,9 @@\r
+>  #include "notmuch-client.h"\r
+>  \r
+>  #include <unistd.h>\r
+> +#ifndef _DIRENT_HAVE_D_TYPE\r
+> +#include <sys/types.h>\r
+> +#endif\r
+>  \r
+>  typedef struct _filename_node {\r
+>      char *filename;\r
+> @@ -167,7 +170,14 @@ _entries_resemble_maildir (struct dirent **entries, int count)\r
+>      int i, found = 0;\r
+>  \r
+>      for (i = 0; i < count; i++) {\r
+> +#ifdef _DIRENT_HAVE_D_TYPE\r
+>      if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)\r
+> +#else\r
+> +    struct stat statbuf;\r
+> +    if (stat(entries[i]->d_name, &statbuf) == -1)\r
+> +            continue;\r
+> +    if (! S_ISDIR(statbuf.st_mode))\r
+> +#endif\r
+>          continue;\r
+>  \r
+>      if (strcmp(entries[i]->d_name, "new") == 0 ||\r
+> @@ -258,6 +268,9 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+>      struct stat st;\r
+>      notmuch_bool_t is_maildir, new_directory;\r
+>      const char **tag;\r
+> +#ifndef _DIRENT_HAVE_D_TYPE\r
+> +    struct stat statbuf;\r
+> +#endif\r
+>  \r
+>      if (stat (path, &st)) {\r
+>      fprintf (stderr, "Error reading directory %s: %s\n",\r
+> @@ -328,9 +341,16 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+>       * scandir results, then it might be a directory (and if not,\r
+>       * then we'll stat and return immediately in the next level of\r
+>       * recursion). */\r
+> +#ifdef _DIRENT_HAVE_D_TYPE\r
+>      if (entry->d_type != DT_DIR &&\r
+>          entry->d_type != DT_LNK &&\r
+>          entry->d_type != DT_UNKNOWN)\r
+> +#else\r
+> +    if (stat(entry->d_name, &statbuf) == -1)\r
+> +            continue;\r
+> +    if (!(statbuf.st_mode & S_IFDIR) &&\r
+> +        !(statbuf.st_mode & S_IFLNK))\r
+> +#endif\r
+>      {\r
+>          continue;\r
+>      }\r
+> @@ -427,7 +447,11 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+>       *\r
+>       * In either case, a stat does the trick.\r
+>       */\r
+> +#ifdef _DIRENT_HAVE_D_TYPE\r
+>      if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {\r
+> +#else\r
+> +    if (stat(entry->d_name, &statbuf) == -1 || statbuf.st_mode & S_IFLNK) {\r
+> +#endif\r
+>          int err;\r
+>  \r
+>          next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
+> @@ -443,7 +467,11 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+>  \r
+>          if (! S_ISREG (st.st_mode))\r
+>              continue;\r
+> +#ifdef _DIRENT_HAVE_D_TYPE\r
+>      } else if (entry->d_type != DT_REG) {\r
+> +#else\r
+> +    } else if (statbuf.st_mode & S_IFREG) {\r
+> +#endif\r
+>          continue;\r
+>      }\r
+>  \r