1 | /*
|
---|
2 | smbget: a wget-like utility with support for recursive downloading and
|
---|
3 | smb:// urls
|
---|
4 | Copyright (C) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | #include "includes.h"
|
---|
20 | #include "libsmbclient.h"
|
---|
21 |
|
---|
22 | #if _FILE_OFFSET_BITS==64
|
---|
23 | #define OFF_T_FORMAT "%lld"
|
---|
24 | #define OFF_T_FORMAT_CAST long long
|
---|
25 | #else
|
---|
26 | #define OFF_T_FORMAT "%ld"
|
---|
27 | #define OFF_T_FORMAT_CAST long
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | static int columns = 0;
|
---|
31 |
|
---|
32 | static int debuglevel, update;
|
---|
33 | static char *outputfile;
|
---|
34 |
|
---|
35 |
|
---|
36 | static time_t total_start_time = 0;
|
---|
37 | static off_t total_bytes = 0;
|
---|
38 |
|
---|
39 | #define SMB_MAXPATHLEN MAXPATHLEN
|
---|
40 |
|
---|
41 | /* Number of bytes to read when checking whether local and remote file are really the same file */
|
---|
42 | #define RESUME_CHECK_SIZE 512
|
---|
43 | #define RESUME_DOWNLOAD_OFFSET 1024
|
---|
44 | #define RESUME_CHECK_OFFSET RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
|
---|
45 | /* Number of bytes to read at once */
|
---|
46 | #define SMB_DEFAULT_BLOCKSIZE 64000
|
---|
47 |
|
---|
48 | static const char *username = NULL, *password = NULL, *workgroup = NULL;
|
---|
49 | static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
|
---|
50 | static int blocksize = SMB_DEFAULT_BLOCKSIZE;
|
---|
51 |
|
---|
52 | static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile);
|
---|
53 |
|
---|
54 | static int get_num_cols(void)
|
---|
55 | {
|
---|
56 | #ifdef TIOCGWINSZ
|
---|
57 | struct winsize ws;
|
---|
58 | if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
|
---|
59 | return 0;
|
---|
60 | }
|
---|
61 | return ws.ws_col;
|
---|
62 | #else
|
---|
63 | #warning No support for TIOCGWINSZ
|
---|
64 | char *cols = getenv("COLUMNS");
|
---|
65 | if(!cols) return 0;
|
---|
66 | return atoi(cols);
|
---|
67 | #endif
|
---|
68 | }
|
---|
69 |
|
---|
70 | static void change_columns(int sig)
|
---|
71 | {
|
---|
72 | columns = get_num_cols();
|
---|
73 | }
|
---|
74 |
|
---|
75 | static void human_readable(off_t s, char *buffer, int l)
|
---|
76 | {
|
---|
77 | if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGB",
|
---|
78 | 1.0 * s / (1024 * 1024 * 1024));
|
---|
79 | else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMB",
|
---|
80 | 1.0 * s / (1024 * 1024));
|
---|
81 | else if(s > 1024) snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
|
---|
82 | else snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
|
---|
83 | }
|
---|
84 |
|
---|
85 | static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
|
---|
86 | {
|
---|
87 | static char hasasked = 0;
|
---|
88 | char *wgtmp, *usertmp;
|
---|
89 | char tmp[128];
|
---|
90 |
|
---|
91 | if(hasasked) return;
|
---|
92 | hasasked = 1;
|
---|
93 |
|
---|
94 | if(!nonprompt && !username) {
|
---|
95 | printf("Username for %s at %s [guest] ", shr, srv);
|
---|
96 | if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
|
---|
97 | return;
|
---|
98 | }
|
---|
99 | if ((strlen(tmp) > 0) && (tmp[strlen(tmp)-1] == '\n')) {
|
---|
100 | tmp[strlen(tmp)-1] = '\0';
|
---|
101 | }
|
---|
102 | strncpy(un, tmp, unlen-1);
|
---|
103 | } else if(username) strncpy(un, username, unlen-1);
|
---|
104 |
|
---|
105 | if(!nonprompt && !password) {
|
---|
106 | char *prompt, *pass;
|
---|
107 | if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) == -1) {
|
---|
108 | return;
|
---|
109 | }
|
---|
110 | pass = getpass(prompt);
|
---|
111 | free(prompt);
|
---|
112 | strncpy(pw, pass, pwlen-1);
|
---|
113 | } else if(password) strncpy(pw, password, pwlen-1);
|
---|
114 |
|
---|
115 | if(workgroup)strncpy(wg, workgroup, wglen-1);
|
---|
116 |
|
---|
117 | wgtmp = SMB_STRNDUP(wg, wglen);
|
---|
118 | usertmp = SMB_STRNDUP(un, unlen);
|
---|
119 | if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
|
---|
120 | free(wgtmp); free(usertmp);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* Return 1 on error, 0 on success. */
|
---|
124 |
|
---|
125 | static int smb_download_dir(const char *base, const char *name, int resume)
|
---|
126 | {
|
---|
127 | char path[SMB_MAXPATHLEN];
|
---|
128 | int dirhandle;
|
---|
129 | struct smbc_dirent *dirent;
|
---|
130 | const char *relname = name;
|
---|
131 | char *tmpname;
|
---|
132 | struct stat remotestat;
|
---|
133 | int ret = 0;
|
---|
134 |
|
---|
135 | snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
|
---|
136 |
|
---|
137 | /* List files in directory and call smb_download_file on them */
|
---|
138 | dirhandle = smbc_opendir(path);
|
---|
139 | if(dirhandle < 1) {
|
---|
140 | if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL);
|
---|
141 | fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
|
---|
142 | return 1;
|
---|
143 | }
|
---|
144 |
|
---|
145 | while(*relname == '/')relname++;
|
---|
146 | mkdir(relname, 0755);
|
---|
147 |
|
---|
148 | tmpname = SMB_STRDUP(name);
|
---|
149 |
|
---|
150 | while((dirent = smbc_readdir(dirhandle))) {
|
---|
151 | char *newname;
|
---|
152 | if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
|
---|
153 | if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
|
---|
154 | return 1;
|
---|
155 | }
|
---|
156 | switch(dirent->smbc_type) {
|
---|
157 | case SMBC_DIR:
|
---|
158 | ret = smb_download_dir(base, newname, resume);
|
---|
159 | break;
|
---|
160 |
|
---|
161 | case SMBC_WORKGROUP:
|
---|
162 | ret = smb_download_dir("smb://", dirent->name, resume);
|
---|
163 | break;
|
---|
164 |
|
---|
165 | case SMBC_SERVER:
|
---|
166 | ret = smb_download_dir("smb://", dirent->name, resume);
|
---|
167 | break;
|
---|
168 |
|
---|
169 | case SMBC_FILE:
|
---|
170 | ret = smb_download_file(base, newname, 1, resume, NULL);
|
---|
171 | break;
|
---|
172 |
|
---|
173 | case SMBC_FILE_SHARE:
|
---|
174 | ret = smb_download_dir(base, newname, resume);
|
---|
175 | break;
|
---|
176 |
|
---|
177 | case SMBC_PRINTER_SHARE:
|
---|
178 | if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
|
---|
179 | break;
|
---|
180 |
|
---|
181 | case SMBC_COMMS_SHARE:
|
---|
182 | if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
|
---|
183 | break;
|
---|
184 |
|
---|
185 | case SMBC_IPC_SHARE:
|
---|
186 | if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
|
---|
187 | break;
|
---|
188 |
|
---|
189 | default:
|
---|
190 | fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | free(newname);
|
---|
194 | }
|
---|
195 | free(tmpname);
|
---|
196 |
|
---|
197 | if(keep_permissions) {
|
---|
198 | if(smbc_fstat(dirhandle, &remotestat) < 0) {
|
---|
199 | fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
|
---|
200 | smbc_closedir(dirhandle);
|
---|
201 | return 1;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if(chmod(relname, remotestat.st_mode) < 0) {
|
---|
205 | fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname,
|
---|
206 | (unsigned int)remotestat.st_mode);
|
---|
207 | smbc_closedir(dirhandle);
|
---|
208 | return 1;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | smbc_closedir(dirhandle);
|
---|
213 | return ret;
|
---|
214 | }
|
---|
215 |
|
---|
216 | static char *print_time(long t)
|
---|
217 | {
|
---|
218 | static char buffer[100];
|
---|
219 | int secs, mins, hours;
|
---|
220 | if(t < -1) {
|
---|
221 | strncpy(buffer, "Unknown", sizeof(buffer));
|
---|
222 | return buffer;
|
---|
223 | }
|
---|
224 |
|
---|
225 | secs = (int)t % 60;
|
---|
226 | mins = (int)t / 60 % 60;
|
---|
227 | hours = (int)t / (60 * 60);
|
---|
228 | snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
|
---|
229 | return buffer;
|
---|
230 | }
|
---|
231 |
|
---|
232 | static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
|
---|
233 | {
|
---|
234 | double avg = 0.0;
|
---|
235 | long eta = -1;
|
---|
236 | double prcnt = 0.0;
|
---|
237 | char hpos[20], htotal[20], havg[20];
|
---|
238 | char *status, *filename;
|
---|
239 | int len;
|
---|
240 | if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
|
---|
241 | eta = (total - pos) / avg;
|
---|
242 | if(total)prcnt = 100.0 * pos / total;
|
---|
243 |
|
---|
244 | human_readable(pos, hpos, sizeof(hpos));
|
---|
245 | human_readable(total, htotal, sizeof(htotal));
|
---|
246 | human_readable(avg, havg, sizeof(havg));
|
---|
247 |
|
---|
248 | len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
|
---|
249 | if (len == -1) {
|
---|
250 | return;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if(columns) {
|
---|
254 | int required = strlen(name), available = columns - len - strlen("[] ");
|
---|
255 | if(required > available) {
|
---|
256 | if (asprintf(&filename, "...%s", name + required - available + 3) == -1) {
|
---|
257 | return;
|
---|
258 | }
|
---|
259 | } else {
|
---|
260 | filename = SMB_STRNDUP(name, available);
|
---|
261 | }
|
---|
262 | } else filename = SMB_STRDUP(name);
|
---|
263 |
|
---|
264 | fprintf(stderr, "\r[%s] %s", filename, status);
|
---|
265 |
|
---|
266 | free(filename); free(status);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /* Return 1 on error, 0 on success. */
|
---|
270 |
|
---|
271 | static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) {
|
---|
272 | int remotehandle, localhandle;
|
---|
273 | time_t start_time = time(NULL);
|
---|
274 | const char *newpath;
|
---|
275 | char path[SMB_MAXPATHLEN];
|
---|
276 | char checkbuf[2][RESUME_CHECK_SIZE];
|
---|
277 | char *readbuf = NULL;
|
---|
278 | off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
|
---|
279 | struct stat localstat, remotestat;
|
---|
280 |
|
---|
281 | snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
|
---|
282 |
|
---|
283 | remotehandle = smbc_open(path, O_RDONLY, 0755);
|
---|
284 |
|
---|
285 | if(remotehandle < 0) {
|
---|
286 | switch(errno) {
|
---|
287 | case EISDIR:
|
---|
288 | if(!recursive) {
|
---|
289 | fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
|
---|
290 | return 1;
|
---|
291 | }
|
---|
292 | return smb_download_dir(base, name, resume);
|
---|
293 |
|
---|
294 | case ENOENT:
|
---|
295 | fprintf(stderr, "%s can't be found on the remote server\n", path);
|
---|
296 | return 1;
|
---|
297 |
|
---|
298 | case ENOMEM:
|
---|
299 | fprintf(stderr, "Not enough memory\n");
|
---|
300 | return 1;
|
---|
301 |
|
---|
302 | case ENODEV:
|
---|
303 | fprintf(stderr, "The share name used in %s does not exist\n", path);
|
---|
304 | return 1;
|
---|
305 |
|
---|
306 | case EACCES:
|
---|
307 | fprintf(stderr, "You don't have enough permissions to access %s\n", path);
|
---|
308 | return 1;
|
---|
309 |
|
---|
310 | default:
|
---|
311 | perror("smbc_open");
|
---|
312 | return 1;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | if(smbc_fstat(remotehandle, &remotestat) < 0) {
|
---|
317 | fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
|
---|
318 | return 1;
|
---|
319 | }
|
---|
320 |
|
---|
321 | if(outfile) newpath = outfile;
|
---|
322 | else if(!name[0]) {
|
---|
323 | newpath = strrchr(base, '/');
|
---|
324 | if(newpath)newpath++; else newpath = base;
|
---|
325 | } else newpath = name;
|
---|
326 |
|
---|
327 | if(newpath[0] == '/')newpath++;
|
---|
328 |
|
---|
329 | /* Open local file according to the mode */
|
---|
330 | if(update) {
|
---|
331 | /* if it is up-to-date, skip */
|
---|
332 | if(stat(newpath, &localstat) == 0 &&
|
---|
333 | localstat.st_mtime >= remotestat.st_mtime) {
|
---|
334 | if(verbose)
|
---|
335 | printf("%s is up-to-date, skipping\n", newpath);
|
---|
336 | smbc_close(remotehandle);
|
---|
337 | return 0;
|
---|
338 | }
|
---|
339 | /* else open it for writing and truncate if it exists */
|
---|
340 | localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
|
---|
341 | if(localhandle < 0) {
|
---|
342 | fprintf(stderr, "Can't open %s : %s\n", newpath,
|
---|
343 | strerror(errno));
|
---|
344 | smbc_close(remotehandle);
|
---|
345 | return 1;
|
---|
346 | }
|
---|
347 | /* no offset */
|
---|
348 | } else if(!send_stdout) {
|
---|
349 | localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
|
---|
350 | if(localhandle < 0) {
|
---|
351 | fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
|
---|
352 | smbc_close(remotehandle);
|
---|
353 | return 1;
|
---|
354 | }
|
---|
355 |
|
---|
356 | if (fstat(localhandle, &localstat) != 0) {
|
---|
357 | fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno));
|
---|
358 | smbc_close(remotehandle);
|
---|
359 | close(localhandle);
|
---|
360 | return 1;
|
---|
361 | }
|
---|
362 |
|
---|
363 | start_offset = localstat.st_size;
|
---|
364 |
|
---|
365 | if(localstat.st_size && localstat.st_size == remotestat.st_size) {
|
---|
366 | if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
|
---|
367 | else if(!quiet)fprintf(stderr, "%s\n", path);
|
---|
368 | smbc_close(remotehandle);
|
---|
369 | close(localhandle);
|
---|
370 | return 0;
|
---|
371 | }
|
---|
372 |
|
---|
373 | if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
|
---|
374 | offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
|
---|
375 | offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
|
---|
376 | if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
|
---|
377 | "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",
|
---|
378 | newpath, (OFF_T_FORMAT_CAST)offset_check,
|
---|
379 | (OFF_T_FORMAT_CAST)localstat.st_size,
|
---|
380 | (OFF_T_FORMAT_CAST)remotestat.st_size);
|
---|
381 | }
|
---|
382 |
|
---|
383 | if(offset_check) {
|
---|
384 | off_t off1, off2;
|
---|
385 | /* First, check all bytes from offset_check to offset_download */
|
---|
386 | off1 = lseek(localhandle, offset_check, SEEK_SET);
|
---|
387 | if(off1 < 0) {
|
---|
388 | fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
|
---|
389 | (OFF_T_FORMAT_CAST)offset_check, newpath);
|
---|
390 | smbc_close(remotehandle); close(localhandle);
|
---|
391 | return 1;
|
---|
392 | }
|
---|
393 |
|
---|
394 | off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
|
---|
395 | if(off2 < 0) {
|
---|
396 | fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
|
---|
397 | (OFF_T_FORMAT_CAST)offset_check, newpath);
|
---|
398 | smbc_close(remotehandle); close(localhandle);
|
---|
399 | return 1;
|
---|
400 | }
|
---|
401 |
|
---|
402 | if(off1 != off2) {
|
---|
403 | fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
|
---|
404 | (OFF_T_FORMAT_CAST)off1,
|
---|
405 | (OFF_T_FORMAT_CAST)off2);
|
---|
406 | return 1;
|
---|
407 | }
|
---|
408 |
|
---|
409 | if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
|
---|
410 | fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
|
---|
411 | smbc_close(remotehandle); close(localhandle);
|
---|
412 | return 1;
|
---|
413 | }
|
---|
414 |
|
---|
415 | if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
|
---|
416 | fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
|
---|
417 | smbc_close(remotehandle); close(localhandle);
|
---|
418 | return 1;
|
---|
419 | }
|
---|
420 |
|
---|
421 | if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
|
---|
422 | if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", (OFF_T_FORMAT_CAST)offset_download);
|
---|
423 | } else {
|
---|
424 | fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
|
---|
425 | smbc_close(remotehandle); close(localhandle);
|
---|
426 | return 1;
|
---|
427 | }
|
---|
428 | }
|
---|
429 | } else {
|
---|
430 | localhandle = STDOUT_FILENO;
|
---|
431 | start_offset = 0;
|
---|
432 | offset_download = 0;
|
---|
433 | offset_check = 0;
|
---|
434 | }
|
---|
435 |
|
---|
436 | readbuf = (char *)SMB_MALLOC(blocksize);
|
---|
437 |
|
---|
438 | /* Now, download all bytes from offset_download to the end */
|
---|
439 | for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
|
---|
440 | ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
|
---|
441 | if(bytesread < 0) {
|
---|
442 | fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
|
---|
443 | smbc_close(remotehandle);
|
---|
444 | if (localhandle != STDOUT_FILENO) close(localhandle);
|
---|
445 | free(readbuf);
|
---|
446 | return 1;
|
---|
447 | }
|
---|
448 |
|
---|
449 | total_bytes += bytesread;
|
---|
450 |
|
---|
451 | if(write(localhandle, readbuf, bytesread) < 0) {
|
---|
452 | fprintf(stderr, "Can't write %u bytes to local file %s at offset "OFF_T_FORMAT"\n", (unsigned int)bytesread, path, (OFF_T_FORMAT_CAST)curpos);
|
---|
453 | free(readbuf);
|
---|
454 | smbc_close(remotehandle);
|
---|
455 | if (localhandle != STDOUT_FILENO) close(localhandle);
|
---|
456 | return 1;
|
---|
457 | }
|
---|
458 |
|
---|
459 | if(dots)fputc('.', stderr);
|
---|
460 | else if(!quiet) {
|
---|
461 | print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size);
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | free(readbuf);
|
---|
466 |
|
---|
467 | if(dots){
|
---|
468 | fputc('\n', stderr);
|
---|
469 | printf("%s downloaded\n", path);
|
---|
470 | } else if(!quiet) {
|
---|
471 | int i;
|
---|
472 | fprintf(stderr, "\r%s", path);
|
---|
473 | if(columns) {
|
---|
474 | for(i = strlen(path); i < columns; i++) {
|
---|
475 | fputc(' ', stderr);
|
---|
476 | }
|
---|
477 | }
|
---|
478 | fputc('\n', stderr);
|
---|
479 | }
|
---|
480 |
|
---|
481 | if(keep_permissions && !send_stdout) {
|
---|
482 | if(fchmod(localhandle, remotestat.st_mode) < 0) {
|
---|
483 | fprintf(stderr, "Unable to change mode of local file %s to %o\n", path,
|
---|
484 | (unsigned int)remotestat.st_mode);
|
---|
485 | smbc_close(remotehandle);
|
---|
486 | close(localhandle);
|
---|
487 | return 1;
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | smbc_close(remotehandle);
|
---|
492 | if (localhandle != STDOUT_FILENO) close(localhandle);
|
---|
493 | return 0;
|
---|
494 | }
|
---|
495 |
|
---|
496 | static void clean_exit(void)
|
---|
497 | {
|
---|
498 | char bs[100];
|
---|
499 | human_readable(total_bytes, bs, sizeof(bs));
|
---|
500 | if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
|
---|
501 | (unsigned long)(time(NULL) - total_start_time));
|
---|
502 | exit(0);
|
---|
503 | }
|
---|
504 |
|
---|
505 | static void signal_quit(int v)
|
---|
506 | {
|
---|
507 | clean_exit();
|
---|
508 | }
|
---|
509 |
|
---|
510 | static int readrcfile(const char *name, const struct poptOption long_options[])
|
---|
511 | {
|
---|
512 | FILE *fd = fopen(name, "r");
|
---|
513 | int lineno = 0, i;
|
---|
514 | char var[101], val[101];
|
---|
515 | char found;
|
---|
516 | int *intdata; char **stringdata;
|
---|
517 | if(!fd) {
|
---|
518 | fprintf(stderr, "Can't open RC file %s\n", name);
|
---|
519 | return 1;
|
---|
520 | }
|
---|
521 |
|
---|
522 | while(!feof(fd)) {
|
---|
523 | lineno++;
|
---|
524 | if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
|
---|
525 | fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
|
---|
526 | continue;
|
---|
527 | }
|
---|
528 |
|
---|
529 | found = 0;
|
---|
530 |
|
---|
531 | for(i = 0; long_options[i].shortName; i++) {
|
---|
532 | if(!long_options[i].longName)continue;
|
---|
533 | if(strcmp(long_options[i].longName, var)) continue;
|
---|
534 | if(!long_options[i].arg)continue;
|
---|
535 |
|
---|
536 | switch(long_options[i].argInfo) {
|
---|
537 | case POPT_ARG_NONE:
|
---|
538 | intdata = (int *)long_options[i].arg;
|
---|
539 | if(!strcmp(val, "on")) *intdata = 1;
|
---|
540 | else if(!strcmp(val, "off")) *intdata = 0;
|
---|
541 | else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
|
---|
542 | break;
|
---|
543 | case POPT_ARG_INT:
|
---|
544 | intdata = (int *)long_options[i].arg;
|
---|
545 | *intdata = atoi(val);
|
---|
546 | break;
|
---|
547 | case POPT_ARG_STRING:
|
---|
548 | stringdata = (char **)long_options[i].arg;
|
---|
549 | *stringdata = SMB_STRDUP(val);
|
---|
550 | break;
|
---|
551 | default:
|
---|
552 | fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
|
---|
553 | break;
|
---|
554 | }
|
---|
555 |
|
---|
556 | found = 1;
|
---|
557 | }
|
---|
558 | if(!found) {
|
---|
559 | fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | fclose(fd);
|
---|
564 | return 0;
|
---|
565 | }
|
---|
566 |
|
---|
567 | int main(int argc, const char **argv)
|
---|
568 | {
|
---|
569 | int c = 0;
|
---|
570 | const char *file = NULL;
|
---|
571 | char *rcfile = NULL;
|
---|
572 | bool smb_encrypt = false;
|
---|
573 | int resume = 0, recursive = 0;
|
---|
574 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
575 | int ret = 0;
|
---|
576 | struct poptOption long_options[] = {
|
---|
577 | {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
|
---|
578 | {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
|
---|
579 | {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
|
---|
580 | {"update", 'U', POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
|
---|
581 | {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
|
---|
582 | {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
|
---|
583 | {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
|
---|
584 | {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
|
---|
585 | {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
|
---|
586 | {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
|
---|
587 | {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
|
---|
588 | {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
|
---|
589 | {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
|
---|
590 | {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
|
---|
591 | {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
|
---|
592 | {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
|
---|
593 | {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
|
---|
594 | {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
|
---|
595 | POPT_AUTOHELP
|
---|
596 | POPT_TABLEEND
|
---|
597 | };
|
---|
598 | poptContext pc;
|
---|
599 |
|
---|
600 | load_case_tables();
|
---|
601 |
|
---|
602 | /* only read rcfile if it exists */
|
---|
603 | if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
|
---|
604 | return 1;
|
---|
605 | }
|
---|
606 | if(access(rcfile, F_OK) == 0)
|
---|
607 | readrcfile(rcfile, long_options);
|
---|
608 | free(rcfile);
|
---|
609 |
|
---|
610 | #ifdef SIGWINCH
|
---|
611 | signal(SIGWINCH, change_columns);
|
---|
612 | #endif
|
---|
613 | signal(SIGINT, signal_quit);
|
---|
614 | signal(SIGTERM, signal_quit);
|
---|
615 |
|
---|
616 | pc = poptGetContext(argv[0], argc, argv, long_options, 0);
|
---|
617 |
|
---|
618 | while((c = poptGetNextOpt(pc)) >= 0) {
|
---|
619 | switch(c) {
|
---|
620 | case 'f':
|
---|
621 | readrcfile(poptGetOptArg(pc), long_options);
|
---|
622 | break;
|
---|
623 | case 'a':
|
---|
624 | username = ""; password = "";
|
---|
625 | break;
|
---|
626 | case 'e':
|
---|
627 | smb_encrypt = true;
|
---|
628 | break;
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | if((send_stdout || resume || outputfile) && update) {
|
---|
633 | fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n");
|
---|
634 | return 1;
|
---|
635 | }
|
---|
636 | if((send_stdout || outputfile) && recursive) {
|
---|
637 | fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
|
---|
638 | return 1;
|
---|
639 | }
|
---|
640 |
|
---|
641 | if(outputfile && send_stdout) {
|
---|
642 | fprintf(stderr, "The -o and -O options cannot be used together.\n");
|
---|
643 | return 1;
|
---|
644 | }
|
---|
645 |
|
---|
646 | if(smbc_init(get_auth_data, debuglevel) < 0) {
|
---|
647 | fprintf(stderr, "Unable to initialize libsmbclient\n");
|
---|
648 | return 1;
|
---|
649 | }
|
---|
650 |
|
---|
651 | if (smb_encrypt) {
|
---|
652 | SMBCCTX *smb_ctx = smbc_set_context(NULL);
|
---|
653 | smbc_option_set(smb_ctx,
|
---|
654 | CONST_DISCARD(char *, "smb_encrypt_level"),
|
---|
655 | "require");
|
---|
656 | }
|
---|
657 |
|
---|
658 | columns = get_num_cols();
|
---|
659 |
|
---|
660 | total_start_time = time(NULL);
|
---|
661 |
|
---|
662 | while ( (file = poptGetArg(pc)) ) {
|
---|
663 | if (!recursive)
|
---|
664 | ret = smb_download_file(file, "", recursive, resume, outputfile);
|
---|
665 | else
|
---|
666 | ret = smb_download_dir(file, "", resume);
|
---|
667 | }
|
---|
668 |
|
---|
669 | TALLOC_FREE(frame);
|
---|
670 | if ( ret == 0){
|
---|
671 | clean_exit();
|
---|
672 | }
|
---|
673 | return ret;
|
---|
674 | }
|
---|