curl-library
Help with libcurl on FTP upload
Date: Tue, 22 Jul 2008 16:07:00 +0000
im using ftpupload.c sample:
my code:
bool FTPUpload(const char *host, const char *srcFile, const char *destFile = NULL, const char *renameTo = NULL, int timeout = 0)
{
if(!host)
{
Warning(""PLUGIN_TAG_LONG" Invalid host\n");
return false;
}
if(!srcFile)
{
Warning(""PLUGIN_TAG_LONG" Invalid source file\n");
return false;
}
if(!g_pFullFileSystem->FileExists(srcFile, "MOD"))
{
Warning(""PLUGIN_TAG_LONG" Can't find file: %s\n", srcFile);
return false;
}
if(!destFile)destFile = srcFile;
CURL *curl;
CURLcode res = CURLE_GOT_NOTHING;
FILE *hd_src;
struct stat file_info;
struct curl_slist *headerlist=NULL;
char *buf_1 = Utils::StrNew(Utils::StrFormat("RNFR %s", destFile));
char *buf_2 = NULL;
if(renameTo)
buf_2 = Utils::StrNew(Utils::StrFormat("RNTO %s", renameTo));
char *remoteUrl = Utils::StrNew(Utils::StrFormat("%s/%s", host, destFile));
//static const char buf_1 [] = cmda;
// static const char buf_2 [] = cmda;
if (!Utils::StrLeftEq(remoteUrl, "ftp://"))
remoteUrl = Utils::StrNew(Utils::StrFormat("ftp://%s", remoteUrl));
const char *localfile = Utils::StrNew(Utils::StrFormat("%s/%s", szGamedir, srcFile));
/* get the file size of the local file */
if(stat(localfile, &file_info))
{
Warning(""PLUGIN_TAG_LONG" Couldn't open '%s': %s\n", srcFile, strerror(errno));
return false;
}
Msg(""PLUGIN_TAG_LONG" Uploading '%s' %ld Bytes.\n", srcFile, file_info.st_size);
float timeTook = engine->Time();
time_t rawtime;
struct tm * timeinfo;
char buffTimeOld[80];
char buffTimeNow[80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime(buffTimeOld,80,"%X%p",timeinfo);
/* get a FILE * of the same file */
hd_src = fopen(localfile, "rb");
/* get a curl handle */
curl = curl_easy_init();
if(curl)
{
/* build a list of commands to pass to libcurl */
headerlist = curl_slist_append(headerlist, buf_1);
if(buf_2)
headerlist = curl_slist_append(headerlist, buf_2);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* specify target */
curl_easy_setopt(curl,CURLOPT_URL, remoteUrl);
/* Set timeout */
if(timeout > 0)
curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
/* 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);
/* Set the size of the file to upload (optional). If you give a *_LARGE
option you MUST make sure that the type of the passed-in argument is a
curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
make sure that to pass in a type 'long' argument. */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,(curl_off_t)file_info.st_size);
/* 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);
}
else
Warning(""PLUGIN_TAG_LONG" Couldn't initalize upload!\n");
fclose(hd_src); /* close the local file */
timeTook = engine->Time()-timeTook;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime(buffTimeNow,80,"%X%p",timeinfo);
if(renameTo)
destFile = renameTo;
if(res != CURLE_OK)
Warning(""PLUGIN_TAG_LONG" Fail (%d) on try upload a file to FTP, check your host, username, password\n", res);
else
{
event_ftp fakeEvent; // Event stuff
fakeEvent.srcFile = srcFile; // Event stuff
fakeEvent.destFile = destFile; // Event stuff
fakeEvent.fizeSize = file_info.st_size; // Event stuff
fakeEvent.timeTook = timeTook; // Event stuff
strncpy(fakeEvent.startAt, buffTimeOld, 80); // Event stuff
strncpy(fakeEvent.endAt, buffTimeNow, 80); // Event stuff
Msg("***************** "PLUGIN_TAG_LONG" *****************\n");
Msg("%s <<TO>> %s\nUpload took '%f' seconds.\nSize: %ld\nSource: %s\nTarget: %s\n", buffTimeOld, buffTimeNow, timeTook, file_info.st_size, srcFile, destFile);
Msg("************************************************\n");
queueEventFTP.AddToTail(fakeEvent);
}
return true;
}
When i do: st_ftpupload "*****:*****@***********/httpdocs" motd.txt
i get a sucess mensage:
ServerTools: Uploading 'motd.txt' 1134 Bytes.
***************** ServerTools: *****************
16:51:20PM <<TO>> 16:51:21PM
Upload took '0.547361' seconds.
Size: 1134
Source: motd.txt
Target: motd.txt
************************************************
But if i upload using folders path will not work like: st_ftpupload "*****:*****@***********/httpdocs" maps/motd.txt
i get:
ServerTools: Uploading 'maps/motd.txt' 1134 Bytes.ServerTools: Fail (21) on try upload a file to FTP, check your host, username, password
CURLE_QUOTE_ERROR, /* 21 - quote command failure */
Also if i go to ftp, under folder "httpdocs/maps/" i can see the file created "motd.txt", but not get renamed
Why i get this errors? now to fix it?
thanks -sn4k3
_________________________________________________________________
Cansado de espaço para só 50 fotos? Conheça o Spaces, o site de relacionamentos com até 6,000 fotos!
http://www.amigosdomessenger.com.br
Received on 2008-07-22