File: bosmeta.c

package info (click to toggle)
aranym 1.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,488 kB
  • sloc: cpp: 178,117; ansic: 170,405; perl: 5,637; sh: 5,547; asm: 2,282; makefile: 1,324; objc: 218
file content (337 lines) | stat: -rw-r--r-- 7,010 bytes parent folder | download | duplicates (9)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>

#include "bosmeta.h"
#include "dosdir.h"

#if 0
#include "nfd.h"
#define TRACE(x) NFD(x)
#define DEBUG(x) NFD(x)
#else
#define TRACE(x)
#define DEBUG(x)
#endif


/* x:\dev\phys folder lists respectively */
LIST root;

static LIST root_dev;
static LIST root_dev_phys;

/* cookie release/dup */
static
void release_cookie( FCOOKIE *fc);

static
void release_folder( LIST *f) {
	LINKABLE *fc = listFirst(f);
	while (fc) {
		FCOOKIE *trash = (FCOOKIE*)fc;
		fc = listNext(fc);
		release_cookie( trash);
	}
}

static
void release_cookie( FCOOKIE *fc) {
	release_folder( fc->folder);
	free( fc->name);
	free( fc);
}

static
FCOOKIE *dup_cookie( FCOOKIE *s) {
	FCOOKIE *fc;
	fc = malloc(sizeof(FCOOKIE));
	fc->name = strdup(s->name);
	fc->attr = s->attr;
	fc->folder = s->folder;
	fc->bos_dev = s->bos_dev;
	fc->bos_info_flags = s->bos_info_flags;
	return fc;
}

long bosfs_initialize(void) {
	int i;
	metainit_t metainit={0,0,0,0};
	FCOOKIE *fc;

	Metainit(&metainit);
	TRACE(("MetaDOS version=%s, drives=%x\n", metainit.version, metainit.drives_map));
	if (metainit.version == NULL) {
		DEBUG(("MetaDOS not installed\n"));
		return -1;
	}

	if (metainit.drives_map == 0) {
		DEBUG(("No MetaDOS devices present\n"));
		return -1;
	}

	listInit( &root);

	{ /* create the x:\dev folder */
		fc = malloc(sizeof(FCOOKIE));
		fc->name = strdup("dev");
		fc->attr = FA_DIR;
		fc->folder = &root_dev;
		fc->bos_dev = 0;

		/* add it to the folder */
		listInsert( root.head.next, (LINKABLE*)fc);
	}

	listInit( &root_dev);

	{ /* create the x:\dev\phys folder */
		fc = malloc(sizeof(FCOOKIE));
		fc->name = strdup("bos");
		fc->attr = FA_DIR;
		fc->folder = &root_dev_phys;
		fc->bos_dev = 0;

		/* add it to the folder */
		listInsert( root_dev.head.next, (LINKABLE*)fc);
	}

	listInit( &root_dev_phys);

	for (i='A'; i<='Z'; i++) if (metainit.drives_map & (1<<(i-'A'))) {
		metaopen_t metaopen;
		int handle;
		char name[] = "A";
		name[0] = i;

		fc = malloc(sizeof(FCOOKIE));
		fc->name = strdup(name);
		fc->attr = 0;
		fc->folder = NULL;
		fc->bos_dev = i;
		fc->bos_info_flags = BOS_INFO_DEVHIDDEN; /* by default hide from U:\dev */

		DEBUG(("dev: %s->%s\n", name, fc->name));

		/* attempt to get the bos_info */
		handle = Metaopen(i, &metaopen);
		if (handle == 0) {
			bos_info_t info;
			if ( ! Metaioctl(i, METADOS_IOCTL_MAGIC, METADOS_IOCTL_BOSINFO, &info)) {
				DEBUG(("ioctl: %s->%s %lx\n", name, fc->name, info.flags));
				fc->bos_info_flags = info.flags;
			}
			Metaclose(i);

			DEBUG(("dev: %s->%s %lx\n", name, fc->name, fc->bos_info_flags));

			/* the BOSINFO states that the device should not be visible
			 * in x:\dev\bos nor in x:\dev\bos */
			if ( fc->bos_info_flags & BOS_INFO_BOSHIDDEN ) {
				release_cookie( fc );
				continue;
			}

			/* insert the devices that have a decent name also to u:\dev */
			if ( ! (fc->bos_info_flags & BOS_INFO_DEVHIDDEN) && *metaopen.name ) {
				FCOOKIE *cfc = dup_cookie(fc);
				free( cfc->name);
				cfc->name = strdup(metaopen.name);
				listInsert( &root_dev.tail, (LINKABLE*)cfc);
			}
		}

		/* add it to the folder */
		listInsert( root_dev_phys.head.next, (LINKABLE*)fc);
	}

	return 0;
}


long
getxattr (FCOOKIE *fc, struct xattr *res)
{
	res->mode = 0666;
        res->mode |= fc->attr & FA_DIR ? (S_IFDIR|0111) : 0;

	res->index = (long)fc;
	res->dev = (long)&root;
	res->rdev = (long)&root;
	res->nlink = 1;
	res->uid = 0;
	res->gid = 0;
	res->size = 0;
	res->blksize = 512;
	res->nblocks = ( res->size + res->blksize - 1) >> 9;
	// FIXME!
	res->mtime = 0;
	res->mdate = 0;
	res->atime = 0;
	res->adate = 0;
	res->ctime = 0;
	res->cdate = 0;
	res->attr = fc->attr;
	res->reserved2 = 0;
	res->reserved3[0] = 0;
	res->reserved3[1] = 0;
	return 0;
}


long
name2cookie (LIST *folder, const char *name, FCOOKIE **res)
{
	char lowname[32];
	FCOOKIE* fc;

	DEBUG(( "name2cookie: name=%s\n", name));

	/* look for 'name' in the folder */
	listForEach( FCOOKIE*, fc, folder) {
		char *c = lowname;
		char *f = fc->name;
		while ( (*c++ = tolower(*f++)) ) ;

		DEBUG(( "name2cookie: fc->name=%s\n", fc->name));
		if ( ! strcmp( lowname, name) ) {
			*res = fc;
			return 0;
		}
	}

	return -ENOENT;
}

/*
 * routines for parsing path names
 */

/*
 * relpath2cookie converts a TOS file name into a file cookie representing
 * the directory the file resides in, and a character string representing
 * the name of the file in that directory. The character string is
 * copied into the "lastname" array. If lastname is NULL, then the cookie
 * returned actually represents the file, instead of just the directory
 * the file is in.
 *
 * note that lastname, if non-null, should be big enough to contain all the
 * characters in "path", since if the file system doesn't want the kernel
 * to do path name parsing we may end up just copying path to lastname
 * and returning the current or root directory, as appropriate
 *
 * "dir" is the directory relative to which the search should start.
 * if you just want the current directory, use path2cookie instead.
 *
 */

# define DIRSEP(p)  (((p) == '\\') || ((p) == '/'))

long
relpath2cookie (FCOOKIE *dir, const char *path, char *lastname, FCOOKIE **res)
{
	long r = 0;
	short do_last = (lastname == NULL);

	char temp[PATH_MAX];
	if ( ! lastname ) lastname = temp;
	
	DEBUG(( "relpath2cookie: dir=%s path=%s\n", dir->name, path));

	/* first, check for a drive letter
	 */
	if (path[1] == ':')
	{
		char c = tolower ((int)path[0] & 0xff);
		if (c >= 'a' && c <= 'z')
			path += 2;
		else if (c >= '1' && c <= '6')
			path += 2;
	}
	
	while (*path)
	{
		/* now we must have a directory, since there are more things
		 * in the path
		 */
		if ( ! (dir->attr & FA_DIR) )
			return -ENOTDIR;

		/*  skip slashes
		*/
		while (DIRSEP (*path))
			path++;

		/* if there's nothing left in the path, we can break here
		 */
		if (!*path)
		{
			*res = dir;
			*lastname = '\0';
			return 0;
		}

		/* next, peel off the next name in the path
		*/
		{
			register int len;
			register char c, *s;

			len = 0;
			s = lastname;
			c = *path;
			while (c && !DIRSEP (c))
			{
				if (len++ < PATH_MAX)
					*s++ = tolower(c);
				c = *++path;
			}

			*s = 0;
		}

		/* if there are no more names in the path, and we don't want
		 * to actually look up the last name, then we're done
		 */
		if (!do_last && !*path)
		{
			*res = dir;
			return 0;
		}

		r = name2cookie (dir->folder, lastname, res);
		if (r)
		{
			if (r == -ENOENT && *path)
			{
				/* the "file" we didn't find was treated as a directory */
				return -ENOTDIR;
			}
			return r;
		}

		dir = *res;
	}

	return 0;
}

long
path2cookie (const char *path, char *lastname, FCOOKIE **res)
{
	static FCOOKIE rootfc;
	long r;

	rootfc.name = "devdir:"; // FIXME: DEBUG!
	rootfc.attr = FA_DIR;
	rootfc.folder = &root;
	rootfc.bos_dev = 0;
	r  = relpath2cookie ( &rootfc, path, lastname, res);
	DEBUG(( "path2cookie: r=%d\n", r));
	return r;
}