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