Newer
Older
Daniel Stenberg
committed
* 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_TYPE:
case CURLE_FTP_COULDNT_RETR_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're in the out of memory cleanup path) */
ftpc->prevpath = NULL; /* no path */
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);
#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
if(conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD) {
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]) {
sclose(conn->sock[SECONDARYSOCKET]);
conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
}
}
Daniel Stenberg
committed
if((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 transfered. 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) {
/* 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;
}
FTPSENDF(conn, "%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;
}
/*
* ftp_nextconnect()
*
* This function shall be called when the second FTP (data) connection is
* connected.
*/
static CURLcode ftp_nextconnect(struct connectdata *conn)
{
struct SessionHandle *data=conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
CURLcode result = CURLE_OK;
/* the ftp struct is inited in ftp_connect() */
Daniel Stenberg
committed
struct FTP *ftp = data->state.proto.ftp;
DEBUGF(infof(data, "DO-MORE phase starts\n"));
Daniel Stenberg
committed
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(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;
/* 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_easy_statemach(conn);
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);
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... */
Daniel Stenberg
committed
struct FTP *ftp = conn->data->state.proto.ftp;
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 */
if(conn->data->state.used_interface == Curl_if_multi)
result = 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;
}
static void wc_data_dtor(void *ptr)
{
struct ftp_wc_tmpdata *tmp = ptr;
if(tmp)
Curl_ftp_parselist_data_free(&tmp->parser);
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
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 = malloc(sizeof(struct ftp_wc_tmpdata));
if(!ftp_tmp) {
return CURLE_OUT_OF_MEMORY;
}
/* INITIALIZE parselist structure */
ftp_tmp->parser = Curl_ftp_parselist_data_alloc();
if(!ftp_tmp->parser) {
free(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) {
return ret;
}
/* backup old write_function */
ftp_tmp->backup.write_function = conn->data->set.fwrite_func;
/* parsing write function (callback included directly from ftplistparser.c) */
conn->data->set.fwrite_func = Curl_ftp_parselist;
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
/* 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;
wildcard->path = strdup(conn->data->state.path);
if(!wildcard->path) {
return CURLE_OUT_OF_MEMORY;
}
infof(conn->data, "Wildcard - Parsing started\n");
return CURLE_OK;
}
static CURLcode wc_statemach(struct connectdata *conn)
{
struct ftp_conn *ftpc = &conn->proto.ftpc;
struct WildcardData *wildcard = &(conn->data->wildcard);
struct ftp_wc_tmpdata *ftp_tmp = wildcard->tmp;
char *tmp_path;
CURLcode ret = CURLE_OK;
long userresponse = 0;
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 */
ftp_tmp = wildcard->tmp;
conn->data->set.fwrite_func = ftp_tmp->backup.write_function;
conn->data->set.out = ftp_tmp->backup.file_descriptor;
wildcard->state = CURLWC_DOWNLOADING;
if(Curl_ftp_parselist_geterror(ftp_tmp->parser)) {
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
/* 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;
}
ret = wc_statemach(conn);
break;
case CURLWC_DOWNLOADING: {
/* filelist has at least one file, lets get first one */
struct curl_fileinfo *finfo = wildcard->filelist->head->ptr;
tmp_path = malloc(strlen(conn->data->state.path) +
strlen(finfo->filename) + 1);
if(!tmp_path) {
return CURLE_OUT_OF_MEMORY;
}
tmp_path[0] = 0;
/* make full path to matched file */
strcat(tmp_path, wildcard->path);
strcat(tmp_path, finfo->filename);
/* switch default "state.pathbuffer" and tmp_path, good to see
ftp_parse_url_path function to understand this trick */
if(conn->data->state.pathbuffer)
free(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) {
userresponse = conn->data->set.chunk_bgn(
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;
ret = wc_statemach(conn);
} break;
case CURLWC_CLEAN:
ret = CURLE_OK;
if(ftp_tmp) {
ret = Curl_ftp_parselist_geterror(ftp_tmp->parser);
}
wildcard->state = ret ? CURLWC_ERROR : CURLWC_DONE;
break;
case CURLWC_DONE:
case CURLWC_ERROR:
break;
}
return ret;
}
/***********************************************************************
*
* ftp_do()
*
* 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.
*/
static CURLcode ftp_do(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 ftp_connect() function.
*/
retcode = ftp_init(conn);
if(retcode)
return retcode;
if(conn->data->set.wildcardmatch) {
retcode = wc_statemach(conn);
if(conn->data->wildcard.state == CURLWC_SKIP ||
conn->data->wildcard.state == CURLWC_DONE) {
/* do not call ftp_regular_transfer */
return CURLE_OK;
}
if(retcode) /* error, loop or skipping the file */
return retcode;
}
else { /* no wildcard FSM needed */
retcode = ftp_parse_url_path(conn);
if(retcode)
return retcode;
}
Daniel Stenberg
committed
retcode = ftp_regular_transfer(conn, done);
Daniel Stenberg
committed
Daniel Stenberg
committed
CURLcode Curl_ftpsendf(struct connectdata *conn,
const char *fmt, ...)
ssize_t bytes_written;
#define SBUF_SIZE 1024
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 */
#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) {
PPSENDF(&conn->proto.ftpc.pp, "QUIT", NULL);
state(conn, FTP_QUIT);
result = ftp_easy_statemach(conn);
}
return result;
}
/***********************************************************************
*
* ftp_disconnect()
*
* Disconnect from an FTP server. Cleanup protocol-specific per-connection
* resources. BLOCKING.
static CURLcode ftp_disconnect(struct connectdata *conn)
struct ftp_conn *ftpc= &conn->proto.ftpc;
struct pingpong *pp = &ftpc->pp;
/* 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! */
Daniel Stenberg
committed
(void)ftp_quit(conn); /* ignore errors on the QUIT */
if(ftpc->entrypath) {
struct SessionHandle *data = conn->data;
Daniel Stenberg
committed
if(data->state.most_recent_ftp_entrypath == ftpc->entrypath) {
Daniel Stenberg
committed
data->state.most_recent_ftp_entrypath = NULL;
Daniel Stenberg
committed
free(ftpc->entrypath);
ftpc->entrypath = NULL;
}
Daniel Stenberg
committed
freedirs(ftpc);
if(ftpc->prevpath) {
free(ftpc->prevpath);
ftpc->prevpath = NULL;
Daniel Stenberg
committed
}
Patrick Monnerat
committed
if(ftpc->server_os) {
free(ftpc->server_os);
ftpc->server_os = NULL;
}
Daniel Stenberg
committed
Curl_pp_disconnect(pp);
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() */
Daniel Stenberg
committed
struct FTP *ftp = data->state.proto.ftp;
struct ftp_conn *ftpc = &conn->proto.ftpc;
const char *slash_pos; /* position of the first '/' char in curpos */
const char *path_to_use = data->state.path;
const char *cur_pos;
const char *filename = NULL;
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 */
/*
The best time to check whether the path is a file or directory is right
here. so:
the first condition in the if() right here, is there just in case
someone decides to set path to NULL one day
*/
Daniel Stenberg
committed
if(data->state.path &&
data->state.path[0] &&
(data->state.path[strlen(data->state.path) - 1] != '/') )
filename = data->state.path; /* this is a full file path */
/*
ftpc->file is not used anywhere other than for operations on a file.
In other words, never for directory operations.
So we can safely leave filename as NULL here and use it as a
argument in dir/file decisions.
*/
case FTPFILE_SINGLECWD:
/* get the last slash */
Daniel Stenberg
committed
if(!path_to_use[0]) {
/* no dir, no file */
ftpc->dirdepth = 0;
break;
}
Daniel Stenberg
committed
if(slash_pos || !*cur_pos) {
ftpc->dirs = 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]) {
Daniel Stenberg
committed
freedirs(ftpc);
return CURLE_OUT_OF_MEMORY;
}
ftpc->dirdepth = 1; /* we consider it to be a single dir */
filename = slash_pos ? slash_pos+1 : cur_pos; /* rest is file name */
filename = cur_pos; /* this is a file name only */
default: /* allow pretty much anything */
case FTPFILE_MULTICWD:
ftpc->dirdepth = 0;
ftpc->diralloc = 5; /* default dir depth to allocate */
ftpc->dirs = calloc(ftpc->diralloc, sizeof(ftpc->dirs[0]));
if(!ftpc->dirs)
Daniel Stenberg
committed
/* 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 */
Daniel Stenberg
committed
while((slash_pos = strchr(cur_pos, '/')) != NULL) {
Daniel Stenberg
committed
/* 1 or 0 to indicate absolute directory */
Daniel Stenberg
committed
bool absolute_dir = (bool)((cur_pos - data->state.path > 0) &&
Daniel Stenberg
committed
(ftpc->dirdepth == 0));
/* seek out the next path component */
Daniel Stenberg
committed
if(slash_pos-cur_pos) {
Daniel Stenberg
committed
/* 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);
Daniel Stenberg
committed
if(!ftpc->dirs[ftpc->dirdepth]) { /* run out of memory ... */
Daniel Stenberg
committed
failf(data, "no memory");
Daniel Stenberg
committed
freedirs(ftpc);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
}
Daniel Stenberg
committed
if(isBadFtpString(ftpc->dirs[ftpc->dirdepth])) {
Daniel Stenberg
committed
free(ftpc->dirs[ftpc->dirdepth]);
Daniel Stenberg
committed
freedirs(ftpc);
Daniel Stenberg
committed
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) {
Daniel Stenberg
committed
freedirs(ftpc);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
}
ftpc->dirs = (char **)bigger;