1 /*
2 Copyright (C) by Peter Lieven <pl@kamp.de> 2013
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/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.h"
55 #include "libnfs-raw.h"
56 #include "libnfs-raw-mount.h"
57
print_usage(void)58 void print_usage(void)
59 {
60 fprintf(stderr, "Usage: nfs-io [-?|--help|--usage] [stat|creat|trunc|unlink|mkdir|rmdir] <url>\n");
61 }
62
main(int argc,char * argv[])63 int main(int argc, char *argv[])
64 {
65 int ret = 1;
66 struct nfs_context *nfs = NULL;
67 struct nfsfh *nfsfh = NULL;
68 struct nfs_url *url = NULL;
69
70 #ifdef WIN32
71 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
72 printf("Failed to start Winsock2\n");
73 exit(10);
74 }
75 #endif
76
77 #ifdef AROS
78 aros_init_socket();
79 #endif
80
81 if (argc < 3) {
82 fprintf(stderr, "No URL specified.\n");
83 goto finished;
84 }
85
86 nfs = nfs_init_context();
87 if (nfs == NULL) {
88 printf("failed to init context\n");
89 goto finished;
90 }
91
92 url = nfs_parse_url_full(nfs, argv[argc - 1]);
93 if (url == NULL) {
94 fprintf(stderr, "%s\n", nfs_get_error(nfs));
95 goto finished;
96 }
97
98 if (nfs_mount(nfs, url->server, url->path) != 0) {
99 fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs));
100 goto finished;
101 }
102
103 if (!strncmp(argv[1], "creat", 5)) {
104 ret = nfs_creat(nfs, url->file, 0600, &nfsfh);
105 } else if (!strncmp(argv[1], "unlink", 6)) {
106 ret = nfs_unlink(nfs, url->file);
107 } else if (!strncmp(argv[1], "mkdir", 5)) {
108 ret = nfs_mkdir(nfs, url->file);
109 } else if (!strncmp(argv[1], "rmdir", 5)) {
110 ret = nfs_rmdir(nfs, url->file);
111 } else if (!strncmp(argv[1], "trunc", 5)) {
112 ret = nfs_truncate(nfs, url->file, 0);
113 } else if (!strncmp(argv[1], "stat", 4)) {
114 struct nfs_stat_64 st;
115 ret = nfs_stat64(nfs, url->file, &st);
116 if (!ret) {
117 switch (st.nfs_mode & S_IFMT) {
118 #ifndef WIN32
119 case S_IFLNK:
120 printf("l");
121 break;
122 #endif
123 case S_IFREG:
124 printf("-");
125 break;
126 case S_IFDIR:
127 printf("d");
128 break;
129 case S_IFCHR:
130 printf("c");
131 break;
132 case S_IFBLK:
133 printf("b");
134 break;
135 }
136 printf("%c%c%c",
137 "-r"[!!(st.nfs_mode & S_IRUSR)],
138 "-w"[!!(st.nfs_mode & S_IWUSR)],
139 "-x"[!!(st.nfs_mode & S_IXUSR)]
140 );
141 printf("%c%c%c",
142 "-r"[!!(st.nfs_mode & S_IRGRP)],
143 "-w"[!!(st.nfs_mode & S_IWGRP)],
144 "-x"[!!(st.nfs_mode & S_IXGRP)]
145 );
146 printf("%c%c%c",
147 "-r"[!!(st.nfs_mode & S_IROTH)],
148 "-w"[!!(st.nfs_mode & S_IWOTH)],
149 "-x"[!!(st.nfs_mode & S_IXOTH)]
150 );
151 printf(" %2d", (int)st.nfs_nlink);
152 printf(" %5d", (int)st.nfs_uid);
153 printf(" %5d", (int)st.nfs_gid);
154 printf(" %12" PRId64, st.nfs_size);
155 printf("\n");
156 }
157 } else {
158 goto finished;
159 }
160
161 if (ret) {
162 fprintf(stderr, "ERROR: %s\n", nfs_get_error(nfs));
163 }
164
165 finished:
166 if (ret > 0) {
167 print_usage();
168 }
169 nfs_destroy_url(url);
170 if (nfs != NULL) {
171 if (nfsfh) {
172 nfs_close(nfs, nfsfh);
173 }
174 nfs_destroy_context(nfs);
175 }
176 return !!ret;
177 }
178
179