1 /* $NetBSD: sdread.c,v 1.2 2009/10/13 18:41:06 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 <ufs/ufs/ufsmount.h> 33 #include <msdosfs/msdosfsmount.h> 34 35 #include <rump/rump.h> 36 #include <rump/rump_syscalls.h> 37 38 #include <err.h> 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 /* 47 * Proof-of-concept program: 48 * 49 * Mount rump file system from device driver stack included in the 50 * rump kernel. Optionally copy a file out of the mounted file system. 51 */ 52 53 int 54 main(int argc, char *argv[]) 55 { 56 char buf[2048]; 57 struct msdosfs_args args; 58 struct ufs_args uargs; 59 struct dirent *dp; 60 const char *msg = NULL; 61 int fd, n, fd_h, sverrno; 62 63 if (argc > 1 && argc != 3) { 64 fprintf(stderr, "usage: a.out [src hostdest]\n"); 65 exit(1); 66 } 67 68 memset(&args, 0, sizeof(args)); 69 args.fspec = strdup("/dev/sd0e"); 70 args.version = MSDOSFSMNT_VERSION; 71 72 memset(&uargs, 0, sizeof(uargs)); 73 uargs.fspec = strdup("/dev/sd0e"); 74 75 rump_init(); 76 77 if (rump_sys_mkdir("/mp", 0777) == -1) 78 err(1, "mkdir"); 79 if (rump_sys_mount(MOUNT_MSDOS, "/mp", MNT_RDONLY, 80 &args, sizeof(args)) == -1) { 81 if (rump_sys_mount(MOUNT_FFS, "/mp", MNT_RDONLY, 82 &uargs, sizeof(uargs)) == -1) { 83 err(1, "mount"); 84 } 85 } 86 87 fd = rump_sys_open("/mp", O_RDONLY, 0); 88 if (fd == -1) { 89 msg = "open dir"; 90 goto out; 91 } 92 93 while ((n = rump_sys_getdents(fd, buf, sizeof(buf))) > 0) { 94 for (dp = (struct dirent *)buf; 95 (char *)dp - buf < n; 96 dp = _DIRENT_NEXT(dp)) { 97 printf("%" PRIu64 ": %s\n", dp->d_fileno, dp->d_name); 98 } 99 } 100 rump_sys_close(fd); 101 if (argc == 1) 102 goto out; 103 104 rump_sys_chdir("/mp"); 105 fd = rump_sys_open(argv[1], O_RDONLY, 0); 106 if (fd == -1) { 107 msg = "open fs file"; 108 goto out; 109 } 110 111 fd_h = open(argv[2], O_RDWR | O_CREAT, 0777); 112 if (fd_h == -1) { 113 msg = "open host file"; 114 goto out; 115 } 116 117 while ((n = rump_sys_read(fd, buf, sizeof(buf))) == sizeof(buf)) { 118 if (write(fd_h, buf, sizeof(buf)) != sizeof(buf)) { 119 msg = "write host file"; 120 goto out; 121 } 122 } 123 if (n == -1) { 124 msg = "read fs file"; 125 goto out; 126 } 127 128 if (n > 0) { 129 if (write(fd_h, buf, n) == -1) 130 msg = "write tail"; 131 } 132 133 out: 134 sverrno = errno; 135 rump_sys_chdir("/"); 136 rump_sys_close(fd); 137 close(fd_h); 138 if (rump_sys_unmount("/mp", 0) == -1) 139 err(1, "unmount"); 140 141 if (msg) { 142 errno = sverrno; 143 err(1, "%s", msg); 144 } 145 146 return 0; 147 } 148