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