summaryrefslogtreecommitdiff
path: root/unionfs/lib.c
blob: 0423c1bc70839db7b5ad2ca0f5acd370f3844631 (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
/* Hurd unionfs
   Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
   Written by Moritz Schulte <moritz@duesseldorf.ccc.de>.

   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 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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.  */

#define _GNU_SOURCE

#include <hurd/netfs.h>
#include <error.h>
#include <dirent.h>
#include <errno.h>
#include <sys/mman.h>
#include <stddef.h>

#include "lib.h"

/* Lock, which must be held, during printing of debugging
   messages.  */
struct mutex debug_msg_lock = MUTEX_INITIALIZER;

/* Returns no error if PATH points to a directory.  */
error_t check_dir (char *path)
{
  struct stat filestat;
  error_t err = 0;

  err = stat (path, &filestat);
  if (err)
    return err;

  if (!S_ISDIR (filestat.st_mode))
    return ENOTDIR;

  return 0;
}

/* Fetch directory entries for DIR; store the raw data as returned by
   the dir_readdir RPC in *DIRENT_DATA, the size of *DIRENT_DATA in
   *DIRENT_DATA_SIZE and a list of pointers to the dirent structures
   in *DIRENT_LIST.  */
error_t
dir_entries_get (file_t dir, char **dirent_data, 
		 size_t *dirent_data_size, struct dirent ***dirent_list)
{
  error_t err;
  size_t data_size;
  int entries_num;
  char *data;

  err = dir_readdir (dir, &data, &data_size, 0, -1, 0, &entries_num);
  if (! err)
    {
      struct dirent **list;
  
      list = malloc (sizeof (struct dirent *) * (entries_num + 1));
      if (list)
	{	
	  struct dirent *dp;
	  int i;

	  for (i = 0, dp = (struct dirent *) data;
	       i < entries_num;
	       i++, dp = (struct dirent *) ((char *) dp + dp->d_reclen))
	    *(list + i) = dp;
	  *(list + i) = NULL;

	  *dirent_data = data;
	  *dirent_data_size = data_size;
	  *dirent_list = list;
	}
      else
	{
	  munmap (data, data_size);
	  err = ENOMEM;
	}
    }
  return err;
}

/* Lookup the file named NAME beneath DIR (or the cwd, if DIR is not a
   valid port.  Try to open with FLAGS0 first, and if that fails with
   FLAGS1; MODE is the mode to user for newly created files.  On
   success, stat the looked up port and store it in *PORT, the
   according stat information are stored in *STAT.  */
error_t
file_lookup (file_t dir, char *name, int flags0, int flags1,
	     int mode, file_t *port, struct stat *stat)
{
  error_t err = 0;
  file_t p;
  struct stat s;

  file_t do_file_lookup (file_t d, char *n, int f, int m)
    {
      if (port_valid (d))
	p = file_name_lookup_under (d, n, f, m);
      else if (errno == EACCES)
	p = file_name_lookup (n, f, m);
      return p;
    }

  p = do_file_lookup (dir, name, flags0, mode);
  if (! port_valid (p))
    p = do_file_lookup (dir, name, flags1, mode);

  if (port_valid (p))
    {
      if (stat)
	{
	  err = io_stat (p, &s);
	  if (err)
	    port_dealloc (p);
	}
    }
  else
    err = errno;

  if (! err)
    {
      *port = p;
      if (stat)
	*stat = s;
    }
  return err;
}

#include <fcntl.h>

char *
make_filepath (char *path, char *filename)
{
  int length;
  char *filepath;

  length = strlen (path) + strlen (filename) + 2;
  filepath = malloc (length);
  if (filepath == NULL) 
    return NULL;
  
  strncpy (filepath, path, length);
  strncat (filepath, filename, strlen (filename));

  return filepath;
}

error_t
for_each_subdir (char *path, error_t (*func) (char *, char *))
{
  struct dirent **dirent, **dirent_list;
  char *dirent_data;
  size_t dirent_data_size;
  file_t dir;
  error_t err;

  dir = file_name_lookup (path, O_READ, 0);

  err = dir_entries_get (dir, &dirent_data, &dirent_data_size, &dirent_list);
  if (err)
    return err;

  for (dirent = dirent_list; (! err) && (*dirent); dirent++)
    {
      char *name;
      struct stat filestat;

      if ((!strcmp ((*dirent)->d_name, "."))
	  || (!strcmp ((*dirent)->d_name, "..")))
	continue;

      name = make_filepath (path, (*dirent)->d_name);

      err = stat (name, &filestat);

      free (name);

      if (err)
	{
	  fprintf (stderr, "unionfs: couldn't stat %s%s\n",
		   path, (*dirent)->d_name);
	  continue;
	}

      if (!S_ISDIR(filestat.st_mode))
	continue;
 
      func ((*dirent)->d_name, path);
    }

  return 0;
}

error_t
for_each_subdir_priv (char *path, error_t (*func) (char *, char *, void *),
		      void *priv)
{
  struct dirent **dirent, **dirent_list;
  char *dirent_data;
  size_t dirent_data_size;
  file_t dir;
  error_t err;

  dir = file_name_lookup (path, O_READ, 0);

  err = dir_entries_get (dir, &dirent_data, &dirent_data_size, &dirent_list);
  if (err)
    return err;

  for (dirent = dirent_list; (!err) && (*dirent); dirent++)
    {
      char *name;
      struct stat filestat;

      if ((!strcmp ((*dirent)->d_name, "."))
	  || (!strcmp ((*dirent)->d_name, "..")))
	continue;

      name = make_filepath (path, (*dirent)->d_name);

      err = stat (name, &filestat);

      free (name);

      if (err)
	{
	  fprintf (stderr, "unionfs: couldn't stat %s%s\n",
		   path, (*dirent)->d_name);
	  continue;
	}

      if (!S_ISDIR(filestat.st_mode))
	continue;
 
      func ((*dirent)->d_name, path, priv);
    }

  return 0;
}

error_t
for_each_file_priv (char *path, error_t (*func) (char *, char *, void *),
		    void *priv)
{
  struct dirent **dirent, **dirent_list;
  char *dirent_data;
  size_t dirent_data_size;
  file_t dir;
  error_t err;

  dir = file_name_lookup (path, O_READ, 0);

  err = dir_entries_get (dir, &dirent_data, &dirent_data_size, &dirent_list);
  if (err)
    return err;

  for (dirent = dirent_list; (!err) && (*dirent); dirent++)
    {

      if ((!strcmp ((*dirent)->d_name, "."))
	  || (!strcmp ((*dirent)->d_name, "..")))
	continue;

      func ((*dirent)->d_name, path, priv);
    }

  return err;
}