File: dirhelpers.c

package info (click to toggle)
ladish 1%2Bdfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 36,406; python: 11,237; cpp: 705; makefile: 22; ruby: 20; sh: 17
file content (406 lines) | stat: -rw-r--r-- 8,220 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
 * LADI Session Handler (ladish)
 *
 * Copyright (C) 2009,2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
 *
 **************************************************************************
 * This file contains implementation of the directory helper functions
 **************************************************************************
 *
 * LADI Session Handler 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 2 of the License, or
 * (at your option) any later version.
 *
 * LADI Session Handler 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 LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "../common.h"
#include "catdup.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <dirent.h>

bool check_dir_exists(const char * dirname)
{
  struct stat st;

  ASSERT(*dirname);             /* empty string? */

  if (stat(dirname, &st) != 0)
  {
    return false;
  }

  return S_ISDIR(st.st_mode);
}

/* ret=true, err=0 - directory was created successfully */
/* ret=true, err=EEXIST - directory already exists */
/* ret=true, err=ENOENT -  A directory component in dirname does not exist or is a dangling symbolic link */
/* ret=true, err=error - directory creation failed in a bad way */
/* ret=false, err=error - dirname is not valid directory path */
static bool safe_mkdir(const char * dirname, int mode, int * err)
{
  struct stat st;

  ASSERT(*dirname);             /* empty string? */

  if (mkdir(dirname, mode) == 0)
  {
    *err = 0;
    return true;
  }

  if (errno != EEXIST)
  {
    *err = errno;
    return true;
  }

  /* dirname path exists, not necessarily as a directory.
     This includes the case where pathname is a symbolic link,
     dangling or not. */

  if (stat(dirname, &st) != 0)
  {
    *err = errno;
    log_error("Failed to stat \"%s\": %d (%s)", dirname, errno, strerror(errno));
    return false;
  }
  else if (!S_ISDIR(st.st_mode))
  {
    *err = ENOTDIR;
    log_error("\"%s\" exists but is not directory.", dirname);
    return false;
  }

  *err = EEXIST;
  return true;
}

static bool rmkdir(char * buffer, int mode)
{
  int err;
  char * p;
  size_t len;
  bool last;

  len = 0;
  p = buffer;
loop:
  last = *p == 0;
  if (!last)
  {
    if (*p != '/')
    {
      len++;
      p++;
      goto loop;
    }

    if (len == 0)
    {
      /* skip extra '/' chars */
      p++;
      goto loop;
    }

    *p = 0;
  }
  else if (len == 0)
  {
    return true;
  }

  if (!safe_mkdir(buffer, mode, &err))
  {
    return false;
  }

  switch (err)
  {
  case 0:
    log_info("Directory \"%s\" created", buffer);
    /* fall through */
  case EEXIST:
    break;
  default:
    log_error("Failed to create \"%s\" directory: %d (%s)", buffer, errno, strerror(errno));
    return false;
  }

  if (!last)
  {
    *p = '/';
    len = 0;
    p++;
    goto loop;
  }

  return true;
}

bool ensure_dir_exist(const char * dirname, int mode)
{
  size_t len;
  char * buffer;
  int err;
  bool ret;

  if (!safe_mkdir(dirname, mode, &err))
  {
    return false;
  }

  if (err == 0 || err == EEXIST)
  {
    return true;
  }

  if (errno != ENOENT)
  {
    log_error("Failed to create \"%s\" directory: %d (%s)", dirname, errno, strerror(errno));
    return false;
  }

  /* A directory component in dirname does not exist or is a dangling symbolic link */

  len = strlen(dirname);

  buffer = malloc(len + 1);
  if (buffer == NULL)
  {
    log_error("malloc(%zu) failed.", len);
    return false;
  }

  memcpy(buffer, dirname, len);
  buffer[len] = 0;

  ret = rmkdir(buffer, mode);

  free(buffer);

  return ret;
}

bool ensure_dir_exist_varg(int mode, ...)
{
  va_list ap;
  const char * str;
  size_t len;
  char * buffer;
  char * p;
  int ret;

  len = 0;
  va_start(ap, mode);
  while ((str = va_arg(ap, const char *)) != NULL)
  {
    len += strlen(str);
  }
  va_end(ap);
  ASSERT(len > 0);
  len++;

  buffer = malloc(len);
  if (buffer == NULL)
  {
    log_error("malloc(%zu) failed.", len);
    return false;
  }

  p = buffer;
  va_start(ap, mode);
  while ((str = va_arg(ap, const char *)) != NULL)
  {
    len = strlen(str);
    memcpy(p, str, len);
    p += len;
  }
  va_end(ap);
  ASSERT(p != buffer);
  *p = 0;

  ret = rmkdir(buffer, mode);

  free(buffer);

  return ret;
}

bool ladish_rmdir_recursive(const char * dirpath)
{
  DIR * dir;
  struct dirent * dentry_ptr;
  char * entry_fullpath;
  struct stat st;
  bool success;

  success = false;

  dir = opendir(dirpath);
  if (dir == NULL)
  {
    log_error("Cannot open directory '%s': %d (%s)", dirpath, errno, strerror(errno));
    goto exit;
  }

  while ((dentry_ptr = readdir(dir)) != NULL)
  {
    if (strcmp(dentry_ptr->d_name, ".") == 0 ||
        strcmp(dentry_ptr->d_name, "..") == 0)
    {
      continue;
    }

    entry_fullpath = catdup3(dirpath, "/", dentry_ptr->d_name);
    if (entry_fullpath == NULL)
    {
      log_error("catdup() failed");
      goto close;
    }

    if (stat(entry_fullpath, &st) != 0)
    {
      log_error("failed to stat '%s': %d (%s)", entry_fullpath, errno, strerror(errno));
    }
    else
    {
      if (S_ISDIR(st.st_mode))
      {
        if (!ladish_rmdir_recursive(entry_fullpath))
        {
          goto free;
        }
      }
      else
      {
        if (unlink(entry_fullpath) < 0)
        {
          log_error("unlink('%s') failed. errno = %d (%s)", dirpath, errno, strerror(errno));
          goto free;
        }
      }
    }

    free(entry_fullpath);
  }

  if (rmdir(dirpath) < 0)
  {
    log_error("rmdir('%s') failed. errno = %d (%s)", dirpath, errno, strerror(errno));
  }
  else
  {
    success = true;
  }

  goto close;

free:
  free(entry_fullpath);
close:
  closedir(dir);
exit:
  return success;
}

bool ladish_rotate(const char * src, const char * dst, unsigned int max_backups)
{
  size_t len = strlen(dst) + 100;
  char paths[2][len];
  struct stat st;
  char * path;
  const char * older_path;
  bool oldest_found;
  unsigned int backup;

  ASSERT(max_backups > 0);

  oldest_found = false;
  path = NULL;
  older_path = NULL;
  backup = max_backups;
  while (backup > 0)
  {
    path = paths[backup % 2];
    snprintf(path, len, "%s.%u", dst, backup);

    if (stat(path, &st) != 0)
    {
      if (!oldest_found && errno == ENOENT)
      {
        log_info("\"%s\" does not exist", path);
        goto next;
      }

      log_error("Failed to stat \"%s\": %d (%s)", path, errno, strerror(errno));
      return false;
    }

    if (!S_ISDIR(st.st_mode))
    {
      log_error("\"%s\" exists but is not directory.", path);
      return false;
    }

    oldest_found = true;

    if (backup < max_backups)
    {
      ASSERT(older_path != NULL);
      log_info("rename '%s' -> '%s'", path, older_path);
      if (rename(path, older_path) != 0)
      {
        log_error("rename('%s' -> '%s') failed. errno = %d (%s)", path, older_path, errno, strerror(errno));
        return false;
      }
    }
    else
    {
      /* try to remove dst.max_backups */
      log_info("rmdir '%s'", path);
      if (!ladish_rmdir_recursive(path))
      {
        return false;
      }
    }

  next:
    older_path = path;
    backup--;
  }

  ASSERT(path != NULL);

  log_info("rename '%s' -> '%s'", dst, path);
  if (rename(dst, path) != 0 && errno != ENOENT)
  {
    log_error("rename('%s' -> '%s') failed. errno = %d (%s)", dst, path, errno, strerror(errno));
    return false;
  }

  log_info("rename '%s' -> '%s'", src, dst);
  if (rename(src, dst) != 0)
  {
    log_error("rename('%s' -> '%s') failed. errno = %d (%s)", src, dst, errno, strerror(errno));
    return false;
  }

  return true;
}