643 lines
18 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2023-07-02 19:51:09 +02:00
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
2015-04-27 22:25:09 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2015-04-27 22:25:09 +02:00
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
2022-11-16 20:14:03 +01:00
* SPDX-License-Identifier: curl
*
2015-04-27 22:25:09 +02:00
***************************************************************************/
#include "curl_setup.h"
#ifndef CURL_DISABLE_FILE
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
2024-07-09 14:46:46 +02:00
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
2015-04-27 22:25:09 +02:00
#include "strtoofft.h"
#include "urldata.h"
#include <curl/curl.h>
#include "progress.h"
#include "sendf.h"
#include "escape.h"
#include "file.h"
#include "speedcheck.h"
#include "getinfo.h"
2024-07-09 14:46:46 +02:00
#include "multiif.h"
2015-04-27 22:25:09 +02:00
#include "transfer.h"
#include "url.h"
#include "parsedate.h" /* for the week day and month names */
#include "warnless.h"
2018-08-09 18:06:22 +02:00
#include "curl_range.h"
2016-09-11 17:22:32 +02:00
/* The last 3 #include files should be in this order */
2015-11-17 17:22:37 +01:00
#include "curl_printf.h"
#include "curl_memory.h"
2015-04-27 22:25:09 +02:00
#include "memdebug.h"
2024-04-14 22:45:38 +02:00
#if defined(_WIN32) || defined(MSDOS) || defined(__EMX__)
2015-04-27 22:25:09 +02:00
#define DOS_FILESYSTEM 1
2022-11-16 20:14:03 +01:00
#elif defined(__amigaos4__)
#define AMIGA_FILESYSTEM 1
2015-04-27 22:25:09 +02:00
#endif
#ifdef OPEN_NEEDS_ARG3
# define open_readonly(p,f) open((p),(f),(0))
#else
# define open_readonly(p,f) open((p),(f))
#endif
/*
* Forward declarations.
*/
2021-09-14 00:13:48 +02:00
static CURLcode file_do(struct Curl_easy *data, bool *done);
static CURLcode file_done(struct Curl_easy *data,
2015-04-27 22:25:09 +02:00
CURLcode status, bool premature);
2021-09-14 00:13:48 +02:00
static CURLcode file_connect(struct Curl_easy *data, bool *done);
static CURLcode file_disconnect(struct Curl_easy *data,
struct connectdata *conn,
2015-04-27 22:25:09 +02:00
bool dead_connection);
2021-09-14 00:13:48 +02:00
static CURLcode file_setup_connection(struct Curl_easy *data,
struct connectdata *conn);
2015-04-27 22:25:09 +02:00
/*
* FILE scheme handler.
*/
const struct Curl_handler Curl_handler_file = {
2024-07-09 14:46:46 +02:00
"file", /* scheme */
2015-04-27 22:25:09 +02:00
file_setup_connection, /* setup_connection */
file_do, /* do_it */
file_done, /* done */
ZERO_NULL, /* do_more */
file_connect, /* connect_it */
ZERO_NULL, /* connecting */
ZERO_NULL, /* doing */
ZERO_NULL, /* proto_getsock */
ZERO_NULL, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
file_disconnect, /* disconnect */
2024-04-14 22:45:38 +02:00
ZERO_NULL, /* write_resp */
2024-07-09 14:46:46 +02:00
ZERO_NULL, /* write_resp_hd */
2018-01-26 17:06:56 +01:00
ZERO_NULL, /* connection_check */
2021-09-14 00:13:48 +02:00
ZERO_NULL, /* attach connection */
2015-04-27 22:25:09 +02:00
0, /* defport */
CURLPROTO_FILE, /* protocol */
2021-09-14 00:13:48 +02:00
CURLPROTO_FILE, /* family */
2015-04-27 22:25:09 +02:00
PROTOPT_NONETWORK | PROTOPT_NOURLQUERY /* flags */
};
2021-09-14 00:13:48 +02:00
static CURLcode file_setup_connection(struct Curl_easy *data,
struct connectdata *conn)
2015-04-27 22:25:09 +02:00
{
2021-09-14 00:13:48 +02:00
(void)conn;
2015-04-27 22:25:09 +02:00
/* allocate the FILE specific struct */
2021-09-14 00:13:48 +02:00
data->req.p.file = calloc(1, sizeof(struct FILEPROTO));
if(!data->req.p.file)
2015-04-27 22:25:09 +02:00
return CURLE_OUT_OF_MEMORY;
return CURLE_OK;
}
/*
* file_connect() gets called from Curl_protocol_connect() to allow us to
2024-11-11 15:18:55 +01:00
* do protocol-specific actions at connect-time. We emulate a
2015-04-27 22:25:09 +02:00
* connect-then-transfer protocol and "connect" to the file here
*/
2021-09-14 00:13:48 +02:00
static CURLcode file_connect(struct Curl_easy *data, bool *done)
2015-04-27 22:25:09 +02:00
{
char *real_path;
2021-09-14 00:13:48 +02:00
struct FILEPROTO *file = data->req.p.file;
2015-04-27 22:25:09 +02:00
int fd;
#ifdef DOS_FILESYSTEM
2017-04-14 19:02:05 +02:00
size_t i;
2015-04-27 22:25:09 +02:00
char *actual_path;
#endif
2017-04-14 19:02:05 +02:00
size_t real_path_len;
2023-05-23 16:38:00 +02:00
CURLcode result;
if(file->path) {
/* already connected.
* the handler->connect_it() is normally only called once, but
* FILE does a special check on setting up the connection which
* calls this explicitly. */
*done = TRUE;
return CURLE_OK;
}
2015-04-27 22:25:09 +02:00
2023-05-23 16:38:00 +02:00
result = Curl_urldecode(data->state.up.path, 0, &real_path,
&real_path_len, REJECT_ZERO);
2017-04-14 19:02:05 +02:00
if(result)
return result;
2015-04-27 22:25:09 +02:00
#ifdef DOS_FILESYSTEM
2024-11-11 15:18:55 +01:00
/* If the first character is a slash, and there is
2015-04-27 22:25:09 +02:00
something that looks like a drive at the beginning of
2024-11-11 15:18:55 +01:00
the path, skip the slash. If we remove the initial
2015-04-27 22:25:09 +02:00
slash in all cases, paths without drive letters end up
2024-11-11 15:18:55 +01:00
relative to the current directory which is not how
2015-04-27 22:25:09 +02:00
browsers work.
Some browsers accept | instead of : as the drive letter
separator, so we do too.
On other platforms, we need the slash to indicate an
2024-11-11 15:18:55 +01:00
absolute pathname. On Windows, absolute paths start
2015-04-27 22:25:09 +02:00
with a drive letter.
*/
actual_path = real_path;
if((actual_path[0] == '/') &&
actual_path[1] &&
(actual_path[2] == ':' || actual_path[2] == '|')) {
actual_path[2] = ':';
actual_path++;
2015-11-17 17:22:37 +01:00
real_path_len--;
2015-04-27 22:25:09 +02:00
}
/* change path separators from '/' to '\\' for DOS, Windows and OS/2 */
2018-01-26 17:06:56 +01:00
for(i = 0; i < real_path_len; ++i)
2015-04-27 22:25:09 +02:00
if(actual_path[i] == '/')
actual_path[i] = '\\';
2016-09-11 17:22:32 +02:00
else if(!actual_path[i]) { /* binary zero */
Curl_safefree(real_path);
2015-11-17 17:22:37 +01:00
return CURLE_URL_MALFORMAT;
2016-09-11 17:22:32 +02:00
}
2015-04-27 22:25:09 +02:00
fd = open_readonly(actual_path, O_RDONLY|O_BINARY);
file->path = actual_path;
#else
2016-09-11 17:22:32 +02:00
if(memchr(real_path, 0, real_path_len)) {
2015-11-17 17:22:37 +01:00
/* binary zeroes indicate foul play */
2016-09-11 17:22:32 +02:00
Curl_safefree(real_path);
2015-11-17 17:22:37 +01:00
return CURLE_URL_MALFORMAT;
2016-09-11 17:22:32 +02:00
}
2015-11-17 17:22:37 +01:00
2022-11-16 20:14:03 +01:00
#ifdef AMIGA_FILESYSTEM
/*
* A leading slash in an AmigaDOS path denotes the parent
* directory, and hence we block this as it is relative.
* Absolute paths start with 'volumename:', so we check for
2024-11-11 15:18:55 +01:00
* this first. Failing that, we treat the path as a real Unix
2022-11-16 20:14:03 +01:00
* path, but only if the application was compiled with -lunix.
*/
fd = -1;
file->path = real_path;
if(real_path[0] == '/') {
extern int __unix_path_semantics;
if(strchr(real_path + 1, ':')) {
/* Amiga absolute path */
fd = open_readonly(real_path + 1, O_RDONLY);
file->path++;
}
else if(__unix_path_semantics) {
/* -lunix fallback */
fd = open_readonly(real_path, O_RDONLY);
}
}
#else
2015-04-27 22:25:09 +02:00
fd = open_readonly(real_path, O_RDONLY);
file->path = real_path;
2022-11-16 20:14:03 +01:00
#endif
2015-04-27 22:25:09 +02:00
#endif
2023-05-23 16:38:00 +02:00
Curl_safefree(file->freepath);
2015-04-27 22:25:09 +02:00
file->freepath = real_path; /* free this when done */
file->fd = fd;
2023-07-02 19:51:09 +02:00
if(!data->state.upload && (fd == -1)) {
2018-11-29 20:27:00 +01:00
failf(data, "Couldn't open file %s", data->state.up.path);
2021-09-14 00:13:48 +02:00
file_done(data, CURLE_FILE_COULDNT_READ_FILE, FALSE);
2015-04-27 22:25:09 +02:00
return CURLE_FILE_COULDNT_READ_FILE;
}
*done = TRUE;
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
static CURLcode file_done(struct Curl_easy *data,
CURLcode status, bool premature)
2015-04-27 22:25:09 +02:00
{
2021-09-14 00:13:48 +02:00
struct FILEPROTO *file = data->req.p.file;
2015-04-27 22:25:09 +02:00
(void)status; /* not used */
(void)premature; /* not used */
if(file) {
Curl_safefree(file->freepath);
file->path = NULL;
if(file->fd != -1)
close(file->fd);
file->fd = -1;
}
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
static CURLcode file_disconnect(struct Curl_easy *data,
struct connectdata *conn,
2015-04-27 22:25:09 +02:00
bool dead_connection)
{
(void)dead_connection; /* not used */
2021-09-14 00:13:48 +02:00
(void)conn;
2022-11-16 20:14:03 +01:00
return file_done(data, CURLE_OK, FALSE);
2015-04-27 22:25:09 +02:00
}
#ifdef DOS_FILESYSTEM
#define DIRSEP '\\'
#else
#define DIRSEP '/'
#endif
2021-09-14 00:13:48 +02:00
static CURLcode file_upload(struct Curl_easy *data)
2015-04-27 22:25:09 +02:00
{
2021-09-14 00:13:48 +02:00
struct FILEPROTO *file = data->req.p.file;
2015-04-27 22:25:09 +02:00
const char *dir = strchr(file->path, DIRSEP);
int fd;
int mode;
2015-11-17 17:22:37 +01:00
CURLcode result = CURLE_OK;
2024-07-09 14:46:46 +02:00
char *xfer_ulbuf;
size_t xfer_ulblen;
2015-04-27 22:25:09 +02:00
curl_off_t bytecount = 0;
struct_stat file_stat;
2024-04-14 22:45:38 +02:00
const char *sendbuf;
2024-07-09 14:46:46 +02:00
bool eos = FALSE;
2015-04-27 22:25:09 +02:00
/*
2024-11-11 15:18:55 +01:00
* Since FILE: does not do the full init, we need to provide some extra
2015-04-27 22:25:09 +02:00
* assignments here.
*/
if(!dir)
return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
if(!dir[1])
return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
#ifdef O_BINARY
#define MODE_DEFAULT O_WRONLY|O_CREAT|O_BINARY
#else
#define MODE_DEFAULT O_WRONLY|O_CREAT
#endif
if(data->state.resume_from)
mode = MODE_DEFAULT|O_APPEND;
else
mode = MODE_DEFAULT|O_TRUNC;
2021-09-14 00:13:48 +02:00
fd = open(file->path, mode, data->set.new_file_perms);
2015-04-27 22:25:09 +02:00
if(fd < 0) {
2024-11-11 15:18:55 +01:00
failf(data, "cannot open %s for writing", file->path);
2015-04-27 22:25:09 +02:00
return CURLE_WRITE_ERROR;
}
if(-1 != data->state.infilesize)
/* known size of data to "upload" */
Curl_pgrsSetUploadSize(data, data->state.infilesize);
/* treat the negative resume offset value as the case of "-" */
if(data->state.resume_from < 0) {
if(fstat(fd, &file_stat)) {
close(fd);
2024-11-11 15:18:55 +01:00
failf(data, "cannot get the size of %s", file->path);
2015-04-27 22:25:09 +02:00
return CURLE_WRITE_ERROR;
}
2017-07-20 19:35:53 +02:00
data->state.resume_from = (curl_off_t)file_stat.st_size;
2015-04-27 22:25:09 +02:00
}
2024-07-09 14:46:46 +02:00
result = Curl_multi_xfer_ulbuf_borrow(data, &xfer_ulbuf, &xfer_ulblen);
if(result)
goto out;
while(!result && !eos) {
2018-10-28 12:09:07 +01:00
size_t nread;
2023-05-23 16:38:00 +02:00
ssize_t nwrite;
2018-10-28 12:09:07 +01:00
size_t readcount;
2024-07-09 14:46:46 +02:00
result = Curl_client_read(data, xfer_ulbuf, xfer_ulblen, &readcount, &eos);
2015-11-17 17:22:37 +01:00
if(result)
2015-04-27 22:25:09 +02:00
break;
2019-11-11 23:01:05 +01:00
if(!readcount)
2015-04-27 22:25:09 +02:00
break;
2018-10-28 12:09:07 +01:00
nread = readcount;
2015-04-27 22:25:09 +02:00
2023-05-23 16:38:00 +02:00
/* skip bytes before resume point */
2015-04-27 22:25:09 +02:00
if(data->state.resume_from) {
2016-09-11 17:22:32 +02:00
if((curl_off_t)nread <= data->state.resume_from) {
2015-04-27 22:25:09 +02:00
data->state.resume_from -= nread;
nread = 0;
2024-07-09 14:46:46 +02:00
sendbuf = xfer_ulbuf;
2015-04-27 22:25:09 +02:00
}
else {
2024-07-09 14:46:46 +02:00
sendbuf = xfer_ulbuf + data->state.resume_from;
2015-04-27 22:25:09 +02:00
nread -= (size_t)data->state.resume_from;
data->state.resume_from = 0;
}
}
else
2024-07-09 14:46:46 +02:00
sendbuf = xfer_ulbuf;
2015-04-27 22:25:09 +02:00
/* write the data to the target */
2024-04-14 22:45:38 +02:00
nwrite = write(fd, sendbuf, nread);
2023-05-23 16:38:00 +02:00
if((size_t)nwrite != nread) {
2015-11-17 17:22:37 +01:00
result = CURLE_SEND_ERROR;
2015-04-27 22:25:09 +02:00
break;
}
bytecount += nread;
Curl_pgrsSetUploadCounter(data, bytecount);
2021-09-14 00:13:48 +02:00
if(Curl_pgrsUpdate(data))
2015-11-17 17:22:37 +01:00
result = CURLE_ABORTED_BY_CALLBACK;
2015-04-27 22:25:09 +02:00
else
2018-04-23 21:13:27 +02:00
result = Curl_speedcheck(data, Curl_now());
2015-04-27 22:25:09 +02:00
}
2021-09-14 00:13:48 +02:00
if(!result && Curl_pgrsUpdate(data))
2015-11-17 17:22:37 +01:00
result = CURLE_ABORTED_BY_CALLBACK;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
out:
2015-04-27 22:25:09 +02:00
close(fd);
2024-07-09 14:46:46 +02:00
Curl_multi_xfer_ulbuf_release(data, xfer_ulbuf);
2015-04-27 22:25:09 +02:00
2015-11-17 17:22:37 +01:00
return result;
2015-04-27 22:25:09 +02:00
}
/*
* file_do() is the protocol-specific function for the do-phase, separated
* from the connect-phase above. Other protocols merely setup the transfer in
* the do-phase, to have it done in the main transfer loop but since some
2024-11-11 15:18:55 +01:00
* platforms we support do not allow select()ing etc on file handles (as
2015-04-27 22:25:09 +02:00
* opposed to sockets) we instead perform the whole do-operation in this
* function.
*/
2021-09-14 00:13:48 +02:00
static CURLcode file_do(struct Curl_easy *data, bool *done)
2015-04-27 22:25:09 +02:00
{
2024-11-11 15:18:55 +01:00
/* This implementation ignores the hostname in conformance with
2015-04-27 22:25:09 +02:00
RFC 1738. Only local files (reachable via the standard file system)
are supported. This means that files on remotely mounted directories
(via NFS, Samba, NT sharing) can be accessed through a file:// URL
*/
2015-11-17 17:22:37 +01:00
CURLcode result = CURLE_OK;
2015-04-27 22:25:09 +02:00
struct_stat statbuf; /* struct_stat instead of struct stat just to allow the
Windows version to have a different struct without
having to redefine the simple word 'stat' */
2021-09-14 00:13:48 +02:00
curl_off_t expected_size = -1;
2016-09-11 17:22:32 +02:00
bool size_known;
2018-01-26 17:06:56 +01:00
bool fstated = FALSE;
2015-04-27 22:25:09 +02:00
int fd;
struct FILEPROTO *file;
2024-07-09 14:46:46 +02:00
char *xfer_buf;
size_t xfer_blen;
2015-04-27 22:25:09 +02:00
*done = TRUE; /* unconditionally */
2023-07-02 19:51:09 +02:00
if(data->state.upload)
2021-09-14 00:13:48 +02:00
return file_upload(data);
2015-04-27 22:25:09 +02:00
2021-09-14 00:13:48 +02:00
file = data->req.p.file;
2015-04-27 22:25:09 +02:00
/* get the fd from the connection phase */
fd = file->fd;
/* VMS: This only works reliable for STREAMLF files */
if(-1 != fstat(fd, &statbuf)) {
2021-09-14 00:13:48 +02:00
if(!S_ISDIR(statbuf.st_mode))
expected_size = statbuf.st_size;
2015-04-27 22:25:09 +02:00
/* and store the modification time */
2018-08-09 18:06:22 +02:00
data->info.filetime = statbuf.st_mtime;
2015-04-27 22:25:09 +02:00
fstated = TRUE;
}
2024-07-09 14:46:46 +02:00
if(fstated && !data->state.range && data->set.timecondition &&
!Curl_meets_timecondition(data, data->info.filetime))
return CURLE_OK;
2015-04-27 22:25:09 +02:00
2018-11-29 20:27:00 +01:00
if(fstated) {
2016-09-11 17:22:32 +02:00
time_t filetime;
struct tm buffer;
const struct tm *tm = &buffer;
2017-07-20 19:35:53 +02:00
char header[80];
2021-09-14 00:13:48 +02:00
int headerlen;
2024-11-11 15:18:55 +01:00
static const char accept_ranges[]= { "Accept-ranges: bytes\r\n" };
2021-09-14 00:13:48 +02:00
if(expected_size >= 0) {
2024-11-11 15:18:55 +01:00
headerlen =
msnprintf(header, sizeof(header), "Content-Length: %" FMT_OFF_T "\r\n",
expected_size);
2021-09-14 00:13:48 +02:00
result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
if(result)
return result;
result = Curl_client_write(data, CLIENTWRITE_HEADER,
2024-11-11 15:18:55 +01:00
accept_ranges, sizeof(accept_ranges) - 1);
2021-09-14 00:13:48 +02:00
if(result != CURLE_OK)
return result;
}
2015-04-27 22:25:09 +02:00
2016-09-11 17:22:32 +02:00
filetime = (time_t)statbuf.st_mtime;
result = Curl_gmtime(filetime, &buffer);
if(result)
return result;
/* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
2024-11-11 15:18:55 +01:00
headerlen =
msnprintf(header, sizeof(header),
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
tm->tm_mday,
Curl_month[tm->tm_mon],
tm->tm_year + 1900,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
2021-09-14 00:13:48 +02:00
result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
2024-11-11 15:18:55 +01:00
if(!result)
/* end of headers */
result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
2018-11-29 20:27:00 +01:00
if(result)
return result;
/* set the file size to make it available post transfer */
Curl_pgrsSetDownloadSize(data, expected_size);
2023-05-23 16:38:00 +02:00
if(data->req.no_body)
2024-11-11 15:18:55 +01:00
return CURLE_OK;
2015-04-27 22:25:09 +02:00
}
/* Check whether file range has been specified */
2021-09-14 00:13:48 +02:00
result = Curl_range(data);
2018-08-09 18:06:22 +02:00
if(result)
return result;
2015-04-27 22:25:09 +02:00
/* Adjust the start offset in case we want to get the N last bytes
2018-10-28 12:09:07 +01:00
* of the stream if the filesize could be determined */
2015-04-27 22:25:09 +02:00
if(data->state.resume_from < 0) {
if(!fstated) {
2024-11-11 15:18:55 +01:00
failf(data, "cannot get the size of file.");
2015-04-27 22:25:09 +02:00
return CURLE_READ_ERROR;
}
2017-07-20 19:35:53 +02:00
data->state.resume_from += (curl_off_t)statbuf.st_size;
2015-04-27 22:25:09 +02:00
}
2021-09-14 00:13:48 +02:00
if(data->state.resume_from > 0) {
/* We check explicitly if we have a start offset, because
2024-11-11 15:18:55 +01:00
* expected_size may be -1 if we do not know how large the file is,
2021-09-14 00:13:48 +02:00
* in which case we should not adjust it. */
if(data->state.resume_from <= expected_size)
expected_size -= data->state.resume_from;
else {
failf(data, "failed to resume file:// transfer");
return CURLE_BAD_DOWNLOAD_RESUME;
}
2015-04-27 22:25:09 +02:00
}
/* A high water mark has been specified so we obey... */
if(data->req.maxdownload > 0)
expected_size = data->req.maxdownload;
2021-09-14 00:13:48 +02:00
if(!fstated || (expected_size <= 0))
2016-09-11 17:22:32 +02:00
size_known = FALSE;
else
size_known = TRUE;
2015-04-27 22:25:09 +02:00
/* The following is a shortcut implementation of file reading
this is both more efficient than the former call to download() and
it avoids problems with select() and recv() on file descriptors
in Winsock */
2021-09-14 00:13:48 +02:00
if(size_known)
2015-04-27 22:25:09 +02:00
Curl_pgrsSetDownloadSize(data, expected_size);
if(data->state.resume_from) {
2024-07-09 14:46:46 +02:00
if(!S_ISDIR(statbuf.st_mode)) {
if(data->state.resume_from !=
lseek(fd, data->state.resume_from, SEEK_SET))
return CURLE_BAD_DOWNLOAD_RESUME;
}
else {
2015-04-27 22:25:09 +02:00
return CURLE_BAD_DOWNLOAD_RESUME;
2024-07-09 14:46:46 +02:00
}
2015-04-27 22:25:09 +02:00
}
2024-07-09 14:46:46 +02:00
result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
if(result)
goto out;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
if(!S_ISDIR(statbuf.st_mode)) {
while(!result) {
ssize_t nread;
2024-11-11 15:18:55 +01:00
/* Do not fill a whole buffer if we want less than all data */
2024-07-09 14:46:46 +02:00
size_t bytestoread;
2016-09-11 17:22:32 +02:00
2024-07-09 14:46:46 +02:00
if(size_known) {
bytestoread = (expected_size < (curl_off_t)(xfer_blen-1)) ?
curlx_sotouz(expected_size) : (xfer_blen-1);
}
else
bytestoread = xfer_blen-1;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
nread = read(fd, xfer_buf, bytestoread);
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
if(nread > 0)
xfer_buf[nread] = 0;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
if(nread <= 0 || (size_known && (expected_size == 0)))
break;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
if(size_known)
expected_size -= nread;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
result = Curl_client_write(data, CLIENTWRITE_BODY, xfer_buf, nread);
if(result)
goto out;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
if(Curl_pgrsUpdate(data))
result = CURLE_ABORTED_BY_CALLBACK;
else
result = Curl_speedcheck(data, Curl_now());
if(result)
goto out;
}
2015-04-27 22:25:09 +02:00
}
2024-07-09 14:46:46 +02:00
else {
#ifdef HAVE_OPENDIR
DIR *dir = opendir(file->path);
struct dirent *entry;
if(!dir) {
result = CURLE_READ_ERROR;
goto out;
}
else {
while((entry = readdir(dir))) {
if(entry->d_name[0] != '.') {
result = Curl_client_write(data, CLIENTWRITE_BODY,
entry->d_name, strlen(entry->d_name));
if(result)
break;
result = Curl_client_write(data, CLIENTWRITE_BODY, "\n", 1);
if(result)
break;
}
}
closedir(dir);
}
#else
failf(data, "Directory listing not yet implemented on this platform.");
result = CURLE_READ_ERROR;
#endif
}
2021-09-14 00:13:48 +02:00
if(Curl_pgrsUpdate(data))
2015-11-17 17:22:37 +01:00
result = CURLE_ABORTED_BY_CALLBACK;
2015-04-27 22:25:09 +02:00
2024-07-09 14:46:46 +02:00
out:
Curl_multi_xfer_buf_release(data, xfer_buf);
2015-11-17 17:22:37 +01:00
return result;
2015-04-27 22:25:09 +02:00
}
#endif