/src/libeio/demo.c

http://github.com/jacksonh/manos · C · 194 lines · 152 code · 40 blank · 2 comment · 10 complexity · fe5038b1706df0efbdc5d24446dc445c MD5 · raw file

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