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