libcurl FTP on Linux










0















FTP using libcurl:



i am trying to download file from server...After downloading the file.. checking the file size its showing 34179. but actual file size is different..i tried for many files its giving the same size ..i do not know
whether i have to add some more function..



 #include <stdio.h>
#include <string.h>

#include <curl/curl.h>

/* <DESC>
* Checks a single file's size and mtime from an FTP server.
* </DESC>
*/

static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)

(void)ptr;
(void)data;
/* we are not interested in the headers itself,
so we only return the size we would have saved ... */
return (size_t)(size * nmemb);


int main(void)

char ftpurl = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
CURL *curl;
CURLcode res;
long filetime = -1;
double filesize = 0.0;
const char *filename = strrchr(ftpurl, '/') + 1;

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl)
curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
/* No download if the file */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Ask for filetime */
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
/* No header output: TODO 14.1 http-style HEAD output for ftp */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
/* Switch on full protocol/debug output */
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */

res = curl_easy_perform(curl);

if(CURLE_OK == res)
/* https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
if((CURLE_OK == res) && (filetime >= 0))
time_t file_time = (time_t)filetime;
printf("filetime %s: %s", filename, ctime(&file_time));

res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
&filesize);
if((CURLE_OK == res) && (filesize>0.0))
printf("filesize %s: %0.0f bytesn", filename, filesize);

else
/* we failed */
fprintf(stderr, "curl told us %dn", res);


/* always cleanup */
curl_easy_cleanup(curl);


curl_global_cleanup();

return 0;



thanks in advance..










share|improve this question



















  • 1





    "...but actual file size is different..." - this just says it does not match without providing any details how it does not match. Please specify if you see a pattern, i.e. always too big or always too large, fixed difference or variable. Also check if the reported file size is the wrong one or the actual file size on disk.

    – Steffen Ullrich
    Nov 16 '18 at 5:30
















0















FTP using libcurl:



i am trying to download file from server...After downloading the file.. checking the file size its showing 34179. but actual file size is different..i tried for many files its giving the same size ..i do not know
whether i have to add some more function..



 #include <stdio.h>
#include <string.h>

#include <curl/curl.h>

/* <DESC>
* Checks a single file's size and mtime from an FTP server.
* </DESC>
*/

static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)

(void)ptr;
(void)data;
/* we are not interested in the headers itself,
so we only return the size we would have saved ... */
return (size_t)(size * nmemb);


int main(void)

char ftpurl = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
CURL *curl;
CURLcode res;
long filetime = -1;
double filesize = 0.0;
const char *filename = strrchr(ftpurl, '/') + 1;

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl)
curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
/* No download if the file */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Ask for filetime */
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
/* No header output: TODO 14.1 http-style HEAD output for ftp */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
/* Switch on full protocol/debug output */
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */

res = curl_easy_perform(curl);

if(CURLE_OK == res)
/* https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
if((CURLE_OK == res) && (filetime >= 0))
time_t file_time = (time_t)filetime;
printf("filetime %s: %s", filename, ctime(&file_time));

res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
&filesize);
if((CURLE_OK == res) && (filesize>0.0))
printf("filesize %s: %0.0f bytesn", filename, filesize);

else
/* we failed */
fprintf(stderr, "curl told us %dn", res);


/* always cleanup */
curl_easy_cleanup(curl);


curl_global_cleanup();

return 0;



thanks in advance..










share|improve this question



















  • 1





    "...but actual file size is different..." - this just says it does not match without providing any details how it does not match. Please specify if you see a pattern, i.e. always too big or always too large, fixed difference or variable. Also check if the reported file size is the wrong one or the actual file size on disk.

    – Steffen Ullrich
    Nov 16 '18 at 5:30














0












0








0








FTP using libcurl:



i am trying to download file from server...After downloading the file.. checking the file size its showing 34179. but actual file size is different..i tried for many files its giving the same size ..i do not know
whether i have to add some more function..



 #include <stdio.h>
#include <string.h>

#include <curl/curl.h>

/* <DESC>
* Checks a single file's size and mtime from an FTP server.
* </DESC>
*/

static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)

(void)ptr;
(void)data;
/* we are not interested in the headers itself,
so we only return the size we would have saved ... */
return (size_t)(size * nmemb);


int main(void)

char ftpurl = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
CURL *curl;
CURLcode res;
long filetime = -1;
double filesize = 0.0;
const char *filename = strrchr(ftpurl, '/') + 1;

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl)
curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
/* No download if the file */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Ask for filetime */
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
/* No header output: TODO 14.1 http-style HEAD output for ftp */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
/* Switch on full protocol/debug output */
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */

res = curl_easy_perform(curl);

if(CURLE_OK == res)
/* https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
if((CURLE_OK == res) && (filetime >= 0))
time_t file_time = (time_t)filetime;
printf("filetime %s: %s", filename, ctime(&file_time));

res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
&filesize);
if((CURLE_OK == res) && (filesize>0.0))
printf("filesize %s: %0.0f bytesn", filename, filesize);

else
/* we failed */
fprintf(stderr, "curl told us %dn", res);


/* always cleanup */
curl_easy_cleanup(curl);


curl_global_cleanup();

return 0;



thanks in advance..










share|improve this question
















FTP using libcurl:



i am trying to download file from server...After downloading the file.. checking the file size its showing 34179. but actual file size is different..i tried for many files its giving the same size ..i do not know
whether i have to add some more function..



 #include <stdio.h>
#include <string.h>

#include <curl/curl.h>

/* <DESC>
* Checks a single file's size and mtime from an FTP server.
* </DESC>
*/

static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)

(void)ptr;
(void)data;
/* we are not interested in the headers itself,
so we only return the size we would have saved ... */
return (size_t)(size * nmemb);


int main(void)

char ftpurl = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
CURL *curl;
CURLcode res;
long filetime = -1;
double filesize = 0.0;
const char *filename = strrchr(ftpurl, '/') + 1;

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl)
curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
/* No download if the file */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Ask for filetime */
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
/* No header output: TODO 14.1 http-style HEAD output for ftp */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
/* Switch on full protocol/debug output */
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */

res = curl_easy_perform(curl);

if(CURLE_OK == res)
/* https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
if((CURLE_OK == res) && (filetime >= 0))
time_t file_time = (time_t)filetime;
printf("filetime %s: %s", filename, ctime(&file_time));

res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
&filesize);
if((CURLE_OK == res) && (filesize>0.0))
printf("filesize %s: %0.0f bytesn", filename, filesize);

else
/* we failed */
fprintf(stderr, "curl told us %dn", res);


/* always cleanup */
curl_easy_cleanup(curl);


curl_global_cleanup();

return 0;



thanks in advance..







linux ftp libcurl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 11:21









Daniel Stenberg

31.7k866124




31.7k866124










asked Nov 16 '18 at 4:37









Karthik RKarthik R

112




112







  • 1





    "...but actual file size is different..." - this just says it does not match without providing any details how it does not match. Please specify if you see a pattern, i.e. always too big or always too large, fixed difference or variable. Also check if the reported file size is the wrong one or the actual file size on disk.

    – Steffen Ullrich
    Nov 16 '18 at 5:30













  • 1





    "...but actual file size is different..." - this just says it does not match without providing any details how it does not match. Please specify if you see a pattern, i.e. always too big or always too large, fixed difference or variable. Also check if the reported file size is the wrong one or the actual file size on disk.

    – Steffen Ullrich
    Nov 16 '18 at 5:30








1




1





"...but actual file size is different..." - this just says it does not match without providing any details how it does not match. Please specify if you see a pattern, i.e. always too big or always too large, fixed difference or variable. Also check if the reported file size is the wrong one or the actual file size on disk.

– Steffen Ullrich
Nov 16 '18 at 5:30






"...but actual file size is different..." - this just says it does not match without providing any details how it does not match. Please specify if you see a pattern, i.e. always too big or always too large, fixed difference or variable. Also check if the reported file size is the wrong one or the actual file size on disk.

– Steffen Ullrich
Nov 16 '18 at 5:30













1 Answer
1






active

oldest

votes


















0














Ok, so if you want to download the file, why do you ask libcurl to not download the "body", which is the actual file... ?



/* No download if the file */ 
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331514%2flibcurl-ftp-on-linux%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Ok, so if you want to download the file, why do you ask libcurl to not download the "body", which is the actual file... ?



    /* No download if the file */ 
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);





    share|improve this answer



























      0














      Ok, so if you want to download the file, why do you ask libcurl to not download the "body", which is the actual file... ?



      /* No download if the file */ 
      curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);





      share|improve this answer

























        0












        0








        0







        Ok, so if you want to download the file, why do you ask libcurl to not download the "body", which is the actual file... ?



        /* No download if the file */ 
        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);





        share|improve this answer













        Ok, so if you want to download the file, why do you ask libcurl to not download the "body", which is the actual file... ?



        /* No download if the file */ 
        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 16:39









        Daniel StenbergDaniel Stenberg

        31.7k866124




        31.7k866124





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331514%2flibcurl-ftp-on-linux%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric