curl-library
using libcurl produces unaligned file size
From: Walter Reiner <walter.reiner_at_gmx.net>
Date: Mon, 13 Jun 2005 14:08:03 +0200 (MEST)
Date: Mon, 13 Jun 2005 14:08:03 +0200 (MEST)
--- Hi, i have a problem using libcurl in a C program and i hope you can help me. my problem is, that i can't do some postquote commands after the transfer, because the transfer seems to be not ok. what i did? i downloaded the "debug.c" example programm and it works fine: ---- debug.c ----- #define LOCAL_FILE "uploadthis.txt" #define UPLOAD_FILE_AS "while-uploading-walter.txt" #define REMOTE_URL "ftp://user:passwd@host/vhosts/wreiner/getip/ips/" UPLOAD_FILE_AS int main(int argc, char **argv) { CURL *curl; CURLcode res; FILE *ftpfile; FILE * hd_src ; int hd ; struct stat file_info; struct data config; struct curl_slist *headerlist=NULL; char buf_1[10000]; memset(buf_1, 0, sizeof(buf_1)); sprintf(buf_1, "SITE CHMOD 664 %s", UPLOAD_FILE_AS); config.trace_ascii = 1; /* enable ascii tracing */ /* get the file size of the local file */ hd = open(LOCAL_FILE, O_RDONLY) ; fstat(hd, &file_info); close(hd) ; hd_src = fopen(LOCAL_FILE, "rb"); /* In windows, this will init the winsock stuff */ curl_global_init(CURL_GLOBAL_ALL); /* get a curl handle */ curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace); curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); /* build a list of commands to pass to libcurl */ headerlist = curl_slist_append(headerlist, buf_1); /* enable uploading */ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1) ; /* specify target */ curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL); /* pass in that last of FTP commands to run after the transfer */ curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); /* now specify which file to upload */ curl_easy_setopt(curl, CURLOPT_READDATA, hd_src); /* Now run off and do what you've been told! */ res = curl_easy_perform(curl); /* clean up the FTP commands list */ curl_slist_free_all (headerlist); /* always cleanup */ curl_easy_cleanup(curl); } fclose(hd_src); /* close the local file */ curl_global_cleanup(); return 0; } ---- debug.c ----- this is the output from the "debug.c" example ---- cut ---- == Info: disabling EPSV usage => Send header, 6 bytes (0x6) 0000: PASV <= Recv header, 49 bytes (0x31) 0000: 227 Entering Passive Mode (172,16,69,253,10,29) == Info: Trying 172.16.69.253... == Info: connected == Info: Connecting to 172.16.69.253 (172.16.69.253) port 2589 == Info: DO phase is comlete == Info: DO-MORE phase starts => Send header, 8 bytes (0x8) 0000: TYPE I <= Recv header, 20 bytes (0x14) 0000: 200 Type set to I. => Send header, 33 bytes (0x21) 0000: STOR while-uploading-walter.txt <= Recv header, 75 bytes (0x4b) 0000: 150 Opening BINARY mode data connection for 'while-uploading-wal 0040: ter.txt'. == Info: DO-MORE phase ends => Send data, 46 bytes (0x2e) 0000: pc261011;172.16.69.22;Sun Jun 12 18:08:33 2005 == Info: Remembering we are in dir vhosts/wreiner/getip/ips/ <= Recv header, 24 bytes (0x18) 0000: 226 Transfer complete. => Send header, 43 bytes (0x2b) 0000: SITE CHMOD 664 while-uploading-walter.txt <= Recv header, 31 bytes (0x1f) 0000: 200 CHMOD command successful. == Info: Connection #0 to host portatux left intact => Send header, 6 bytes (0x6) 0000: QUIT <= Recv header, 14 bytes (0xe) 0000: 221 Goodbye. == Info: Closing connection #0 ---- cut ---- as you can see everything is working as expected. so i've changed the function to my needs ---- uplftp.c ---- #define FTP_USER "user" #define FTP_PASSWD "passwd" #define FTP_HOST "host" #define FTP_DIR "/dir/to/store/" #define CURL_CMD_LEN (500) int UploadFileViaCurl(char *pcFac, char *pcSrcFile, char *pcDestFile) { CURLcode res; char acRemoteUrl[2048 + 1]; char acCmd3[CURL_CMD_LEN + 1]; int iHd = 0, iRv = 0; FILE *fhHddSrc = NULL; CURL *curl; struct stat file_info; struct curl_slist *headerlist = NULL; /* debug */ struct data config; const char *pcFctName = "UploadFileToFtp()"; /* reset var */ memset(acRemoteUrl, 0, sizeof(acRemoteUrl)); memset(acCmd3, 0, sizeof(acCmd3)); /* enable ascii tracing */ config.trace_ascii = 1; /* build ftp server string */ sprintf(acRemoteUrl, "ftp://%s:%s@%s%s%s", FTP_USER, FTP_PASSWD, FTP_HOST, FTP_DIR, pcDestFile); /* build ftp commands */ sprintf(acCmd3, "SITE CHMOD 664 %s", pcDestFile); /* get the file size of given file */ iHd = open(pcSrcFile, O_RDONLY); fstat(iHd, &file_info); close(iHd); /* open file binary read */ fhHddSrc = fopen(pcSrcFile, "rb"); if (fhHddSrc == NULL) { fprintf(stderr, "%s: Error opening File (%s)", pcFctName, pcSrcFile); return (-1); } /* init curl */ curl_global_init(CURL_GLOBAL_ALL); /* get a curl easy handle */ curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace); curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); /* append commands to list */ headerlist = curl_slist_append(headerlist, acCmd3); /* enable uploading */ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1); /* specifiy target */ curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL); /* FTP commands to run after the transfer */ curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); /* file to upload */ curl_easy_setopt(curl, CURLOPT_READDATA, fhHddSrc); /* size of file to upload */ curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); /* start transfer */ res = curl_easy_perform(curl); /* cleanup ftp command list */ curl_slist_free_all(headerlist); /* cleanup */ curl_easy_cleanup(curl); } iRv = fclose(fhHddSrc); if (iRv == EOF) { fprintf(stderr, "%s: Error closing file (%s) but transfer was ok", pcFctName, pcSrcFile); return (-1); } curl_global_cleanup(); return (0); } ---- uplftp.c ---- thats the debug output from my function: ---- cut ---- == Info: disabling EPSV usage => Send header, 6 bytes (0x6) 0000: PASV <= Recv header, 50 bytes (0x32) 0000: 227 Entering Passive Mode (172,16,69,253,10,147) == Info: Trying 172.16.69.253... == Info: connected == Info: Connecting to 172.16.69.253 (172.16.69.253) port 2707 == Info: DO phase is comlete == Info: DO-MORE phase starts => Send header, 8 bytes (0x8) 0000: TYPE I <= Recv header, 20 bytes (0x14) 0000: 200 Type set to I. => Send header, 10 bytes (0xa) 0000: STOR tmp <= Recv header, 52 bytes (0x34) 0000: 150 Opening BINARY mode data connection for 'tmp'. == Info: DO-MORE phase ends => Send data, 46 bytes (0x2e) 0000: pc261011;172.16.69.22;Fri Jun 10 17:43:54 2005 == Info: Remembering we are in dir vhosts/wreiner/getip/ips/ == Info: Uploaded unaligned file size (46 out of 577774999411097646 bytes) == Info: Closing connection #0 ---- cut ---- i can open the transfered file on the server and it seems to be ok but curl comes up with the "unaligned file size"-error. establishing the connection is working well on both versions. both times it's the same server and both times the same file. so what could be wrong - i can't find the error? I'm using: Debian sid libcurl3-dev - 7.13.2-2 libc6 - 2.3.2.ds1-21 thanks for help. best regards :walter -- Weitersagen: GMX DSL-Flatrates mit Tempo-Garantie! Ab 4,99 Euro/Monat: http://www.gmx.net/de/go/dslReceived on 2005-06-13