add URL parsing functions
[deb_libnfs.git] / examples / nfs-ls.c
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 #define _FILE_OFFSET_BITS 64
19 #define _GNU_SOURCE
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifdef AROS
26 #include "aros_compat.h"
27 #endif
28
29 #ifdef WIN32
30 #include "win32_compat.h"
31 #pragma comment(lib, "ws2_32.lib")
32 WSADATA wsaData;
33 #define PRId64 "ll"
34 #else
35 #include <inttypes.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <sys/statvfs.h>
39 #ifndef AROS
40 #include <sys/statvfs.h>
41 #endif
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdint.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include "libnfs-zdr.h"
55 #include "libnfs.h"
56 #include "libnfs-raw.h"
57 #include "libnfs-raw-mount.h"
58
59 struct client {
60 char *server;
61 char *export;
62 uint32_t mount_port;
63 int is_finished;
64 };
65
66 int recursive = 0, summary = 0;
67
68 void print_usage(void)
69 {
70 fprintf(stderr, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] <url>\n");
71 }
72
73 void process_dir(struct nfs_context *nfs, char *dir, int level) {
74 int ret;
75 struct nfsdirent *nfsdirent;
76 struct statvfs svfs;
77 struct nfsdir *nfsdir;
78 struct stat st;
79
80 if (!level) {
81 printf("Recursion detected!\n");
82 exit(10);
83 }
84
85 ret = nfs_opendir(nfs, dir, &nfsdir);
86 if (ret != 0) {
87 printf("Failed to opendir(\"%s\")\n", dir, nfs_get_error(nfs));
88 exit(10);
89 }
90 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
91 char path[1024];
92
93 if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
94 continue;
95 }
96
97 snprintf(path, 1024, "%s/%s", dir, nfsdirent->name);
98 ret = nfs_stat(nfs, path, &st);
99 if (ret != 0) {
100 fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs));
101 continue;
102 }
103
104 switch (st.st_mode & S_IFMT) {
105 #ifndef WIN32
106 case S_IFLNK:
107 printf("l");
108 break;
109 #endif
110 case S_IFREG:
111 printf("-");
112 break;
113 case S_IFDIR:
114 printf("d");
115 break;
116 case S_IFCHR:
117 printf("c");
118 break;
119 case S_IFBLK:
120 printf("b");
121 break;
122 }
123 printf("%c%c%c",
124 "-r"[!!(st.st_mode & S_IRUSR)],
125 "-w"[!!(st.st_mode & S_IWUSR)],
126 "-x"[!!(st.st_mode & S_IXUSR)]
127 );
128 printf("%c%c%c",
129 "-r"[!!(st.st_mode & S_IRGRP)],
130 "-w"[!!(st.st_mode & S_IWGRP)],
131 "-x"[!!(st.st_mode & S_IXGRP)]
132 );
133 printf("%c%c%c",
134 "-r"[!!(st.st_mode & S_IROTH)],
135 "-w"[!!(st.st_mode & S_IWOTH)],
136 "-x"[!!(st.st_mode & S_IXOTH)]
137 );
138 printf(" %2d", (int) st.st_nlink);
139 printf(" %5d", st.st_uid);
140 printf(" %5d", st.st_gid);
141 printf(" %12" PRId64, st.st_size);
142
143 printf(" %s\n", path + 1);
144
145 if (recursive && (st.st_mode & S_IFMT) == S_IFDIR) {
146 process_dir(nfs, path, level - 1);
147 }
148 }
149 nfs_closedir(nfs, nfsdir);
150 }
151
152 int main(int argc, char *argv[])
153 {
154 struct nfs_context *nfs = NULL;
155 int i, ret = 1, res;
156 uint64_t offset;
157 struct client client;
158 struct nfsfh *nfsfh;
159 struct statvfs stvfs;
160 struct nfs_url *url;
161 exports export, tmp;
162 char *server = NULL, *path = NULL, *strp;
163
164 #ifdef WIN32
165 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
166 printf("Failed to start Winsock2\n");
167 exit(10);
168 }
169 #endif
170
171 #ifdef AROS
172 aros_init_socket();
173 #endif
174
175 if (argc < 2) {
176 fprintf(stderr, "No URL specified.\n");
177 goto finished;
178 }
179
180 for (i=1; i < argc -1; i++) {
181 if (!strcmp(argv[i], "-R") || !strcmp(argv[i], "--recursive")) {
182 recursive++;
183 } else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--summary")) {
184 summary++;
185 } else {
186 goto finished;
187 }
188 }
189
190 nfs = nfs_init_context();
191 if (nfs == NULL) {
192 printf("failed to init context\n");
193 goto finished;
194 }
195
196 url = nfs_parse_url_dir(nfs, argv[argc - 1]);
197 if (url == NULL) {
198 fprintf(stderr, "%s\n", nfs_get_error(nfs));
199 goto finished;
200 }
201
202 client.server = url->server;
203 client.export = url->path;
204 client.is_finished = 0;
205
206 if (nfs_mount(nfs, client.server, client.export) != 0) {
207 fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs));
208 goto finished;
209 }
210
211 process_dir(nfs, "", 16);
212
213 if (summary) {
214 if (nfs_statvfs(nfs, "/", &stvfs) != 0) {
215 goto finished;
216 }
217 printf("\n%12" PRId64 " of %12" PRId64 " bytes free.\n",
218 stvfs.f_frsize * stvfs.f_bfree, stvfs.f_frsize * stvfs.f_blocks);
219 }
220
221 ret = 0;
222
223 finished:
224 if (ret) {
225 print_usage();
226 }
227 free(server);
228 free(path);
229 nfs_destroy_url(url);
230 if (nfs != NULL) {
231 nfs_destroy_context(nfs);
232 }
233 return ret;
234 }
235