2011-06-01 21:15:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/ftpc/ftpc_getfile.c
|
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-03 01:45:31 +02:00
|
|
|
#include "ftpc_config.h"
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2021-05-20 05:32:57 +02:00
|
|
|
#include <assert.h>
|
2011-06-01 21:15:14 +02:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/ftpc.h"
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
#include "ftpc_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_recvinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize to receive a file
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
static int ftpc_recvinit(struct ftpc_session_s *session,
|
|
|
|
FAR const char *path,
|
2011-06-01 21:15:14 +02:00
|
|
|
uint8_t xfrmode, off_t offset)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Reset transfer related variables */
|
|
|
|
|
|
|
|
ftpc_xfrreset(session);
|
|
|
|
|
|
|
|
ret = ftpc_xfrinit(session);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2020-02-22 19:52:12 +01:00
|
|
|
/* Configure the transfer: Initial file offset and transfer mode */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
session->offset = 0;
|
2011-06-01 21:15:14 +02:00
|
|
|
ftpc_xfrmode(session, xfrmode);
|
|
|
|
|
|
|
|
/* Handle the resume offset (caller is responsible for fseeking in the
|
|
|
|
* file)
|
|
|
|
*/
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
if (offset > 0)
|
|
|
|
{
|
|
|
|
/* Send the REST command. This command sets the offset where the
|
|
|
|
* transfer should start. This must come after PORT or PASV commands.
|
|
|
|
*/
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
ret = ftpc_cmd(session, "REST %ld", offset);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nwarn("WARNING: REST command failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
session->size = offset;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2011-06-02 03:14:12 +02:00
|
|
|
/* Send the RETR (Retrieve a remote file) command. Normally the server
|
|
|
|
* responds with a mark using code 150:
|
|
|
|
*
|
|
|
|
* - "150 File status okay; about to open data connection"
|
|
|
|
*
|
|
|
|
* It then stops accepting new connections, attempts to send the contents
|
|
|
|
* of the file over the data connection, and closes the data connection.
|
|
|
|
* Finally it either accepts the RETR request with:
|
|
|
|
*
|
|
|
|
* - "226 Closing data connection" if the entire file was successfully
|
|
|
|
* written to the server's TCP buffers
|
|
|
|
*
|
|
|
|
* Or rejects the RETR request with:
|
|
|
|
*
|
|
|
|
* - "425 Can't open data connection" if no TCP connection was established
|
|
|
|
* - "426 Connection closed; transfer aborted" if the TCP connection was
|
|
|
|
* established but then broken by the client or by network failure
|
|
|
|
* - "451 Requested action aborted: local error in processing" or
|
|
|
|
* "551 Requested action aborted: page type unknown" if the server had
|
|
|
|
* trouble reading the file from disk.
|
|
|
|
*/
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
ret = ftpc_cmd(session, "RETR %s", path);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nwarn("WARNING: RETR command failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* In active mode, we need to accept a connection on the data socket
|
|
|
|
* (in passive mode, we have already connected the data channel to
|
|
|
|
* the FTP server).
|
2011-06-04 22:13:03 +02:00
|
|
|
*/
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
if (!FTPC_IS_PASSIVE(session))
|
2011-06-04 22:13:03 +02:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
ret = ftpc_sockaccept(&session->dacceptor, &session->data);
|
2011-06-05 16:08:26 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Data connection not accepted\n");
|
2011-06-05 16:08:26 +02:00
|
|
|
}
|
2011-06-04 22:13:03 +02:00
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_recvbinary
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Receive a binary file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpc_recvbinary(FAR struct ftpc_session_s *session,
|
|
|
|
FAR FILE *rinstream, FAR FILE *loutstream)
|
|
|
|
{
|
|
|
|
ssize_t nread;
|
|
|
|
ssize_t nwritten;
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
/* Loop until the entire file is received */
|
2011-06-02 01:09:29 +02:00
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
for (; ; )
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Read the data from the socket */
|
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
nread = fread(session->buffer, sizeof(char), CONFIG_FTP_BUFSIZE,
|
|
|
|
rinstream);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (nread <= 0)
|
|
|
|
{
|
2011-06-05 18:46:27 +02:00
|
|
|
/* nread < 0 is an error */
|
2011-06-02 01:09:29 +02:00
|
|
|
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
/* errno should already be set by fread */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_xfrabort(session, rinstream);
|
2011-06-13 17:43:56 +02:00
|
|
|
return ERROR;
|
2011-06-02 01:09:29 +02:00
|
|
|
}
|
2011-06-05 18:46:27 +02:00
|
|
|
|
|
|
|
/* nread == 0 means end of file. Return success */
|
|
|
|
|
|
|
|
return OK;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Write the data to the file */
|
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
nwritten = fwrite(session->buffer, sizeof(char), nread, loutstream);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (nwritten != nread)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_xfrabort(session, loutstream);
|
2011-06-05 18:46:27 +02:00
|
|
|
|
|
|
|
/* If nwritten < 0 errno should already be set by fwrite.
|
|
|
|
* What would a short write mean?
|
|
|
|
*/
|
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
return ERROR;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2011-06-05 18:46:27 +02:00
|
|
|
/* Increment the size of the file written */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-06-05 18:46:27 +02:00
|
|
|
session->size += nwritten;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_getfile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a file from the remote host.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
int ftpc_getfile(SESSION handle, FAR const char *rname,
|
|
|
|
FAR const char *lname,
|
2011-06-01 21:15:14 +02:00
|
|
|
uint8_t how, uint8_t xfrmode)
|
|
|
|
{
|
|
|
|
FAR struct ftpc_session_s *session = (FAR struct ftpc_session_s *)handle;
|
|
|
|
struct stat statbuf;
|
|
|
|
FILE *loutstream;
|
2011-06-03 22:27:30 +02:00
|
|
|
FAR char *abslpath;
|
2011-06-01 21:15:14 +02:00
|
|
|
off_t offset;
|
|
|
|
int ret;
|
|
|
|
|
2011-06-04 00:53:36 +02:00
|
|
|
/* Don't call this with a NULL remote file name */
|
|
|
|
|
|
|
|
DEBUGASSERT(rname);
|
|
|
|
|
|
|
|
/* If the local name is not specified, then it is assumed to the same as
|
|
|
|
* the remote file name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!lname)
|
|
|
|
{
|
|
|
|
lname = rname;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:27:30 +02:00
|
|
|
/* Get the full path to the local file */
|
|
|
|
|
|
|
|
abslpath = ftpc_abslpath(session, lname);
|
|
|
|
if (!abslpath)
|
|
|
|
{
|
2020-11-24 23:28:20 +01:00
|
|
|
nwarn("WARNING: ftpc_abslpath(%s) failed: %d\n", lname, errno);
|
2011-06-03 22:27:30 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2021-06-27 08:38:52 +02:00
|
|
|
offset = 0;
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
/* Get information about the local file */
|
|
|
|
|
2011-06-03 22:27:30 +02:00
|
|
|
ret = stat(abslpath, &statbuf);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
/* It already exists. Is it a directory? */
|
|
|
|
|
|
|
|
if (S_ISDIR(statbuf.st_mode))
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nwarn("WARNING: '%s' is a directory\n", abslpath);
|
2011-06-03 22:27:30 +02:00
|
|
|
goto errout_with_abspath;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2021-06-27 08:38:52 +02:00
|
|
|
/* Is it write-able? */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
#ifdef S_IWRITE
|
2021-06-27 08:38:52 +02:00
|
|
|
if (!(statbuf.st_mode & S_IWRITE))
|
|
|
|
{
|
|
|
|
nwarn("WARNING: '%s' permission denied\n", abslpath);
|
|
|
|
goto errout_with_abspath;
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
#endif
|
|
|
|
|
2021-06-27 08:38:52 +02:00
|
|
|
/* Are we resuming the transfers? Is so then the starting offset is
|
|
|
|
* the size of the existing, partial file.
|
|
|
|
*/
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2021-06-27 08:38:52 +02:00
|
|
|
if (how == FTPC_GET_RESUME)
|
|
|
|
{
|
|
|
|
offset = statbuf.st_size;
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup to receive the file */
|
|
|
|
|
|
|
|
ret = ftpc_recvinit(session, rname, xfrmode, offset);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpc_recvinit failed\n");
|
2011-06-03 22:27:30 +02:00
|
|
|
goto errout_with_abspath;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
loutstream = fopen(abslpath,
|
|
|
|
(offset > 0 || (how == FTPC_GET_APPEND)) ? "a" : "w");
|
2011-06-01 21:15:14 +02:00
|
|
|
if (!loutstream)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: fopen failed: %d\n", errno);
|
2011-06-03 22:27:30 +02:00
|
|
|
goto errout_with_abspath;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
/* If the offset is non-zero, then seek to that offset in the file */
|
2011-06-03 22:27:30 +02:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
if (offset > 0)
|
|
|
|
{
|
|
|
|
ret = fseek(loutstream, offset, SEEK_SET);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: fseek failed: %d\n", errno);
|
2011-06-03 22:27:30 +02:00
|
|
|
goto errout_with_outstream;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And receive the new file data */
|
|
|
|
|
|
|
|
if (xfrmode == FTPC_XFRMODE_ASCII)
|
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
ret = ftpc_recvtext(session, session->data.instream, loutstream);
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
ret = ftpc_recvbinary(session, session->data.instream, loutstream);
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ftpc_sockclose(&session->data);
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
fptc_getreply(session);
|
|
|
|
}
|
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
/* Check for success */
|
2011-06-03 22:27:30 +02:00
|
|
|
|
|
|
|
if (ret == OK && !FTPC_INTERRUPTED(session))
|
|
|
|
{
|
|
|
|
fclose(loutstream);
|
2011-06-13 17:43:56 +02:00
|
|
|
free(abslpath);
|
2011-06-03 22:27:30 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Various error exits */
|
|
|
|
|
|
|
|
errout_with_outstream:
|
2011-06-01 21:15:14 +02:00
|
|
|
fclose(loutstream);
|
2011-06-03 22:27:30 +02:00
|
|
|
errout_with_abspath:
|
|
|
|
free(abslpath);
|
|
|
|
session->offset = 0;
|
|
|
|
errout:
|
|
|
|
return ERROR;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-06-13 17:43:56 +02:00
|
|
|
* Name: ftpc_recvtext
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Receive a text file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_recvtext(FAR struct ftpc_session_s *session,
|
|
|
|
FAR FILE *rinstream, FAR FILE *loutstream)
|
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
/* Read the next character from the incoming data stream */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
while ((ch = fgetc(rinstream)) != EOF)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Is it a carriage return? Compress \r\n to \n */
|
|
|
|
|
|
|
|
if (ch == '\r')
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Get the next character */
|
|
|
|
|
|
|
|
ch = fgetc(rinstream);
|
|
|
|
if (ch == EOF)
|
|
|
|
{
|
|
|
|
/* Ooops... */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_xfrabort(session, rinstream);
|
2011-06-13 17:43:56 +02:00
|
|
|
return ERROR;
|
2011-06-05 16:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If its not a newline, then keep the carriage return */
|
|
|
|
|
|
|
|
if (ch != '\n')
|
|
|
|
{
|
|
|
|
ungetc(ch, rinstream);
|
|
|
|
ch = '\r';
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
/* Then write the character to the output file */
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
if (fputc(ch, loutstream) == EOF)
|
|
|
|
{
|
|
|
|
ftpc_xfrabort(session, loutstream);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
/* Increase the actual size of the file by one */
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2020-11-24 23:27:01 +01:00
|
|
|
session->size++;
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
return OK;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|