nfsclient-async.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /* Example program using the highlevel async interface.
  15. */
  16. #define SERVER "10.1.1.27"
  17. #define EXPORT "/VIRTUAL"
  18. #define NFSFILE "/BOOKS/Classics/Dracula.djvu"
  19. #define NFSDIR "/BOOKS/Classics/"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <poll.h>
  28. #include <ccan/nfs/nfs.h>
  29. struct client {
  30. char *server;
  31. char *export;
  32. uint32_t mount_port;
  33. struct nfsfh *nfsfh;
  34. int is_finished;
  35. };
  36. void nfs_opendir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
  37. {
  38. struct client *client = private_data;
  39. struct nfsdir *nfsdir = data;
  40. struct nfsdirent *nfsdirent;
  41. if (status < 0) {
  42. printf("opendir failed with \"%s\"\n", (char *)data);
  43. exit(10);
  44. }
  45. printf("opendir successful\n");
  46. while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
  47. printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
  48. }
  49. nfs_closedir(nfs, nfsdir);
  50. client->is_finished = 1;
  51. }
  52. void nfs_close_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
  53. {
  54. struct client *client = private_data;
  55. if (status < 0) {
  56. printf("close failed with \"%s\"\n", (char *)data);
  57. exit(10);
  58. }
  59. printf("close successful\n");
  60. printf("call opendir(%s)\n", NFSDIR);
  61. if (nfs_opendir_async(nfs, NFSDIR, nfs_opendir_cb, client) != 0) {
  62. printf("Failed to start async nfs close\n");
  63. exit(10);
  64. }
  65. }
  66. void nfs_fstat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
  67. {
  68. struct client *client = private_data;
  69. struct stat *st;
  70. if (status < 0) {
  71. printf("fstat call failed with \"%s\"\n", (char *)data);
  72. exit(10);
  73. }
  74. printf("Got reply from server for fstat(%s).\n", NFSFILE);
  75. st = (struct stat *)data;
  76. printf("Mode %04o\n", st->st_mode);
  77. printf("Size %d\n", (int)st->st_size);
  78. printf("Inode %04o\n", (int)st->st_ino);
  79. printf("Close file\n");
  80. if (nfs_close_async(nfs, client->nfsfh, nfs_close_cb, client) != 0) {
  81. printf("Failed to start async nfs close\n");
  82. exit(10);
  83. }
  84. }
  85. void nfs_read_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
  86. {
  87. struct client *client = private_data;
  88. char *read_data;
  89. int i;
  90. if (status < 0) {
  91. printf("read failed with \"%s\"\n", (char *)data);
  92. exit(10);
  93. }
  94. printf("read successful with %d bytes of data\n", status);
  95. read_data = data;
  96. for (i=0;i<16;i++) {
  97. printf("%02x ", read_data[i]&0xff);
  98. }
  99. printf("\n");
  100. printf("Fstat file :%s\n", NFSFILE);
  101. if (nfs_fstat_async(nfs, client->nfsfh, nfs_fstat_cb, client) != 0) {
  102. printf("Failed to start async nfs fstat\n");
  103. exit(10);
  104. }
  105. }
  106. void nfs_open_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
  107. {
  108. struct client *client = private_data;
  109. struct nfsfh *nfsfh;
  110. if (status < 0) {
  111. printf("open call failed with \"%s\"\n", (char *)data);
  112. exit(10);
  113. }
  114. nfsfh = data;
  115. client->nfsfh = nfsfh;
  116. printf("Got reply from server for open(%s). Handle:%p\n", NFSFILE, data);
  117. printf("Read first 16 bytes\n");
  118. if (nfs_pread_async(nfs, nfsfh, 0, 16, nfs_read_cb, client) != 0) {
  119. printf("Failed to start async nfs open\n");
  120. exit(10);
  121. }
  122. }
  123. void nfs_stat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
  124. {
  125. struct client *client = private_data;
  126. struct stat *st;
  127. if (status < 0) {
  128. printf("stat call failed with \"%s\"\n", (char *)data);
  129. exit(10);
  130. }
  131. printf("Got reply from server for stat(%s).\n", NFSFILE);
  132. st = (struct stat *)data;
  133. printf("Mode %04o\n", st->st_mode);
  134. printf("Size %d\n", (int)st->st_size);
  135. printf("Inode %04o\n", (int)st->st_ino);
  136. printf("Open file for reading :%s\n", NFSFILE);
  137. if (nfs_open_async(nfs, NFSFILE, O_RDONLY, nfs_open_cb, client) != 0) {
  138. printf("Failed to start async nfs open\n");
  139. exit(10);
  140. }
  141. }
  142. void nfs_mount_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
  143. {
  144. struct client *client = private_data;
  145. if (status < 0) {
  146. printf("mount/mnt call failed with \"%s\"\n", (char *)data);
  147. exit(10);
  148. }
  149. printf("Got reply from server for MOUNT/MNT procedure.\n");
  150. printf("Stat file :%s\n", NFSFILE);
  151. if (nfs_stat_async(nfs, NFSFILE, nfs_stat_cb, client) != 0) {
  152. printf("Failed to start async nfs stat\n");
  153. exit(10);
  154. }
  155. }
  156. int main(int argc, char *argv[])
  157. {
  158. struct nfs_context *nfs;
  159. struct pollfd pfd;
  160. int ret;
  161. struct client client;
  162. client.server = SERVER;
  163. client.export = EXPORT;
  164. client.is_finished = 0;
  165. nfs = nfs_init_context();
  166. if (nfs == NULL) {
  167. printf("failed to init context\n");
  168. exit(10);
  169. }
  170. ret = nfs_mount_async(nfs, client.server, client.export, nfs_mount_cb, &client);
  171. if (ret != 0) {
  172. printf("Failed to start async nfs mount\n");
  173. exit(10);
  174. }
  175. for (;;) {
  176. pfd.fd = nfs_get_fd(nfs);
  177. pfd.events = nfs_which_events(nfs);
  178. if (poll(&pfd, 1, -1) < 0) {
  179. printf("Poll failed");
  180. exit(10);
  181. }
  182. if (nfs_service(nfs, pfd.revents) < 0) {
  183. printf("nfs_service failed\n");
  184. break;
  185. }
  186. if (client.is_finished) {
  187. break;
  188. }
  189. }
  190. nfs_destroy_context(nfs);
  191. printf("nfsclient finished\n");
  192. return 0;
  193. }