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