ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/demo.c
Revision: 1.1
Committed: Mon May 12 00:31:43 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_02
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <poll.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10
11 #include "eio.h"
12
13 int respipe [2];
14
15 void
16 want_poll (void)
17 {
18 char dummy;
19 printf ("want_poll ()\n");
20 write (respipe [1], &dummy, 1);
21 }
22
23 void
24 done_poll (void)
25 {
26 char dummy;
27 printf ("done_poll ()\n");
28 read (respipe [0], &dummy, 1);
29 }
30
31 void
32 event_loop (void)
33 {
34 // an event loop. yeah.
35 struct pollfd pfd;
36 pfd.fd = respipe [0];
37 pfd.events = POLLIN;
38
39 printf ("\nentering event loop\n");
40 while (eio_nreqs ())
41 {
42 poll (&pfd, 1, -1);
43 printf ("eio_poll () = %d\n", eio_poll ());
44 }
45 printf ("leaving event loop\n");
46 }
47
48 int
49 res_cb (eio_req *req)
50 {
51 printf ("res_cb(%d|%s) = %d\n", req->type, req->data ? req->data : "?", EIO_RESULT (req));
52
53 if (req->result < 0)
54 abort ();
55
56 return 0;
57 }
58
59 int
60 readdir_cb (eio_req *req)
61 {
62 char *buf = (char *)EIO_BUF (req);
63
64 printf ("readdir_cb = %d\n", EIO_RESULT (req));
65
66 if (EIO_RESULT (req) < 0)
67 return 0;
68
69 while (EIO_RESULT (req)--)
70 {
71 printf ("readdir = <%s>\n", buf);
72 buf += strlen (buf) + 1;
73 }
74
75 return 0;
76 }
77
78 int
79 stat_cb (eio_req *req)
80 {
81 struct stat *buf = EIO_STAT_BUF (req);
82
83 if (req->type == EIO_FSTAT)
84 printf ("fstat_cb = %d\n", EIO_RESULT (req));
85 else
86 printf ("stat_cb(%s) = %d\n", EIO_PATH (req), EIO_RESULT (req));
87
88 if (!EIO_RESULT (req))
89 printf ("stat size %d perm 0%o\n", buf->st_size, buf->st_mode & 0777);
90
91 return 0;
92 }
93
94 int
95 read_cb (eio_req *req)
96 {
97 unsigned char *buf = (unsigned char *)EIO_BUF (req);
98
99 printf ("read_cb = %d (%02x%02x%02x%02x %02x%02x%02x%02x)\n",
100 EIO_RESULT (req),
101 buf [0], buf [1], buf [2], buf [3],
102 buf [4], buf [5], buf [6], buf [7]);
103
104 return 0;
105 }
106
107 int last_fd;
108
109 int
110 open_cb (eio_req *req)
111 {
112 printf ("open_cb = %d\n", EIO_RESULT (req));
113
114 last_fd = EIO_RESULT (req);
115
116 return 0;
117 }
118
119 int
120 main (void)
121 {
122 printf ("pipe ()\n");
123 if (pipe (respipe)) abort ();
124
125 printf ("eio_init ()\n");
126 if (eio_init (want_poll, done_poll)) abort ();
127
128 do
129 {
130 /* avoid relative paths yourself(!) */
131 eio_mkdir ("eio-test-dir", 0777, res_cb)
132 ->data = "mkdir";
133 eio_nop (res_cb)
134 ->data = "nop";
135 event_loop ();
136
137 eio_stat ("eio-test-dir", stat_cb);
138 eio_lstat ("eio-test-dir", stat_cb);
139 eio_open ("eio-test-dir/eio-test-file", O_RDWR | O_CREAT, 0777, open_cb);
140 eio_symlink ("test", "eio-test-dir/eio-symlink", res_cb)
141 ->data = "symlink";
142 eio_mknod ("eio-test-dir/eio-fifo", S_IFIFO, 0, res_cb)
143 ->data = "mknod";
144 event_loop ();
145
146 eio_utime ("eio-test-dir", 12345.678, 23456.789, res_cb)
147 ->data = "utime";
148 eio_futime (last_fd, 92345.678, 93456.789, res_cb)
149 ->data = "futime";
150 eio_chown ("eio-test-dir", getuid (), getgid (), res_cb)
151 ->data = "chown";
152 eio_fchown (last_fd, getuid (), getgid (), res_cb)
153 ->data = "fchown";
154 eio_fchmod (last_fd, 0123, res_cb)
155 ->data = "fchmod";
156 eio_readdir ("eio-test-dir", readdir_cb);
157 eio_readdir ("/nonexistant", readdir_cb);
158 eio_fstat (last_fd, stat_cb);
159 eio_write (last_fd, "test\nfail\n", 10, 4, res_cb)
160 ->data = "write";
161 event_loop ();
162
163 eio_read (last_fd, 0, 8, 0, read_cb);
164 eio_readlink ("eio-test-dir/eio-symlink", res_cb)
165 ->data = "readlink";
166 event_loop ();
167
168 eio_dup2 (1, 2, res_cb) // dup stdout to stderr
169 ->data = "dup2";
170 eio_chmod ("eio-test-dir", 0765, res_cb)
171 ->data = "chmod";
172 eio_ftruncate (last_fd, 9, res_cb)
173 ->data = "ftruncate";
174 eio_fdatasync (last_fd, res_cb)
175 ->data = "fdatasync";
176 eio_fsync (last_fd, res_cb)
177 ->data = "fsync";
178 eio_sync (res_cb)
179 ->data = "sync";
180 eio_busy (0.5, res_cb)
181 ->data = "busy";
182 event_loop ();
183
184 eio_sendfile (1, last_fd, 4, 5, res_cb) // write "test\n" to stdout
185 ->data = "sendfile";
186 eio_fstat (last_fd, stat_cb);
187 event_loop ();
188
189 eio_truncate ("eio-test-dir/eio-test-file", 6, res_cb)
190 ->data = "truncate";
191 eio_readahead (last_fd, 0, 64, res_cb)
192 ->data = "readahead";
193 event_loop ();
194
195 eio_close (last_fd, res_cb)
196 ->data = "close";
197 eio_link ("eio-test-dir/eio-test-file", "eio-test-dir/eio-test-file-2", res_cb)
198 ->data = "link";
199 event_loop ();
200
201 eio_rename ("eio-test-dir/eio-test-file", "eio-test-dir/eio-test-file-renamed", res_cb)
202 ->data = "rename";
203 event_loop ();
204
205 eio_unlink ("eio-test-dir/eio-fifo", res_cb)
206 ->data = "unlink";
207 eio_unlink ("eio-test-dir/eio-symlink", res_cb)
208 ->data = "unlink";
209 eio_unlink ("eio-test-dir/eio-test-file-2", res_cb)
210 ->data = "unlink";
211 eio_unlink ("eio-test-dir/eio-test-file-renamed", res_cb)
212 ->data = "unlink";
213 event_loop ();
214
215 eio_rmdir ("eio-test-dir", res_cb)
216 ->data = "rmdir";
217 event_loop ();
218 }
219 while (0);
220
221 return 0;
222 }
223