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
|
/* Hurd unionfs
Copyright (C) 2001, 2002 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. */
/* Underlying filesystem management. */
#define _GNU_SOURCE
#include <hurd/netfs.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include "ulfs.h"
/* The start of the ulfs chain. */
ulfs_t *ulfs_chain_start;
/* The end of the ulfs chain, we need this, to go through the chain in
reversed order. */
ulfs_t *ulfs_chain_end;
/* Number of registered underlying filesystems. */
unsigned int ulfs_num;
/* The lock protecting the ulfs data structures. */
struct mutex ulfs_lock = MUTEX_INITIALIZER;
/* Create a new ulfs element. */
error_t
ulfs_create (char *path, ulfs_t **ulfs)
{
ulfs_t *ulfs_new = malloc (sizeof (ulfs_t));
error_t err = 0;
if (! ulfs_new)
err = ENOMEM;
else
{
char *path_cp = path ? strdup (path) : NULL;
if (path && (! path_cp))
{
err = ENOMEM;
free (ulfs_new);
}
else
{
ulfs_new->path = path_cp;
ulfs_new->flags = 0;
ulfs_new->next = NULL;
ulfs_new->prev = NULL;
ulfs_new->prevp = NULL;
*ulfs = ulfs_new;
}
}
return err;
}
/* Destroy an ulfs element. */
void
ulfs_destroy (ulfs_t *ulfs)
{
free (ulfs->path);
free (ulfs);
}
/* Install ULFS into the linked list of registered filesystems. */
void
ulfs_install (ulfs_t *ulfs)
{
ulfs->next = ulfs_chain_start;
ulfs->prev = NULL;
ulfs->prevp = &ulfs_chain_start;
if (ulfs_chain_start)
{
ulfs_chain_start->prev = ulfs;
ulfs_chain_start->prevp = &ulfs->next;
}
else
ulfs_chain_end = ulfs;
ulfs_chain_start = ulfs;
}
/* Remove ULFS from the linked list of registered filesystems. */
void
ulfs_uninstall (ulfs_t *ulfs)
{
*ulfs->prevp = ulfs->next;
if (ulfs->next)
{
ulfs->next->prev = ulfs->prev;
ulfs->next->prevp = &ulfs->next;
}
else
ulfs_chain_end = ulfs->prev;
}
/* Get an ulfs element by it's index. */
error_t
ulfs_get_num (int num, ulfs_t **ulfs)
{
error_t err = EINVAL;
ulfs_t *u;
int i;
for (u = ulfs_chain_start, i = 0;
u && i < num;
u = u->next, i++);
if (u)
{
err = 0;
*ulfs = u;
}
return err;
}
/* Get an ulfs element by the associated path. */
error_t
ulfs_get_path (char *path, ulfs_t **ulfs)
{
error_t err = ENOENT;
ulfs_t *u;
for (u = ulfs_chain_start;
u && (! (((! path) && path == u->path)
|| (path && u->path && (! strcmp (path, u->path)))));
u = u->next);
if (u)
{
err = 0;
*ulfs = u;
}
return err;
}
/* Register a new underlying filesystem. */
error_t
ulfs_register (char *path, int flags)
{
ulfs_t *ulfs;
error_t err;
mutex_lock (&ulfs_lock);
err = ulfs_create (path, &ulfs);
if (! err)
{
ulfs->flags = flags;
ulfs_install (ulfs);
ulfs_num++;
}
mutex_unlock (&ulfs_lock);
return err;
}
/* Unregister an underlying filesystem. */
error_t
ulfs_unregister (char *path)
{
ulfs_t *ulfs;
error_t err;
mutex_lock (&ulfs_lock);
err = ulfs_get_path (path, &ulfs);
if (! err)
{
ulfs_uninstall (ulfs);
ulfs_destroy (ulfs);
ulfs_num--;
}
mutex_unlock (&ulfs_lock);
return err;
}
|