Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/file.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
#include "urldata.h"
26
#include "file.h"
27
28
#ifndef CURL_DISABLE_FILE
29
30
#ifdef HAVE_NETINET_IN_H
31
#include <netinet/in.h>
32
#endif
33
#ifdef HAVE_NETDB_H
34
#include <netdb.h>
35
#endif
36
#ifdef HAVE_ARPA_INET_H
37
#include <arpa/inet.h>
38
#endif
39
#ifdef HAVE_NET_IF_H
40
#include <net/if.h>
41
#endif
42
#ifdef HAVE_SYS_IOCTL_H
43
#include <sys/ioctl.h>
44
#endif
45
46
#ifdef HAVE_SYS_PARAM_H
47
#include <sys/param.h>
48
#endif
49
50
#ifdef HAVE_DIRENT_H
51
#include <dirent.h>
52
#endif
53
54
#include "progress.h"
55
#include "connect.h"
56
#include "sendf.h"
57
#include "curl_trc.h"
58
#include "escape.h"
59
#include "multiif.h"
60
#include "transfer.h"
61
#include "url.h"
62
#include "parsedate.h" /* for the week day and month names */
63
#include "curlx/win32-fopen.h"
64
#include "curl_range.h"
65
66
#if defined(_WIN32) || defined(MSDOS)
67
#define DOS_FILESYSTEM 1
68
#elif defined(__amigaos4__)
69
#define AMIGA_FILESYSTEM 1
70
#endif
71
72
/* meta key for storing protocol meta at easy handle */
73
0
#define CURL_META_FILE_EASY   "meta:proto:file:easy"
74
75
struct FILEPROTO {
76
  char *path; /* the path we operate on */
77
  char *freepath; /* pointer to the allocated block we must free, this might
78
                     differ from the 'path' pointer */
79
  int fd;     /* open file descriptor to read from! */
80
  bool is_dir;
81
};
82
83
static void file_cleanup(struct FILEPROTO *file)
84
0
{
85
0
  curlx_safefree(file->freepath);
86
0
  file->path = NULL;
87
0
  file->is_dir = FALSE;
88
0
  if(file->fd != -1) {
89
0
    curlx_close(file->fd);
90
0
    file->fd = -1;
91
0
  }
92
0
}
93
94
static void file_easy_dtor(const void *key, size_t klen, void *entry)
95
0
{
96
0
  struct FILEPROTO *file = entry;
97
0
  (void)key;
98
0
  (void)klen;
99
0
  file_cleanup(file);
100
0
  curlx_free(file);
101
0
}
102
103
static CURLcode file_setup_connection(struct Curl_easy *data,
104
                                      struct connectdata *conn)
105
0
{
106
0
  struct FILEPROTO *filep;
107
0
  (void)conn;
108
  /* allocate the FILE specific struct */
109
0
  filep = curlx_calloc(1, sizeof(*filep));
110
0
  if(filep)
111
0
    filep->fd = -1;
112
0
  if(!filep ||
113
0
     Curl_meta_set(data, CURL_META_FILE_EASY, filep, file_easy_dtor))
114
0
    return CURLE_OUT_OF_MEMORY;
115
116
0
  return CURLE_OK;
117
0
}
118
119
static CURLcode file_done(struct Curl_easy *data,
120
                          CURLcode status, bool premature)
121
0
{
122
0
  struct FILEPROTO *file = Curl_meta_get(data, CURL_META_FILE_EASY);
123
0
  (void)status;
124
0
  (void)premature;
125
126
0
  if(file)
127
0
    file_cleanup(file);
128
129
0
  return CURLE_OK;
130
0
}
131
132
static int file_stat(const char *path, curlx_struct_stat *statbuf)
133
0
{
134
#ifdef _WIN32
135
  int result = curlx_stat(path, statbuf);
136
137
  if(result) {
138
    size_t pathlen = strlen(path);
139
140
    /* MSVCRT's narrow stat() rejects trailing directory separators. */
141
    if((pathlen > 3) &&
142
       ((path[pathlen - 1] == '\\') || (path[pathlen - 1] == '/'))) {
143
      char *trimmed = curlx_strdup(path);
144
145
      if(trimmed) {
146
        do {
147
          trimmed[--pathlen] = '\0';
148
        } while((pathlen > 3) &&
149
                ((trimmed[pathlen - 1] == '\\') ||
150
                 (trimmed[pathlen - 1] == '/')));
151
152
        result = curlx_stat(trimmed, statbuf);
153
        curlx_free(trimmed);
154
      }
155
    }
156
  }
157
158
  return result;
159
#else
160
0
  return curlx_stat(path, statbuf);
161
0
#endif
162
0
}
163
164
/*
165
 * file_connect() gets called from Curl_protocol_connect() to allow us to
166
 * do protocol-specific actions at connect-time. We emulate a
167
 * connect-then-transfer protocol and "connect" to the file here
168
 */
169
static CURLcode file_connect(struct Curl_easy *data, bool *done)
170
0
{
171
0
  char *real_path;
172
0
  struct FILEPROTO *file = Curl_meta_get(data, CURL_META_FILE_EASY);
173
#ifdef _WIN32
174
  curlx_struct_stat statbuf;
175
#endif
176
0
  int fd;
177
#ifdef DOS_FILESYSTEM
178
  size_t i;
179
  char *actual_path;
180
#endif
181
0
  size_t real_path_len;
182
0
  CURLcode result;
183
184
0
  if(!file)
185
0
    return CURLE_FAILED_INIT;
186
187
0
  if(file->path) {
188
    /* already connected.
189
     * the handler->connect_it() is normally only called once, but
190
     * FILE does a special check on setting up the connection which
191
     * calls this explicitly. */
192
0
    *done = TRUE;
193
0
    return CURLE_OK;
194
0
  }
195
196
0
  result = Curl_urldecode(data->state.up.path, 0, &real_path,
197
0
                          &real_path_len, REJECT_ZERO);
198
0
  if(result)
199
0
    return result;
200
201
#ifdef DOS_FILESYSTEM
202
  /* If the first character is a slash, and there is
203
     something that looks like a drive at the beginning of
204
     the path, skip the slash. If we remove the initial
205
     slash in all cases, paths without drive letters end up
206
     relative to the current directory which is not how
207
     browsers work.
208
209
     Some browsers accept | instead of : as the drive letter
210
     separator, so we do too.
211
212
     On other platforms, we need the slash to indicate an
213
     absolute pathname. On Windows, absolute paths start
214
     with a drive letter. */
215
  actual_path = real_path;
216
  if((actual_path[0] == '/') &&
217
      actual_path[1] &&
218
     (actual_path[2] == ':' || actual_path[2] == '|')) {
219
    actual_path[2] = ':';
220
    actual_path++;
221
    real_path_len--;
222
  }
223
224
  /* change path separators from '/' to '\\' for DOS, Windows and OS/2 */
225
  for(i = 0; i < real_path_len; ++i)
226
    if(actual_path[i] == '/')
227
      actual_path[i] = '\\';
228
    else if(!actual_path[i]) { /* binary zero */
229
      curlx_safefree(real_path);
230
      return CURLE_URL_MALFORMAT;
231
    }
232
233
  fd = curlx_open(actual_path, O_RDONLY | CURL_O_BINARY);
234
  file->path = actual_path;
235
#else
236
0
  if(memchr(real_path, 0, real_path_len)) {
237
    /* binary zeroes indicate foul play */
238
0
    curlx_safefree(real_path);
239
0
    return CURLE_URL_MALFORMAT;
240
0
  }
241
242
#ifdef AMIGA_FILESYSTEM
243
  /*
244
   * A leading slash in an AmigaDOS path denotes the parent
245
   * directory, and hence we block this as it is relative.
246
   * Absolute paths start with 'volumename:', so we check for
247
   * this first. Failing that, we treat the path as a real Unix
248
   * path, but only if the application was compiled with -lunix.
249
   */
250
  fd = -1;
251
  file->path = real_path;
252
253
  if(real_path[0] == '/') {
254
    extern int __unix_path_semantics;
255
    if(strchr(real_path + 1, ':')) {
256
      /* Amiga absolute path */
257
      fd = curlx_open(real_path + 1, O_RDONLY);
258
      file->path++;
259
    }
260
    else if(__unix_path_semantics) {
261
      /* -lunix fallback */
262
      fd = curlx_open(real_path, O_RDONLY);
263
    }
264
  }
265
#else
266
0
  fd = curlx_open(real_path, O_RDONLY);
267
0
  file->path = real_path;
268
0
#endif
269
0
#endif
270
0
  curlx_free(file->freepath);
271
0
  file->freepath = real_path; /* free this when done */
272
273
0
  file->fd = fd;
274
#ifdef _WIN32
275
  if(!data->state.upload && (fd == -1) &&
276
     !file_stat(file->path, &statbuf) && S_ISDIR(statbuf.st_mode))
277
    file->is_dir = TRUE;
278
#endif
279
280
0
  if(!data->state.upload && (fd == -1) && !file->is_dir) {
281
0
    failf(data, "Could not open file %s", data->state.up.path);
282
0
    file_done(data, CURLE_FILE_COULDNT_READ_FILE, FALSE);
283
0
    return CURLE_FILE_COULDNT_READ_FILE;
284
0
  }
285
0
  *done = TRUE;
286
287
0
  return CURLE_OK;
288
0
}
289
290
static CURLcode file_disconnect(struct Curl_easy *data,
291
                                struct connectdata *conn,
292
                                bool dead_connection)
293
0
{
294
0
  (void)dead_connection;
295
0
  (void)conn;
296
0
  return file_done(data, CURLE_OK, FALSE);
297
0
}
298
299
#ifdef DOS_FILESYSTEM
300
#define DIRSEP '\\'
301
#else
302
0
#define DIRSEP '/'
303
#endif
304
305
static CURLcode file_upload(struct Curl_easy *data,
306
                            struct FILEPROTO *file)
307
0
{
308
0
  const char *dir = strchr(file->path, DIRSEP);
309
0
  int fd;
310
0
  int mode;
311
0
  CURLcode result = CURLE_OK;
312
0
  char *xfer_ulbuf;
313
0
  size_t xfer_ulblen;
314
0
  curlx_struct_stat file_stat;
315
0
  const char *sendbuf;
316
0
  bool eos = FALSE;
317
318
  /*
319
   * Since FILE: does not do the full init, we need to provide some extra
320
   * assignments here.
321
   */
322
323
0
  if(!dir)
324
0
    return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
325
326
0
  if(!dir[1])
327
0
    return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
328
329
0
  mode = O_WRONLY | O_CREAT | CURL_O_BINARY;
330
0
  if(data->state.resume_from)
331
0
    mode |= O_APPEND;
332
0
  else
333
0
    mode |= O_TRUNC;
334
335
0
  fd = curlx_open(file->path, mode,
336
#ifdef _WIN32
337
                  data->set.new_file_perms & (_S_IREAD | _S_IWRITE)
338
#else
339
0
                  (mode_t)data->set.new_file_perms
340
0
#endif
341
0
                  );
342
0
  if(fd < 0) {
343
0
    failf(data, "cannot open %s for writing", file->path);
344
0
    return CURLE_WRITE_ERROR;
345
0
  }
346
347
0
  if(data->state.infilesize != -1)
348
    /* known size of data to "upload" */
349
0
    Curl_pgrsSetUploadSize(data, data->state.infilesize);
350
351
  /* treat the negative resume offset value as the case of "-" */
352
0
  if(data->state.resume_from < 0) {
353
0
    if(curlx_fstat(fd, &file_stat)) {
354
0
      curlx_close(fd);
355
0
      failf(data, "cannot get the size of %s", file->path);
356
0
      return CURLE_WRITE_ERROR;
357
0
    }
358
0
    data->state.resume_from = (curl_off_t)file_stat.st_size;
359
0
  }
360
361
0
  result = Curl_multi_xfer_ulbuf_borrow(data, &xfer_ulbuf, &xfer_ulblen);
362
0
  if(result)
363
0
    goto out;
364
365
0
  while(!result && !eos) {
366
0
    size_t nread, nwritten;
367
0
    ssize_t rv;
368
0
    size_t readcount;
369
370
0
    result = Curl_client_read(data, xfer_ulbuf, xfer_ulblen, &readcount, &eos);
371
0
    if(result)
372
0
      break;
373
374
0
    if(!readcount)
375
0
      break;
376
377
0
    nread = readcount;
378
379
    /* skip bytes before resume point */
380
0
    if(data->state.resume_from) {
381
0
      if((curl_off_t)nread <= data->state.resume_from) {
382
0
        data->state.resume_from -= nread;
383
0
        nread = 0;
384
0
        sendbuf = xfer_ulbuf;
385
0
      }
386
0
      else {
387
0
        sendbuf = xfer_ulbuf + data->state.resume_from;
388
0
        nread -= (size_t)data->state.resume_from;
389
0
        data->state.resume_from = 0;
390
0
      }
391
0
    }
392
0
    else
393
0
      sendbuf = xfer_ulbuf;
394
395
    /* write the data to the target */
396
0
    rv = write(fd, sendbuf, nread);
397
0
    if(!curlx_sztouz(rv, &nwritten) || (nwritten != nread)) {
398
0
      result = CURLE_SEND_ERROR;
399
0
      break;
400
0
    }
401
0
    Curl_pgrs_upload_inc(data, nwritten);
402
403
0
    result = Curl_pgrsCheck(data);
404
0
    if(!result && Curl_timeleft_ms(data) < 0) {
405
0
      failf(data, "File transfer timed out");
406
0
      result = CURLE_OPERATION_TIMEDOUT;
407
0
    }
408
0
  }
409
0
  if(!result)
410
0
    result = Curl_pgrsUpdate(data);
411
412
0
out:
413
0
  if(curlx_close(fd) && !result)
414
0
    result = CURLE_WRITE_ERROR;
415
0
  Curl_multi_xfer_ulbuf_release(data, xfer_ulbuf);
416
417
0
  return result;
418
0
}
419
420
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
421
static CURLcode win32_file_list(struct Curl_easy *data, const char *path)
422
{
423
  WIN32_FIND_DATA entry;
424
  HANDLE handle;
425
  char *pattern;
426
  size_t pathlen = strlen(path);
427
  CURLcode result = CURLE_OK;
428
  DWORD error;
429
430
  pattern = curl_maprintf("%s%s*", path,
431
                          pathlen &&
432
                          (path[pathlen - 1] == '\\' ||
433
                           path[pathlen - 1] == '/') ? "" : "\\");
434
  if(!pattern)
435
    return CURLE_OUT_OF_MEMORY;
436
437
  handle = curlx_FindFirstFile(pattern, &entry);
438
  curlx_free(pattern);
439
  if(handle == INVALID_HANDLE_VALUE)
440
    return CURLE_READ_ERROR;
441
442
  do {
443
    if(entry.cFileName[0] != TEXT('.')) {
444
      char *name = curlx_convert_tchar_to_UTF8(entry.cFileName);
445
446
      if(!name) {
447
        result = CURLE_OUT_OF_MEMORY;
448
        break;
449
      }
450
451
      result = Curl_client_write(data, CLIENTWRITE_BODY,
452
                                 name, strlen(name));
453
      curlx_free(name);
454
      if(result)
455
        break;
456
457
      result = Curl_client_write(data, CLIENTWRITE_BODY, "\n", 1);
458
      if(result)
459
        break;
460
    }
461
  } while(FindNextFile(handle, &entry));
462
463
  error = GetLastError();
464
  if(!result && error != ERROR_NO_MORE_FILES)
465
    result = CURLE_READ_ERROR;
466
467
  FindClose(handle);
468
  return result;
469
}
470
#endif
471
472
static CURLcode file_list_dir(struct Curl_easy *data, const char *path)
473
0
{
474
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
475
  return win32_file_list(data, path);
476
#elif defined(HAVE_OPENDIR)
477
  DIR *dir = opendir(path);
478
0
  struct dirent *entry;
479
0
  CURLcode result = CURLE_OK;
480
481
0
  if(!dir)
482
0
    return CURLE_READ_ERROR;
483
484
0
  while((entry = readdir(dir))) {
485
0
    if(entry->d_name[0] != '.') {
486
0
      result = Curl_client_write(data, CLIENTWRITE_BODY,
487
0
                                 entry->d_name, strlen(entry->d_name));
488
0
      if(result)
489
0
        break;
490
0
      result = Curl_client_write(data, CLIENTWRITE_BODY, "\n", 1);
491
0
      if(result)
492
0
        break;
493
0
    }
494
0
  }
495
0
  closedir(dir);
496
0
  return result;
497
#else
498
  (void)path;
499
  failf(data, "Directory listing not yet implemented on this platform.");
500
  return CURLE_READ_ERROR;
501
#endif
502
0
}
503
504
static CURLcode file_check_eof(struct Curl_easy *data, int fd,
505
                               const curlx_struct_stat *initial,
506
                               curl_off_t expected_size)
507
0
{
508
0
  if(expected_size &&
509
0
     ((curl_off_t)initial->st_size - data->state.resume_from >
510
0
      data->progress.dl.cur_size)) {
511
0
    curlx_struct_stat current;
512
    /* Virtual files can report a size larger than their readable data. */
513
0
    if(!curlx_fstat(fd, &current) && (current.st_size < initial->st_size)) {
514
0
      failf(data, "File ended before the expected size");
515
0
      return CURLE_PARTIAL_FILE;
516
0
    }
517
0
  }
518
0
  return CURLE_OK;
519
0
}
520
521
static CURLcode file_send_headers(struct Curl_easy *data,
522
                                  curl_off_t expected_size,
523
                                  const curlx_struct_stat *statbuf)
524
0
{
525
0
  CURLcode result;
526
0
  time_t filetime;
527
0
  struct tm buffer;
528
0
  const struct tm *tm = &buffer;
529
0
  char header[80];
530
0
  int headerlen;
531
0
  static const char accept_ranges[] = { "Accept-ranges: bytes\r\n" };
532
533
0
  if(expected_size >= 0) {
534
0
    headerlen =
535
0
      curl_msnprintf(header, sizeof(header),
536
0
                     "Content-Length: %" FMT_OFF_T "\r\n", expected_size);
537
0
    result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
538
0
    if(result)
539
0
      return result;
540
541
0
    result = Curl_client_write(data, CLIENTWRITE_HEADER,
542
0
                               accept_ranges, CURL_CSTRLEN(accept_ranges));
543
0
    if(result)
544
0
      return result;
545
0
  }
546
547
0
  filetime = (time_t)statbuf->st_mtime;
548
0
  result = curlx_gmtime(filetime, &buffer);
549
0
  if(result)
550
0
    return result;
551
552
  /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
553
0
  headerlen =
554
0
    curl_msnprintf(header, sizeof(header),
555
0
                   "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
556
0
                   Curl_wkday[tm->tm_wday ? tm->tm_wday - 1 : 6],
557
0
                   tm->tm_mday,
558
0
                   Curl_month[tm->tm_mon],
559
0
                   tm->tm_year + 1900,
560
0
                   tm->tm_hour,
561
0
                   tm->tm_min,
562
0
                   tm->tm_sec);
563
0
  result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
564
0
  if(!result)
565
    /* end of headers */
566
0
    result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
567
0
  if(result)
568
0
    return result;
569
570
  /* set the file size to make it available post transfer */
571
0
  Curl_pgrsSetDownloadSize(data, expected_size);
572
0
  return CURLE_OK;
573
0
}
574
575
static CURLcode file_read(struct Curl_easy *data, int fd,
576
                          curl_off_t expected_size,
577
                          bool size_known,
578
                          const curlx_struct_stat *statbuf)
579
0
{
580
0
  char *xfer_buf;
581
0
  size_t xfer_blen;
582
0
  CURLcode result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
583
0
  if(result)
584
0
    return result;
585
586
0
  while(!result) {
587
0
    ssize_t nread;
588
    /* Do not fill a whole buffer if we want less than all data */
589
0
    size_t bytestoread;
590
591
0
    if(size_known) {
592
0
      bytestoread = (expected_size < (curl_off_t)(xfer_blen - 1)) ?
593
0
        curlx_sotouz(expected_size) : (xfer_blen - 1);
594
0
    }
595
0
    else
596
0
      bytestoread = xfer_blen - 1;
597
598
0
    nread = read(fd, xfer_buf, bytestoread);
599
0
    if(nread < 0)
600
0
      result = CURLE_READ_ERROR;
601
0
    else if(!nread && size_known)
602
0
      result = file_check_eof(data, fd, statbuf, expected_size);
603
604
0
    if(nread > 0)
605
0
      xfer_buf[nread] = 0;
606
607
0
    if(nread <= 0 || (size_known && (expected_size == 0)))
608
0
      break;
609
610
0
    if(size_known)
611
0
      expected_size -= nread;
612
613
0
    result = Curl_client_write(data, CLIENTWRITE_BODY, xfer_buf, nread);
614
0
    if(result)
615
0
      break;
616
617
0
    result = Curl_pgrsCheck(data);
618
0
    if(!result && Curl_timeleft_ms(data) < 0) {
619
0
      failf(data, "File transfer timed out");
620
0
      result = CURLE_OPERATION_TIMEDOUT;
621
0
    }
622
0
  }
623
624
0
  Curl_multi_xfer_buf_release(data, xfer_buf);
625
0
  return result;
626
0
}
627
628
/*
629
 * file_do() is the protocol-specific function for the do-phase, separated
630
 * from the connect-phase above. Other protocols merely setup the transfer in
631
 * the do-phase, to have it done in the main transfer loop but since some
632
 * platforms we support do not allow select()ing etc on file handles (as
633
 * opposed to sockets) we instead perform the whole do-operation in this
634
 * function.
635
 */
636
static CURLcode file_do(struct Curl_easy *data, bool *done)
637
0
{
638
  /* This implementation ignores the hostname in conformance with
639
     RFC 1738. Only local files (reachable via the standard file system)
640
     are supported. This means that files on remotely mounted directories
641
     (via NFS, Samba, NT sharing) can be accessed through a file:// URL */
642
0
  struct FILEPROTO *file = Curl_meta_get(data, CURL_META_FILE_EASY);
643
0
  CURLcode result = CURLE_OK;
644
0
  curlx_struct_stat statbuf;
645
0
  curl_off_t expected_size = -1;
646
0
  bool size_known;
647
0
  bool fstated = FALSE;
648
0
  int fd;
649
650
0
  *done = TRUE; /* unconditionally */
651
0
  if(!file)
652
0
    return CURLE_FAILED_INIT;
653
654
0
  if(data->state.upload)
655
0
    return file_upload(data, file);
656
657
  /* get the fd from the connection phase */
658
0
  fd = file->fd;
659
660
  /* VMS: This only works reliable for STREAMLF files */
661
0
  if((file->is_dir ? file_stat(file->path, &statbuf) :
662
0
                     curlx_fstat(fd, &statbuf)) != -1) {
663
0
    file->is_dir = (fd == -1) || S_ISDIR(statbuf.st_mode);
664
0
    if(!file->is_dir)
665
0
      expected_size = statbuf.st_size;
666
    /* and store the modification time */
667
0
    data->info.filetime = statbuf.st_mtime;
668
0
    fstated = TRUE;
669
0
  }
670
671
0
  if(fstated && !data->state.range && data->set.timecondition &&
672
0
     !Curl_meets_timecondition(data, data->info.filetime))
673
0
    return CURLE_OK;
674
675
0
  if(fstated) {
676
0
    result = file_send_headers(data, expected_size, &statbuf);
677
0
    if(result || data->req.no_body)
678
0
      return result;
679
0
  }
680
681
  /* Check whether file range has been specified */
682
0
  result = Curl_range(data);
683
0
  if(result)
684
0
    return result;
685
686
  /* Adjust the start offset in case we want to get the N last bytes
687
   * of the stream if the filesize could be determined */
688
0
  if(data->state.resume_from < 0) {
689
0
    if(!fstated) {
690
0
      failf(data, "cannot get the size of file.");
691
0
      return CURLE_READ_ERROR;
692
0
    }
693
0
    if((curl_off_t)statbuf.st_size < -data->state.resume_from) {
694
0
      failf(data, "Offset (%" FMT_OFF_T ") was beyond file size (%"
695
0
            FMT_OFF_T ")", data->state.resume_from,
696
0
            (curl_off_t)statbuf.st_size);
697
0
      return CURLE_BAD_DOWNLOAD_RESUME;
698
0
    }
699
0
    data->state.resume_from += (curl_off_t)statbuf.st_size;
700
0
  }
701
702
0
  if(data->state.resume_from > 0) {
703
    /* We check explicitly if we have a start offset, because
704
     * expected_size may be -1 if we do not know how large the file is,
705
     * in which case we should not adjust it. */
706
0
    if(data->state.resume_from <= expected_size)
707
0
      expected_size -= data->state.resume_from;
708
0
    else {
709
0
      failf(data, "failed to resume file:// transfer");
710
0
      return CURLE_BAD_DOWNLOAD_RESUME;
711
0
    }
712
0
  }
713
714
  /* A high water mark has been specified so we obey... */
715
0
  if(data->req.maxdownload > 0)
716
0
    expected_size = data->req.maxdownload;
717
718
0
  if(!fstated || (expected_size <= 0))
719
0
    size_known = FALSE;
720
0
  else
721
0
    size_known = TRUE;
722
723
  /* The following is a shortcut implementation of file reading
724
     this is both more efficient than the former call to download() and
725
     it avoids problems with select() and recv() on file descriptors
726
     in Winsock */
727
0
  if(size_known)
728
0
    Curl_pgrsSetDownloadSize(data, expected_size);
729
730
0
  if(data->state.resume_from) {
731
0
    if(!file->is_dir) {
732
0
      if(data->state.resume_from !=
733
0
         curl_lseek(fd, data->state.resume_from, SEEK_SET))
734
0
        return CURLE_BAD_DOWNLOAD_RESUME;
735
0
    }
736
0
    else {
737
0
      return CURLE_BAD_DOWNLOAD_RESUME;
738
0
    }
739
0
  }
740
741
0
  if(!file->is_dir)
742
0
    result = file_read(data, fd, expected_size, size_known, &statbuf);
743
0
  else
744
0
    result = file_list_dir(data, file->path);
745
746
0
  if(!result)
747
0
    result = Curl_pgrsUpdate(data);
748
749
0
  return result;
750
0
}
751
752
const struct Curl_protocol Curl_protocol_file = {
753
  file_setup_connection,                /* setup_connection */
754
  ZERO_NULL,                            /* setup_filters */
755
  file_do,                              /* do_it */
756
  file_done,                            /* done */
757
  ZERO_NULL,                            /* do_more */
758
  file_connect,                         /* connect_it */
759
  ZERO_NULL,                            /* connecting */
760
  ZERO_NULL,                            /* doing */
761
  ZERO_NULL,                            /* proto_pollset */
762
  ZERO_NULL,                            /* doing_pollset */
763
  ZERO_NULL,                            /* domore_pollset */
764
  ZERO_NULL,                            /* perform_pollset */
765
  file_disconnect,                      /* disconnect */
766
  ZERO_NULL,                            /* write_resp */
767
  ZERO_NULL,                            /* write_resp_hd */
768
  ZERO_NULL,                            /* connection_is_dead */
769
  ZERO_NULL,                            /* attach connection */
770
  ZERO_NULL,                            /* follow */
771
};
772
773
#endif