Newer
Older
if(result)
return result;
}
/* When we connect, we start in the state where we await the 220
response */
Daniel Stenberg
committed
ftp_respinit(conn); /* init the response reader stuff */
state(conn, FTP_WAIT220);
ftpc->response = Curl_tvnow(); /* start response time-out now! */
if(data->state.used_interface == Curl_if_multi)
result = Curl_ftp_multi_statemach(conn, done);
else {
result = ftp_easy_statemach(conn);
if(!result)
*done = TRUE;
}
return result;
Daniel Stenberg
committed
}
/***********************************************************************
*
* Curl_ftp_done()
*
* The DONE function. This does what needs to be done after a single DO has
* performed.
* Input argument is already checked for validity.
Daniel Stenberg
committed
CURLcode Curl_ftp_done(struct connectdata *conn, CURLcode status, bool premature)
Daniel Stenberg
committed
{
struct SessionHandle *data = conn->data;
struct FTP *ftp = data->reqdata.proto.ftp;
struct ftp_conn *ftpc = &conn->proto.ftpc;
ssize_t nread;
int ftpcode;
CURLcode result=CURLE_OK;
bool was_ctl_valid = ftpc->ctl_valid;
char *path;
char *path_to_use = data->reqdata.path;
struct Curl_transfer_keeper *k = &data->reqdata.keep;
Daniel Stenberg
committed
if(!ftp)
/* When the easy handle is removed from the multi while libcurl is still
* trying to resolve the host name, it seems that the ftp struct is not
* yet initialized, but the removal action calls Curl_done() which calls
* this function. So we simply return success if no ftp pointer is set.
*/
return CURLE_OK;
switch(status) {
case CURLE_BAD_DOWNLOAD_RESUME:
case CURLE_FTP_WEIRD_PASV_REPLY:
case CURLE_FTP_PORT_FAILED:
case CURLE_FTP_COULDNT_SET_BINARY:
case CURLE_FTP_COULDNT_RETR_FILE:
case CURLE_FTP_COULDNT_STOR_FILE:
case CURLE_FTP_ACCESS_DENIED:
case CURLE_FILESIZE_EXCEEDED:
/* the connection stays alive fine even though this happened */
/* fall-through */
case CURLE_OK: /* doesn't affect the control connection's status */
Daniel Stenberg
committed
if (!premature) {
ftpc->ctl_valid = was_ctl_valid;
break;
}
/* until we cope better with prematurely ended requests, let them
Daniel Stenberg
committed
* fallback as if in complete failure */
default: /* by default, an error means the control connection is
wedged and should not be used anymore */
ftpc->ctl_valid = FALSE;
ftpc->cwdfail = TRUE; /* set this TRUE to prevent us to remember the
current path, as this connection is going */
conn->bits.close = TRUE; /* marked for closure */
break;
}
/* now store a copy of the directory we are in */
if(ftpc->prevpath)
free(ftpc->prevpath);
/* get the "raw" path */
path = curl_easy_unescape(data, path_to_use, 0, NULL);
if(!path) {
/* out of memory, but we can limp along anyway (and should try to
* since we're in the out of memory cleanup path) */
ftpc->prevpath = NULL; /* no path */
} else {
size_t flen = ftp->file?strlen(ftp->file):0; /* file is "raw" already */
size_t dlen = strlen(path)-flen;
if(dlen && !ftpc->cwdfail) {
ftpc->prevpath = path;
if(flen)
/* if 'path' is not the whole string */
ftpc->prevpath[dlen]=0; /* terminate */
infof(data, "Remembering we are in dir %s\n", ftpc->prevpath);
}
else {
ftpc->prevpath = NULL; /* no path */
free(path);
}
}
/* free the dir tree and file parts */
freedirs(conn);
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
Curl_sec_fflush_fd(conn, conn->sock[SECONDARYSOCKET]);
#endif
/* shut down the socket to inform the server we're done */
#ifdef _WIN32_WCE
shutdown(conn->sock[SECONDARYSOCKET],2); /* SD_BOTH */
#endif
sclose(conn->sock[SECONDARYSOCKET]);
conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
Daniel Stenberg
committed
if(!ftp->no_transfer && !status && !premature) {
Daniel Stenberg
committed
/*
* Let's see what the server says about the transfer we just performed,
* but lower the timeout as sometimes this connection has died while the
* data has been transfered. This happens when doing through NATs etc that
* abandon old silent connections.
*/
long old_time = ftpc->response_time;
Daniel Stenberg
committed
Daniel Stenberg
committed
ftpc->response_time = 60000; /* give it only a minute for now */
Daniel Stenberg
committed
result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
ftpc->response_time = old_time; /* set this back to previous value */
if(!nread && (CURLE_OPERATION_TIMEDOUT == result)) {
failf(data, "control connection looks dead");
ftpc->ctl_valid = FALSE; /* mark control connection as bad */
return result;
if(!ftpc->dont_check) {
/* 226 Transfer complete, 250 Requested file action okay, completed. */
if((ftpcode != 226) && (ftpcode != 250)) {
failf(data, "server did not report OK, got %d", ftpcode);
Daniel Stenberg
committed
result = CURLE_PARTIAL_FILE;
}
Daniel Stenberg
committed
if(result || premature)
Daniel Stenberg
committed
/* the response code from the transfer showed an error already so no
use checking further */
;
else if(data->set.upload) {
if((-1 != data->set.infilesize) &&
(data->set.infilesize != *ftp->bytecountp) &&
!data->set.crlf &&
!ftp->no_transfer) {
failf(data, "Uploaded unaligned file size (%" FORMAT_OFF_T
" out of %" FORMAT_OFF_T " bytes)",
*ftp->bytecountp, data->set.infilesize);
result = CURLE_PARTIAL_FILE;
}
}
else {
if((-1 != k->size) && (k->size != *ftp->bytecountp) &&
#ifdef CURL_DO_LINEEND_CONV
/* Most FTP servers don't adjust their file SIZE response for CRLFs, so
* we'll check to see if the discrepancy can be explained by the number
* of CRLFs we've changed to LFs.
*/
((k->size + data->state.crlf_conversions) != *ftp->bytecountp) &&
#endif /* CURL_DO_LINEEND_CONV */
(k->maxdownload != *ftp->bytecountp)) {
Daniel Stenberg
committed
failf(data, "Received only partial file: %" FORMAT_OFF_T " bytes",
*ftp->bytecountp);
result = CURLE_PARTIAL_FILE;
}
else if(!ftpc->dont_check &&
Daniel Stenberg
committed
!*ftp->bytecountp &&
(k->size>0)) {
Daniel Stenberg
committed
failf(data, "No data was received!");
result = CURLE_FTP_COULDNT_RETR_FILE;
}
}
/* clear these for next connection */
ftp->no_transfer = FALSE;
ftpc->dont_check = FALSE;
/* Send any post-transfer QUOTE strings? */
Daniel Stenberg
committed
if(!status && !result && !premature && data->set.postquote)
result = ftp_sendquote(conn, data->set.postquote);
return result;
}
/***********************************************************************
*
* ftp_sendquote()
*
* Where a 'quote' means a list of custom commands to send to the server.
* The quote list is passed as an argument.
*/
static
CURLcode ftp_sendquote(struct connectdata *conn, struct curl_slist *quote)
{
struct curl_slist *item;
ssize_t nread;
int ftpcode;
CURLcode result;
item = quote;
while (item) {
if (item->data) {
FTPSENDF(conn, "%s", item->data);
result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
if (result)
return result;
if (ftpcode >= 400) {
failf(conn->data, "QUOT string not accepted: %s", item->data);
return CURLE_FTP_QUOTE_ERROR;
item = item->next;
}
return CURLE_OK;
}
Daniel Stenberg
committed
/***********************************************************************
*
* ftp_need_type()
*
* Returns TRUE if we in the current situation should send TYPE
*/
static int ftp_need_type(struct connectdata *conn,
bool ascii_wanted)
{
return conn->proto.ftpc.transfertype != (ascii_wanted?'A':'I');
Daniel Stenberg
committed
}
/***********************************************************************
*
* ftp_nb_type()
*
* Set TYPE. We only deal with ASCII or BINARY so this function
* sets one of them.
* If the transfer type is not sent, simulate on OK response in newstate
*/
static CURLcode ftp_nb_type(struct connectdata *conn,
bool ascii, ftpstate newstate)
{
struct ftp_conn *ftpc = &conn->proto.ftpc;
Daniel Stenberg
committed
CURLcode result;
int want = ascii?'A':'I';
if (ftpc->transfertype == want) {
Daniel Stenberg
committed
state(conn, newstate);
return ftp_state_type_resp(conn, 200, newstate);
}
NBFTPSENDF(conn, "TYPE %c", want);
state(conn, newstate);
/* keep track of our current transfer type */
Daniel Stenberg
committed
return CURLE_OK;
}
/***************************************************************************
*
* ftp_pasv_verbose()
*
* This function only outputs some informationals about this second connection
* when we've issued a PASV command before and thus we have connected to a
* possibly new IP address.
*
*/
static void
ftp_pasv_verbose(struct connectdata *conn,
Curl_addrinfo *ai,
char *newhost, /* ascii version */
int port)
{
char buf[256];
Curl_printable_address(ai, buf, sizeof(buf));
infof(conn->data, "Connecting to %s (%s) port %d\n", newhost, buf, port);
}
/*
Check if this is a range download, and if so, set the internal variables
properly.
*/
static CURLcode ftp_range(struct connectdata *conn)
{
curl_off_t from, to;
curl_off_t totalsize=-1;
char *ptr;
char *ptr2;
struct SessionHandle *data = conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
if(data->reqdata.use_range && data->reqdata.range) {
from=curlx_strtoofft(data->reqdata.range, &ptr, 0);
Daniel Stenberg
committed
while(ptr && *ptr && (ISSPACE(*ptr) || (*ptr=='-')))
ptr++;
to=curlx_strtoofft(ptr, &ptr2, 0);
if(ptr == ptr2) {
/* we didn't get any digit */
to=-1;
}
if((-1 == to) && (from>=0)) {
/* X - */
data->reqdata.resume_from = from;
DEBUGF(infof(conn->data, "FTP RANGE %" FORMAT_OFF_T " to end of file\n",
from));
}
else if(from < 0) {
/* -Y */
totalsize = -from;
data->reqdata.maxdownload = -from;
data->reqdata.resume_from = from;
DEBUGF(infof(conn->data, "FTP RANGE the last %" FORMAT_OFF_T " bytes\n",
totalsize));
}
else {
/* X-Y */
totalsize = to-from;
data->reqdata.maxdownload = totalsize+1; /* include last byte */
data->reqdata.resume_from = from;
DEBUGF(infof(conn->data, "FTP RANGE from %" FORMAT_OFF_T
" getting %" FORMAT_OFF_T " bytes\n",
from, data->reqdata.maxdownload));
}
DEBUGF(infof(conn->data, "range-download from %" FORMAT_OFF_T
" to %" FORMAT_OFF_T ", totally %" FORMAT_OFF_T " bytes\n",
from, to, data->reqdata.maxdownload));
ftpc->dont_check = TRUE; /* dont check for successful transfer */
}
Daniel Stenberg
committed
else
data->reqdata.maxdownload = -1;
return CURLE_OK;
}
/*
* Curl_ftp_nextconnect()
*
* This function shall be called when the second FTP (data) connection is
* connected.
*/
CURLcode Curl_ftp_nextconnect(struct connectdata *conn)
{
struct SessionHandle *data=conn->data;
CURLcode result = CURLE_OK;
/* the ftp struct is inited in Curl_ftp_connect() */
struct FTP *ftp = data->reqdata.proto.ftp;
DEBUGF(infof(data, "DO-MORE phase starts\n"));
Daniel Stenberg
committed
Daniel Stenberg
committed
if(!ftp->no_transfer) {
/* a transfer is about to take place */
if(data->set.upload) {
Daniel Stenberg
committed
result = ftp_nb_type(conn, data->set.prefer_ascii,
FTP_STOR_TYPE);
if (result)
return result;
/* download */
ftp->downloadsize = -1; /* unknown as of yet */
result = ftp_range(conn);
if(result)
;
else if((data->set.ftp_list_only) || !ftp->file) {
/* The specified path ends with a slash, and therefore we think this
is a directory that is requested, use LIST. But before that we
need to set ASCII transfer mode. */
Daniel Stenberg
committed
result = ftp_nb_type(conn, 1, FTP_LIST_TYPE);
if (result)
return result;
}
else {
Daniel Stenberg
committed
result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_RETR_TYPE);
if (result)
return result;
}
result = ftp_easy_statemach(conn);
if(ftp->no_transfer)
/* no data to transfer. FIX: it feels like a kludge to have this here
too! */
result=Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
DEBUGF(infof(data, "DO-MORE phase ends with %d\n", result));
return result;
/***********************************************************************
*
* ftp_perform()
*
* This is the actual DO function for FTP. Get a file/directory according to
* the options previously setup.
*/
static
CURLcode ftp_perform(struct connectdata *conn,
bool *connected, /* connect status after PASV / PORT */
bool *dophase_done)
{
/* this is FTP and no proxy */
CURLcode result=CURLE_OK;
DEBUGF(infof(conn->data, "DO phase starts\n"));
Daniel Stenberg
committed
if(conn->bits.no_body) {
/* requested no body means no transfer... */
struct FTP *ftp = conn->data->reqdata.proto.ftp;
ftp->no_transfer = TRUE;
}
*dophase_done = FALSE; /* not done yet */
/* start the first command in the DO phase */
result = ftp_state_quote(conn, TRUE, FTP_QUOTE);
if(result)
Daniel Stenberg
committed
return result;
/* run the state-machine */
if(conn->data->state.used_interface == Curl_if_multi)
result = Curl_ftp_multi_statemach(conn, dophase_done);
else {
result = ftp_easy_statemach(conn);
*dophase_done = TRUE; /* with the easy interface we are done here */
}
*connected = conn->bits.tcpconnect;
if(*dophase_done)
DEBUGF(infof(conn->data, "DO phase is complete\n"));
return result;
}
/***********************************************************************
*
* Curl_ftp()
*
* This function is registered as 'curl_do' function. It decodes the path
* parts etc as a wrapper to the actual DO function (ftp_perform).
*
* The input argument is already checked for validity.
*/
CURLcode Curl_ftp(struct connectdata *conn, bool *done)
CURLcode retcode = CURLE_OK;
Daniel Stenberg
committed
*done = FALSE; /* default to false */
/*
Since connections can be re-used between SessionHandles, this might be a
connection already existing but on a fresh SessionHandle struct so we must
make sure we have a good 'struct FTP' to play with. For new connections,
the struct FTP is allocated and setup in the Curl_ftp_connect() function.
*/
retcode = ftp_init(conn);
if(retcode)
return retcode;
Daniel Stenberg
committed
retcode = ftp_parse_url_path(conn);
if (retcode)
return retcode;
retcode = ftp_regular_transfer(conn, done);
Daniel Stenberg
committed
/***********************************************************************
*
* Curl_(nb)ftpsendf()
*
* Sends the formated string as a ftp command to a ftp server
*
* NOTE: we build the command in a fixed-length buffer, which sets length
* restrictions on the command!
*
* The "nb" version is made to Never Block.
CURLcode Curl_nbftpsendf(struct connectdata *conn,
const char *fmt, ...)
{
ssize_t bytes_written;
/* may still not be big enough for some krb5 tokens */
#define SBUF_SIZE 1024
char s[SBUF_SIZE];
size_t write_len;
char *sptr=s;
CURLcode res = CURLE_OK;
struct SessionHandle *data = conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
enum protection_level data_sec = conn->data_prot;
#endif
va_list ap;
va_start(ap, fmt);
vsnprintf(s, SBUF_SIZE-3, fmt, ap);
va_end(ap);
strcat(s, "\r\n"); /* append a trailing CRLF */
bytes_written=0;
write_len = strlen(s);
Daniel Stenberg
committed
ftp_respinit(conn);
#ifdef CURL_DOES_CONVERSIONS
res = Curl_convert_to_network(data, s, write_len);
/* Curl_convert_to_network calls failf if unsuccessful */
if(res != CURLE_OK) {
return res;
}
#endif /* CURL_DOES_CONVERSIONS */
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
conn->data_prot = prot_cmd;
#endif
res = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
&bytes_written);
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
conn->data_prot = data_sec;
#endif
if(CURLE_OK != res)
return res;
if(conn->data->set.verbose)
Curl_debug(conn->data, CURLINFO_HEADER_OUT,
sptr, (size_t)bytes_written, conn);
if(bytes_written != (ssize_t)write_len) {
/* the whole chunk was not sent, store the rest of the data */
write_len -= bytes_written;
sptr += bytes_written;
ftpc->sendthis = malloc(write_len);
if(ftpc->sendthis) {
memcpy(ftpc->sendthis, sptr, write_len);
ftpc->sendsize = ftpc->sendleft = write_len;
}
else {
failf(data, "out of memory");
res = CURLE_OUT_OF_MEMORY;
}
}
else
ftpc->response = Curl_tvnow();
return res;
}
Daniel Stenberg
committed
CURLcode Curl_ftpsendf(struct connectdata *conn,
const char *fmt, ...)
ssize_t bytes_written;
char s[SBUF_SIZE];
char *sptr=s;
CURLcode res = CURLE_OK;
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
enum protection_level data_sec = conn->data_prot;
#endif
va_list ap;
va_start(ap, fmt);
vsnprintf(s, SBUF_SIZE-3, fmt, ap);
va_end(ap);
strcat(s, "\r\n"); /* append a trailing CRLF */
Daniel Stenberg
committed
write_len = strlen(s);
#ifdef CURL_DOES_CONVERSIONS
res = Curl_convert_to_network(conn->data, s, write_len);
/* Curl_convert_to_network calls failf if unsuccessful */
if(res != CURLE_OK) {
return(res);
}
#endif /* CURL_DOES_CONVERSIONS */
while(1) {
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
conn->data_prot = prot_cmd;
#endif
Daniel Stenberg
committed
res = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
&bytes_written);
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
conn->data_prot = data_sec;
#endif
if(CURLE_OK != res)
break;
if(conn->data->set.verbose)
Curl_debug(conn->data, CURLINFO_HEADER_OUT,
sptr, (size_t)bytes_written, conn);
if(bytes_written != (ssize_t)write_len) {
write_len -= bytes_written;
sptr += bytes_written;
}
else
break;
}
return res;
}
/***********************************************************************
*
Daniel Stenberg
committed
* ftp_quit()
*
* This should be called before calling sclose() on an ftp control connection
* (not data connections). We should then wait for the response from the
* server before returning. The calling code should then try to close the
* connection.
*
*/
Daniel Stenberg
committed
static CURLcode ftp_quit(struct connectdata *conn)
{
CURLcode result = CURLE_OK;
if(conn->proto.ftpc.ctl_valid) {
NBFTPSENDF(conn, "QUIT", NULL);
state(conn, FTP_QUIT);
result = ftp_easy_statemach(conn);
}
return result;
}
/***********************************************************************
*
* Curl_ftp_disconnect()
*
* Disconnect from an FTP server. Cleanup protocol-specific per-connection
* resources. BLOCKING.
CURLcode Curl_ftp_disconnect(struct connectdata *conn)
{
struct ftp_conn *ftpc= &conn->proto.ftpc;
/* We cannot send quit unconditionally. If this connection is stale or
bad in any way, sending quit and waiting around here will make the
disconnect wait in vain and cause more problems than we need to.
Daniel Stenberg
committed
ftp_quit() will check the state of ftp->ctl_valid. If it's ok it
will try to send the QUIT command, otherwise it will just return.
*/
Daniel Stenberg
committed
/* The FTP session may or may not have been allocated/setup at this point! */
if(conn->data->reqdata.proto.ftp) {
Daniel Stenberg
committed
(void)ftp_quit(conn); /* ignore errors on the QUIT */
Daniel Stenberg
committed
if(ftpc->entrypath) {
struct SessionHandle *data = conn->data;
data->state.most_recent_ftp_entrypath = NULL;
free(ftpc->entrypath);
ftpc->entrypath = NULL;
}
if(ftpc->cache) {
free(ftpc->cache);
ftpc->cache = NULL;
}
freedirs(conn);
if(ftpc->prevpath) {
free(ftpc->prevpath);
ftpc->prevpath = NULL;
Daniel Stenberg
committed
}
Sterling Hughes
committed
/***********************************************************************
*
Daniel Stenberg
committed
* ftp_parse_url_path()
Daniel Stenberg
committed
* Parse the URL path into separate path components.
*
*/
static
Daniel Stenberg
committed
CURLcode ftp_parse_url_path(struct connectdata *conn)
{
struct SessionHandle *data = conn->data;
/* the ftp struct is already inited in ftp_connect() */
struct FTP *ftp = data->reqdata.proto.ftp;
struct ftp_conn *ftpc = &conn->proto.ftpc;
char *slash_pos; /* position of the first '/' char in curpos */
char *path_to_use = data->reqdata.path;
char *cur_pos;
cur_pos = path_to_use; /* current position in path. point at the begin
of next path component */
ftpc->ctl_valid = FALSE;
ftpc->cwdfail = FALSE;
switch(data->set.ftp_filemethod) {
case FTPFILE_NOCWD:
/* fastest, but less standard-compliant */
ftp->file = data->reqdata.path; /* this is a full file path */
case FTPFILE_SINGLECWD:
/* get the last slash */
Daniel Stenberg
committed
if(!path_to_use[0]) {
/* no dir, no file */
ftpc->dirdepth = 0;
ftp->file = NULL;
break;
}
Daniel Stenberg
committed
if(slash_pos || !*cur_pos) {
ftpc->dirs = (char **)calloc(1, sizeof(ftpc->dirs[0]));
if(!ftpc->dirs)
ftpc->dirs[0] = curl_easy_unescape(conn->data, slash_pos ? cur_pos : "/",
slash_pos?(int)(slash_pos-cur_pos):1,
NULL);
if(!ftpc->dirs[0]) {
return CURLE_OUT_OF_MEMORY;
}
ftpc->dirdepth = 1; /* we consider it to be a single dir */
ftp->file = slash_pos ? slash_pos+1 : cur_pos; /* rest is file name */
else
ftp->file = cur_pos; /* this is a file name only */
break;
default: /* allow pretty much anything */
case FTPFILE_MULTICWD:
ftpc->dirdepth = 0;
ftpc->diralloc = 5; /* default dir depth to allocate */
ftpc->dirs = (char **)calloc(ftpc->diralloc, sizeof(ftpc->dirs[0]));
if(!ftpc->dirs)
Daniel Stenberg
committed
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
/* we have a special case for listing the root dir only */
if(strequal(path_to_use, "/")) {
cur_pos++; /* make it point to the zero byte */
ftpc->dirs[0] = strdup("/");
ftpc->dirdepth++;
}
else {
/* parse the URL path into separate path components */
while ((slash_pos = strchr(cur_pos, '/')) != NULL) {
/* 1 or 0 to indicate absolute directory */
bool absolute_dir = (bool)((cur_pos - data->reqdata.path > 0) &&
(ftpc->dirdepth == 0));
/* seek out the next path component */
if (slash_pos-cur_pos) {
/* we skip empty path components, like "x//y" since the FTP command
CWD requires a parameter and a non-existant parameter a) doesn't
work on many servers and b) has no effect on the others. */
int len = (int)(slash_pos - cur_pos + absolute_dir);
ftpc->dirs[ftpc->dirdepth] =
curl_easy_unescape(conn->data, cur_pos - absolute_dir, len, NULL);
if (!ftpc->dirs[ftpc->dirdepth]) { /* run out of memory ... */
failf(data, "no memory");
freedirs(conn);
return CURLE_OUT_OF_MEMORY;
}
if (isBadFtpString(ftpc->dirs[ftpc->dirdepth])) {
free(ftpc->dirs[ftpc->dirdepth]);
freedirs(conn);
return CURLE_URL_MALFORMAT;
}
Daniel Stenberg
committed
else {
cur_pos = slash_pos + 1; /* jump to the rest of the string */
continue;
Daniel Stenberg
committed
cur_pos = slash_pos + 1; /* jump to the rest of the string */
if(++ftpc->dirdepth >= ftpc->diralloc) {
/* enlarge array */
char *bigger;
ftpc->diralloc *= 2; /* double the size each time */
bigger = realloc(ftpc->dirs, ftpc->diralloc * sizeof(ftpc->dirs[0]));
if(!bigger) {
freedirs(conn);
return CURLE_OUT_OF_MEMORY;
}
ftpc->dirs = (char **)bigger;
}
}
}
ftp->file = cur_pos; /* the rest is the file name */
}
Daniel Stenberg
committed
if(ftp->file && *ftp->file) {
ftp->file = curl_easy_unescape(conn->data, ftp->file, 0, NULL);
if(NULL == ftp->file) {
freedirs(conn);
failf(data, "no memory");
return CURLE_OUT_OF_MEMORY;
}
if (isBadFtpString(ftp->file)) {
freedirs(conn);
return CURLE_URL_MALFORMAT;
}
}
else
ftp->file=NULL; /* instead of point to a zero byte, we make it a NULL
pointer */
Daniel Stenberg
committed
if(data->set.upload && !ftp->file && !ftp->no_transfer) {
/* We need a file name when uploading. Return error! */
failf(data, "Uploading to a URL without a file name!");
return CURLE_URL_MALFORMAT;
}
ftpc->cwddone = FALSE; /* default to not done */
if(ftpc->prevpath) {
Daniel Stenberg
committed
/* prevpath is "raw" so we convert the input path before we compare the
strings */
char *path = curl_easy_unescape(conn->data, data->reqdata.path, 0, NULL);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
dlen = strlen(path) - (ftp->file?strlen(ftp->file):0);
if((dlen == strlen(ftpc->prevpath)) &&
curl_strnequal(path, ftpc->prevpath, dlen)) {
infof(data, "Request has same path as previous transfer\n");
ftpc->cwddone = TRUE;
Daniel Stenberg
committed
free(path);
Daniel Stenberg
committed
}
/* call this when the DO phase has completed */
static CURLcode ftp_dophase_done(struct connectdata *conn,
bool connected)
{
CURLcode result = CURLE_OK;
struct FTP *ftp = conn->data->reqdata.proto.ftp;
struct ftp_conn *ftpc = &conn->proto.ftpc;
if(connected)
result = Curl_ftp_nextconnect(conn);
if(result && (conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD)) {
/* Failure detected, close the second socket if it was created already */
sclose(conn->sock[SECONDARYSOCKET]);
conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
}
if(ftp->no_transfer)
/* no data to transfer */
result=Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
else if(!connected)
/* since we didn't connect now, we want do_more to get called */
conn->bits.do_more = TRUE;
ftpc->ctl_valid = TRUE; /* seems good */
return result;
}
/* called from multi.c while DOing */
CURLcode Curl_ftp_doing(struct connectdata *conn,
bool *dophase_done)
{
CURLcode result;
result = Curl_ftp_multi_statemach(conn, dophase_done);
if(*dophase_done) {
result = ftp_dophase_done(conn, FALSE /* not connected */);
DEBUGF(infof(conn->data, "DO phase is complete\n"));
}
return result;
}
Daniel Stenberg
committed
/***********************************************************************
*
* ftp_regular_transfer()
*
* The input argument is already checked for validity.
*
* Performs all commands done before a regular transfer between a local and a
* remote host.
Daniel Stenberg
committed
*
* ftp->ctl_valid starts out as FALSE, and gets set to TRUE if we reach the
* Curl_ftp_done() function without finding any major problem.
*/
static
CURLcode ftp_regular_transfer(struct connectdata *conn,
bool *dophase_done)
Daniel Stenberg
committed
{
CURLcode result=CURLE_OK;
Daniel Stenberg
committed
bool connected=0;
struct SessionHandle *data = conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
data->reqdata.size = -1; /* make sure this is unknown at this point */
Daniel Stenberg
committed
Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0);
Curl_pgrsSetDownloadSize(data, 0);
ftpc->ctl_valid = TRUE; /* starts good */
result = ftp_perform(conn,
&connected, /* have we connected after PASV/PORT */
dophase_done); /* all commands in the DO-phase done? */
if(CURLE_OK == result) {
if(!*dophase_done)
/* the DO phase has not completed yet */
return CURLE_OK;
result = ftp_dophase_done(conn, connected);
if(result)
return result;
}
else
freedirs(conn);
return result;
}
#endif /* CURL_DISABLE_FTP */