Patch to PSFTP: implement mkdir, rmdir, rm and scripting. Still to
[u/mdw/putty] / sftp.c
diff --git a/sftp.c b/sftp.c
index ff9e043..4491349 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -33,6 +33,9 @@ struct sftp_packet {
     int type;
 };
 
+static const char *fxp_error_message;
+static int fxp_errtype;
+
 /* ----------------------------------------------------------------------
  * SFTP packet construction functions.
  */
@@ -62,6 +65,7 @@ static struct sftp_packet *sftp_pkt_init(int pkt_type)
     pkt->length = 0;
     pkt->maxlen = 0;
     sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
+    fxp_error_message = NULL;
     return pkt;
 }
 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
@@ -234,9 +238,6 @@ static char *mkstr(char *s, int len)
  * SFTP primitives.
  */
 
-static const char *fxp_error_message;
-static int fxp_errtype;
-
 /*
  * Deal with (and free) an FXP_STATUS packet. Return 1 if
  * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
@@ -480,6 +481,76 @@ void fxp_close(struct fxp_handle *handle)
     sfree(handle);
 }
 
+int fxp_mkdir(char *path)
+{
+    struct sftp_packet *pktin, *pktout;
+    int id;
+
+    pktout = sftp_pkt_init(SSH_FXP_MKDIR);
+    sftp_pkt_adduint32(pktout, 0x234); /* request id */
+    sftp_pkt_addstring_start(pktout);
+    sftp_pkt_addstring_data(pktout, path, strlen(path));
+    sftp_pkt_adduint32(pktout, 0);     /* (FIXME) empty ATTRS structure */
+    sftp_send(pktout);
+    pktin = sftp_recv();
+    id = sftp_pkt_getuint32(pktin);
+    if (id != 0x234) {
+       fxp_internal_error("request ID mismatch\n");
+       return 0;
+    }
+    id = fxp_got_status(pktin);
+    if (id != 1) {
+       return 0;
+    }
+    return 1;
+}
+
+int fxp_rmdir(char *path)
+{
+    struct sftp_packet *pktin, *pktout;
+    int id;
+
+    pktout = sftp_pkt_init(SSH_FXP_RMDIR);
+    sftp_pkt_adduint32(pktout, 0x345); /* request id */
+    sftp_pkt_addstring_start(pktout);
+    sftp_pkt_addstring_data(pktout, path, strlen(path));
+    sftp_send(pktout);
+    pktin = sftp_recv();
+    id = sftp_pkt_getuint32(pktin);
+    if (id != 0x345) {
+       fxp_internal_error("request ID mismatch\n");
+       return 0;
+    }
+    id = fxp_got_status(pktin);
+    if (id != 1) {
+       return 0;
+    }
+    return 1;
+}
+
+int fxp_rm(char *fname)
+{
+    struct sftp_packet *pktin, *pktout;
+    int id;
+
+    pktout = sftp_pkt_init(SSH_FXP_REMOVE);
+    sftp_pkt_adduint32(pktout, 0x678); /* request id */
+    sftp_pkt_addstring_start(pktout);
+    sftp_pkt_addstring_data(pktout, fname, strlen(fname));
+    sftp_send(pktout);
+    pktin = sftp_recv();
+    id = sftp_pkt_getuint32(pktin);
+    if (id != 0x678) {
+       fxp_internal_error("request ID mismatch\n");
+       return 0;
+    }
+    id = fxp_got_status(pktin);
+    if (id != 1) {
+       return 0;
+    }
+    return 1;
+}
+
 /*
  * Read from a file. Returns the number of bytes read, or -1 on an
  * error, or possibly 0 if EOF. (I'm not entirely sure whether it