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