summaryrefslogtreecommitdiff
path: root/libtreefs/dir-lookup.c
blob: 33fefe699a16516742a3ed00c0629eef73e19172 (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
/* Default treefs_s_dir_lookup hook

   Copyright (C) 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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. */

#include <fcntl.h>
#include <string.h>

#include <hurd/fsys.h>

#include "treefs.h"
#include "treefs-s-hooks.h"

/* Default dir_lookup hook.  This code was originally copied from diskfs.  */ 
error_t
_treefs_s_dir_lookup (struct treefs_handle *h,
		      char *path, int flags, mode_t mode,
		      enum retry_type *retry, char *retry_name,
		      file_t *result, mach_msg_type_name_t *result_type)
{
  struct treefs_node *dir;
  struct treefs_node *node;
  unsigned symlink_expansions = 0;
  error_t err = 0;
  char *path_buf = 0;
  int path_buf_len = 0;
  int lastcomp = 0;
  int mustbedir = 0;

  flags &= O_HURD;
  mode &= ~S_IFMT;
  
  /* Skip leading slashes */
  while (path[0] == '/')
    path++;

  *result_type = MACH_MSG_TYPE_MAKE_SEND;
  *retry = FS_RETRY_NORMAL;
  retry_name[0] = '\0';

  if (path[0] == '\0')
    {
      /* Set things up in the state expected by the code from gotit: on. */
      dir = 0;
      node = h->po->node;
      pthread_mutex_lock (&node->lock);
      treefs_node_ref (node);
      goto gotit;
    }

  dir = h->po->node;
  pthread_mutex_lock (&dir->lock);
  node = 0;

  treefs_node_ref (dir);	/* acquire a ref for later node_release */

  do
    {
      char *nextname;

      assert_backtrace (!lastcomp);
      
      /* Find the name of the next pathname component */
      nextname = index (path, '/');

      if (nextname)
	{
	  *nextname++ = '\0';
	  while (*nextname == '/')
	    nextname++;
	  if (*nextname == '\0')
	    {
	      /* These are the rules for filenames ending in /. */
	      nextname = 0;
	      lastcomp = 1;
	      mustbedir = 1;
	      
	    }
	  else
	    lastcomp = 0;
	}
      else
	lastcomp = 1;
	  
      node = 0;

      /* Lookup the next pathname component.  */
      if (!lastcomp)
	err = treefs_dir_lookup (dir, path, h->auth, 0, 0, &node);
      else
	/* ... and in this case, the last.  Note that the S_IFREG only
	   applies in the case of O_CREAT, which is turned off for
	   directories anyway.  */
	err =
	  treefs_dir_lookup (dir, path, h->auth, flags, mode | S_IFREG, &node);

      /* If we get an error we're done */
      if (err == EAGAIN)
	{
	  if (h->po->parent_port != MACH_PORT_NULL)
	    {
	      *retry = FS_RETRY_REAUTH;
	      *result = h->po->parent_port;
	      *result_type = MACH_MSG_TYPE_COPY_SEND;
	      if (!lastcomp)
		strcpy (retry_name, nextname);
	      err = 0;
	      goto out;
	    }
	  else
	    /* The global filesystem root...  .. == .  */
	    {
	      err = 0;
	      node = dir;
	      treefs_node_ref (node);
	    }
	}
      
      if (err)
	goto out;

      /* If this is translated, start the translator (if necessary)
	 and return.  */
      /* The check for `node != dir' simplifies this code a great
	 deal.  Such a translator should already have been started,
	 so there's no lossage in doing it this way. */
      if ((!lastcomp || !(flags & O_NOTRANS))
	  && node != dir)
	{
	  file_t dir_port = MACH_PORT_NULL, child_fsys;

	  /* Be very careful not to hold an inode lock while fetching
	     a translator lock and vice versa.  */

	  pthread_mutex_unlock (&node->lock);
	  pthread_mutex_unlock (&dir->lock);

	  do
	    {
	      err =
		treefs_node_get_active_trans (node, dir, h->po->parent_port,
					      &dir_port, &child_fsys);
	      if (err == 0 && child_fsys != MACH_PORT_NULL)
		{
		  err =
		    fsys_getroot (child_fsys, dir_port,
				  MACH_MSG_TYPE_COPY_SEND,
				  h->auth->uids, h->auth->nuids,
				  h->auth->gids, h->auth->ngids,
				  lastcomp ? flags : 0,
				  retry, retry_name, result);
		  /* If we got MACH_SEND_INVALID_DEST or MIG_SERVER_DIED, then
		     the server is dead.  Zero out the old control port and try
		     everything again.  */
		  if (err == MACH_SEND_INVALID_DEST || err == EMIG_SERVER_DIED)
		    treefs_node_drop_active_trans (node, child_fsys);
		}
	    }
	  while (err == MACH_SEND_INVALID_DEST || err == EMIG_SERVER_DIED);

	  if (err || child_fsys)
	    {
	      /* We're done; return to the user.  If there are more
		 components after this name, be sure to append them to the
		 user's retry path. */
	      if (!err && !lastcomp)
		{
		  strcat (retry_name, "/");
		  strcat (retry_name, nextname);
		}

	      *result_type = MACH_MSG_TYPE_MOVE_SEND;

	      treefs_node_unref (dir);
	      treefs_node_unref (node);
	      if (dir_port)
		mach_port_deallocate (mach_task_self (), dir_port);

	      return err;
	    }
	  
	  /* We're here if we tried the translator check, and it
	     failed.   Lock everything back, and make sure we do it
	     in the right order. */
	  if (strcmp (path, "..") != 0)
	    {
	      if (pthread_mutex_trylock (&dir->lock))
	        {
		  pthread_mutex_unlock (&node->lock);
		  pthread_mutex_lock (&dir->lock);
		  pthread_mutex_lock (&node->lock);
		}
	    }
	  else
	    pthread_mutex_lock (&dir->lock);
	}
      
      if (treefs_node_type (node) == S_IFLNK
	  && !(lastcomp && (flags & (O_NOLINK|O_NOTRANS))))
	/* Handle symlink interpretation */
	{
	  unsigned nextname_len = nextname ? strlen (nextname) + 1 : 0;
	  /* max space we currently have for the sym link */
	  unsigned sym_len = path_buf_len - nextname_len - 1;

	  if (symlink_expansions++ > node->fsys->max_symlinks)
	    {
	      err = ELOOP;
	      goto out;
	    }
	
	  err = treefs_node_get_symlink (node, path_buf, &sym_len);
	  if (err == E2BIG)
	    /* Symlink contents + extra path won't fit in our buffer, so
	       reallocate it and try again.  */
	    {
	      path_buf_len = sym_len + nextname_len + 1 + 1;
	      path_buf = alloca (path_buf_len);
	      err = treefs_node_get_symlink (node, path_buf, &sym_len);
	    }
	  if (err)
	    goto out;

	  if (nextname)
	    {
	      path_buf[sym_len] = '/';
	      bcopy (nextname, path_buf + sym_len + 1, nextname_len - 1);
	    }
	  if (mustbedir)
	    {
	      path_buf[nextnamelen + sym_len] = '/';
	      path_buf[nextnamelen + sym_len + 1] = '\0';
	    }
	  else
	    path_buf[nextname_len + sym_len] = '\0';

	  if (path_buf[0] == '/')
	    {
	      /* Punt to the caller.  */
	      *retry = FS_RETRY_MAGICAL;
	      *result = MACH_PORT_NULL;
	      strcpy (retry_name, path_buf);
	      goto out;
	    }
	  
	  path = path_buf;
	  mustbedir = 0;
	  if (lastcomp)
	    {
	      lastcomp = 0;
	      /* Symlinks to nonexistent files aren't allowed to cause
		 creation, so clear the flag here. */
	      flags &= ~O_CREAT;
	    }
	  treefs_node_release (node);
	  node = 0;
	}
      else
	{
	  /* Handle normal nodes */
	  path = nextname;
	  if (node == dir)
	    treefs_node_unref (dir);
	  else
	    treefs_node_release (dir);
	  if (!lastcomp)
	    {
	      dir = node;
	      node = 0;
	    }
	  else
	    dir = 0;
	}
    } while (path && *path);
  
 gotit:
  /* At this point, node is the node to return.  */

  if (mustbedir && !treefs_node_isdir (node))
    err = ENOTDIR;
  if (err)
    goto out;

  err = treefs_node_create_right (node, flags, h->po->parent_port, h->auth,
				  result);

 out:
  if (node)
    {
      if (dir == node)
	treefs_node_unref (node);
      else
	treefs_node_release (node);
    }
  if (dir)
    treefs_node_release (dir);
  return err;  
}