sdread.c revision 1.2
11.2Spooka/* $NetBSD: sdread.c,v 1.2 2009/10/13 18:41:06 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.2Spooka#include <ufs/ufs/ufsmount.h> 331.1Spooka#include <msdosfs/msdosfsmount.h> 341.1Spooka 351.1Spooka#include <rump/rump.h> 361.1Spooka#include <rump/rump_syscalls.h> 371.1Spooka 381.1Spooka#include <err.h> 391.1Spooka#include <errno.h> 401.1Spooka#include <fcntl.h> 411.1Spooka#include <stdio.h> 421.1Spooka#include <stdlib.h> 431.1Spooka#include <string.h> 441.1Spooka#include <unistd.h> 451.1Spooka 461.1Spooka/* 471.1Spooka * Proof-of-concept program: 481.1Spooka * 491.1Spooka * Mount rump file system from device driver stack included in the 501.1Spooka * rump kernel. Optionally copy a file out of the mounted file system. 511.1Spooka */ 521.1Spooka 531.1Spookaint 541.1Spookamain(int argc, char *argv[]) 551.1Spooka{ 561.1Spooka char buf[2048]; 571.1Spooka struct msdosfs_args args; 581.2Spooka struct ufs_args uargs; 591.1Spooka struct dirent *dp; 601.1Spooka const char *msg = NULL; 611.1Spooka int fd, n, fd_h, sverrno; 621.1Spooka 631.1Spooka if (argc > 1 && argc != 3) { 641.1Spooka fprintf(stderr, "usage: a.out [src hostdest]\n"); 651.1Spooka exit(1); 661.1Spooka } 671.1Spooka 681.1Spooka memset(&args, 0, sizeof(args)); 691.1Spooka args.fspec = strdup("/dev/sd0e"); 701.1Spooka args.version = MSDOSFSMNT_VERSION; 711.1Spooka 721.2Spooka memset(&uargs, 0, sizeof(uargs)); 731.2Spooka uargs.fspec = strdup("/dev/sd0e"); 741.2Spooka 751.1Spooka rump_init(); 761.1Spooka 771.1Spooka if (rump_sys_mkdir("/mp", 0777) == -1) 781.1Spooka err(1, "mkdir"); 791.1Spooka if (rump_sys_mount(MOUNT_MSDOS, "/mp", MNT_RDONLY, 801.2Spooka &args, sizeof(args)) == -1) { 811.2Spooka if (rump_sys_mount(MOUNT_FFS, "/mp", MNT_RDONLY, 821.2Spooka &uargs, sizeof(uargs)) == -1) { 831.2Spooka err(1, "mount"); 841.2Spooka } 851.2Spooka } 861.1Spooka 871.1Spooka fd = rump_sys_open("/mp", O_RDONLY, 0); 881.1Spooka if (fd == -1) { 891.1Spooka msg = "open dir"; 901.1Spooka goto out; 911.1Spooka } 921.1Spooka 931.1Spooka while ((n = rump_sys_getdents(fd, buf, sizeof(buf))) > 0) { 941.1Spooka for (dp = (struct dirent *)buf; 951.1Spooka (char *)dp - buf < n; 961.1Spooka dp = _DIRENT_NEXT(dp)) { 971.1Spooka printf("%" PRIu64 ": %s\n", dp->d_fileno, dp->d_name); 981.1Spooka } 991.1Spooka } 1001.1Spooka rump_sys_close(fd); 1011.1Spooka if (argc == 1) 1021.1Spooka goto out; 1031.1Spooka 1041.1Spooka rump_sys_chdir("/mp"); 1051.1Spooka fd = rump_sys_open(argv[1], O_RDONLY, 0); 1061.1Spooka if (fd == -1) { 1071.1Spooka msg = "open fs file"; 1081.1Spooka goto out; 1091.1Spooka } 1101.1Spooka 1111.1Spooka fd_h = open(argv[2], O_RDWR | O_CREAT, 0777); 1121.1Spooka if (fd_h == -1) { 1131.1Spooka msg = "open host file"; 1141.1Spooka goto out; 1151.1Spooka } 1161.1Spooka 1171.1Spooka while ((n = rump_sys_read(fd, buf, sizeof(buf))) == sizeof(buf)) { 1181.1Spooka if (write(fd_h, buf, sizeof(buf)) != sizeof(buf)) { 1191.1Spooka msg = "write host file"; 1201.1Spooka goto out; 1211.1Spooka } 1221.1Spooka } 1231.1Spooka if (n == -1) { 1241.1Spooka msg = "read fs file"; 1251.1Spooka goto out; 1261.1Spooka } 1271.1Spooka 1281.1Spooka if (n > 0) { 1291.1Spooka if (write(fd_h, buf, n) == -1) 1301.1Spooka msg = "write tail"; 1311.1Spooka } 1321.1Spooka 1331.1Spooka out: 1341.1Spooka sverrno = errno; 1351.1Spooka rump_sys_chdir("/"); 1361.1Spooka rump_sys_close(fd); 1371.1Spooka close(fd_h); 1381.1Spooka if (rump_sys_unmount("/mp", 0) == -1) 1391.1Spooka err(1, "unmount"); 1401.1Spooka 1411.1Spooka if (msg) { 1421.1Spooka errno = sverrno; 1431.1Spooka err(1, "%s", msg); 1441.1Spooka } 1451.1Spooka 1461.1Spooka return 0; 1471.1Spooka} 148