summaryrefslogtreecommitdiff
path: root/lib/savedirinfo.c
blob: 0d4b3464572851b8de5881297be5ad27fbd2283c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* savedirinfo.c -- Save the list of files in a directory, with additional information.

   Copyright 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2010,
   2011 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/* Written by James Youngman, <jay@gnu.org>. */
/* Derived from savedir.c, written by David MacKenzie <djm@gnu.org>. */

/* config.h must be included first. */
#include <config.h>

/* system headers. */
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

/* gnulib headers. */
#include "dirent-safer.h"
#include "xalloc.h"

/* find headers. */
#include "extendbuf.h"
#include "savedirinfo.h"

#ifdef CLOSEDIR_VOID
/* Fake a return value. */
# define CLOSEDIR(d) (closedir (d), 0)
#else
# define CLOSEDIR(d) closedir (d)
#endif

#if defined HAVE_STRUCT_DIRENT_D_TYPE
/* Convert the value of struct dirent.d_type into a value for
 * struct stat.st_mode (at least the file type bits), or zero
 * if the type is DT_UNKNOWN or is a value we don't know about.
 */
static mode_t
type_to_mode (unsigned type)
{
  switch (type)
    {
#ifdef DT_FIFO
    case DT_FIFO: return S_IFIFO;
#endif
#ifdef DT_CHR
    case DT_CHR:  return S_IFCHR;
#endif
#ifdef DT_DIR
    case DT_DIR:  return S_IFDIR;
#endif
#ifdef DT_BLK
    case DT_BLK:  return S_IFBLK;
#endif
#ifdef DT_REG
    case DT_REG:  return S_IFREG;
#endif
#ifdef DT_LNK
    case DT_LNK:  return S_IFLNK;
#endif
#ifdef DT_SOCK
    case DT_SOCK: return S_IFSOCK;
#endif
    default:
      return 0;			/* Unknown. */
    }
}
#endif

struct new_savedir_direntry_internal
{
  int    flags;			/* from SaveDirDataFlags */
  mode_t type_info;
  size_t buffer_offset;
};



static int
savedir_cmp (const void *p1, const void *p2)
{
  const struct savedir_direntry *de1, *de2;
  de1 = p1;
  de2 = p2;
  return strcmp (de1->name, de2->name); /* POSIX order, not locale order. */
}


static struct savedir_direntry*
convertentries (const struct savedir_dirinfo *info,
		struct new_savedir_direntry_internal *internal)
{
  char *p = info->buffer;
  struct savedir_direntry *result;
  int n =info->size;
  int i;


  result = xmalloc (sizeof (*result) * info->size);

  for (i=0; i<n; ++i)
    {
      result[i].flags = internal[i].flags;
      result[i].type_info = internal[i].type_info;
      result[i].name = &p[internal[i].buffer_offset];
    }
  return result;
}


struct savedir_dirinfo *
xsavedir (const char *dir, int flags)
{
  DIR *dirp;
  struct dirent *dp;
  struct savedir_dirinfo *result = NULL;
  struct new_savedir_direntry_internal *internal;

  size_t namebuf_allocated = 0u, namebuf_used = 0u;
  size_t entrybuf_allocated = 0u;
  int save_errno;

  dirp = opendir_safer (dir);
  if (dirp == NULL)
    return NULL;

  errno = 0;
  result = xmalloc (sizeof (*result));
  result->buffer = NULL;
  result->size = 0u;
  result->entries = NULL;
  internal = NULL;

  while ((dp = readdir (dirp)) != NULL)
    {
      /* Skip "", ".", and "..".  "" is returned by at least one buggy
         implementation: Solaris 2.4 readdir on NFS file systems.  */
      char const *entry = dp->d_name;
      if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
	{
	  /* Remember the name. */
	  size_t entry_size = strlen (entry) + 1;
	  result->buffer = xextendbuf (result->buffer, namebuf_used+entry_size, &namebuf_allocated);
	  memcpy ((result->buffer) + namebuf_used, entry, entry_size);

	  /* Remember the other stuff. */
	  internal = xextendbuf (internal, (1+result->size)*sizeof (*internal), &entrybuf_allocated);
	  internal[result->size].flags = 0;

	  internal[result->size].type_info = 0;
#if defined HAVE_STRUCT_DIRENT_D_TYPE
	  internal[result->size].type_info = type_to_mode (dp->d_type);
	  if (dp->d_type != DT_UNKNOWN)
	    internal[result->size].flags |= SavedirHaveFileType;
#endif
	  internal[result->size].buffer_offset = namebuf_used;

	  /* Prepare for the next iteration */
	  ++(result->size);
	  namebuf_used += entry_size;
	}
    }

  result->buffer = xextendbuf (result->buffer, namebuf_used+1, &namebuf_allocated);
  result->buffer[namebuf_used] = '\0';

  /* convert the result to its externally-usable form. */
  result->entries = convertentries (result, internal);
  free (internal);
  internal = NULL;


  if (flags & SavedirSort)
    {
      qsort (result->entries,
	     result->size, sizeof (*result->entries),
	     savedir_cmp);
    }


  save_errno = errno;
  if (CLOSEDIR (dirp) != 0)
    save_errno = errno;
  if (save_errno != 0)
    {
      free (result->buffer);
      free (result);
      errno = save_errno;
      return NULL;
    }

  return result;
}

void free_dirinfo (struct savedir_dirinfo *p)
{
  free (p->entries);
  p->entries = NULL;
  free (p->buffer);
  p->buffer = NULL;
  free (p);
}



char *
savedirinfo (const char *dir, struct savedir_extrainfo **extra)
{
  struct savedir_dirinfo *p = xsavedir (dir, SavedirSort);
  char *buf, *s;
  size_t bufbytes = 0;
  unsigned int i;

  if (p)
    {
      struct savedir_extrainfo *pex = xmalloc (p->size * sizeof (*extra));
      for (i=0; i<p->size; ++i)
	{
	  bufbytes += strlen (p->entries[i].name);
	  ++bufbytes;		/* the \0 */

	  pex[i].type_info = p->entries[i].type_info;
	}

      s = buf = xmalloc (bufbytes+1);
      for (i=0; i<p->size; ++i)
	{
	  size_t len = strlen (p->entries[i].name);
	  memcpy (s, p->entries[i].name, len);
	  s += len;
	  *s = 0;		/* Place a NUL */
	  ++s;			/* Skip the NUL. */
	}
      *s = 0;			/* final (doubled) terminating NUL */

      if (extra)
	*extra = pex;
      else
	free (pex);
      return buf;
    }
  else
    {
      return NULL;
    }
}