sdread.c revision 1.1
1/*	$NetBSD: sdread.c,v 1.1 2009/10/05 13:05:31 pooka Exp $	*/
2
3/*
4 * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/types.h>
29#include <sys/dirent.h>
30#include <sys/mount.h>
31
32#include <msdosfs/msdosfsmount.h>
33
34#include <rump/rump.h>
35#include <rump/rump_syscalls.h>
36
37#include <err.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45/*
46 * Proof-of-concept program:
47 *
48 * Mount rump file system from device driver stack included in the
49 * rump kernel.  Optionally copy a file out of the mounted file system.
50 */
51
52int
53main(int argc, char *argv[])
54{
55	char buf[2048];
56	struct msdosfs_args args;
57	struct dirent *dp;
58	const char *msg = NULL;
59	int fd, n, fd_h, sverrno;
60
61	if (argc > 1 && argc != 3) {
62		fprintf(stderr, "usage: a.out [src hostdest]\n");
63		exit(1);
64	}
65
66	memset(&args, 0, sizeof(args));
67	args.fspec = strdup("/dev/sd0e");
68	args.version = MSDOSFSMNT_VERSION;
69
70	rump_init();
71
72	if (rump_sys_mkdir("/mp", 0777) == -1)
73		err(1, "mkdir");
74	if (rump_sys_mount(MOUNT_MSDOS, "/mp", MNT_RDONLY,
75	    &args, sizeof(args)) == -1)
76		err(1, "mount");
77
78	fd = rump_sys_open("/mp", O_RDONLY, 0);
79	if (fd == -1) {
80		msg = "open dir";
81		goto out;
82	}
83
84	while ((n = rump_sys_getdents(fd, buf, sizeof(buf))) > 0) {
85		for (dp = (struct dirent *)buf;
86		    (char *)dp - buf < n;
87		    dp = _DIRENT_NEXT(dp)) {
88			printf("%" PRIu64 ": %s\n", dp->d_fileno, dp->d_name);
89		}
90	}
91	rump_sys_close(fd);
92	if (argc == 1)
93		goto out;
94
95	rump_sys_chdir("/mp");
96	fd = rump_sys_open(argv[1], O_RDONLY, 0);
97	if (fd == -1) {
98		msg = "open fs file";
99		goto out;
100	}
101
102	fd_h = open(argv[2], O_RDWR | O_CREAT, 0777);
103	if (fd_h == -1) {
104		msg = "open host file";
105		goto out;
106	}
107
108	while ((n = rump_sys_read(fd, buf, sizeof(buf))) == sizeof(buf)) {
109		if (write(fd_h, buf, sizeof(buf)) != sizeof(buf)) {
110			msg = "write host file";
111			goto out;
112		}
113	}
114	if (n == -1) {
115		msg = "read fs file";
116		goto out;
117	}
118
119	if (n > 0) {
120		if (write(fd_h, buf, n) == -1)
121			msg = "write tail";
122	}
123
124 out:
125	sverrno = errno;
126	rump_sys_chdir("/");
127	rump_sys_close(fd);
128	close(fd_h);
129	if (rump_sys_unmount("/mp", 0) == -1)
130		err(1, "unmount");
131
132	if (msg) {
133		errno = sverrno;
134		err(1, "%s", msg);
135	}
136
137	return 0;
138}
139