summaryrefslogtreecommitdiff
path: root/src/libs/uio/fstypes.c
blob: d940e8fb5515824f37a9b3f3ff2f9ead26b35e87 (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
/*
 * Copyright (C) 2003  Serge van den Boom
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * Nota bene: later versions of the GNU General Public License do not apply
 * to this program.
 *
 * 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
 *
 */

#include <errno.h>
#include <stdio.h>

#include "iointrn.h"
#include "uioport.h"
#include "fstypes.h"
#include "mem.h"
#include "defaultfs.h"
#ifdef uio_MEM_DEBUG
#	include "memdebug.h"
#endif


static uio_bool uio_validFileSystemHandler(uio_FileSystemHandler *handler);
static uio_FileSystemInfo *uio_FileSystemInfo_new(uio_FileSystemID id,
		uio_FileSystemHandler *handler, char *name);
static uio_FileSystemInfo **uio_getFileSystemInfoPtr(uio_FileSystemID id);

static inline uio_FileSystemInfo *uio_FileSystemInfo_alloc(void);

static inline void uio_FileSystemInfo_free(
		uio_FileSystemInfo *fileSystemInfo);


uio_FileSystemInfo *uio_fileSystems = NULL;
		// list sorted by id



void
uio_registerDefaultFileSystems(void) {
	int i;
	int num;
	uio_FileSystemID registerResult;

	num = uio_numDefaultFileSystems();
	for (i = 0; i < num; i++) {
		registerResult = uio_registerFileSystem(
				defaultFileSystems[i].id,
				defaultFileSystems[i].name,
				defaultFileSystems[i].handler);
		switch (registerResult) {
			case 0:
				fprintf(stderr, "Warning: Default file system '%s' is "
						"already registered.\n",
						defaultFileSystems[i].name);
				break;
			case -1:
				fprintf(stderr, "Error: Could not register '%s' file \n"
						"system: %s\n", defaultFileSystems[i].name,
						strerror(errno));
				break;
			default:
				assert(registerResult == defaultFileSystems[i].id);
				break;
		}
	}
}

void
uio_unRegisterDefaultFileSystems(void) {
	int i;
	int num;

	num = uio_numDefaultFileSystems();
	for (i = 0; i < num; i++) {
		if (uio_unRegisterFileSystem(defaultFileSystems[i].id) == -1) {
			fprintf(stderr, "Could not unregister '%s' file system: %s\n",
					defaultFileSystems[i].name, strerror(errno));
		}
	}
}

// if wantedID = 0, just pick one
// if wantedID != 0, 0 will be returned if that id wasn't available
// a copy of 'name' is made
uio_FileSystemID
uio_registerFileSystem(uio_FileSystemID wantedID, const char *name,
		uio_FileSystemHandler *handler) {
	uio_FileSystemInfo **ptr;

	if (!uio_validFileSystemHandler(handler)) {
		errno = EINVAL;
		return -1;
	}
	if (wantedID == 0) {
		// Search for the first free id >= uio_FIRST_CUSTOM_ID
		// it is put in wantedID

		for (ptr = &uio_fileSystems; *ptr != NULL; ptr = &(*ptr)->next)
			if ((*ptr)->id >= uio_FS_FIRST_CUSTOM_ID)
				break;

		wantedID = uio_FS_FIRST_CUSTOM_ID;
		while (*ptr != NULL) {
			if ((*ptr)->id != wantedID) {
				// wantedID is not in use
				break;
			}
			wantedID++;
			ptr = &(*ptr)->next;
		}
		// wantedID contains the new ID
	} else {
		// search for the place in the list where to insert the wanted
		// id, keeping the list sorted
		for (ptr = &uio_fileSystems; *ptr != NULL; ptr = &(*ptr)->next) {
			if ((*ptr)->id <= wantedID) {
				if ((*ptr)->id == wantedID)
					return 0;
				break;
			}
		}

	}
	// ptr points to the place where the new link can inserted
	
	if (handler->init != NULL && handler->init() == -1) {
		// errno is set
		return -1;
	}
			
	{
		uio_FileSystemInfo *newInfo;

		newInfo = uio_FileSystemInfo_new(wantedID, handler, uio_strdup(name));
		newInfo->next = *ptr;
		*ptr = newInfo;
		return wantedID;
	}
}

int
uio_unRegisterFileSystem(uio_FileSystemID id) {
	uio_FileSystemInfo **ptr;
	uio_FileSystemInfo *temp;

	ptr = uio_getFileSystemInfoPtr(id);
	if (ptr == NULL) {
		errno = EINVAL;
		return -1;
	}
	if ((*ptr)->ref > 1) {
		errno = EBUSY;
		return -1;
	}

	if ((*ptr)->handler->unInit != NULL &&
			((*ptr)->handler->unInit() == -1)) {
		// errno is set
		return -1;
	}
	
	temp = *ptr;
	*ptr = (*ptr)->next;

//	uio_FileSystemHandler_unref(temp->handler);
	uio_free(temp->name);
	uio_FileSystemInfo_free(temp);
	
	return 0;	
}

static uio_bool
uio_validFileSystemHandler(uio_FileSystemHandler *handler) {
	// Check for the essentials
	if (handler->mount == NULL ||
			handler->umount == NULL ||
			handler->open == NULL ||
			handler->close == NULL ||
			handler->read == NULL ||
			handler->openEntries == NULL ||
			handler->readEntries == NULL ||
			handler->closeEntries == NULL) {
#ifdef DEBUG
		fprintf(stderr, "Invalid file system handler.\n");
#endif
		return false;
	}
	return true;
}

uio_FileSystemHandler *
uio_getFileSystemHandler(uio_FileSystemID id) {
	uio_FileSystemInfo *ptr;

	for (ptr = uio_fileSystems; ptr != NULL; ptr = ptr->next) {
		if (ptr->id == id)
			return ptr->handler;
	}
	return NULL;
}

uio_FileSystemInfo *
uio_getFileSystemInfo(uio_FileSystemID id) {
	uio_FileSystemInfo *ptr;

	for (ptr = uio_fileSystems; ptr != NULL; ptr = ptr->next) {
		if (ptr->id == id)
			return ptr;
	}
	return NULL;
}

static uio_FileSystemInfo **
uio_getFileSystemInfoPtr(uio_FileSystemID id) {
	uio_FileSystemInfo **ptr;

	for (ptr = &uio_fileSystems; *ptr != NULL; ptr = &(*ptr)->next) {
		if ((*ptr)->id == id)
			return ptr;
	}
	return NULL;
}

// sets ref to 1
static uio_FileSystemInfo *
uio_FileSystemInfo_new(uio_FileSystemID id, uio_FileSystemHandler *handler,
		char *name) {
	uio_FileSystemInfo *result;

	result = uio_FileSystemInfo_alloc();
	result->id = id;
	result->handler = handler;
	result->name = name;
	result->ref = 1;
	return result;
}

// *** Allocators ***

static inline uio_FileSystemInfo *
uio_FileSystemInfo_alloc(void) {
	uio_FileSystemInfo *result = uio_malloc(sizeof (uio_FileSystemInfo));
#ifdef uio_MEM_DEBUG
	uio_MemDebug_debugAlloc(uio_FileSystemInfo, (void *) result);
#endif
	return result;
}


// *** Deallocators ***

static inline void
uio_FileSystemInfo_free(uio_FileSystemInfo *fileSystemInfo) {
#ifdef uio_MEM_DEBUG
	uio_MemDebug_debugFree(uio_FileSystemInfo, (void *) fileSystemInfo);
#endif
	uio_free(fileSystemInfo);
}