Re: notmuch for documents
[notmuch-archives.git] / 1e / 5a5d77537b8ab983e3b4ab5c1eb3b9d618420d
1 Return-Path: <tomc@caurea.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id C7EB94196F2\r
6         for <notmuch@notmuchmail.org>; Tue, 13 Apr 2010 11:53:53 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.9\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.9 tagged_above=-999 required=5\r
12         tests=[BAYES_00=-1.9] autolearn=ham\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 9wdXvOH33o7V for <notmuch@notmuchmail.org>;\r
16         Tue, 13 Apr 2010 11:53:52 -0700 (PDT)\r
17 Received: from scotch.caurea.org (ks311007.kimsufi.com [188.165.197.188])\r
18         by olra.theworths.org (Postfix) with ESMTP id 879D1431FC1\r
19         for <notmuch@notmuchmail.org>; Tue, 13 Apr 2010 11:53:52 -0700 (PDT)\r
20 Received: by scotch.caurea.org (Postfix, from userid 101)\r
21         id 9FE9012B8C0; Tue, 13 Apr 2010 20:54:05 +0200 (CEST)\r
22 From: Tomas Carnecky <tom@dbservice.com>\r
23 To: notmuch@notmuchmail.org\r
24 Subject: [PATCH] Use wrapper functions to find the type of struct dirent\r
25 Date: Tue, 13 Apr 2010 20:54:03 +0200\r
26 Message-Id: <1271184843-3373-1-git-send-email-tom@dbservice.com>\r
27 X-Mailer: git-send-email 1.7.0.5\r
28 X-BeenThere: notmuch@notmuchmail.org\r
29 X-Mailman-Version: 2.1.13\r
30 Precedence: list\r
31 List-Id: "Use and development of the notmuch mail system."\r
32         <notmuch.notmuchmail.org>\r
33 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
34         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
35 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
36 List-Post: <mailto:notmuch@notmuchmail.org>\r
37 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
38 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
39         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
40 X-List-Received-Date: Tue, 13 Apr 2010 18:53:53 -0000\r
41 \r
42 Some platforms don't provide DT_REG/DT_LNK/DT_DIR. Create wrapper functions\r
43 which test the presence of those symbols and fall back to stat(2).\r
44 \r
45 ---\r
46 Not sure if I got the handling of DT_UNKNOWN correct in all cases. Someone\r
47 please double-check that.\r
48 \r
49  notmuch-new.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++------\r
50  1 files changed, 62 insertions(+), 8 deletions(-)\r
51 \r
52 diff --git a/notmuch-new.c b/notmuch-new.c\r
53 index 44b50aa..95ddddb 100644\r
54 --- a/notmuch-new.c\r
55 +++ b/notmuch-new.c\r
56 @@ -140,6 +140,62 @@ dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)\r
57      return strcmp ((*a)->d_name, (*b)->d_name);\r
58  }\r
59  \r
60 +/* Helper functions to test if a given dirent is of a certain type\r
61 + */\r
62 +static int\r
63 +_is_reg(const char *path, struct dirent *entry)\r
64 +{\r
65 +#ifdef DT_REG\r
66 +    if (entry->d_type == DT_REG)\r
67 +        return 1;\r
68 +#endif\r
69 +\r
70 +    char buffer[PATH_MAX];\r
71 +    snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);\r
72 +\r
73 +    struct stat sbuf;\r
74 +    if (!stat(buffer, &sbuf))\r
75 +        return S_ISREG(sbuf.st_mode);\r
76 +\r
77 +    return 0;\r
78 +}\r
79 +\r
80 +static int\r
81 +_is_dir(const char *path, struct dirent *entry)\r
82 +{\r
83 +#ifdef DT_DIR\r
84 +    if (entry->d_type == DT_DIR)\r
85 +        return 1;\r
86 +#endif\r
87 +\r
88 +    char buffer[PATH_MAX];\r
89 +    snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);\r
90 +\r
91 +    struct stat sbuf;\r
92 +    if (!stat(buffer, &sbuf))\r
93 +        return S_ISDIR(sbuf.st_mode);\r
94 +\r
95 +    return 0;\r
96 +}\r
97 +\r
98 +static int\r
99 +_is_lnk(const char *path, struct dirent *entry)\r
100 +{\r
101 +#ifdef DT_LNK\r
102 +    if (entry->d_type == DT_LNK)\r
103 +        return 1;\r
104 +#endif\r
105 +\r
106 +    char buffer[PATH_MAX];\r
107 +    snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);\r
108 +\r
109 +    struct stat sbuf;\r
110 +    if (!stat(buffer, &sbuf))\r
111 +        return S_ISLNK(sbuf.st_mode);\r
112 +\r
113 +    return 0;\r
114 +}\r
115 +\r
116  /* Test if the directory looks like a Maildir directory.\r
117   *\r
118   * Search through the array of directory entries to see if we can find all\r
119 @@ -148,12 +204,12 @@ dirent_sort_strcmp_name (const struct dirent **a, const struct dirent **b)\r
120   * Return 1 if the directory looks like a Maildir and 0 otherwise.\r
121   */\r
122  static int\r
123 -_entries_resemble_maildir (struct dirent **entries, int count)\r
124 +_entries_resemble_maildir (const char *path, struct dirent **entries, int count)\r
125  {\r
126      int i, found = 0;\r
127  \r
128      for (i = 0; i < count; i++) {\r
129 -       if (entries[i]->d_type != DT_DIR && entries[i]->d_type != DT_UNKNOWN)\r
130 +       if (!_is_dir(path, entries[i]))\r
131             continue;\r
132  \r
133         if (strcmp(entries[i]->d_name, "new") == 0 ||\r
134 @@ -265,7 +321,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
135      }\r
136  \r
137      /* Pass 1: Recurse into all sub-directories. */\r
138 -    is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);\r
139 +    is_maildir = _entries_resemble_maildir (path, fs_entries, num_fs_entries);\r
140  \r
141      for (i = 0; i < num_fs_entries; i++) {\r
142         if (interrupted)\r
143 @@ -280,9 +336,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
144          * scandir results, then it might be a directory (and if not,\r
145          * then we'll stat and return immediately in the next level of\r
146          * recursion). */\r
147 -       if (entry->d_type != DT_DIR &&\r
148 -           entry->d_type != DT_LNK &&\r
149 -           entry->d_type != DT_UNKNOWN)\r
150 +       if (!_is_dir(path, entry) && !_is_lnk(path, entry))\r
151         {\r
152             continue;\r
153         }\r
154 @@ -360,7 +414,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
155          *\r
156          * In either case, a stat does the trick.\r
157          */\r
158 -       if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {\r
159 +       if (_is_lnk(path, entry)) {\r
160             int err;\r
161  \r
162             next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
163 @@ -376,7 +430,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
164  \r
165             if (! S_ISREG (st.st_mode))\r
166                 continue;\r
167 -       } else if (entry->d_type != DT_REG) {\r
168 +       } else if (!_is_reg(path, entry)) {\r
169             continue;\r
170         }\r
171  \r
172 -- \r
173 1.7.0.5\r
174 \r