sdread.c revision 1.1
11.1Spooka/*	$NetBSD: sdread.c,v 1.1 2009/10/05 13:05:31 pooka Exp $	*/
21.1Spooka
31.1Spooka/*
41.1Spooka * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
51.1Spooka *
61.1Spooka * Redistribution and use in source and binary forms, with or without
71.1Spooka * modification, are permitted provided that the following conditions
81.1Spooka * are met:
91.1Spooka * 1. Redistributions of source code must retain the above copyright
101.1Spooka *    notice, this list of conditions and the following disclaimer.
111.1Spooka * 2. Redistributions in binary form must reproduce the above copyright
121.1Spooka *    notice, this list of conditions and the following disclaimer in the
131.1Spooka *    documentation and/or other materials provided with the distribution.
141.1Spooka *
151.1Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
161.1Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
171.1Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
181.1Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
211.1Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Spooka * SUCH DAMAGE.
261.1Spooka */
271.1Spooka
281.1Spooka#include <sys/types.h>
291.1Spooka#include <sys/dirent.h>
301.1Spooka#include <sys/mount.h>
311.1Spooka
321.1Spooka#include <msdosfs/msdosfsmount.h>
331.1Spooka
341.1Spooka#include <rump/rump.h>
351.1Spooka#include <rump/rump_syscalls.h>
361.1Spooka
371.1Spooka#include <err.h>
381.1Spooka#include <errno.h>
391.1Spooka#include <fcntl.h>
401.1Spooka#include <stdio.h>
411.1Spooka#include <stdlib.h>
421.1Spooka#include <string.h>
431.1Spooka#include <unistd.h>
441.1Spooka
451.1Spooka/*
461.1Spooka * Proof-of-concept program:
471.1Spooka *
481.1Spooka * Mount rump file system from device driver stack included in the
491.1Spooka * rump kernel.  Optionally copy a file out of the mounted file system.
501.1Spooka */
511.1Spooka
521.1Spookaint
531.1Spookamain(int argc, char *argv[])
541.1Spooka{
551.1Spooka	char buf[2048];
561.1Spooka	struct msdosfs_args args;
571.1Spooka	struct dirent *dp;
581.1Spooka	const char *msg = NULL;
591.1Spooka	int fd, n, fd_h, sverrno;
601.1Spooka
611.1Spooka	if (argc > 1 && argc != 3) {
621.1Spooka		fprintf(stderr, "usage: a.out [src hostdest]\n");
631.1Spooka		exit(1);
641.1Spooka	}
651.1Spooka
661.1Spooka	memset(&args, 0, sizeof(args));
671.1Spooka	args.fspec = strdup("/dev/sd0e");
681.1Spooka	args.version = MSDOSFSMNT_VERSION;
691.1Spooka
701.1Spooka	rump_init();
711.1Spooka
721.1Spooka	if (rump_sys_mkdir("/mp", 0777) == -1)
731.1Spooka		err(1, "mkdir");
741.1Spooka	if (rump_sys_mount(MOUNT_MSDOS, "/mp", MNT_RDONLY,
751.1Spooka	    &args, sizeof(args)) == -1)
761.1Spooka		err(1, "mount");
771.1Spooka
781.1Spooka	fd = rump_sys_open("/mp", O_RDONLY, 0);
791.1Spooka	if (fd == -1) {
801.1Spooka		msg = "open dir";
811.1Spooka		goto out;
821.1Spooka	}
831.1Spooka
841.1Spooka	while ((n = rump_sys_getdents(fd, buf, sizeof(buf))) > 0) {
851.1Spooka		for (dp = (struct dirent *)buf;
861.1Spooka		    (char *)dp - buf < n;
871.1Spooka		    dp = _DIRENT_NEXT(dp)) {
881.1Spooka			printf("%" PRIu64 ": %s\n", dp->d_fileno, dp->d_name);
891.1Spooka		}
901.1Spooka	}
911.1Spooka	rump_sys_close(fd);
921.1Spooka	if (argc == 1)
931.1Spooka		goto out;
941.1Spooka
951.1Spooka	rump_sys_chdir("/mp");
961.1Spooka	fd = rump_sys_open(argv[1], O_RDONLY, 0);
971.1Spooka	if (fd == -1) {
981.1Spooka		msg = "open fs file";
991.1Spooka		goto out;
1001.1Spooka	}
1011.1Spooka
1021.1Spooka	fd_h = open(argv[2], O_RDWR | O_CREAT, 0777);
1031.1Spooka	if (fd_h == -1) {
1041.1Spooka		msg = "open host file";
1051.1Spooka		goto out;
1061.1Spooka	}
1071.1Spooka
1081.1Spooka	while ((n = rump_sys_read(fd, buf, sizeof(buf))) == sizeof(buf)) {
1091.1Spooka		if (write(fd_h, buf, sizeof(buf)) != sizeof(buf)) {
1101.1Spooka			msg = "write host file";
1111.1Spooka			goto out;
1121.1Spooka		}
1131.1Spooka	}
1141.1Spooka	if (n == -1) {
1151.1Spooka		msg = "read fs file";
1161.1Spooka		goto out;
1171.1Spooka	}
1181.1Spooka
1191.1Spooka	if (n > 0) {
1201.1Spooka		if (write(fd_h, buf, n) == -1)
1211.1Spooka			msg = "write tail";
1221.1Spooka	}
1231.1Spooka
1241.1Spooka out:
1251.1Spooka	sverrno = errno;
1261.1Spooka	rump_sys_chdir("/");
1271.1Spooka	rump_sys_close(fd);
1281.1Spooka	close(fd_h);
1291.1Spooka	if (rump_sys_unmount("/mp", 0) == -1)
1301.1Spooka		err(1, "unmount");
1311.1Spooka
1321.1Spooka	if (msg) {
1331.1Spooka		errno = sverrno;
1341.1Spooka		err(1, "%s", msg);
1351.1Spooka	}
1361.1Spooka
1371.1Spooka	return 0;
1381.1Spooka}
139