summaryrefslogtreecommitdiff
path: root/libtreefs/fsys-getroot.c
blob: cd735bfeaa3afa335b9892ebdea4a5b81ab27197 (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
/*

   Copyright (C) 1995 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   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 <hurd/fsys.h>

#include "treefs.h"

error_t
_treefs_s_fsys_getroot (struct treefs_fsys *fsys,
			mach_port_t dotdot,
			uid_t *uids, unsigned nuids,
			uid_t *gids, unsigned ngids,
			int flags, retry_type *retry, char *retry_name,
			file_t *result, mach_msg_type_name_t *result_type)
{
  error_t err;
  mode_t type;
  struct treefs_node *root;
  struct treefs_auth *auth;

  flags &= O_HURD;

  err = treefs_fsys_get_root (fsys, &root);
  if (err)
    return err;

  if (!(flags & O_NOTRANS))
    /* Try starting up any translator on the root node.  */
    {
      fsys_t child_fsys;

      do
	{
	  err =
	    treefs_node_get_active_trans (root, 0, 0, &dotdot, &child_fsys);
	  if (err == 0 && child_fsys != MACH_PORT_NULL)
	    /* We think there's an active translator; try contacting it.  */
	    {
	      err =
		fsys_getroot (child_fsys, dotdot, MACH_MSG_TYPE_COPY_SEND,
			      uids, nuids, gids, ngids,
			      flags, 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 (root, control_port);
	    }
	}
      while (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED);

      /* If we got a translator, or an error trying, return immediately.  */
      if (err || child_fsys)
	{
	  if (!err && *result != MACH_PORT_NULL)
	    *result_type = MACH_MSG_TYPE_MOVE_SEND;
	  else
	    *result_type = MACH_MSG_TYPE_COPY_SEND;

	  if (!err)
	    mach_port_deallocate (mach_task_self (), dotdot);
	  treefs_node_unref (root);

	  return err;
	}
    }

  pthread_mutex_lock (&root->lock);

  type = treefs_node_type (root);
  if (type == S_IFLNK && !(flags & (O_NOLINK | O_NOTRANS)))
    /* Handle symlink interpretation */
    {
      int sym_len = 1000;
      char path_buf[sym_len + 1], *path = path_buf;

      err = treefs_node_get_symlink (root, path, &sym_len);
      if (err == E2BIG)
	/* Symlink contents won't fit in our buffer, so
	   reallocate it and try again.  */
	{
	  path = alloca (sym_len + 1);
	  err = treefs_node_get_symlink (node, path, &sym_len);
	}

      if (err)
	goto out;
      
      if (*path == '/')
	{
	  *retry = FS_RETRY_MAGICAL;
	  *result = MACH_PORT_NULL;
	  *result_type = MACH_MSG_TYPE_COPY_SEND;
	  mach_port_deallocate (mach_task_self (), dotdot);
	}
      else
	{
	  *retry = FS_RETRY_REAUTH;
	  *result = dotdot;
	  *result_type = MACH_MSG_TYPE_COPY_SEND;
	}

      strcpy (retry_name, path);
      goto out;
    }
  
  err = treefs_node_create_auth (root, uids, nuids, gids, ngids, &auth);
  if (err)
    goto out;

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

  err = treefs_node_create_right (root, dotdot, flags, auth, result);

  treefs_node_auth_unref (root, auth);

 out:
  treefs_node_release (root);

  return err;
}