- char buf[2048];
- char namebuf[2048];
- char ch;
- int targisdir = 0;
- int settime = 0;
- int exists;
- DWORD attr;
- HANDLE f;
- unsigned long mtime, atime;
- unsigned int mode;
- unsigned long size, i;
- int wrerror = 0;
- unsigned long stat_bytes;
- unsigned long stat_starttime, stat_lasttime;
- char *stat_name;
-
- attr = GetFileAttributes(targ);
- if (attr != -1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
- targisdir = 1;
-
- if (targetshouldbedirectory && !targisdir)
- bump("%s: Not a directory", targ);
-
- ssh_send("", 1);
- while (1) {
- settime = 0;
-gottime:
- if (ssh_recv(&ch, 1) <= 0)
- return;
- if (ch == '\n')
- bump("Protocol error: Unexpected newline");
- i = 0;
- buf[i++] = ch;
- do {
- if (ssh_recv(&ch, 1) <= 0)
- bump("Lost connection");
- buf[i++] = ch;
- } while (i < sizeof(buf) && ch != '\n');
- buf[i-1] = '\0';
- switch (buf[0]) {
- case '\01': /* error */
- fprintf(stderr, "%s\n", buf+1);
- errs++;
- continue;
- case '\02': /* fatal error */
- bump("%s", buf+1);
- case 'E':
- ssh_send("", 1);
- return;
- case 'T':
- if (sscanf(buf, "T%d %*d %d %*d",
- &mtime, &atime) == 2) {
- settime = 1;
- ssh_send("", 1);
- goto gottime;
- }
- bump("Protocol error: Illegal time format");
- case 'C':
- case 'D':
- break;
- default:
- bump("Protocol error: Expected control record");
+ if (using_sftp) {
+ char *newsource;
+
+ if (!fxp_init()) {
+ tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
+ errs++;
+ return 1;
+ }
+ /*
+ * It's possible that the source string we've been given
+ * contains a wildcard. If so, we must split the directory
+ * away from the wildcard itself (throwing an error if any
+ * wildcardness comes before the final slash) and arrange
+ * things so that a dirstack entry will be set up.
+ */
+ newsource = snewn(1+strlen(source), char);
+ if (!wc_unescape(newsource, source)) {
+ /* Yes, here we go; it's a wildcard. Bah. */
+ char *dupsource, *lastpart, *dirpart, *wildcard;
+ dupsource = dupstr(source);
+ lastpart = stripslashes(dupsource, 0);
+ wildcard = dupstr(lastpart);
+ *lastpart = '\0';
+ if (*dupsource && dupsource[1]) {
+ /*
+ * The remains of dupsource are at least two
+ * characters long, meaning the pathname wasn't
+ * empty or just `/'. Hence, we remove the trailing
+ * slash.
+ */
+ lastpart[-1] = '\0';
+ } else if (!*dupsource) {
+ /*
+ * The remains of dupsource are _empty_ - the whole
+ * pathname was a wildcard. Hence we need to
+ * replace it with ".".
+ */
+ sfree(dupsource);
+ dupsource = dupstr(".");
+ }
+
+ /*
+ * Now we have separated our string into dupsource (the
+ * directory part) and wildcard. Both of these will
+ * need freeing at some point. Next step is to remove
+ * wildcard escapes from the directory part, throwing
+ * an error if it contains a real wildcard.
+ */
+ dirpart = snewn(1+strlen(dupsource), char);
+ if (!wc_unescape(dirpart, dupsource)) {
+ tell_user(stderr, "%s: multiple-level wildcards unsupported",
+ source);
+ errs++;
+ sfree(dirpart);
+ sfree(wildcard);
+ sfree(dupsource);
+ return 1;
+ }
+
+ /*
+ * Now we have dirpart (unescaped, ie a valid remote
+ * path), and wildcard (a wildcard). This will be
+ * sufficient to arrange a dirstack entry.
+ */
+ scp_sftp_remotepath = dirpart;
+ scp_sftp_wildcard = wildcard;
+ sfree(dupsource);
+ } else {
+ scp_sftp_remotepath = newsource;
+ scp_sftp_wildcard = NULL;
+ }
+ scp_sftp_preserve = preserve;
+ scp_sftp_recursive = recursive;
+ scp_sftp_donethistarget = 0;
+ scp_sftp_dirstack_head = NULL;
+ }
+ return 0;
+}
+
+int scp_sink_init(void)
+{
+ if (!using_sftp) {
+ back->send(backhandle, "", 1);
+ }
+ return 0;
+}
+
+#define SCP_SINK_FILE 1
+#define SCP_SINK_DIR 2
+#define SCP_SINK_ENDDIR 3
+#define SCP_SINK_RETRY 4 /* not an action; just try again */
+struct scp_sink_action {
+ int action; /* FILE, DIR, ENDDIR */
+ char *buf; /* will need freeing after use */
+ char *name; /* filename or dirname (not ENDDIR) */
+ int mode; /* access mode (not ENDDIR) */
+ unsigned long size; /* file size (not ENDDIR) */
+ int settime; /* 1 if atime and mtime are filled */
+ unsigned long atime, mtime; /* access times for the file */
+};
+
+int scp_get_sink_action(struct scp_sink_action *act)
+{
+ if (using_sftp) {
+ char *fname;
+ int must_free_fname;
+ struct fxp_attrs attrs;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+ int ret;
+
+ if (!scp_sftp_dirstack_head) {
+ if (!scp_sftp_donethistarget) {
+ /*
+ * Simple case: we are only dealing with one file.
+ */
+ fname = scp_sftp_remotepath;
+ must_free_fname = 0;
+ scp_sftp_donethistarget = 1;
+ } else {
+ /*
+ * Even simpler case: one file _which we've done_.
+ * Return 1 (finished).
+ */
+ return 1;
+ }
+ } else {
+ /*
+ * We're now in the middle of stepping through a list
+ * of names returned from fxp_readdir(); so let's carry
+ * on.
+ */
+ struct scp_sftp_dirstack *head = scp_sftp_dirstack_head;
+ while (head->namepos < head->namelen &&
+ (is_dots(head->names[head->namepos].filename) ||
+ (head->wildcard &&
+ !wc_match(head->wildcard,
+ head->names[head->namepos].filename))))
+ head->namepos++; /* skip . and .. */
+ if (head->namepos < head->namelen) {
+ head->matched_something = 1;
+ fname = dupcat(head->dirpath, "/",
+ head->names[head->namepos++].filename,
+ NULL);
+ must_free_fname = 1;
+ } else {
+ /*
+ * We've come to the end of the list; pop it off
+ * the stack and return an ENDDIR action (or RETRY
+ * if this was a wildcard match).
+ */
+ if (head->wildcard) {
+ act->action = SCP_SINK_RETRY;
+ if (!head->matched_something) {
+ tell_user(stderr, "pscp: wildcard '%s' matched "
+ "no files", head->wildcard);
+ errs++;
+ }
+ sfree(head->wildcard);
+
+ } else {
+ act->action = SCP_SINK_ENDDIR;