[PATCH] Re: [PHP-DEV] PHP 4.0 Bug #8187: Partial FTP Downloads
| From: | Jens Kristian Søgaard | Date: | Mon, 11 Dec 2000 17:41:05 +0000 |
| Subject: | [PATCH] Re: [PHP-DEV] PHP 4.0 Bug #8187: Partial FTP Downloads | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-40874@lists.php.net to get a copy of this message | ||
Jens Kristian Søgaard <jk@soegaard.net> writes:
> > Just post a patch to the list with [PATCH] in it and hopefully one of the
> > developers will look at the code.
> What is the easiest, and preferred, way of making these patches?
Well, since no one answerred me - I'll just post the patch the way
I've made it.
I highly suspect that I made it wrongly -- please inform me! Thanks!
--- ../../../old/ext/ftp/ftp.h Thu Sep 14 00:00:31 2000
+++ ftp.h Sun Dec 10 19:44:47 2000
@@ -139,7 +139,7 @@
* returns true on success, false on error
*/
int ftp_get(ftpbuf_t *ftp, FILE *outfp, const char *path,
- ftptype_t type);
+ ftptype_t type, long bytesToTransfer);
/* stores the data from infp as a file on the remote server
* returns true on success, false on error
--- ../../../old/ext/ftp/ftp.c Thu Sep 14 00:00:31 2000
+++ ftp.c Sun Dec 10 21:41:00 2000
@@ -492,12 +492,13 @@
int
-ftp_get(ftpbuf_t *ftp, FILE *outfp, const char *path, ftptype_t type)
+ftp_get(ftpbuf_t *ftp, FILE *outfp, const char *path, ftptype_t type, long bytesToTransfer)
{
databuf_t *data = NULL;
char *ptr;
int lastch;
int rcvd;
+ int stopTransfer;
if (ftp == NULL)
return 0;
@@ -516,11 +517,21 @@
if ((data = data_accept(data)) == NULL)
goto bail;
+ stopTransfer = 0;
lastch = 0;
- while ((rcvd = my_recv(data->fd, data->buf, FTP_BUFSIZE))) {
+ while((rcvd = my_recv(data->fd, data->buf, FTP_BUFSIZE)) && (!stopTransfer)) {
if (rcvd == -1)
goto bail;
+ if( bytesToTransfer > -1 ) {
+ if( bytesToTransfer > FTP_BUFSIZE ) {
+ bytesToTransfer -= FTP_BUFSIZE;
+ } else {
+ rcvd = (rcvd > bytesToTransfer) ? bytesToTransfer : rcvd;
+ stopTransfer = 1;
+ }
+ }
+
if (type == FTPTYPE_ASCII) {
for (ptr = data->buf; rcvd; rcvd--, ptr++) {
if (lastch == '\r' && *ptr != '\n')
@@ -543,8 +554,14 @@
if (ferror(outfp))
goto bail;
- if (!ftp_getresp(ftp) || ftp->resp != 226)
- goto bail;
+ if( stopTransfer ) { /* transfer was aborted, so expect another return code */
+ if( !ftp_getresp(ftp) || (ftp->resp != 426 && ftp->resp != 425 &&
ftp->resp != 226) )
+ goto bail;
+ }
+ else { /* transfer completed, check return code */
+ if (!ftp_getresp(ftp) || ftp->resp != 226)
+ goto bail;
+ }
return 1;
bail:
--- ../../../old/ext/ftp/php_ftp.c Sun Oct 8 13:45:17 2000
+++ php_ftp.c Sun Dec 10 20:51:21 2000
@@ -507,7 +507,7 @@
convert_to_string(arg3);
XTYPE(xtype, arg4);
- if (!ftp_get(ftp, fp, arg3->value.str.val, xtype) || ferror(fp)) {
+ if (!ftp_get(ftp, fp, arg3->value.str.val, xtype,-1) || ferror(fp)) {
php_error(E_WARNING, "ftp_get: %s", ftp->inbuf);
RETURN_FALSE;
}
@@ -551,21 +551,35 @@
Retrieves a file from the FTP server and writes it to a local file */
PHP_FUNCTION(ftp_get)
{
- pval *arg1, *arg2, *arg3, *arg4;
+ pval *arg1, *arg2, *arg3, *arg4, *arg5;
ftpbuf_t *ftp;
ftptype_t xtype;
FILE *outfp, *tmpfp;
int ch;
-
+ long bytesToTransfer = -1;
/* arg1 - ftp
* arg2 - destination (local) file
* arg3 - source (remote) file
* arg4 - transfer mode
+ * arg5 - [bytes to transfer]
*/
- if ( ZEND_NUM_ARGS() != 4 ||
- getParameters(ht, 4, &arg1, &arg2, &arg3, &arg4) == FAILURE)
- {
+
+ switch (ZEND_NUM_ARGS()) {
+ case 4:
+ if (getParameters(ht, 4, &arg1, &arg2, &arg3, &arg4) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ break;
+
+ case 5:
+ if (getParameters(ht, 5, &arg1, &arg2, &arg3, &arg4, &arg5) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_long(arg5);
+ bytesToTransfer = (long) arg5->value.lval;
+ break;
+ default:
WRONG_PARAM_COUNT;
}
@@ -582,7 +596,7 @@
RETURN_FALSE;
}
- if ( !ftp_get(ftp, tmpfp, arg3->value.str.val, xtype) ||
+ if ( !ftp_get(ftp, tmpfp, arg3->value.str.val, xtype,bytesToTransfer) ||
ferror(tmpfp))
{
fclose(tmpfp);
--
Jens Kristian Søgaard,
jk@soegaard.net -- http://www.jksoegaard.dk/
Søger du noget? -- http://www.google.com/
echo|perl -ple'$_+=4E-6*!int rand()**2+rand()**2while$i++-1E6'