aboutsummaryrefslogtreecommitdiff
blob: c94cae151ad679a63522b7c4c27483e66587876b (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * file.c
 *
 * Miscellaneous file related macro's and functions.
 *
 * Copyright 1999-2008 Gentoo Foundation
 * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
 * Licensed under the GPL-2
 */

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>

#include "rcscripts/rcutil.h"

bool
rc_file_exists (const char *pathname)
{
  struct stat buf;
  int retval;

  if (!check_str (pathname))
    return FALSE;

  retval = lstat (pathname, &buf);
  if (-1 != retval)
    retval = TRUE;
  else
    retval = FALSE;

  return retval;
}

bool
rc_is_file (const char *pathname, bool follow_link)
{
  struct stat buf;
  int retval;

  if (!check_str (pathname))
    return FALSE;

  retval = follow_link ? stat (pathname, &buf) : lstat (pathname, &buf);
  if ((-1 != retval) && (S_ISREG (buf.st_mode)))
    retval = TRUE;
  else
    retval = FALSE;

  return retval;
}

bool
rc_is_link (const char *pathname)
{
  struct stat buf;
  int retval;

  if (!check_str (pathname))
    return FALSE;

  retval = lstat (pathname, &buf);
  if ((-1 != retval) && (S_ISLNK (buf.st_mode)))
    retval = TRUE;
  else
    retval = FALSE;

  return retval;
}

bool
rc_is_dir (const char *pathname, bool follow_link)
{
  struct stat buf;
  int retval;

  if (!check_str (pathname))
    return FALSE;

  retval = follow_link ? stat (pathname, &buf) : lstat (pathname, &buf);
  if ((-1 != retval) && (S_ISDIR (buf.st_mode)))
    retval = TRUE;
  else
    retval = FALSE;

  return retval;
}

off_t
rc_get_size (const char *pathname, bool follow_link)
{
  struct stat buf;
  int retval;

  if (!check_str (pathname))
    return 0;

  retval = follow_link ? stat (pathname, &buf) : lstat (pathname, &buf);
  if (-1 != retval)
    retval = buf.st_size;
  else
    retval = 0;

  return retval;
}

time_t
rc_get_mtime (const char *pathname, bool follow_link)
{
  struct stat buf;
  int retval;

  if (!check_str (pathname))
    return 0;

  retval = follow_link ? stat (pathname, &buf) : lstat (pathname, &buf);
  if (-1 != retval)
    retval = buf.st_mtime;
  else
    retval = 0;

  return retval;
}

#if !defined(HAVE_REMOVE)
int
remove (const char *pathname)
{
  int retval;

  if (!check_arg_str (pathname))
    return -1;

  if (rc_is_dir (pathname, FALSE))
    retval = rmdir (pathname);
  else
    retval = unlink (pathname);

  if (0 != errno)
    {
      rc_errno_set (errno);
      DBG_MSG ("Failed to remove() '%s'!\n", pathname);
    }

  return retval;
}
#endif

int
rc_mktree (const char *pathname, mode_t mode)
{
  char *temp_name = NULL;
  char *temp_token = NULL;
  char *token_p;
  char *token;
  int retval;
  int lenght;

  if (!check_arg_str (pathname))
    return -1;

  /* Lenght of 'pathname' + extra for "./" if needed */
  lenght = strlen (pathname) + 2;
  /* lenght + '\0' */
  temp_name = xmalloc (lenght + 1);
  if (NULL == temp_name)
    return -1;

  temp_token = xstrndup (pathname, strlen (pathname));
  if (NULL == temp_token)
    goto error;

  token_p = temp_token;

  if (pathname[0] == '/')
    temp_name[0] = '\0';
  else
    /* If not an absolute path, make it local */
    strncpy (temp_name, ".", lenght);

  token = strsep (&token_p, "/");
  /* First token might be "", but that is OK as it will be when the
   * pathname starts with '/' */
  while (NULL != token)
    {
      strncat (temp_name, "/", lenght - strlen (temp_name));
      strncat (temp_name, token, lenght - strlen (temp_name));

      /* If it does not exist, create the dir.  If it does exit,
       * but is not a directory, we will catch it below. */
      if (!rc_file_exists (temp_name))
	{
	  retval = mkdir (temp_name, mode);
	  if (-1 == retval)
	    {
	      rc_errno_set (errno);
	      DBG_MSG ("Failed to create directory '%s'!\n", temp_name);
	      goto error;
	    }
	  /* Not a directory or symlink pointing to a directory */
	}
      else if (!rc_is_dir (temp_name, TRUE))
	{
	  rc_errno_set (ENOTDIR);
	  DBG_MSG ("Component in '%s' is not a directory!\n", temp_name);
	  goto error;
	}

      do
	{
	  token = strsep (&token_p, "/");
	  /* The first "" was Ok, but rather skip double '/' after that */
	}
      while ((NULL != token) && (0 == strlen (token)));
    }

  free (temp_name);
  free (temp_token);

  return 0;

error:
  free (temp_name);
  free (temp_token);

  return -1;
}

int
rc_rmtree (const char *pathname)
{
  char **dirlist = NULL;
  int i = 0;

  if (!check_arg_str (pathname))
    return -1;

  if (!rc_file_exists (pathname))
    {
      rc_errno_set (ENOENT);
      DBG_MSG ("'%s' does not exist!\n", pathname);
      return -1;
    }

  if (!rc_is_dir (pathname, FALSE))
    {
      rc_errno_set (ENOTDIR);
      DBG_MSG ("'%s' is not a directory!\n", pathname);
      return -1;
    }


  dirlist = rc_ls_dir (pathname, TRUE, FALSE);
  if ((NULL == dirlist) && (rc_errno_is_set ()))
    {
      /* Do not error out - caller should decide itself if it
       * it is an issue */
      DBG_MSG ("Failed to ls_dir() directory '%s'!\n", pathname);
      return -1;
    }

  while ((NULL != dirlist) && (NULL != dirlist[i]))
    {
      /* If it is a directory, call rc_rmtree() again with
       * it as argument */
      if (rc_is_dir (dirlist[i], FALSE))
	{
	  if (-1 == rc_rmtree (dirlist[i]))
	    {
	      DBG_MSG ("Failed to rm_tree() directory '%s'!\n", dirlist[i]);
	      goto error;
	    }
	}

      /* Now actually remove it.  Note that if it was a directory,
       * it should already be removed by above rc_rmtree() call */
      if ((rc_file_exists (dirlist[i]) && (-1 == remove (dirlist[i]))))
	{
	  rc_errno_set (errno);
	  DBG_MSG ("Failed to remove() '%s'!\n", dirlist[i]);
	  goto error;
	}
      i++;
    }

  str_list_free (dirlist);

  /* Now remove the parent */
  if (-1 == remove (pathname))
    {
      rc_errno_set (errno);
      DBG_MSG ("Failed to remove '%s'!\n", pathname);
      goto error;
    }

  return 0;
error:
  str_list_free (dirlist);

  return -1;
}

char **
rc_ls_dir (const char *pathname, bool hidden, bool sort)
{
  DIR *dp;
  struct dirent *dir_entry;
  char **dirlist = NULL;

  if (!check_arg_str (pathname))
    return NULL;

  if (!rc_is_dir (pathname, TRUE))
    {
      /* XXX: Should we error here? */
      DBG_MSG ("'%s' is not a directory.\n", pathname);
      return NULL;
    }

  dp = opendir (pathname);
  if (NULL == dp)
    {
      rc_errno_set (errno);
      DBG_MSG ("Failed to opendir() '%s'!\n", pathname);
      /* errno will be set by opendir() */
      goto error;
    }

  do
    {
      /* Clear errno to distinguish between EOF and error */
      errno = 0;
      dir_entry = readdir (dp);
      /* Only an error if 'errno' != 0, else EOF */
      if ((NULL == dir_entry) && (0 != errno))
	{
	  rc_errno_set (errno);
	  DBG_MSG ("Failed to readdir() '%s'!\n", pathname);
	  goto error;
	}
      if ((NULL != dir_entry)
	  /* Should we display hidden files? */
	  && (hidden ? 1 : dir_entry->d_name[0] != '.'))
	{
	  char *d_name = dir_entry->d_name;
	  char *str_ptr;

	  /* Do not list current or parent entries */
	  if ((0 == strcmp (d_name, ".")) || (0 == strcmp (d_name, "..")))
	    continue;

	  str_ptr = rc_strcatpaths (pathname, d_name);
	  if (NULL == str_ptr)
	    {
	      DBG_MSG ("Failed to allocate buffer!\n");
	      goto error;
	    }

	  if (sort)
	    str_list_add_item_sorted (dirlist, str_ptr, error);
	  else
	    str_list_add_item (dirlist, str_ptr, error);
	}
    }
  while (NULL != dir_entry);

  if (!check_strv (dirlist))
    {
      if (NULL != dirlist)
	str_list_free (dirlist);

      DBG_MSG ("Directory '%s' is empty.\n", pathname);
    }

  closedir (dp);

  return dirlist;

error:
  /* Free dirlist on error */
  str_list_free (dirlist);

  if (NULL != dp)
      closedir (dp);

  return NULL;
}


/*
 * Below two functions (rc_file_map and rc_file_unmap) are
 * from udev-050 (udev_utils.c).
 * (Some are slightly modified, please check udev for originals.)
 *
 * Copyright 2004 Kay Sievers <kay@vrfy.org>
 *
 *	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 version 2 of the License.
 *
 *	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, write to the Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

int
rc_file_map (const char *filename, char **buf, size_t * bufsize)
{
  struct stat stats;
  int fd;

  fd = open (filename, O_RDONLY);
  if (fd < 0)
    {
      rc_errno_set (errno);
      DBG_MSG ("Failed to open file!\n");
      return -1;
    }

  if (fstat (fd, &stats) < 0)
    {
      rc_errno_set (errno);
      DBG_MSG ("Failed to stat file!\n");

      close (fd);

      return -1;
    }

  if (0 == stats.st_size)
    {
      rc_errno_set (EINVAL);
      DBG_MSG ("Failed to mmap file with 0 size!\n");

      close (fd);

      return -1;
    }

  *buf = mmap (NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (*buf == MAP_FAILED)
    {
      rc_errno_set (errno);
      DBG_MSG ("Failed to mmap file!\n");

      close (fd);

      return -1;
    }
  *bufsize = stats.st_size;

  close (fd);

  return 0;
}

void
rc_file_unmap (char *buf, size_t bufsize)
{
  munmap (buf, bufsize);
}