| 1 | /* |
| 2 | Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010 |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /* Example program using the highlevel sync interface |
| 19 | */ |
| 20 | |
| 21 | #define SERVER "10.1.1.27" |
| 22 | #define EXPORT "/VIRTUAL" |
| 23 | #define NFSFILE "/BOOKS/Classics/Dracula.djvu.truncated" |
| 24 | #define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed" |
| 25 | #define NFSFILEW "/BOOKS/Classics/foo" |
| 26 | #define NFSDIR "/BOOKS/Classics/" |
| 27 | |
| 28 | #define _GNU_SOURCE |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdint.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/statvfs.h> |
| 35 | #include <unistd.h> |
| 36 | #include <fcntl.h> |
| 37 | #include "libnfs.h" |
| 38 | #include <rpc/rpc.h> /* for authunix_create() */ |
| 39 | #include "libnfs-raw.h" |
| 40 | #include "libnfs-raw-mount.h" |
| 41 | |
| 42 | struct client { |
| 43 | char *server; |
| 44 | char *export; |
| 45 | uint32_t mount_port; |
| 46 | int is_finished; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | char buf[3*1024*1024+337]; |
| 51 | |
| 52 | int main(int argc _U_, char *argv[] _U_) |
| 53 | { |
| 54 | struct nfs_context *nfs; |
| 55 | int i, ret; |
| 56 | struct client client; |
| 57 | struct stat st; |
| 58 | struct nfsfh *nfsfh; |
| 59 | struct nfsdir *nfsdir; |
| 60 | struct nfsdirent *nfsdirent; |
| 61 | client.server = SERVER; |
| 62 | client.export = EXPORT; |
| 63 | client.is_finished = 0; |
| 64 | off_t offset; |
| 65 | struct statvfs svfs; |
| 66 | exports export, tmp; |
| 67 | |
| 68 | export = mount_getexports(SERVER); |
| 69 | if (export != NULL) { |
| 70 | printf("exports on server %s\n", SERVER); |
| 71 | tmp = export; |
| 72 | while (tmp != NULL) { |
| 73 | printf("Export: %s\n", tmp->ex_dir); |
| 74 | tmp = tmp->ex_next; |
| 75 | } |
| 76 | |
| 77 | mount_free_export_list(export); |
| 78 | } else { |
| 79 | printf("no exports on server %s\n", SERVER); |
| 80 | } |
| 81 | |
| 82 | nfs = nfs_init_context(); |
| 83 | if (nfs == NULL) { |
| 84 | printf("failed to init context\n"); |
| 85 | exit(10); |
| 86 | } |
| 87 | |
| 88 | ret = nfs_mount(nfs, client.server, client.export); |
| 89 | if (ret != 0) { |
| 90 | printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); |
| 91 | exit(10); |
| 92 | } |
| 93 | printf("mounted share successfully %s\n", nfs_get_error(nfs)); |
| 94 | |
| 95 | |
| 96 | ret = nfs_stat(nfs, NFSFILE, &st); |
| 97 | if (ret != 0) { |
| 98 | printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 99 | exit(10); |
| 100 | } |
| 101 | printf("Mode %04o\n", st.st_mode); |
| 102 | printf("Size %d\n", (int)st.st_size); |
| 103 | printf("Inode %04o\n", (int)st.st_ino); |
| 104 | |
| 105 | ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh); |
| 106 | if (ret != 0) { |
| 107 | printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 108 | exit(10); |
| 109 | } |
| 110 | |
| 111 | #if 0 |
| 112 | ret = nfs_read(nfs, nfsfh, 16, buf); |
| 113 | if (ret < 0) { |
| 114 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 115 | exit(10); |
| 116 | } |
| 117 | printf("read %d bytes\n", ret); |
| 118 | for (i=0;i<16;i++) { |
| 119 | printf("%02x ", buf[i]&0xff); |
| 120 | } |
| 121 | printf("\n"); |
| 122 | #endif |
| 123 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
| 124 | if (ret < 0) { |
| 125 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 126 | exit(10); |
| 127 | } |
| 128 | printf("read %d bytes\n", ret); |
| 129 | for (i=0;i<16;i++) { |
| 130 | printf("%02x ", buf[i]&0xff); |
| 131 | } |
| 132 | printf("\n"); |
| 133 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
| 134 | if (ret < 0) { |
| 135 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 136 | exit(10); |
| 137 | } |
| 138 | printf("read %d bytes\n", ret); |
| 139 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
| 140 | if (ret < 0) { |
| 141 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 142 | exit(10); |
| 143 | } |
| 144 | printf("read %d bytes\n", ret); |
| 145 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
| 146 | if (ret < 0) { |
| 147 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 148 | exit(10); |
| 149 | } |
| 150 | printf("read %d bytes\n", ret); |
| 151 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
| 152 | if (ret < 0) { |
| 153 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 154 | exit(10); |
| 155 | } |
| 156 | printf("read %d bytes\n", ret); |
| 157 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
| 158 | if (ret < 0) { |
| 159 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 160 | exit(10); |
| 161 | } |
| 162 | printf("read %d bytes\n", ret); |
| 163 | |
| 164 | ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset); |
| 165 | if (ret < 0) { |
| 166 | printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 167 | exit(10); |
| 168 | } |
| 169 | printf("File position is %d\n", (int)offset); |
| 170 | |
| 171 | printf("seek to end of file\n"); |
| 172 | ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset); |
| 173 | if (ret < 0) { |
| 174 | printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 175 | exit(10); |
| 176 | } |
| 177 | printf("File position is %d\n", (int)offset); |
| 178 | |
| 179 | ret = nfs_fstat(nfs, nfsfh, &st); |
| 180 | if (ret != 0) { |
| 181 | printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 182 | exit(10); |
| 183 | } |
| 184 | printf("Mode %04o\n", st.st_mode); |
| 185 | printf("Size %d\n", (int)st.st_size); |
| 186 | printf("Inode %04o\n", (int)st.st_ino); |
| 187 | |
| 188 | |
| 189 | ret = nfs_close(nfs, nfsfh); |
| 190 | if (ret < 0) { |
| 191 | printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs)); |
| 192 | exit(10); |
| 193 | } |
| 194 | |
| 195 | ret = nfs_opendir(nfs, NFSDIR, &nfsdir); |
| 196 | if (ret != 0) { |
| 197 | printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 198 | exit(10); |
| 199 | } |
| 200 | while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) { |
| 201 | char *filename = NULL; |
| 202 | printf("Inode:%d Name:%s ", (int)nfsdirent->inode, nfsdirent->name); |
| 203 | asprintf(&filename, "%s/%s", NFSDIR, nfsdirent->name); |
| 204 | ret = nfs_open(nfs, filename, O_RDONLY, &nfsfh); |
| 205 | free(filename); |
| 206 | if (ret != 0) { |
| 207 | printf("Failed to open(%s) %s\n", filename, nfs_get_error(nfs)); |
| 208 | exit(10); |
| 209 | } |
| 210 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
| 211 | if (ret < 0) { |
| 212 | printf("Error reading file\n"); |
| 213 | exit(10); |
| 214 | } |
| 215 | printf("Read %d bytes\n", ret); |
| 216 | ret = nfs_close(nfs, nfsfh); |
| 217 | if (ret < 0) { |
| 218 | printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs)); |
| 219 | exit(10); |
| 220 | } |
| 221 | } |
| 222 | nfs_closedir(nfs, nfsdir); |
| 223 | |
| 224 | |
| 225 | ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh); |
| 226 | if (ret != 0) { |
| 227 | printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); |
| 228 | exit(10); |
| 229 | } |
| 230 | ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf); |
| 231 | if (ret < 0) { |
| 232 | printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); |
| 233 | exit(10); |
| 234 | } |
| 235 | ret = nfs_fsync(nfs, nfsfh); |
| 236 | if (ret < 0) { |
| 237 | printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); |
| 238 | exit(10); |
| 239 | } |
| 240 | ret = nfs_close(nfs, nfsfh); |
| 241 | if (ret < 0) { |
| 242 | printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); |
| 243 | exit(10); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | ret = nfs_statvfs(nfs, NFSDIR, &svfs); |
| 248 | if (ret < 0) { |
| 249 | printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs)); |
| 250 | exit(10); |
| 251 | } |
| 252 | printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail); |
| 253 | |
| 254 | |
| 255 | ret = nfs_access(nfs, NFSFILE, R_OK); |
| 256 | if (ret != 0) { |
| 257 | printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 258 | } |
| 259 | |
| 260 | /* become root */ |
| 261 | nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL)); |
| 262 | |
| 263 | ret = nfs_link(nfs, NFSFILE, NFSFILER); |
| 264 | if (ret != 0) { |
| 265 | printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs)); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | nfs_destroy_context(nfs); |
| 270 | printf("nfsclient finished\n"); |
| 271 | return 0; |
| 272 | } |
| 273 | |