[BACK]Return to fileio.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / test / fileio

Annotation of prex-old/usr/test/fileio/fileio.c, Revision 1.1

1.1     ! nbrk        1: /*-
        !             2:  * Copyright (c) 2007, Kohsuke Ohtani
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms, with or without
        !             6:  * modification, are permitted provided that the following conditions
        !             7:  * are met:
        !             8:  * 1. Redistributions of source code must retain the above copyright
        !             9:  *    notice, this list of conditions and the following disclaimer.
        !            10:  * 2. Redistributions in binary form must reproduce the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer in the
        !            12:  *    documentation and/or other materials provided with the distribution.
        !            13:  * 3. Neither the name of the author nor the names of any co-contributors
        !            14:  *    may be used to endorse or promote products derived from this software
        !            15:  *    without specific prior written permission.
        !            16:  *
        !            17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        !            18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        !            21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            27:  * SUCH DAMAGE.
        !            28:  */
        !            29:
        !            30: /*
        !            31:  * fileio.c - file I/O test program
        !            32:  */
        !            33:
        !            34: #include <prex/prex.h>
        !            35: #include <server/stdmsg.h>
        !            36: #include <server/object.h>
        !            37:
        !            38: #include <sys/syslog.h>
        !            39: #include <sys/mount.h>
        !            40: #include <sys/fcntl.h>
        !            41:
        !            42: #include <unistd.h>
        !            43: #include <string.h>
        !            44: #include <stdio.h>
        !            45: #include <errno.h>
        !            46:
        !            47: #define IOBUFSZ        512
        !            48: #define READ_TARGET    "/boot/LICENSE"
        !            49: #define WRITE_TARGET   "/tmp/test"
        !            50:
        !            51: static char iobuf[IOBUFSZ];
        !            52:
        !            53: static void
        !            54: test_write(void)
        !            55: {
        !            56:        int fd, i;
        !            57:
        !            58:        if ((fd = open(WRITE_TARGET, O_CREAT|O_RDWR, 0)) < 0)
        !            59:                panic("can not open file " WRITE_TARGET);
        !            60:
        !            61:        for (i = 0; i < 50; i++) {
        !            62:                memset(iobuf, i, IOBUFSZ);
        !            63:                write(fd, iobuf, IOBUFSZ);
        !            64:        }
        !            65:        close(fd);
        !            66: }
        !            67:
        !            68: /*
        !            69:  * Display file contents
        !            70:  */
        !            71: static void
        !            72: cat_file(void)
        !            73: {
        !            74:        int rd, fd;
        !            75:
        !            76:        if ((fd = open(READ_TARGET, O_RDONLY, 0)) < 0)
        !            77:                panic("can not open file " READ_TARGET);
        !            78:
        !            79:        while ((rd = read(fd, iobuf, IOBUFSZ)) > 0)
        !            80:                write(STDOUT_FILENO, iobuf, (size_t)rd);
        !            81:        close(fd);
        !            82: }
        !            83:
        !            84: /*
        !            85:  * Test invalid request
        !            86:  */
        !            87: static void
        !            88: test_invalid(void)
        !            89: {
        !            90:        object_t fs_obj;
        !            91:        struct msg m;
        !            92:
        !            93:        object_lookup(OBJNAME_FS, &fs_obj);
        !            94:        m.hdr.code = 0x300;
        !            95:        msg_send(fs_obj, &m, sizeof(m));
        !            96: }
        !            97:
        !            98: /*
        !            99:  * Test open/close
        !           100:  */
        !           101: static void
        !           102: test_open(void)
        !           103: {
        !           104:        int fd;
        !           105:
        !           106:        for (;;) {
        !           107:                if ((fd = open(READ_TARGET, O_RDONLY, 0)) < 0)
        !           108:                        panic("can not open file " READ_TARGET);
        !           109:                close(fd);
        !           110:        }
        !           111: }
        !           112:
        !           113: /*
        !           114:  * Test file read
        !           115:  */
        !           116: static void
        !           117: test_read(void)
        !           118: {
        !           119:        int rd, fd;
        !           120:
        !           121:        if ((fd = open(READ_TARGET, O_RDONLY, 0)) < 0)
        !           122:                panic("can not open file " READ_TARGET);
        !           123:
        !           124:        for (;;)
        !           125:                while ((rd = read(fd, iobuf, IOBUFSZ)) > 0)
        !           126:                        ;
        !           127:
        !           128:        close(fd);
        !           129: }
        !           130:
        !           131: /*
        !           132:  * Main routine
        !           133:  */
        !           134: int
        !           135: main(int argc, char *argv[])
        !           136: {
        !           137:        char test_str[] = "test stdout...\n\n";
        !           138:
        !           139:        syslog(LOG_INFO, "\nfileio: fs test program\n");
        !           140:
        !           141:        /* Wait 1 sec until loading fs server */
        !           142:        timer_sleep(1000, 0);
        !           143:
        !           144:        /*
        !           145:         * Prepare to use a file system.
        !           146:         */
        !           147:        fslib_init();
        !           148:
        !           149:        /*
        !           150:         * Mount file systems
        !           151:         */
        !           152:        mount("", "/", "ramfs", 0, NULL);
        !           153:        mkdir("/dev", 0);
        !           154:        mount("", "/dev", "devfs", 0, NULL);            /* device */
        !           155:        mkdir("/boot", 0);
        !           156:        mount("/dev/ram0", "/boot", "arfs", 0, NULL);   /* archive */
        !           157:        mkdir("/tmp", 0);
        !           158:        /*
        !           159:         * Prepare stdio
        !           160:         */
        !           161:        open("/dev/tty", O_RDWR);       /* stdin */
        !           162:        dup(0);                         /* stdout */
        !           163:        dup(0);                         /* stderr */
        !           164:
        !           165:        /* Test device write */
        !           166:        write(STDOUT_FILENO, test_str, strlen(test_str));
        !           167:
        !           168:        test_write();
        !           169:
        !           170:        cat_file();             /* test read/write */
        !           171:
        !           172:        test_invalid();         /* test invalid request */
        !           173:
        !           174:        test_read();            /* test read loop */
        !           175:
        !           176:        test_open();            /* test open/close loop */
        !           177:
        !           178:        /*
        !           179:         * Disconnect from a file system.
        !           180:         */
        !           181:        fslib_exit();
        !           182:        return 0;
        !           183: }

CVSweb