Statistics
| Branch: | Revision:

root / prex-0.9.0 / usr / test / fileio / fileio.c @ 03e9c04a

History | View | Annotate | Download (3.91 KB)

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 <sys/prex.h>
35
#include <ipc/ipc.h>
36

    
37
#include <sys/syslog.h>
38
#include <sys/mount.h>
39
#include <sys/fcntl.h>
40

    
41
#include <unistd.h>
42
#include <string.h>
43
#include <stdio.h>
44
#include <errno.h>
45

    
46
#define IOBUFSZ        512
47
#define READ_TARGET        "/boot/LICENSE"
48
#define WRITE_TARGET        "/tmp/test"
49

    
50
static        char iobuf[IOBUFSZ];
51

    
52
static void
53
test_write(void)
54
{
55
        int fd, i;
56

    
57
        if ((fd = open(WRITE_TARGET, O_CREAT|O_RDWR, 0)) < 0)
58
                panic("can not open file " WRITE_TARGET);
59

    
60
        for (i = 0; i < 50; i++) {
61
                memset(iobuf, i, IOBUFSZ);
62
                write(fd, iobuf, IOBUFSZ);
63
        }
64
        close(fd);
65
}
66

    
67
/*
68
 * Display file contents
69
 */
70
static void
71
cat_file(void)
72
{
73
        int rd, fd;
74

    
75
        if ((fd = open(READ_TARGET, O_RDONLY, 0)) < 0)
76
                panic("can not open file " READ_TARGET);
77

    
78
        while ((rd = read(fd, iobuf, IOBUFSZ)) > 0)
79
                write(STDOUT_FILENO, iobuf, (size_t)rd);
80
        close(fd);
81
}
82

    
83
/*
84
 * Test invalid request
85
 */
86
static void
87
test_invalid(void)
88
{
89
        object_t fsobj;
90
        struct msg m;
91

    
92
        object_lookup("!fs", &fsobj);
93
        m.hdr.code = 0x300;
94
        msg_send(fsobj, &m, sizeof(m));
95
}
96

    
97
/*
98
 * Test open/close
99
 */
100
static void
101
test_open(void)
102
{
103
        int fd;
104

    
105
        for (;;) {
106
                if ((fd = open(READ_TARGET, O_RDONLY, 0)) < 0)
107
                        panic("can not open file " READ_TARGET);
108
                close(fd);
109
        }
110
}
111

    
112
/*
113
 * Test file read
114
 */
115
static void
116
test_read(void)
117
{
118
        int rd, fd;
119

    
120
        if ((fd = open(READ_TARGET, O_RDONLY, 0)) < 0)
121
                panic("can not open file " READ_TARGET);
122

    
123
        for (;;)
124
                while ((rd = read(fd, iobuf, IOBUFSZ)) > 0)
125
                        ;
126

    
127
        close(fd);
128
}
129

    
130
/*
131
 * Main routine
132
 */
133
int
134
main(int argc, char *argv[])
135
{
136
        char test_str[] = "test stdout...\n\n";
137

    
138
        syslog(LOG_INFO, "\nfileio: fs test program\n");
139

    
140
        /* Wait 1 sec until loading fs server */
141
        timer_sleep(1000, 0);
142

    
143
        /*
144
         * Prepare to use a file system.
145
         */
146
        fslib_init();
147

    
148
        /*
149
         * Mount file systems
150
         */
151
        mount("", "/", "ramfs", 0, NULL);
152
        mkdir("/dev", 0);
153
        mount("", "/dev", "devfs", 0, NULL);                /* device */
154
        mkdir("/boot", 0);
155
        mount("/dev/ram0", "/boot", "arfs", 0, NULL);        /* archive */
156
        mkdir("/tmp", 0);
157
        /*
158
         * Prepare stdio
159
         */
160
        open("/dev/tty", O_RDWR);        /* stdin */
161
        dup(0);                                /* stdout */
162
        dup(0);                                /* stderr */
163

    
164
        /* Test device write */
165
        write(STDOUT_FILENO, test_str, strlen(test_str));
166

    
167
        test_write();
168

    
169
        cat_file();                /* test read/write */
170

    
171
        test_invalid();                /* test invalid request */
172

    
173
        test_read();                /* test read loop */
174

    
175
        test_open();                /* test open/close loop */
176

    
177
        /*
178
         * Disconnect from a file system.
179
         */
180
        fslib_exit();
181
        return 0;
182
}