nfsclient-sync.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 sync interface
  15. */
  16. #define SERVER "10.1.1.27"
  17. #define EXPORT "/VIRTUAL"
  18. #define NFSFILE "/BOOKS/Classics/Dracula.djvu"
  19. #define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed"
  20. #define NFSFILEW "/BOOKS/Classics/foo"
  21. #define NFSDIR "/BOOKS/Classics/"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/statvfs.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include "libnfs.h"
  31. #include <rpc/rpc.h> /* for authunix_create() */
  32. struct client {
  33. char *server;
  34. char *export;
  35. uint32_t mount_port;
  36. int is_finished;
  37. };
  38. int main(int argc, char *argv[])
  39. {
  40. struct nfs_context *nfs;
  41. int i, ret;
  42. struct client client;
  43. struct stat st;
  44. struct nfsfh *nfsfh;
  45. struct nfsdir *nfsdir;
  46. struct nfsdirent *nfsdirent;
  47. client.server = SERVER;
  48. client.export = EXPORT;
  49. client.is_finished = 0;
  50. char buf[16];
  51. off_t offset;
  52. struct statvfs svfs;
  53. nfs = nfs_init_context();
  54. if (nfs == NULL) {
  55. printf("failed to init context\n");
  56. exit(10);
  57. }
  58. ret = nfs_mount_sync(nfs, client.server, client.export);
  59. if (ret != 0) {
  60. printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
  61. exit(10);
  62. }
  63. printf("mounted share successfully\n");
  64. exit(10);
  65. ret = nfs_stat_sync(nfs, NFSFILE, &st);
  66. if (ret != 0) {
  67. printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  68. exit(10);
  69. }
  70. printf("Mode %04o\n", st.st_mode);
  71. printf("Size %d\n", (int)st.st_size);
  72. printf("Inode %04o\n", (int)st.st_ino);
  73. ret = nfs_open_sync(nfs, NFSFILE, O_RDONLY, &nfsfh);
  74. if (ret != 0) {
  75. printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  76. exit(10);
  77. }
  78. ret = nfs_read_sync(nfs, nfsfh, 16, buf);
  79. if (ret < 0) {
  80. printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  81. exit(10);
  82. }
  83. printf("read %d bytes\n", ret);
  84. for (i=0;i<16;i++) {
  85. printf("%02x ", buf[i]&0xff);
  86. }
  87. printf("\n");
  88. ret = nfs_read_sync(nfs, nfsfh, 16, buf);
  89. if (ret < 0) {
  90. printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  91. exit(10);
  92. }
  93. printf("read %d bytes\n", ret);
  94. for (i=0;i<16;i++) {
  95. printf("%02x ", buf[i]&0xff);
  96. }
  97. printf("\n");
  98. ret = (int)nfs_lseek_sync(nfs, nfsfh, 0, SEEK_CUR, &offset);
  99. if (ret < 0) {
  100. printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  101. exit(10);
  102. }
  103. printf("File position is %d\n", (int)offset);
  104. printf("seek to end of file\n");
  105. ret = (int)nfs_lseek_sync(nfs, nfsfh, 0, SEEK_END, &offset);
  106. if (ret < 0) {
  107. printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  108. exit(10);
  109. }
  110. printf("File position is %d\n", (int)offset);
  111. ret = nfs_fstat_sync(nfs, nfsfh, &st);
  112. if (ret != 0) {
  113. printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  114. exit(10);
  115. }
  116. printf("Mode %04o\n", st.st_mode);
  117. printf("Size %d\n", (int)st.st_size);
  118. printf("Inode %04o\n", (int)st.st_ino);
  119. ret = nfs_close_sync(nfs, nfsfh);
  120. if (ret < 0) {
  121. printf("Failed to close(%s)\n", NFSFILE, nfs_get_error(nfs));
  122. exit(10);
  123. }
  124. ret = nfs_opendir_sync(nfs, NFSDIR, &nfsdir);
  125. if (ret != 0) {
  126. printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  127. exit(10);
  128. }
  129. while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
  130. printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
  131. }
  132. nfs_closedir(nfs, nfsdir);
  133. ret = nfs_open_sync(nfs, NFSFILEW, O_WRONLY, &nfsfh);
  134. if (ret != 0) {
  135. printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
  136. exit(10);
  137. }
  138. ret = nfs_pwrite_sync(nfs, nfsfh, 0, 16, buf);
  139. if (ret < 0) {
  140. printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
  141. exit(10);
  142. }
  143. ret = nfs_fsync_sync(nfs, nfsfh);
  144. if (ret < 0) {
  145. printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
  146. exit(10);
  147. }
  148. ret = nfs_close_sync(nfs, nfsfh);
  149. if (ret < 0) {
  150. printf("Failed to close(%s)\n", NFSFILEW, nfs_get_error(nfs));
  151. exit(10);
  152. }
  153. ret = nfs_statvfs_sync(nfs, NFSDIR, &svfs);
  154. if (ret < 0) {
  155. printf("Failed to statvfs(%s)\n", NFSDIR, nfs_get_error(nfs));
  156. exit(10);
  157. }
  158. printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail);
  159. ret = nfs_access_sync(nfs, NFSFILE, R_OK);
  160. if (ret != 0) {
  161. printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  162. }
  163. /* become root */
  164. nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));
  165. ret = nfs_link_sync(nfs, NFSFILE, NFSFILER);
  166. if (ret != 0) {
  167. printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
  168. }
  169. nfs_destroy_context(nfs);
  170. printf("nfsclient finished\n");
  171. return 0;
  172. }