examples: fixed and made them more similar
The channel read/write functions can return 0 in legitimate cases without it being an error, and we need to loop properly if they return short.
This commit is contained in:
parent
c511177d39
commit
cb42be1a9c
@ -47,7 +47,7 @@ int main(int argc, char *argv[])
|
|||||||
FILE *local;
|
FILE *local;
|
||||||
int rc;
|
int rc;
|
||||||
char mem[1024];
|
char mem[1024];
|
||||||
size_t nread, sent;
|
size_t nread;
|
||||||
char *ptr;
|
char *ptr;
|
||||||
struct stat fileinfo;
|
struct stat fileinfo;
|
||||||
|
|
||||||
@ -168,20 +168,21 @@ int main(int argc, char *argv[])
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr = mem;
|
ptr = mem;
|
||||||
sent = 0;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* write the same data over and over, until error or completion */
|
/* write the same data over and over, until error or completion */
|
||||||
rc = libssh2_channel_write(channel, ptr, nread);
|
rc = libssh2_channel_write(channel, ptr, nread);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
fprintf(stderr, "ERROR %d\n", rc);
|
fprintf(stderr, "ERROR %d\n", rc);
|
||||||
} else {
|
break;
|
||||||
/* rc indicates how many bytes were written this time */
|
|
||||||
sent += rc;
|
|
||||||
}
|
}
|
||||||
} while (rc > 0 && sent < nread);
|
else {
|
||||||
|
/* rc indicates how many bytes were written this time */
|
||||||
|
ptr += rc;
|
||||||
|
nread -= rc;
|
||||||
|
}
|
||||||
|
} while (nread);
|
||||||
|
|
||||||
nread -= sent;
|
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
fprintf(stderr, "Sending EOF\n");
|
fprintf(stderr, "Sending EOF\n");
|
||||||
|
@ -51,7 +51,7 @@ int main(int argc, char *argv[])
|
|||||||
long flag = 1;
|
long flag = 1;
|
||||||
#endif
|
#endif
|
||||||
char mem[1024];
|
char mem[1024];
|
||||||
size_t nread, sent;
|
size_t nread;
|
||||||
char *ptr;
|
char *ptr;
|
||||||
struct stat fileinfo;
|
struct stat fileinfo;
|
||||||
|
|
||||||
@ -182,7 +182,6 @@ int main(int argc, char *argv[])
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr = mem;
|
ptr = mem;
|
||||||
sent = 0;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* write the same data over and over, until error or completion */
|
/* write the same data over and over, until error or completion */
|
||||||
@ -191,12 +190,14 @@ int main(int argc, char *argv[])
|
|||||||
continue;
|
continue;
|
||||||
} else if (rc < 0) {
|
} else if (rc < 0) {
|
||||||
fprintf(stderr, "ERROR %d\n", rc);
|
fprintf(stderr, "ERROR %d\n", rc);
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
/* rc indicates how many bytes were written this time */
|
/* rc indicates how many bytes were written this time */
|
||||||
sent += rc;
|
nread -= rc;
|
||||||
|
ptr += rc;
|
||||||
}
|
}
|
||||||
} while (rc > 0 && sent < nread);
|
} while (nread);
|
||||||
} while (1);
|
} while (!nread); /* only continue if nread was drained */
|
||||||
|
|
||||||
fprintf(stderr, "Sending EOF\n");
|
fprintf(stderr, "Sending EOF\n");
|
||||||
while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);
|
while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);
|
||||||
|
@ -246,7 +246,7 @@ int main(int argc, char *argv[])
|
|||||||
nread);
|
nread);
|
||||||
ptr += rc;
|
ptr += rc;
|
||||||
nread -= nread;
|
nread -= nread;
|
||||||
} while (rc > 0);
|
} while (rc >= 0);
|
||||||
|
|
||||||
if(rc != LIBSSH2_ERROR_EAGAIN) {
|
if(rc != LIBSSH2_ERROR_EAGAIN) {
|
||||||
/* error or end of file */
|
/* error or end of file */
|
||||||
|
@ -185,10 +185,13 @@ int main(int argc, char *argv[])
|
|||||||
do {
|
do {
|
||||||
/* write data in a loop until we block */
|
/* write data in a loop until we block */
|
||||||
rc = libssh2_sftp_write(sftp_handle, ptr, nread);
|
rc = libssh2_sftp_write(sftp_handle, ptr, nread);
|
||||||
|
if(rc < 0)
|
||||||
|
break;
|
||||||
ptr += rc;
|
ptr += rc;
|
||||||
nread -= nread;
|
nread -= rc;
|
||||||
} while (rc > 0);
|
} while (nread);
|
||||||
} while (1);
|
|
||||||
|
} while (rc > 0);
|
||||||
|
|
||||||
libssh2_sftp_close(sftp_handle);
|
libssh2_sftp_close(sftp_handle);
|
||||||
libssh2_sftp_shutdown(sftp_session);
|
libssh2_sftp_shutdown(sftp_session);
|
||||||
|
@ -199,9 +199,9 @@ int main(int argc, char *argv[])
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
ptr += rc;
|
ptr += rc;
|
||||||
nread -= nread;
|
nread -= rc;
|
||||||
} while (rc > 0);
|
} while (nread);
|
||||||
} while (1);
|
} while (rc > 0);
|
||||||
|
|
||||||
fclose(local);
|
fclose(local);
|
||||||
libssh2_sftp_close(sftp_handle);
|
libssh2_sftp_close(sftp_handle);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user