Remove ghastly hack involving fxp_error_message.
[u/mdw/putty] / sftp.c
diff --git a/sftp.c b/sftp.c
index 9c14e74..78fbd32 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -7,13 +7,10 @@
 #include <string.h>
 #include <assert.h>
 
+#include "misc.h"
 #include "int64.h"
 #include "sftp.h"
 
-#define smalloc malloc
-#define srealloc realloc
-#define sfree free
-
 #define GET_32BIT(cp) \
     (((unsigned long)(unsigned char)(cp)[0] << 24) | \
     ((unsigned long)(unsigned char)(cp)[1] << 16) | \
@@ -65,7 +62,6 @@ 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)
@@ -373,6 +369,10 @@ char *fxp_realpath(char *path)
     sftp_pkt_addstring_str(pktout, path);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x123) {
        fxp_internal_error("request ID mismatch\n");
@@ -417,6 +417,10 @@ struct fxp_handle *fxp_open(char *path, int type)
     sftp_pkt_adduint32(pktout, 0);     /* (FIXME) empty ATTRS structure */
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x567) {
        fxp_internal_error("request ID mismatch\n");
@@ -456,6 +460,10 @@ struct fxp_handle *fxp_opendir(char *path)
     sftp_pkt_addstring(pktout, path);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x456) {
        fxp_internal_error("request ID mismatch\n");
@@ -496,6 +504,10 @@ void fxp_close(struct fxp_handle *handle)
     sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x789) {
        fxp_internal_error("request ID mismatch\n");
@@ -517,6 +529,10 @@ int fxp_mkdir(char *path)
     sftp_pkt_adduint32(pktout, 0);     /* (FIXME) empty ATTRS structure */
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x234) {
        fxp_internal_error("request ID mismatch\n");
@@ -539,6 +555,10 @@ int fxp_rmdir(char *path)
     sftp_pkt_addstring(pktout, path);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x345) {
        fxp_internal_error("request ID mismatch\n");
@@ -561,6 +581,10 @@ int fxp_remove(char *fname)
     sftp_pkt_addstring(pktout, fname);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -584,6 +608,10 @@ int fxp_rename(char *srcfname, char *dstfname)
     sftp_pkt_addstring(pktout, dstfname);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -610,6 +638,10 @@ int fxp_stat(char *fname, struct fxp_attrs *attrs)
     sftp_pkt_addstring(pktout, fname);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -636,6 +668,10 @@ int fxp_fstat(struct fxp_handle *handle, struct fxp_attrs *attrs)
     sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -665,6 +701,37 @@ int fxp_setstat(char *fname, struct fxp_attrs attrs)
     sftp_pkt_addattrs(pktout, attrs);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
+    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;
+}
+int fxp_fsetstat(struct fxp_handle *handle, struct fxp_attrs attrs)
+{
+    struct sftp_packet *pktin, *pktout;
+    int id;
+
+    pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
+    sftp_pkt_adduint32(pktout, 0x678); /* request id */
+    sftp_pkt_addstring_start(pktout);
+    sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
+    sftp_pkt_addattrs(pktout, attrs);
+    sftp_send(pktout);
+    pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -697,6 +764,10 @@ int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
     sftp_pkt_adduint32(pktout, len);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return -1;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0xBCD) {
        fxp_internal_error("request ID mismatch");
@@ -736,6 +807,10 @@ struct fxp_names *fxp_readdir(struct fxp_handle *handle)
     sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0xABC) {
        fxp_internal_error("request ID mismatch\n");
@@ -781,6 +856,10 @@ int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
     sftp_pkt_addstring_data(pktout, buffer, len);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0xDCB) {
        fxp_internal_error("request ID mismatch\n");