Newer
Older
break;
Daniel Stenberg
committed
case FTP_CWD:
if(ftpcode/100 != 2) {
/* failure to CWD there */
if(conn->data->set.ftp_create_missing_dirs &&
ftpc->count1 && !ftpc->count2) {
/* try making it */
ftpc->count2++; /* counter to prevent CWD-MKD loops */
PPSENDF(&ftpc->pp, "MKD %s", ftpc->dirs[ftpc->count1 - 1]);
state(conn, FTP_MKD);
}
/* return failure */
failf(data, "Server denied you to change to the given directory");
ftpc->cwdfail = TRUE; /* don't remember this path as we failed
to enter it */
return CURLE_REMOTE_ACCESS_DENIED;
}
else {
/* success */
ftpc->count2=0;
if(++ftpc->count1 <= ftpc->dirdepth) {
/* send next CWD */
PPSENDF(&ftpc->pp, "CWD %s", ftpc->dirs[ftpc->count1 - 1]);
}
else {
if(result)
return result;
}
}
break;
Daniel Stenberg
committed
case FTP_MKD:
Daniel Stenberg
committed
if((ftpcode/100 != 2) && !ftpc->count3--) {
/* failure to MKD the dir */
failf(data, "Failed to MKD dir: %03d", ftpcode);
return CURLE_REMOTE_ACCESS_DENIED;
}
state(conn, FTP_CWD);
/* send CWD */
PPSENDF(&ftpc->pp, "CWD %s", ftpc->dirs[ftpc->count1 - 1]);
break;
case FTP_MDTM:
result = ftp_state_mdtm_resp(conn, ftpcode);
break;
Daniel Stenberg
committed
case FTP_TYPE:
case FTP_LIST_TYPE:
case FTP_RETR_TYPE:
case FTP_STOR_TYPE:
result = ftp_state_type_resp(conn, ftpcode, ftpc->state);
break;
Daniel Stenberg
committed
case FTP_SIZE:
case FTP_RETR_SIZE:
case FTP_STOR_SIZE:
result = ftp_state_size_resp(conn, ftpcode, ftpc->state);
break;
case FTP_REST:
case FTP_RETR_REST:
result = ftp_state_rest_resp(conn, ftpcode, ftpc->state);
break;
Daniel Stenberg
committed
case FTP_PRET:
if(ftpcode != 200) {
/* there only is this one standard OK return code. */
Daniel Stenberg
committed
failf(data, "PRET command not accepted: %03d", ftpcode);
return CURLE_FTP_PRET_FAILED;
}
result = ftp_state_use_pasv(conn);
break;
case FTP_PASV:
result = ftp_state_pasv_resp(conn, ftpcode);
break;
Daniel Stenberg
committed
case FTP_PORT:
result = ftp_state_port_resp(conn, ftpcode);
break;
Daniel Stenberg
committed
case FTP_LIST:
case FTP_RETR:
result = ftp_state_get_resp(conn, ftpcode, ftpc->state);
break;
Daniel Stenberg
committed
case FTP_STOR:
result = ftp_state_stor_resp(conn, ftpcode, ftpc->state);
break;
case FTP_QUIT:
/* fallthrough, just stop! */
default:
/* internal error */
state(conn, FTP_STOP);
break;
}
} /* if(ftpcode) */
return result;
}
Daniel Stenberg
committed
/* called repeatedly until done from multi.c */
static CURLcode ftp_multi_statemach(struct connectdata *conn,
bool *done)
{
struct ftp_conn *ftpc = &conn->proto.ftpc;
CURLcode result = Curl_pp_statemach(&ftpc->pp, FALSE);
Daniel Stenberg
committed
/* Check for the state outside of the Curl_socket_ready() return code checks
since at times we are in fact already in this state when this function
gets called. */
*done = (ftpc->state == FTP_STOP) ? TRUE : FALSE;
Daniel Stenberg
committed
return result;
}
static CURLcode ftp_block_statemach(struct connectdata *conn)
{
struct ftp_conn *ftpc = &conn->proto.ftpc;
struct pingpong *pp = &ftpc->pp;
CURLcode result = CURLE_OK;
Sterling Hughes
committed
while(ftpc->state != FTP_STOP) {
result = Curl_pp_statemach(pp, TRUE);
if(result)
break;
return result;
}
Daniel Stenberg
committed
/*
* ftp_connect() should do everything that is to be considered a part of
* the connection phase.
*
* The variable 'done' points to will be TRUE if the protocol-layer connect
* phase is done when this function returns, or FALSE if not.
*
*/
static CURLcode ftp_connect(struct connectdata *conn,
Patrick Monnerat
committed
bool *done) /* see description above */
{
CURLcode result;
struct ftp_conn *ftpc = &conn->proto.ftpc;
struct pingpong *pp = &ftpc->pp;
*done = FALSE; /* default to not done yet */
Daniel Stenberg
committed
/* We always support persistent connections on ftp */
conn->bits.close = FALSE;
pp->response_time = RESP_TIMEOUT; /* set default response time-out */
pp->statemach_act = ftp_statemach_act;
pp->endofresp = ftp_endofresp;
pp->conn = conn;
if(conn->handler->flags & PROTOPT_SSL) {
/* BLOCKING */
Daniel Stenberg
committed
result = Curl_ssl_connect(conn, FIRSTSOCKET);
if(result)
return result;
}
Curl_pp_init(pp); /* init the generic pingpong data */
/* When we connect, we start in the state where we await the 220
response */
state(conn, FTP_WAIT220);
result = ftp_multi_statemach(conn, done);
return result;
Daniel Stenberg
committed
}
/***********************************************************************
*
* 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.
static CURLcode ftp_done(struct connectdata *conn, CURLcode status,
Patrick Monnerat
committed
bool premature)
Daniel Stenberg
committed
{
struct SessionHandle *data = conn->data;
struct FTP *ftp = data->req.protop;
struct ftp_conn *ftpc = &conn->proto.ftpc;
struct pingpong *pp = &ftpc->pp;
ssize_t nread;
int ftpcode;
bool was_ctl_valid = ftpc->ctl_valid;
char *path;
const char *path_to_use = data->state.path;
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_ACCEPT_FAILED:
case CURLE_FTP_ACCEPT_TIMEOUT:
case CURLE_FTP_COULDNT_SET_TYPE:
case CURLE_FTP_COULDNT_RETR_FILE:
case CURLE_PARTIAL_FILE:
Daniel Stenberg
committed
case CURLE_UPLOAD_FAILED:
case CURLE_REMOTE_ACCESS_DENIED:
case CURLE_FILESIZE_EXCEEDED:
case CURLE_REMOTE_FILE_NOT_FOUND:
Daniel Stenberg
committed
case CURLE_WRITE_ERROR:
/* 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) {
Daniel Stenberg
committed
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 */
Daniel Stenberg
committed
result = status; /* use the already set error code */
break;
}
/* now store a copy of the directory we are in */
if(ftpc->prevpath)
free(ftpc->prevpath);
if(data->set.wildcardmatch) {
if(data->set.chunk_end && ftpc->file) {
data->set.chunk_end(data->wildcard.customptr);
}
ftpc->known_filesize = -1;
}
/* 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 may already be in the out of memory cleanup path) */
if(!result)
result = CURLE_OUT_OF_MEMORY;
ftpc->ctl_valid = FALSE; /* mark control connection as bad */
conn->bits.close = TRUE; /* mark for connection closure */
ftpc->prevpath = NULL; /* no path remembering */
size_t flen = ftpc->file?strlen(ftpc->file):0; /* file is "raw" already */
size_t dlen = strlen(path)-flen;
Daniel Stenberg
committed
if(!ftpc->cwdfail) {
if(dlen && (data->set.ftp_filemethod != FTPFILE_NOCWD)) {
Daniel Stenberg
committed
ftpc->prevpath = path;
if(flen)
/* if 'path' is not the whole string */
ftpc->prevpath[dlen]=0; /* terminate */
}
else {
/* we never changed dir */
ftpc->prevpath=strdup("");
free(path);
}
if(ftpc->prevpath)
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 */
Daniel Stenberg
committed
freedirs(ftpc);
/* shut down the socket to inform the server we're done */
#ifdef _WIN32_WCE
shutdown(conn->sock[SECONDARYSOCKET],2); /* SD_BOTH */
#endif
if(conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD) {
if(!result && ftpc->dont_check && data->req.maxdownload > 0) {
/* partial download completed */
result = Curl_pp_sendf(pp, "%s", "ABOR");
if(result) {
failf(data, "Failure sending ABOR command: %s",
curl_easy_strerror(result));
ftpc->ctl_valid = FALSE; /* mark control connection as bad */
conn->bits.close = TRUE; /* mark for connection closure */
}
if(conn->ssl[SECONDARYSOCKET].use) {
/* The secondary socket is using SSL so we must close down that part
first before we close the socket for real */
Curl_ssl_close(conn, SECONDARYSOCKET);
Daniel Stenberg
committed
/* Note that we keep "use" set to TRUE since that (next) connection is
still requested to use SSL */
}
if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET]) {
Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
conn->bits.tcpconnect[SECONDARYSOCKET] = FALSE;
}
if(!result && (ftp->transfer == FTPTRANSFER_BODY) && ftpc->ctl_valid &&
pp->pending_resp && !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 transferred. This happens when doing through NATs etc that
* abandon old silent connections.
*/
long old_time = pp->response_time;
Daniel Stenberg
committed
pp->response_time = 60*1000; /* give it only a minute for now */
Daniel Stenberg
committed
pp->response = Curl_tvnow(); /* timeout relative now */
Daniel Stenberg
committed
result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
pp->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 */
Daniel Stenberg
committed
conn->bits.close = TRUE; /* mark for closure */
if(ftpc->dont_check && data->req.maxdownload > 0) {
/* we have just sent ABOR and there is no reliable way to check if it was
* successful or not; we have to close the connection now */
infof(data, "partial download completed, closing connection\n");
conn->bits.close = TRUE; /* mark for closure */
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->transfer == FTPTRANSFER_BODY)) {
failf(data, "Uploaded unaligned file size (%" FORMAT_OFF_T
" out of %" FORMAT_OFF_T " bytes)",
Daniel Stenberg
committed
*ftp->bytecountp, data->set.infilesize);
result = CURLE_PARTIAL_FILE;
}
}
else {
Daniel Stenberg
committed
if((-1 != data->req.size) &&
(data->req.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.
*/
Daniel Stenberg
committed
((data->req.size + data->state.crlf_conversions) !=
*ftp->bytecountp) &&
#endif /* CURL_DO_LINEEND_CONV */
Daniel Stenberg
committed
(data->req.maxdownload != *ftp->bytecountp)) {
failf(data, "Received only partial file: %" FORMAT_OFF_T " bytes",
Daniel Stenberg
committed
*ftp->bytecountp);
result = CURLE_PARTIAL_FILE;
}
else if(!ftpc->dont_check &&
Daniel Stenberg
committed
!*ftp->bytecountp &&
Daniel Stenberg
committed
(data->req.size>0)) {
Daniel Stenberg
committed
failf(data, "No data was received!");
result = CURLE_FTP_COULDNT_RETR_FILE;
}
}
/* clear these for next connection */
ftp->transfer = FTPTRANSFER_BODY;
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.
*
* BLOCKING
*/
static
CURLcode ftp_sendquote(struct connectdata *conn, struct curl_slist *quote)
{
struct curl_slist *item;
ssize_t nread;
int ftpcode;
CURLcode result;
struct ftp_conn *ftpc = &conn->proto.ftpc;
struct pingpong *pp = &ftpc->pp;
item = quote;
Daniel Stenberg
committed
while(item) {
if(item->data) {
char *cmd = item->data;
bool acceptfail = FALSE;
/* if a command starts with an asterisk, which a legal FTP command never
can, the command will be allowed to fail without it causing any
aborts or cancels etc. It will cause libcurl to act as if the command
is successful, whatever the server reponds. */
if(cmd[0] == '*') {
cmd++;
acceptfail = TRUE;
}
PPSENDF(&conn->proto.ftpc.pp, "%s", cmd);
pp->response = Curl_tvnow(); /* timeout relative now */
result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
Daniel Stenberg
committed
if(result)
return result;
if(!acceptfail && (ftpcode >= 400)) {
failf(conn->data, "QUOT string not accepted: %s", cmd);
return CURLE_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;
Daniel Stenberg
committed
Daniel Stenberg
committed
if(ftpc->transfertype == want) {
Daniel Stenberg
committed
state(conn, newstate);
return ftp_state_type_resp(conn, 200, newstate);
}
PPSENDF(&ftpc->pp, "TYPE %c", want);
Daniel Stenberg
committed
state(conn, newstate);
/* keep track of our current transfer type */
ftpc->transfertype = want;
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;
char *ptr;
char *ptr2;
struct SessionHandle *data = conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
Daniel Stenberg
committed
if(data->state.use_range && data->state.range) {
from=curlx_strtoofft(data->state.range, &ptr, 0);
while(*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 - */
Daniel Stenberg
committed
data->state.resume_from = from;
DEBUGF(infof(conn->data, "FTP RANGE %" FORMAT_OFF_T " to end of file\n",
}
else if(from < 0) {
/* -Y */
Daniel Stenberg
committed
data->req.maxdownload = -from;
data->state.resume_from = from;
DEBUGF(infof(conn->data, "FTP RANGE the last %" FORMAT_OFF_T " bytes\n",
}
else {
/* X-Y */
data->req.maxdownload = (to-from)+1; /* include last byte */
Daniel Stenberg
committed
data->state.resume_from = from;
DEBUGF(infof(conn->data, "FTP RANGE from %" FORMAT_OFF_T
" getting %" FORMAT_OFF_T " bytes\n",
Daniel Stenberg
committed
from, data->req.maxdownload));
}
DEBUGF(infof(conn->data, "range-download from %" FORMAT_OFF_T
" to %" FORMAT_OFF_T ", totally %" FORMAT_OFF_T " bytes\n",
Daniel Stenberg
committed
from, to, data->req.maxdownload));
ftpc->dont_check = TRUE; /* dont check for successful transfer */
}
Daniel Stenberg
committed
else
Daniel Stenberg
committed
data->req.maxdownload = -1;
return CURLE_OK;
}
/*
*
* This function shall be called when the second FTP (data) connection is
* connected.
*
* 'complete' can return 0 for incomplete, 1 for done and -1 for go back
* (which basically is only for when PASV is being sent to retry a failed
* EPSV).
*/
static CURLcode ftp_do_more(struct connectdata *conn, int *completep)
{
struct SessionHandle *data=conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
CURLcode result = CURLE_OK;
bool connected = FALSE;
bool complete = FALSE;
/* the ftp struct is inited in ftp_connect() */
struct FTP *ftp = data->req.protop;
/* if the second connection isn't done yet, wait for it */
if(!conn->bits.tcpconnect[SECONDARYSOCKET]) {
if(conn->tunnel_state[SECONDARYSOCKET] == TUNNEL_CONNECT) {
/* As we're in TUNNEL_CONNECT state now, we know the proxy name and port
aren't used so we blank their arguments. TODO: make this nicer */
result = Curl_proxyCONNECT(conn, SECONDARYSOCKET, NULL, 0);
return result;
}
result = Curl_is_connected(conn, SECONDARYSOCKET, &connected);
/* Ready to do more? */
if(connected) {
DEBUGF(infof(data, "DO-MORE connected phase starts\n"));
}
else {
if(result && (ftpc->count1 == 0)) {
*completep = -1; /* go back to DOING please */
/* this is a EPSV connect failing, try PASV instead */
return ftp_epsv_disable(conn);
}
Daniel Stenberg
committed
if(ftpc->state) {
/* already in a state so skip the intial commands.
They are only done to kickstart the do_more state */
result = ftp_multi_statemach(conn, &complete);
*completep = (int)complete;
/* if we got an error or if we don't wait for a data connection return
immediately */
if(result || (ftpc->wait_data_conn != TRUE))
return result;
if(ftpc->wait_data_conn)
/* if we reach the end of the FTP state machine here, *complete will be
TRUE but so is ftpc->wait_data_conn, which says we need to wait for
the data connection and therefore we're not actually complete */
*completep = 0;
if(ftp->transfer <= FTPTRANSFER_INFO) {
/* a transfer is about to take place, or if not a file name was given
so we'll do a SIZE on it later and then we need the right TYPE first */
if(ftpc->wait_data_conn == TRUE) {
bool serv_conned;
result = ReceivedServerConnect(conn, &serv_conned);
if(result)
return result; /* Failed to accept data connection */
if(serv_conned) {
/* It looks data connection is established */
result = AcceptServerConnect(conn);
ftpc->wait_data_conn = FALSE;
result = InitiateTransfer(conn);
if(result)
return result;
*completep = 1; /* this state is now complete when the server has
connected back to us */
}
}
else if(data->set.upload) {
result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_STOR_TYPE);
Daniel Stenberg
committed
if(result)
Daniel Stenberg
committed
return result;
result = ftp_multi_statemach(conn, &complete);
*completep = (int)complete;
/* download */
ftp->downloadsize = -1; /* unknown as of yet */
result = ftp_range(conn);
if(result)
;
else if(data->set.ftp_list_only || !ftpc->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. */
/* But only if a body transfer was requested. */
if(ftp->transfer == FTPTRANSFER_BODY) {
result = ftp_nb_type(conn, TRUE, FTP_LIST_TYPE);
Daniel Stenberg
committed
if(result)
return result;
}
/* otherwise just fall through */
}
else {
Daniel Stenberg
committed
result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_RETR_TYPE);
Daniel Stenberg
committed
if(result)
Daniel Stenberg
committed
return result;
}
result = ftp_multi_statemach(conn, &complete);
*completep = (int)complete;
if((result == CURLE_OK) && (ftp->transfer != FTPTRANSFER_BODY))
/* no data to transfer. FIX: it feels like a kludge to have this here
too! */
Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
if(!ftpc->wait_data_conn) {
/* no waiting for the data connection so this is now complete */
*completep = 1;
DEBUGF(infof(data, "DO-MORE phase ends with %d\n", (int)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->data->set.opt_no_body) {
Daniel Stenberg
committed
/* requested no body means no transfer... */
struct FTP *ftp = conn->data->req.protop;
ftp->transfer = FTPTRANSFER_INFO;
Daniel Stenberg
committed
}
*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 */
result = ftp_multi_statemach(conn, dophase_done);
*connected = conn->bits.tcpconnect[SECONDARYSOCKET];
infof(conn->data, "ftp_perform ends with SECONDARY: %d\n", *connected);
if(*dophase_done)
DEBUGF(infof(conn->data, "DO phase is complete1\n"));
return result;
}
static void wc_data_dtor(void *ptr)
{
struct ftp_wc_tmpdata *tmp = ptr;
if(tmp)
Curl_ftp_parselist_data_free(&tmp->parser);
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
Curl_safefree(tmp);
}
static CURLcode init_wc_data(struct connectdata *conn)
{
char *last_slash;
char *path = conn->data->state.path;
struct WildcardData *wildcard = &(conn->data->wildcard);
CURLcode ret = CURLE_OK;
struct ftp_wc_tmpdata *ftp_tmp;
last_slash = strrchr(conn->data->state.path, '/');
if(last_slash) {
last_slash++;
if(last_slash[0] == '\0') {
wildcard->state = CURLWC_CLEAN;
ret = ftp_parse_url_path(conn);
return ret;
}
else {
wildcard->pattern = strdup(last_slash);
if(!wildcard->pattern)
return CURLE_OUT_OF_MEMORY;
last_slash[0] = '\0'; /* cut file from path */
}
}
else { /* there is only 'wildcard pattern' or nothing */
if(path[0]) {
wildcard->pattern = strdup(path);
if(!wildcard->pattern)
return CURLE_OUT_OF_MEMORY;
path[0] = '\0';
}
else { /* only list */
wildcard->state = CURLWC_CLEAN;
ret = ftp_parse_url_path(conn);
return ret;
}
}
/* program continues only if URL is not ending with slash, allocate needed
resources for wildcard transfer */
/* allocate ftp protocol specific temporary wildcard data */
ftp_tmp = calloc(1, sizeof(struct ftp_wc_tmpdata));
Curl_safefree(wildcard->pattern);
return CURLE_OUT_OF_MEMORY;
}
/* INITIALIZE parselist structure */
ftp_tmp->parser = Curl_ftp_parselist_data_alloc();
if(!ftp_tmp->parser) {
Curl_safefree(wildcard->pattern);
Curl_safefree(ftp_tmp);
wildcard->tmp = ftp_tmp; /* put it to the WildcardData tmp pointer */
wildcard->tmp_dtor = wc_data_dtor;
/* wildcard does not support NOCWD option (assert it?) */
if(conn->data->set.ftp_filemethod == FTPFILE_NOCWD)
conn->data->set.ftp_filemethod = FTPFILE_MULTICWD;
/* try to parse ftp url */
ret = ftp_parse_url_path(conn);
if(ret) {
Curl_safefree(wildcard->pattern);
wildcard->tmp_dtor(wildcard->tmp);
wildcard->tmp_dtor = ZERO_NULL;
wildcard->tmp = NULL;
wildcard->path = strdup(conn->data->state.path);
if(!wildcard->path) {
Curl_safefree(wildcard->pattern);
wildcard->tmp_dtor(wildcard->tmp);
wildcard->tmp_dtor = ZERO_NULL;
wildcard->tmp = NULL;
return CURLE_OUT_OF_MEMORY;
}
/* backup old write_function */
ftp_tmp->backup.write_function = conn->data->set.fwrite_func;
/* parsing write function */
conn->data->set.fwrite_func = Curl_ftp_parselist;
/* backup old file descriptor */
ftp_tmp->backup.file_descriptor = conn->data->set.out;
/* let the writefunc callback know what curl pointer is working with */
conn->data->set.out = conn;
infof(conn->data, "Wildcard - Parsing started\n");
return CURLE_OK;
}
/* This is called recursively */
static CURLcode wc_statemach(struct connectdata *conn)
{
struct WildcardData * const wildcard = &(conn->data->wildcard);
switch (wildcard->state) {
case CURLWC_INIT:
ret = init_wc_data(conn);
if(wildcard->state == CURLWC_CLEAN)
/* only listing! */
break;
else
wildcard->state = ret ? CURLWC_ERROR : CURLWC_MATCHING;
break;
case CURLWC_MATCHING: {
/* In this state is LIST response successfully parsed, so lets restore
previous WRITEFUNCTION callback and WRITEDATA pointer */
struct ftp_wc_tmpdata *ftp_tmp = wildcard->tmp;
conn->data->set.fwrite_func = ftp_tmp->backup.write_function;
conn->data->set.out = ftp_tmp->backup.file_descriptor;
ftp_tmp->backup.write_function = ZERO_NULL;
ftp_tmp->backup.file_descriptor = NULL;
wildcard->state = CURLWC_DOWNLOADING;
if(Curl_ftp_parselist_geterror(ftp_tmp->parser)) {
/* error found in LIST parsing */
wildcard->state = CURLWC_CLEAN;
return wc_statemach(conn);
}
else if(wildcard->filelist->size == 0) {
/* no corresponding file */
wildcard->state = CURLWC_CLEAN;
return CURLE_REMOTE_FILE_NOT_FOUND;
}
return wc_statemach(conn);
case CURLWC_DOWNLOADING: {
/* filelist has at least one file, lets get first one */
struct ftp_conn *ftpc = &conn->proto.ftpc;
struct curl_fileinfo *finfo = wildcard->filelist->head->ptr;
char *tmp_path = aprintf("%s%s", wildcard->path, finfo->filename);
if(!tmp_path)
return CURLE_OUT_OF_MEMORY;
/* switch default "state.pathbuffer" and tmp_path, good to see
ftp_parse_url_path function to understand this trick */
Curl_safefree(conn->data->state.pathbuffer);
conn->data->state.pathbuffer = tmp_path;
conn->data->state.path = tmp_path;
infof(conn->data, "Wildcard - START of \"%s\"\n", finfo->filename);
if(conn->data->set.chunk_bgn) {
long userresponse = conn->data->set.chunk_bgn(
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
finfo, wildcard->customptr, (int)wildcard->filelist->size);
switch(userresponse) {
case CURL_CHUNK_BGN_FUNC_SKIP:
infof(conn->data, "Wildcard - \"%s\" skipped by user\n",
finfo->filename);
wildcard->state = CURLWC_SKIP;
return wc_statemach(conn);
case CURL_CHUNK_BGN_FUNC_FAIL:
return CURLE_CHUNK_FAILED;
}
}
if(finfo->filetype != CURLFILETYPE_FILE) {
wildcard->state = CURLWC_SKIP;
return wc_statemach(conn);
}
if(finfo->flags & CURLFINFOFLAG_KNOWN_SIZE)
ftpc->known_filesize = finfo->size;
ret = ftp_parse_url_path(conn);
if(ret) {
return ret;
}
/* we don't need the Curl_fileinfo of first file anymore */
Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
if(wildcard->filelist->size == 0) { /* remains only one file to down. */
wildcard->state = CURLWC_CLEAN;
/* after that will be ftp_do called once again and no transfer
will be done because of CURLWC_CLEAN state */
return CURLE_OK;
}
} break;
case CURLWC_SKIP: {
if(conn->data->set.chunk_end)
conn->data->set.chunk_end(conn->data->wildcard.customptr);
Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
wildcard->state = (wildcard->filelist->size == 0) ?
CURLWC_CLEAN : CURLWC_DOWNLOADING;
return wc_statemach(conn);
case CURLWC_CLEAN: {
struct ftp_wc_tmpdata *ftp_tmp = wildcard->tmp;
ret = CURLE_OK;
if(ftp_tmp) {
ret = Curl_ftp_parselist_geterror(ftp_tmp->parser);
}
wildcard->state = ret ? CURLWC_ERROR : CURLWC_DONE;
case CURLWC_DONE:
case CURLWC_ERROR:
break;
}