File: bmod_populate.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (155 lines) | stat: -rw-r--r-- 4,101 bytes parent folder | download
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
/*
 * Copyright (c) 1999 The University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

#include <oskit/fs/dir.h>
#include <oskit/io/absio.h>
#include <oskit/io/bufio.h>
#include <oskit/machine/multiboot.h>
#include <oskit/machine/base_multiboot.h>
#include <oskit/machine/base_vm.h>
#include <oskit/fs/memfs.h>
#include <stdio.h>
#include <string.h>
#include <oskit/boolean.h>
#include <oskit/c/assert.h>
#include <oskit/io/blkio.h>

/*
 * Populate a directory with the contents of the multiboot bmods
 *
 * If the free_routine is passed in, this routine will free each
 * bmod as it is entered into the filesystem.  The free routine
 * will be called with the start address & length of the bmod.
 * (that's the virtual address).
 */
  
int
bmod_populate(oskit_dir_t *dest,
	      void (*free_routine)(void *data, oskit_addr_t addr,
				   oskit_size_t len),
	      void *free_data)
{
	int flags, i, mods_count;
	oskit_addr_t mods_addr, file_offs;
	oskit_dir_t *sub, *addto;
	oskit_absio_t *thefilebuf;
	oskit_file_t *thefile;
        struct multiboot_module *m;
	oskit_u32_t actual;

	flags = boot_info.flags;
	mods_count = boot_info.mods_count;
	mods_addr = phystokv(boot_info.mods_addr);

	m = (struct multiboot_module *)mods_addr;

	if (!dest) {
		return -1;
	}

	/* Don't bother doing any work if they didn't include any mods */
	if (!(flags & MULTIBOOT_MODS)) {
		return 0;
	}

	for (i = 0; i < mods_count; i++, m++) {
		char *p;
		char *name = (char *) phystokv(m->string);
		oskit_addr_t start, end, blocklen;
		oskit_error_t rc;
		
		/*
		 * Skip any leading slashes in the name.
		 */
		while (*name == '/')
			++name;

		/*
		 * Create any intervening directories in the pathname
		 */

		addto = dest;
		while (1) {
			p = strchr(name, '/');
			if (!p) {
				break;
			}

			*p = '\0';

			rc = oskit_dir_lookup(addto, name,
					      (oskit_file_t **)&sub);

			if (rc == 0) {
				addto = sub;
				oskit_dir_release(sub);
			} else {
				assert(rc == OSKIT_ENOENT);
				rc = oskit_dir_mkdir(addto, name, 0777);
				assert(rc == 0);
				rc = oskit_dir_lookup(addto, name,
						      (oskit_file_t **)&sub);
				assert(rc == 0);
				addto = sub;
				oskit_dir_release(sub);
			}
			name = p + 1;
		}
		/* Create a file in the directory */
		rc = oskit_dir_create(addto, name, 1, 0666, &thefile);
		if (rc == OSKIT_EEXIST) {
			printf("bmod_populate: `%s' already exists, ignored\n",
			       (char *)m->string);
			if (free_routine) {
				start = phystokv(m->mod_start);
				end = phystokv(m->mod_end);
				free_routine(free_data, start, end-start);
			}
			continue;
		}
		assert (rc == 0);
		/* Grab its absio interface */
		rc = oskit_file_query(thefile, &oskit_absio_iid,
				      (void **)&thefilebuf);
		assert (rc == 0);

		start = phystokv(m->mod_start);
		end = phystokv(m->mod_end);

		file_offs = 0;
		while (start < end) {
			blocklen = end - start;
			rc = oskit_bufio_write((oskit_bufio_t *)thefilebuf,
					       (char *)start,
					       file_offs, blocklen,
					       &actual);
			assert(rc == 0);
			start += actual;
			file_offs += actual;
		}
		if (free_routine) {
			start = phystokv(m->mod_start);
			free_routine(free_data, start, (end-start));
		}
		oskit_absio_release(thefilebuf);
		oskit_file_sync(thefile, TRUE);
		oskit_file_release(thefile);

	}	

	return 0;
}