summaryrefslogtreecommitdiff
path: root/lib/mkancesdirs.c
blob: 4737742366806f6a13741467f6dd5202213e2c39 (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
/* Make a file's ancestor directories.

   Copyright (C) 2006 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 2, 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, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */

/* Written by Paul Eggert.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "mkancesdirs.h"

#include <errno.h>
#include <sys/stat.h>

#include "dirname.h"
#include "stat-macros.h"

/* Return 0 if FILE is a directory, otherwise -1 (setting errno).  */

static int
test_dir (char const *file)
{
  struct stat st;
  if (stat (file, &st) == 0)
    {
      if (S_ISDIR (st.st_mode))
	return 0;
      errno = ENOTDIR;
    }
  return -1;
}

/* Ensure that the ancestor directories of FILE exist, using an
   algorithm that should work even if two processes execute this
   function in parallel.  Temporarily modify FILE by storing '\0'
   bytes into it, to access the ancestor directories.

   Create any ancestor directories that don't already exist, by
   invoking MAKE_DIR (ANCESTOR, MAKE_DIR_ARG).  This function should
   return zero if successful, -1 (setting errno) otherwise.

   If successful, return 0 with FILE set back to its original value;
   otherwise, return -1 (setting errno), storing a '\0' into *FILE so
   that it names the ancestor directory that had problems.  */

int
mkancesdirs (char *file,
	     int (*make_dir) (char const *, void *),
	     void *make_dir_arg)
{
  /* This algorithm is O(N**2) but in typical practice the fancier
     O(N) algorithms are slower.  */

  /* Address of the previous directory separator that follows an
     ordinary byte in a file name in the left-to-right scan, or NULL
     if no such separator precedes the current location P.  */
  char *sep = NULL;

  char const *prefix_end = file + FILE_SYSTEM_PREFIX_LEN (file);
  char *p;
  char c;

  /* Search backward through FILE using mkdir to create the
     furthest-away ancestor that is needed.  This loop isn't needed
     for correctness, but typically ancestors already exist so this
     loop speeds things up a bit.

     This loop runs a bit faster if errno initially contains an error
     number corresponding to a failed access to FILE.  However, things
     work correctly regardless of errno's initial value.  */

  for (p = last_component (file); prefix_end < p; p--)
    if (ISSLASH (*p) && ! ISSLASH (p[-1]))
      {
	*p = '\0';

	if (errno == ENOENT && make_dir (file, make_dir_arg) == 0)
	  {
	    *p = '/';
	    break;
	  }

	if (errno != ENOENT)
	  {
	    if (test_dir (file) == 0)
	      {
		*p = '/';
		break;
	      }
	    if (errno != ENOENT)
	      return -1;
	  }

	*p = '/';
      }

  /* Scan forward through FILE, creating directories along the way.
     Try mkdir before stat, so that the procedure works even when two
     or more processes are executing it in parallel.  */

  while ((c = *p++))
    if (ISSLASH (*p))
      {
	if (! ISSLASH (c))
	  sep = p;
      }
    else if (ISSLASH (c) && *p && sep)
      {
	*sep = '\0';
	if (make_dir (file, make_dir_arg) != 0 && test_dir (file) != 0)
	  return -1;
	*sep = '/';
      }


  return 0;
}