2011-06-01 21:15:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/ftpc/ftpc_putfile.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>
|
2022-01-18 16:40:32 +01:00
|
|
|
#include <sys/sendfile.h>
|
2022-07-17 08:05:23 +02:00
|
|
|
#include <inttypes.h>
|
2011-06-01 21:15:14 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <libgen.h>
|
2021-05-20 05:32:57 +02:00
|
|
|
#include <assert.h>
|
2011-06-02 01:33:39 +02:00
|
|
|
#include <errno.h>
|
2011-06-01 21:15:14 +02:00
|
|
|
#include <debug.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_sendbinary
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send a binary file to the remote host.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-18 16:40:32 +01:00
|
|
|
#ifdef CONFIG_FTPC_OVER_SENDFILE
|
2011-06-01 21:15:14 +02:00
|
|
|
static int ftpc_sendbinary(FAR struct ftpc_session_s *session,
|
2022-01-18 16:40:32 +01:00
|
|
|
FAR FILE *linstream)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
off_t offset = session->offset;
|
|
|
|
ssize_t result;
|
|
|
|
ssize_t len;
|
|
|
|
int linfd = fileno(linstream);
|
|
|
|
|
|
|
|
if (linfd == -1 || fstat(linfd, &stat_buf) == -1)
|
|
|
|
{
|
|
|
|
ftpc_xfrabort(session, NULL);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until the entire file is sent */
|
|
|
|
|
|
|
|
len = stat_buf.st_size - offset;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
result = sendfile(session->data.sd, linfd, &offset, len);
|
|
|
|
|
|
|
|
if (result == -1 && errno == EAGAIN)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (result == -1)
|
|
|
|
{
|
|
|
|
ftpc_xfrabort(session, NULL);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= result;
|
|
|
|
|
|
|
|
/* Increment the size of the file sent */
|
|
|
|
|
|
|
|
session->size += result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int ftpc_sendbinary(FAR struct ftpc_session_s *session,
|
|
|
|
FAR FILE *linstream)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
ssize_t nread;
|
|
|
|
ssize_t nwritten;
|
2022-01-18 16:40:32 +01:00
|
|
|
FILE *routstream = session->data.outstream;
|
2011-06-05 20:53:46 +02:00
|
|
|
|
|
|
|
/* Loop until the entire file is sent */
|
|
|
|
|
2020-11-24 23:29:18 +01:00
|
|
|
for (; ; )
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Read data from the file */
|
|
|
|
|
2020-11-24 23:29:18 +01:00
|
|
|
nread = fread(session->buffer, sizeof(char), CONFIG_FTP_BUFSIZE,
|
|
|
|
linstream);
|
2011-06-02 01:09:29 +02:00
|
|
|
if (nread <= 0)
|
|
|
|
{
|
|
|
|
/* nread == 0 is just EOF */
|
|
|
|
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_xfrabort(session, linstream);
|
2011-06-13 17:43:56 +02:00
|
|
|
return ERROR;
|
2011-06-02 01:09:29 +02:00
|
|
|
}
|
2011-06-05 20:53:46 +02:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
/* Return success */
|
2011-06-05 20:53:46 +02:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
return OK;
|
2011-06-02 01:09:29 +02:00
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Send the data */
|
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
nwritten = fwrite(session->buffer, sizeof(char), nread, routstream);
|
2011-06-02 01:09:29 +02:00
|
|
|
if (nwritten != nread)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_xfrabort(session, routstream);
|
2011-06-05 20:53:46 +02:00
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Return failure */
|
2011-06-05 20:53:46 +02:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
return ERROR;
|
2011-06-02 01:09:29 +02:00
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Increment the size of the file sent */
|
|
|
|
|
2011-06-02 01:09:29 +02:00
|
|
|
session->size += nread;
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
2022-01-18 16:40:32 +01:00
|
|
|
#endif
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sendtext
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send a text file to the remote host.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpc_sendtext(FAR struct ftpc_session_s *session,
|
2022-01-18 16:40:32 +01:00
|
|
|
FAR FILE *linstream)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2011-06-05 20:53:46 +02:00
|
|
|
int ch;
|
2011-06-01 21:15:14 +02:00
|
|
|
int ret = OK;
|
2022-01-18 16:40:32 +01:00
|
|
|
FILE *routstream = session->data.outstream;
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Write characters one at a time. */
|
|
|
|
|
2011-06-05 20:53:46 +02:00
|
|
|
while ((ch = fgetc(linstream)) != EOF)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
/* If it is a newline, send a carriage return too */
|
|
|
|
|
2011-06-05 20:53:46 +02:00
|
|
|
if (ch == '\n')
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
if (fputc('\r', routstream) == EOF)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_xfrabort(session, routstream);
|
2011-06-01 21:15:14 +02:00
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Increment the size of the file sent */
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
session->size++;
|
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Send the character */
|
|
|
|
|
2011-06-05 20:53:46 +02:00
|
|
|
if (fputc(ch, routstream) == EOF)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_xfrabort(session, routstream);
|
2011-06-01 21:15:14 +02:00
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Increment the size of the file sent */
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
session->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sendfile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the file to the remote host.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpc_sendfile(struct ftpc_session_s *session, const char *path,
|
|
|
|
FILE *stream, uint8_t how, uint8_t xfrmode)
|
|
|
|
{
|
2016-06-11 22:13:39 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2011-06-13 17:43:56 +02:00
|
|
|
FAR char *rname;
|
2011-06-02 03:14:12 +02:00
|
|
|
FAR char *str;
|
|
|
|
int len;
|
2011-06-13 17:43:56 +02:00
|
|
|
#endif
|
2011-06-01 21:15:14 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Were we asked to store a file uniquely? Does the host support the STOU
|
|
|
|
* command?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (how == FTPC_PUT_UNIQUE && !FTPC_HAS_STOU(session))
|
|
|
|
{
|
|
|
|
/* We cannot store a file uniquely */
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftpc_xfrreset(session);
|
|
|
|
FTPC_SET_PUT(session);
|
|
|
|
|
|
|
|
/* Initialize for the transfer */
|
|
|
|
|
|
|
|
ret = ftpc_xfrinit(session);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftpc_xfrmode(session, xfrmode);
|
|
|
|
|
2011-06-02 03:14:12 +02:00
|
|
|
/* The REST command sets the start position in the file. Some servers
|
|
|
|
* allow REST immediately before STOR for binary files.
|
|
|
|
*/
|
|
|
|
|
2022-01-18 16:40:32 +01:00
|
|
|
if (session->offset > 0)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2022-07-17 08:05:23 +02:00
|
|
|
ret = ftpc_cmd(session, "REST %" PRIdOFF, session->offset);
|
2022-01-18 16:40:32 +01:00
|
|
|
session->size = session->offset;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2011-06-02 03:14:12 +02:00
|
|
|
/* Send the file using STOR, STOU, or APPE:
|
|
|
|
*
|
|
|
|
* - STOR request asks the server to receive the contents of a file from
|
|
|
|
* the data connection already established by the client.
|
|
|
|
* - APPE is just like STOR except that, if the file already exists, the
|
|
|
|
* server appends the client's data to the file.
|
|
|
|
* - STOU is just like STOR except that it asks the server to create a
|
|
|
|
* file under a new pathname selected by the server. If the server
|
|
|
|
* accepts STOU, it provides the pathname in a human-readable format in
|
|
|
|
* the text of its response.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (how)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
case FTPC_PUT_UNIQUE:
|
2011-06-02 03:14:12 +02:00
|
|
|
{
|
|
|
|
ret = ftpc_cmd(session, "STOU %s", path);
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-02 03:14:12 +02:00
|
|
|
/* Check for "502 Command not implemented" */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-02 03:14:12 +02:00
|
|
|
if (session->code == 502)
|
|
|
|
{
|
|
|
|
/* The host does not support the STOU command */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-02 03:14:12 +02:00
|
|
|
FTPC_CLR_STOU(session);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the remote filename from the response */
|
2011-06-13 17:43:56 +02:00
|
|
|
|
2016-06-11 22:13:39 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2011-06-02 03:14:12 +02:00
|
|
|
str = strstr(session->reply, " for ");
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
str += 5;
|
|
|
|
len = strlen(str);
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
if (*str == '\'')
|
|
|
|
{
|
2020-11-24 23:29:18 +01:00
|
|
|
rname = strndup(str + 1, len - 3);
|
2011-06-02 03:14:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-24 23:29:18 +01:00
|
|
|
rname = strndup(str, len - 1);
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Unique filename is: %s\n", rname);
|
2011-06-02 03:14:12 +02:00
|
|
|
}
|
2020-11-24 23:29:18 +01:00
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
free(rname);
|
2011-06-02 03:14:12 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-13 17:43:56 +02:00
|
|
|
#endif
|
2011-06-02 03:14:12 +02:00
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FTPC_PUT_APPEND:
|
|
|
|
ret = ftpc_cmd(session, "APPE %s", path);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FTPC_PUT_NORMAL:
|
|
|
|
default:
|
|
|
|
ret = ftpc_cmd(session, "STOR %s", path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-02 03:14:12 +02:00
|
|
|
/* If the server is willing to create a new file under that name, or
|
|
|
|
* replace an existing file under that name, it responds with a mark
|
|
|
|
* using code 150:
|
|
|
|
*
|
|
|
|
* - "150 File status okay; about to open data connection"
|
|
|
|
*
|
|
|
|
* It then attempts to read the contents of the file from the data
|
|
|
|
* connection, and closes the data connection. Finally it accepts the STOR
|
|
|
|
* with:
|
|
|
|
*
|
|
|
|
* - "226 Closing data connection" if the entire file was successfully
|
|
|
|
* received and stored
|
|
|
|
*
|
|
|
|
* Or rejects the STOR 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",
|
|
|
|
* "452 - Requested action not taken", or "552 Requested file action
|
|
|
|
* aborted" if the server had trouble saving the file to disk.
|
|
|
|
*
|
|
|
|
* The server may reject the STOR request with:
|
|
|
|
*
|
|
|
|
* - "450 Requested file action not taken", "452 - Requested action not
|
|
|
|
* taken" or "553 Requested action not taken" without first responding
|
2014-04-14 00:24:28 +02:00
|
|
|
* with a mark.
|
2011-06-02 03:14:12 +02:00
|
|
|
*/
|
2011-06-01 21:15:14 +02:00
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!FTPC_IS_PASSIVE(session))
|
2011-06-01 21:15:14 +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
|
|
|
return ERROR;
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* Then perform the data transfer */
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
if (xfrmode == FTPC_XFRMODE_ASCII)
|
|
|
|
{
|
2022-01-18 16:40:32 +01:00
|
|
|
ret = ftpc_sendtext(session, stream);
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-18 16:40:32 +01:00
|
|
|
ret = ftpc_sendbinary(session, stream);
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ftpc_sockflush(&session->data);
|
|
|
|
ftpc_sockclose(&session->data);
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
fptc_getreply(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_putfile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put a file on the remote host.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftp_putfile(SESSION handle, const char *lname, const char *rname,
|
|
|
|
uint8_t how, uint8_t xfrmode)
|
|
|
|
{
|
|
|
|
FAR struct ftpc_session_s *session = (FAR struct ftpc_session_s *)handle;
|
2011-06-03 22:27:30 +02:00
|
|
|
FAR char *abslpath;
|
2011-06-01 21:15:14 +02:00
|
|
|
struct stat statbuf;
|
|
|
|
FILE *finstream;
|
|
|
|
int ret;
|
|
|
|
|
2011-06-04 00:53:36 +02:00
|
|
|
/* Don't call this with a NULL local file name */
|
|
|
|
|
|
|
|
DEBUGASSERT(lname);
|
|
|
|
|
|
|
|
/* If the remote name is not specified, then it is assumed to the same as
|
|
|
|
* the local file name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!rname)
|
|
|
|
{
|
|
|
|
rname = lname;
|
|
|
|
}
|
|
|
|
|
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:33:26 +01:00
|
|
|
nwarn("WARNING: ftpc_abslpath(%s) failed: %d\n", lname, errno);
|
2011-06-03 22:27:30 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
/* Make sure that the local file exists */
|
|
|
|
|
2011-06-03 22:27:30 +02:00
|
|
|
ret = stat(abslpath, &statbuf);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2020-11-24 23:33:26 +01:00
|
|
|
nwarn("WARNING: stat(%s) failed: %d\n", abslpath, errno);
|
2011-06-13 17:43:56 +02:00
|
|
|
goto errout_with_abspath;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the local name does not refer to 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
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the local file for reading */
|
|
|
|
|
2011-06-03 22:27:30 +02:00
|
|
|
finstream = fopen(abslpath, "r");
|
2011-06-05 18:46:27 +02:00
|
|
|
if (!finstream)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nwarn("WARNING: 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
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we resuming a transfer? */
|
|
|
|
|
|
|
|
session->offset = 0;
|
|
|
|
if (how == FTPC_PUT_RESUME)
|
|
|
|
{
|
|
|
|
/* Yes... Get the size of the file. This will only work if the
|
|
|
|
* server supports the SIZE command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
session->offset = ftpc_filesize(session, rname);
|
|
|
|
if (session->offset == (off_t)ERROR)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nwarn("WARNING: Failed to get size of remote file: %s\n", rname);
|
2011-06-03 22:27:30 +02:00
|
|
|
goto errout_with_instream;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Seek to the offset in the file corresponding to the size
|
|
|
|
* that we have already sent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = fseek(finstream, session->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_instream;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-13 17:43:56 +02:00
|
|
|
/* Send the file */
|
2011-06-03 22:27:30 +02:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
ret = ftpc_sendfile(session, rname, finstream, how, xfrmode);
|
2011-06-03 22:27:30 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
fclose(finstream);
|
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_instream:
|
2011-06-01 21:15:14 +02:00
|
|
|
fclose(finstream);
|
2011-06-03 22:27:30 +02:00
|
|
|
errout_with_abspath:
|
|
|
|
free(abslpath);
|
|
|
|
errout:
|
|
|
|
return ERROR;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|