Render timing.c robust in the face of strangeness. The strangenesses
[u/mdw/putty] / unix / uxsftp.c
CommitLineData
876eefd4 1/*
2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
3 */
4
5#include <sys/time.h>
6#include <sys/types.h>
7#include <sys/stat.h>
18609086 8#include <stdlib.h>
876eefd4 9#include <fcntl.h>
10#include <dirent.h>
11#include <unistd.h>
12#include <utime.h>
13#include <errno.h>
14#include <assert.h>
9c77ddf6 15#include <glob.h>
876eefd4 16
17#include "putty.h"
18#include "psftp.h"
19
20/*
21 * In PSFTP our selects are synchronous, so these functions are
22 * empty stubs.
23 */
24int uxsel_input_add(int fd, int rwx) { return 0; }
25void uxsel_input_remove(int id) { }
26
27char *x_get_default(const char *key)
28{
29 return NULL; /* this is a stub */
30}
31
32void platform_get_x11_auth(char *display, int *protocol,
33 unsigned char *data, int *datalen)
34{
35 /* Do nothing, therefore no auth. */
36}
37
38/*
39 * Default settings that are specific to PSFTP.
40 */
41char *platform_default_s(const char *name)
42{
43 return NULL;
44}
45
46int platform_default_i(const char *name, int def)
47{
48 return def;
49}
50
51FontSpec platform_default_fontspec(const char *name)
52{
53 FontSpec ret;
54 *ret.name = '\0';
55 return ret;
56}
57
58Filename platform_default_filename(const char *name)
59{
60 Filename ret;
61 if (!strcmp(name, "LogFileName"))
62 strcpy(ret.path, "putty.log");
63 else
64 *ret.path = '\0';
65 return ret;
66}
67
68/*
69 * Stubs for the GUI feedback mechanism in Windows PSCP.
70 */
71void gui_update_stats(char *name, unsigned long size,
72 int percentage, unsigned long elapsed,
73 unsigned long done, unsigned long eta,
74 unsigned long ratebs) {}
75void gui_send_errcount(int list, int errs) {}
76void gui_send_char(int is_stderr, int c) {}
77void gui_enable(char *arg) {}
78
79
80/*
81 * Set local current directory. Returns NULL on success, or else an
82 * error message which must be freed after printing.
83 */
84char *psftp_lcd(char *dir)
85{
86 if (chdir(dir) < 0)
87 return dupprintf("%s: chdir: %s", dir, strerror(errno));
88 else
89 return NULL;
90}
91
92/*
93 * Get local current directory. Returns a string which must be
94 * freed.
95 */
96char *psftp_getcwd(void)
97{
98 char *buffer, *ret;
99 int size = 256;
100
101 buffer = snewn(size, char);
102 while (1) {
103 ret = getcwd(buffer, size);
104 if (ret != NULL)
105 return ret;
106 if (errno != ERANGE) {
107 sfree(buffer);
108 return dupprintf("[cwd unavailable: %s]", strerror(errno));
109 }
110 /*
111 * Otherwise, ERANGE was returned, meaning the buffer
112 * wasn't big enough.
113 */
114 size = size * 3 / 2;
115 buffer = sresize(buffer, size, char);
116 }
117}
118
119struct RFile {
120 int fd;
121};
122
123RFile *open_existing_file(char *name, unsigned long *size,
124 unsigned long *mtime, unsigned long *atime)
125{
126 int fd;
127 RFile *ret;
128
129 fd = open(name, O_RDONLY);
130 if (fd < 0)
131 return NULL;
132
133 ret = snew(RFile);
134 ret->fd = fd;
135
136 if (size || mtime || atime) {
137 struct stat statbuf;
138 if (fstat(fd, &statbuf) < 0) {
139 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
140 memset(&statbuf, 0, sizeof(statbuf));
141 }
142
143 if (size)
144 *size = statbuf.st_size;
145
146 if (mtime)
147 *mtime = statbuf.st_mtime;
148
149 if (atime)
150 *atime = statbuf.st_atime;
151 }
152
153 return ret;
154}
155
156int read_from_file(RFile *f, void *buffer, int length)
157{
158 return read(f->fd, buffer, length);
159}
160
161void close_rfile(RFile *f)
162{
163 close(f->fd);
164 sfree(f);
165}
166
167struct WFile {
168 int fd;
169 char *name;
170};
171
172WFile *open_new_file(char *name)
173{
174 int fd;
175 WFile *ret;
176
177 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
178 if (fd < 0)
179 return NULL;
180
181 ret = snew(WFile);
182 ret->fd = fd;
183 ret->name = dupstr(name);
184
185 return ret;
186}
187
188int write_to_file(WFile *f, void *buffer, int length)
189{
190 char *p = (char *)buffer;
191 int so_far = 0;
192
193 /* Keep trying until we've really written as much as we can. */
194 while (length > 0) {
195 int ret = write(f->fd, p, length);
196
197 if (ret < 0)
198 return ret;
199
200 if (ret == 0)
201 break;
202
203 p += ret;
204 length -= ret;
205 so_far += ret;
206 }
207
208 return so_far;
209}
210
211void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
212{
213 struct utimbuf ut;
214
215 ut.actime = atime;
216 ut.modtime = mtime;
217
218 utime(f->name, &ut);
219}
220
221/* Closes and frees the WFile */
222void close_wfile(WFile *f)
223{
224 close(f->fd);
225 sfree(f->name);
226 sfree(f);
227}
228
229int file_type(char *name)
230{
231 struct stat statbuf;
232
233 if (stat(name, &statbuf) < 0) {
234 if (errno != ENOENT)
235 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
236 return FILE_TYPE_NONEXISTENT;
237 }
238
239 if (S_ISREG(statbuf.st_mode))
240 return FILE_TYPE_FILE;
241
242 if (S_ISDIR(statbuf.st_mode))
243 return FILE_TYPE_DIRECTORY;
244
245 return FILE_TYPE_WEIRD;
246}
247
248struct DirHandle {
249 DIR *dir;
250};
251
252DirHandle *open_directory(char *name)
253{
254 DIR *dir;
255 DirHandle *ret;
256
257 dir = opendir(name);
258 if (!dir)
259 return NULL;
260
261 ret = snew(DirHandle);
262 ret->dir = dir;
263 return ret;
264}
265
266char *read_filename(DirHandle *dir)
267{
268 struct dirent *de;
269
270 do {
271 de = readdir(dir->dir);
272 if (de == NULL)
273 return NULL;
274 } while ((de->d_name[0] == '.' &&
275 (de->d_name[1] == '\0' ||
276 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
277
278 return dupstr(de->d_name);
279}
280
281void close_directory(DirHandle *dir)
282{
283 closedir(dir->dir);
284 sfree(dir);
285}
286
287int test_wildcard(char *name, int cmdline)
288{
876eefd4 289 struct stat statbuf;
290
9c77ddf6 291 if (stat(name, &statbuf) == 0) {
876eefd4 292 return WCTYPE_FILENAME;
9c77ddf6 293 } else if (cmdline) {
294 /*
295 * On Unix, we never need to parse wildcards coming from
296 * the command line, because the shell will have expanded
297 * them into a filename list already.
298 */
299 return WCTYPE_NONEXISTENT;
300 } else {
301 glob_t globbed;
302 int ret = WCTYPE_NONEXISTENT;
303
304 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
305 if (globbed.gl_pathc > 0)
306 ret = WCTYPE_WILDCARD;
307 globfree(&globbed);
308 }
309
310 return ret;
311 }
876eefd4 312}
313
314/*
9c77ddf6 315 * Actually return matching file names for a local wildcard.
876eefd4 316 */
317struct WildcardMatcher {
9c77ddf6 318 glob_t globbed;
319 int i;
876eefd4 320};
9c77ddf6 321WildcardMatcher *begin_wildcard_matching(char *name) {
322 WildcardMatcher *ret = snew(WildcardMatcher);
323
324 if (glob(name, 0, NULL, &ret->globbed) < 0) {
325 sfree(ret);
326 return NULL;
327 }
328
329 ret->i = 0;
330
331 return ret;
332}
333char *wildcard_get_filename(WildcardMatcher *dir) {
334 if (dir->i < dir->globbed.gl_pathc) {
335 return dupstr(dir->globbed.gl_pathv[dir->i++]);
336 } else
337 return NULL;
338}
339void finish_wildcard_matching(WildcardMatcher *dir) {
340 globfree(&dir->globbed);
341 sfree(dir);
342}
876eefd4 343
e9d14678 344int vet_filename(char *name)
345{
346 if (strchr(name, '/'))
347 return FALSE;
348
349 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
350 return FALSE;
351
352 return TRUE;
353}
354
876eefd4 355int create_directory(char *name)
356{
357 return mkdir(name, 0777) == 0;
358}
359
360char *dir_file_cat(char *dir, char *file)
361{
362 return dupcat(dir, "/", file, NULL);
363}
364
365/*
39934deb 366 * Do a select() between all currently active network fds and
367 * optionally stdin.
876eefd4 368 */
65857773 369static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
876eefd4 370{
371 fd_set rset, wset, xset;
372 int i, fdcount, fdsize, *fdlist;
373 int fd, fdstate, rwx, ret, maxfd;
39934deb 374 long now = GETTICKCOUNT();
876eefd4 375
376 fdlist = NULL;
377 fdcount = fdsize = 0;
378
39934deb 379 do {
876eefd4 380
39934deb 381 /* Count the currently active fds. */
382 i = 0;
383 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
384 fd = next_fd(&fdstate, &rwx)) i++;
876eefd4 385
65857773 386 if (i < 1 && !no_fds_ok)
39934deb 387 return -1; /* doom */
876eefd4 388
39934deb 389 /* Expand the fdlist buffer if necessary. */
390 if (i > fdsize) {
391 fdsize = i + 16;
392 fdlist = sresize(fdlist, fdsize, int);
393 }
876eefd4 394
39934deb 395 FD_ZERO(&rset);
396 FD_ZERO(&wset);
397 FD_ZERO(&xset);
398 maxfd = 0;
876eefd4 399
39934deb 400 /*
401 * Add all currently open fds to the select sets, and store
402 * them in fdlist as well.
403 */
404 fdcount = 0;
405 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
406 fd = next_fd(&fdstate, &rwx)) {
407 fdlist[fdcount++] = fd;
408 if (rwx & 1)
409 FD_SET_MAX(fd, maxfd, rset);
410 if (rwx & 2)
411 FD_SET_MAX(fd, maxfd, wset);
412 if (rwx & 4)
413 FD_SET_MAX(fd, maxfd, xset);
414 }
415
416 if (include_stdin)
417 FD_SET_MAX(0, maxfd, rset);
418
419 do {
420 long next, ticks;
421 struct timeval tv, *ptv;
422
423 if (run_timers(now, &next)) {
424 ticks = next - GETTICKCOUNT();
425 if (ticks <= 0)
426 ticks = 1; /* just in case */
427 tv.tv_sec = ticks / 1000;
428 tv.tv_usec = ticks % 1000 * 1000;
429 ptv = &tv;
430 } else {
431 ptv = NULL;
432 }
433 ret = select(maxfd, &rset, &wset, &xset, ptv);
434 if (ret == 0)
435 now = next;
2ac3322e 436 else {
437 long newnow = GETTICKCOUNT();
438 /*
439 * Check to see whether the system clock has
440 * changed massively during the select.
441 */
442 if (newnow - now < 0 || newnow - now > next - now) {
443 /*
444 * If so, look at the elapsed time in the
445 * select and use it to compute a new
446 * tickcount_offset.
447 */
448 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
449 /* So we'd like GETTICKCOUNT to have returned othernow,
450 * but instead it return newnow. Hence ... */
451 tickcount_offset += othernow - newnow;
452 now = othernow;
453 } else {
454 now = newnow;
455 }
456 }
39934deb 457 } while (ret < 0 && errno != EINTR);
458 } while (ret == 0);
876eefd4 459
460 if (ret < 0) {
461 perror("select");
462 exit(1);
463 }
464
465 for (i = 0; i < fdcount; i++) {
466 fd = fdlist[i];
467 /*
468 * We must process exceptional notifications before
469 * ordinary readability ones, or we may go straight
470 * past the urgent marker.
471 */
472 if (FD_ISSET(fd, &xset))
473 select_result(fd, 4);
474 if (FD_ISSET(fd, &rset))
475 select_result(fd, 1);
476 if (FD_ISSET(fd, &wset))
477 select_result(fd, 2);
478 }
479
480 sfree(fdlist);
481
39934deb 482 return FD_ISSET(0, &rset) ? 1 : 0;
483}
484
485/*
486 * Wait for some network data and process it.
487 */
488int ssh_sftp_loop_iteration(void)
489{
65857773 490 return ssh_sftp_do_select(FALSE, FALSE);
39934deb 491}
492
493/*
494 * Read a PSFTP command line from stdin.
495 */
65857773 496char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
39934deb 497{
498 char *buf;
499 int buflen, bufsize, ret;
500
501 fputs(prompt, stdout);
502 fflush(stdout);
503
504 buf = NULL;
505 buflen = bufsize = 0;
506
507 while (1) {
65857773 508 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
39934deb 509 if (ret < 0) {
510 printf("connection died\n");
511 return NULL; /* woop woop */
512 }
513 if (ret > 0) {
514 if (buflen >= bufsize) {
515 bufsize = buflen + 512;
516 buf = sresize(buf, bufsize, char);
517 }
518 ret = read(0, buf+buflen, 1);
519 if (ret < 0) {
520 perror("read");
521 return NULL;
522 }
523 if (ret == 0) {
524 /* eof on stdin; no error, but no answer either */
525 return NULL;
526 }
527
528 if (buf[buflen++] == '\n') {
529 /* we have a full line */
530 return buf;
531 }
532 }
533 }
876eefd4 534}
535
536/*
537 * Main program: do platform-specific initialisation and then call
538 * psftp_main().
539 */
540int main(int argc, char *argv[])
541{
542 uxsel_init();
543 return psftp_main(argc, argv);
544}