Coverage Report

Created: 2025-11-11 06:28

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
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_FILE
28
29
#ifdef HAVE_NETINET_IN_H
30
#include <netinet/in.h>
31
#endif
32
#ifdef HAVE_NETDB_H
33
#include <netdb.h>
34
#endif
35
#ifdef HAVE_ARPA_INET_H
36
#include <arpa/inet.h>
37
#endif
38
#ifdef HAVE_NET_IF_H
39
#include <net/if.h>
40
#endif
41
#ifdef HAVE_SYS_IOCTL_H
42
#include <sys/ioctl.h>
43
#endif
44
45
#ifdef HAVE_SYS_PARAM_H
46
#include <sys/param.h>
47
#endif
48
49
#ifdef HAVE_SYS_TYPES_H
50
#include <sys/types.h>
51
#endif
52
53
#ifdef HAVE_DIRENT_H
54
#include <dirent.h>
55
#endif
56
57
#include "urldata.h"
58
#include <curl/curl.h>
59
#include "progress.h"
60
#include "sendf.h"
61
#include "escape.h"
62
#include "file.h"
63
#include "speedcheck.h"
64
#include "multiif.h"
65
#include "transfer.h"
66
#include "url.h"
67
#include "parsedate.h" /* for the week day and month names */
68
#include "curlx/fopen.h"
69
#include "curlx/warnless.h"
70
#include "curl_range.h"
71
72
/* The last 2 #include files should be in this order */
73
#include "curl_memory.h"
74
#include "memdebug.h"
75
76
#if defined(_WIN32) || defined(MSDOS)
77
#define DOS_FILESYSTEM 1
78
#elif defined(__amigaos4__)
79
#define AMIGA_FILESYSTEM 1
80
#endif
81
82
/* meta key for storing protocol meta at easy handle */
83
9.18k
#define CURL_META_FILE_EASY   "meta:proto:file:easy"
84
85
struct FILEPROTO {
86
  char *path; /* the path we operate on */
87
  char *freepath; /* pointer to the allocated block we must free, this might
88
                     differ from the 'path' pointer */
89
  int fd;     /* open file descriptor to read from! */
90
};
91
92
/*
93
 * Forward declarations.
94
 */
95
96
static CURLcode file_do(struct Curl_easy *data, bool *done);
97
static CURLcode file_done(struct Curl_easy *data,
98
                          CURLcode status, bool premature);
99
static CURLcode file_connect(struct Curl_easy *data, bool *done);
100
static CURLcode file_disconnect(struct Curl_easy *data,
101
                                struct connectdata *conn,
102
                                bool dead_connection);
103
static CURLcode file_setup_connection(struct Curl_easy *data,
104
                                      struct connectdata *conn);
105
106
/*
107
 * FILE scheme handler.
108
 */
109
110
const struct Curl_handler Curl_handler_file = {
111
  "file",                               /* scheme */
112
  file_setup_connection,                /* setup_connection */
113
  file_do,                              /* do_it */
114
  file_done,                            /* done */
115
  ZERO_NULL,                            /* do_more */
116
  file_connect,                         /* connect_it */
117
  ZERO_NULL,                            /* connecting */
118
  ZERO_NULL,                            /* doing */
119
  ZERO_NULL,                            /* proto_pollset */
120
  ZERO_NULL,                            /* doing_pollset */
121
  ZERO_NULL,                            /* domore_pollset */
122
  ZERO_NULL,                            /* perform_pollset */
123
  file_disconnect,                      /* disconnect */
124
  ZERO_NULL,                            /* write_resp */
125
  ZERO_NULL,                            /* write_resp_hd */
126
  ZERO_NULL,                            /* connection_check */
127
  ZERO_NULL,                            /* attach connection */
128
  ZERO_NULL,                            /* follow */
129
  0,                                    /* defport */
130
  CURLPROTO_FILE,                       /* protocol */
131
  CURLPROTO_FILE,                       /* family */
132
  PROTOPT_NONETWORK | PROTOPT_NOURLQUERY /* flags */
133
};
134
135
136
static void file_cleanup(struct FILEPROTO *file)
137
3.22k
{
138
3.22k
  Curl_safefree(file->freepath);
139
3.22k
  file->path = NULL;
140
3.22k
  if(file->fd != -1) {
141
1.33k
    close(file->fd);
142
1.33k
    file->fd = -1;
143
1.33k
  }
144
3.22k
}
145
146
static void file_easy_dtor(void *key, size_t klen, void *entry)
147
1.61k
{
148
1.61k
  struct FILEPROTO *file = entry;
149
1.61k
  (void)key;
150
1.61k
  (void)klen;
151
1.61k
  file_cleanup(file);
152
1.61k
  free(file);
153
1.61k
}
154
155
static CURLcode file_setup_connection(struct Curl_easy *data,
156
                                      struct connectdata *conn)
157
1.61k
{
158
1.61k
  struct FILEPROTO *filep;
159
1.61k
  (void)conn;
160
  /* allocate the FILE specific struct */
161
1.61k
  filep = calloc(1, sizeof(*filep));
162
1.61k
  if(!filep ||
163
1.61k
     Curl_meta_set(data, CURL_META_FILE_EASY, filep, file_easy_dtor))
164
0
    return CURLE_OUT_OF_MEMORY;
165
166
1.61k
  return CURLE_OK;
167
1.61k
}
168
169
/*
170
 * file_connect() gets called from Curl_protocol_connect() to allow us to
171
 * do protocol-specific actions at connect-time. We emulate a
172
 * connect-then-transfer protocol and "connect" to the file here
173
 */
174
static CURLcode file_connect(struct Curl_easy *data, bool *done)
175
2.98k
{
176
2.98k
  char *real_path;
177
2.98k
  struct FILEPROTO *file = Curl_meta_get(data, CURL_META_FILE_EASY);
178
2.98k
  int fd;
179
#ifdef DOS_FILESYSTEM
180
  size_t i;
181
  char *actual_path;
182
#endif
183
2.98k
  size_t real_path_len;
184
2.98k
  CURLcode result;
185
186
2.98k
  if(!file)
187
0
    return CURLE_FAILED_INIT;
188
189
2.98k
  if(file->path) {
190
    /* already connected.
191
     * the handler->connect_it() is normally only called once, but
192
     * FILE does a special check on setting up the connection which
193
     * calls this explicitly. */
194
1.36k
    *done = TRUE;
195
1.36k
    return CURLE_OK;
196
1.36k
  }
197
198
1.61k
  result = Curl_urldecode(data->state.up.path, 0, &real_path,
199
1.61k
                          &real_path_len, REJECT_ZERO);
200
1.61k
  if(result)
201
2
    return result;
202
203
#ifdef DOS_FILESYSTEM
204
  /* If the first character is a slash, and there is
205
     something that looks like a drive at the beginning of
206
     the path, skip the slash. If we remove the initial
207
     slash in all cases, paths without drive letters end up
208
     relative to the current directory which is not how
209
     browsers work.
210
211
     Some browsers accept | instead of : as the drive letter
212
     separator, so we do too.
213
214
     On other platforms, we need the slash to indicate an
215
     absolute pathname. On Windows, absolute paths start
216
     with a drive letter.
217
  */
218
  actual_path = real_path;
219
  if((actual_path[0] == '/') &&
220
      actual_path[1] &&
221
     (actual_path[2] == ':' || actual_path[2] == '|')) {
222
    actual_path[2] = ':';
223
    actual_path++;
224
    real_path_len--;
225
  }
226
227
  /* change path separators from '/' to '\\' for DOS, Windows and OS/2 */
228
  for(i = 0; i < real_path_len; ++i)
229
    if(actual_path[i] == '/')
230
      actual_path[i] = '\\';
231
    else if(!actual_path[i]) { /* binary zero */
232
      Curl_safefree(real_path);
233
      return CURLE_URL_MALFORMAT;
234
    }
235
236
  fd = curlx_open(actual_path, O_RDONLY | CURL_O_BINARY);
237
  file->path = actual_path;
238
#else
239
1.61k
  if(memchr(real_path, 0, real_path_len)) {
240
    /* binary zeroes indicate foul play */
241
0
    Curl_safefree(real_path);
242
0
    return CURLE_URL_MALFORMAT;
243
0
  }
244
245
  #ifdef AMIGA_FILESYSTEM
246
  /*
247
   * A leading slash in an AmigaDOS path denotes the parent
248
   * directory, and hence we block this as it is relative.
249
   * Absolute paths start with 'volumename:', so we check for
250
   * this first. Failing that, we treat the path as a real Unix
251
   * path, but only if the application was compiled with -lunix.
252
   */
253
  fd = -1;
254
  file->path = real_path;
255
256
  if(real_path[0] == '/') {
257
    extern int __unix_path_semantics;
258
    if(strchr(real_path + 1, ':')) {
259
      /* Amiga absolute path */
260
      fd = curlx_open(real_path + 1, O_RDONLY);
261
      file->path++;
262
    }
263
    else if(__unix_path_semantics) {
264
      /* -lunix fallback */
265
      fd = curlx_open(real_path, O_RDONLY);
266
    }
267
  }
268
  #else
269
1.61k
  fd = curlx_open(real_path, O_RDONLY);
270
1.61k
  file->path = real_path;
271
1.61k
  #endif
272
1.61k
#endif
273
1.61k
  free(file->freepath);
274
1.61k
  file->freepath = real_path; /* free this when done */
275
276
1.61k
  file->fd = fd;
277
1.61k
  if(!data->state.upload && (fd == -1)) {
278
241
    failf(data, "Couldn't open file %s", data->state.up.path);
279
241
    file_done(data, CURLE_FILE_COULDNT_READ_FILE, FALSE);
280
241
    return CURLE_FILE_COULDNT_READ_FILE;
281
241
  }
282
1.37k
  *done = TRUE;
283
284
1.37k
  return CURLE_OK;
285
1.61k
}
286
287
static CURLcode file_done(struct Curl_easy *data,
288
                          CURLcode status, bool premature)
289
3.22k
{
290
3.22k
  struct FILEPROTO *file = Curl_meta_get(data, CURL_META_FILE_EASY);
291
3.22k
  (void)status;
292
3.22k
  (void)premature;
293
294
3.22k
  if(file)
295
1.61k
    file_cleanup(file);
296
297
3.22k
  return CURLE_OK;
298
3.22k
}
299
300
static CURLcode file_disconnect(struct Curl_easy *data,
301
                                struct connectdata *conn,
302
                                bool dead_connection)
303
1.61k
{
304
1.61k
  (void)dead_connection;
305
1.61k
  (void)conn;
306
1.61k
  return file_done(data, CURLE_OK, FALSE);
307
1.61k
}
308
309
#ifdef DOS_FILESYSTEM
310
#define DIRSEP '\\'
311
#else
312
62
#define DIRSEP '/'
313
#endif
314
315
static CURLcode file_upload(struct Curl_easy *data,
316
                            struct FILEPROTO *file)
317
62
{
318
62
  const char *dir = strchr(file->path, DIRSEP);
319
62
  int fd;
320
62
  int mode;
321
62
  CURLcode result = CURLE_OK;
322
62
  char *xfer_ulbuf;
323
62
  size_t xfer_ulblen;
324
62
  curl_off_t bytecount = 0;
325
62
  struct_stat file_stat;
326
62
  const char *sendbuf;
327
62
  bool eos = FALSE;
328
329
  /*
330
   * Since FILE: does not do the full init, we need to provide some extra
331
   * assignments here.
332
   */
333
334
62
  if(!dir)
335
0
    return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
336
337
62
  if(!dir[1])
338
6
    return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
339
340
56
  mode = O_WRONLY|O_CREAT|CURL_O_BINARY;
341
56
  if(data->state.resume_from)
342
27
    mode |= O_APPEND;
343
29
  else
344
29
    mode |= O_TRUNC;
345
346
#if (defined(ANDROID) || defined(__ANDROID__)) && \
347
  (defined(__i386__) || defined(__arm__))
348
  fd = curlx_open(file->path, mode, (mode_t)data->set.new_file_perms);
349
#else
350
56
  fd = curlx_open(file->path, mode, data->set.new_file_perms);
351
56
#endif
352
56
  if(fd < 0) {
353
28
    failf(data, "cannot open %s for writing", file->path);
354
28
    return CURLE_WRITE_ERROR;
355
28
  }
356
357
28
  if(data->state.infilesize != -1)
358
    /* known size of data to "upload" */
359
28
    Curl_pgrsSetUploadSize(data, data->state.infilesize);
360
361
  /* treat the negative resume offset value as the case of "-" */
362
28
  if(data->state.resume_from < 0) {
363
0
    if(fstat(fd, &file_stat)) {
364
0
      close(fd);
365
0
      failf(data, "cannot get the size of %s", file->path);
366
0
      return CURLE_WRITE_ERROR;
367
0
    }
368
0
    data->state.resume_from = (curl_off_t)file_stat.st_size;
369
0
  }
370
371
28
  result = Curl_multi_xfer_ulbuf_borrow(data, &xfer_ulbuf, &xfer_ulblen);
372
28
  if(result)
373
0
    goto out;
374
375
28
  while(!result && !eos) {
376
28
    size_t nread;
377
28
    ssize_t nwrite;
378
28
    size_t readcount;
379
380
28
    result = Curl_client_read(data, xfer_ulbuf, xfer_ulblen, &readcount, &eos);
381
28
    if(result)
382
0
      break;
383
384
28
    if(!readcount)
385
28
      break;
386
387
0
    nread = readcount;
388
389
    /* skip bytes before resume point */
390
0
    if(data->state.resume_from) {
391
0
      if((curl_off_t)nread <= data->state.resume_from) {
392
0
        data->state.resume_from -= nread;
393
0
        nread = 0;
394
0
        sendbuf = xfer_ulbuf;
395
0
      }
396
0
      else {
397
0
        sendbuf = xfer_ulbuf + data->state.resume_from;
398
0
        nread -= (size_t)data->state.resume_from;
399
0
        data->state.resume_from = 0;
400
0
      }
401
0
    }
402
0
    else
403
0
      sendbuf = xfer_ulbuf;
404
405
    /* write the data to the target */
406
0
    nwrite = write(fd, sendbuf, nread);
407
0
    if((size_t)nwrite != nread) {
408
0
      result = CURLE_SEND_ERROR;
409
0
      break;
410
0
    }
411
412
0
    bytecount += nread;
413
414
0
    Curl_pgrsSetUploadCounter(data, bytecount);
415
416
0
    if(Curl_pgrsUpdate(data))
417
0
      result = CURLE_ABORTED_BY_CALLBACK;
418
0
    else
419
0
      result = Curl_speedcheck(data, curlx_now());
420
0
  }
421
28
  if(!result && Curl_pgrsUpdate(data))
422
0
    result = CURLE_ABORTED_BY_CALLBACK;
423
424
28
out:
425
28
  close(fd);
426
28
  Curl_multi_xfer_ulbuf_release(data, xfer_ulbuf);
427
428
28
  return result;
429
28
}
430
431
/*
432
 * file_do() is the protocol-specific function for the do-phase, separated
433
 * from the connect-phase above. Other protocols merely setup the transfer in
434
 * the do-phase, to have it done in the main transfer loop but since some
435
 * platforms we support do not allow select()ing etc on file handles (as
436
 * opposed to sockets) we instead perform the whole do-operation in this
437
 * function.
438
 */
439
static CURLcode file_do(struct Curl_easy *data, bool *done)
440
1.36k
{
441
  /* This implementation ignores the hostname in conformance with
442
     RFC 1738. Only local files (reachable via the standard file system)
443
     are supported. This means that files on remotely mounted directories
444
     (via NFS, Samba, NT sharing) can be accessed through a file:// URL
445
  */
446
1.36k
  struct FILEPROTO *file = Curl_meta_get(data, CURL_META_FILE_EASY);
447
1.36k
  CURLcode result = CURLE_OK;
448
1.36k
  struct_stat statbuf; /* struct_stat instead of struct stat just to allow the
449
                          Windows version to have a different struct without
450
                          having to redefine the simple word 'stat' */
451
1.36k
  curl_off_t expected_size = -1;
452
1.36k
  bool size_known;
453
1.36k
  bool fstated = FALSE;
454
1.36k
  int fd;
455
1.36k
  char *xfer_buf;
456
1.36k
  size_t xfer_blen;
457
458
1.36k
  *done = TRUE; /* unconditionally */
459
1.36k
  if(!file)
460
0
    return CURLE_FAILED_INIT;
461
462
1.36k
  if(data->state.upload)
463
62
    return file_upload(data, file);
464
465
  /* get the fd from the connection phase */
466
1.30k
  fd = file->fd;
467
468
  /* VMS: This only works reliable for STREAMLF files */
469
1.30k
  if(fstat(fd, &statbuf) != -1) {
470
1.30k
    if(!S_ISDIR(statbuf.st_mode))
471
367
      expected_size = statbuf.st_size;
472
    /* and store the modification time */
473
1.30k
    data->info.filetime = statbuf.st_mtime;
474
1.30k
    fstated = TRUE;
475
1.30k
  }
476
477
1.30k
  if(fstated && !data->state.range && data->set.timecondition &&
478
74
     !Curl_meets_timecondition(data, data->info.filetime))
479
35
    return CURLE_OK;
480
481
1.26k
  if(fstated) {
482
1.26k
    time_t filetime;
483
1.26k
    struct tm buffer;
484
1.26k
    const struct tm *tm = &buffer;
485
1.26k
    char header[80];
486
1.26k
    int headerlen;
487
1.26k
    static const char accept_ranges[]= { "Accept-ranges: bytes\r\n" };
488
1.26k
    if(expected_size >= 0) {
489
367
      headerlen =
490
367
        curl_msnprintf(header, sizeof(header),
491
367
                       "Content-Length: %" FMT_OFF_T "\r\n", expected_size);
492
367
      result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
493
367
      if(result)
494
0
        return result;
495
496
367
      result = Curl_client_write(data, CLIENTWRITE_HEADER,
497
367
                                 accept_ranges, sizeof(accept_ranges) - 1);
498
367
      if(result != CURLE_OK)
499
0
        return result;
500
367
    }
501
502
1.26k
    filetime = (time_t)statbuf.st_mtime;
503
1.26k
    result = Curl_gmtime(filetime, &buffer);
504
1.26k
    if(result)
505
0
      return result;
506
507
    /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
508
1.26k
    headerlen =
509
1.26k
      curl_msnprintf(header, sizeof(header),
510
1.26k
                     "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
511
1.26k
                     Curl_wkday[tm->tm_wday ? tm->tm_wday-1 : 6],
512
1.26k
                     tm->tm_mday,
513
1.26k
                     Curl_month[tm->tm_mon],
514
1.26k
                     tm->tm_year + 1900,
515
1.26k
                     tm->tm_hour,
516
1.26k
                     tm->tm_min,
517
1.26k
                     tm->tm_sec);
518
1.26k
    result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
519
1.26k
    if(!result)
520
      /* end of headers */
521
1.26k
      result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
522
1.26k
    if(result)
523
0
      return result;
524
    /* set the file size to make it available post transfer */
525
1.26k
    Curl_pgrsSetDownloadSize(data, expected_size);
526
1.26k
    if(data->req.no_body)
527
2
      return CURLE_OK;
528
1.26k
  }
529
530
  /* Check whether file range has been specified */
531
1.26k
  result = Curl_range(data);
532
1.26k
  if(result)
533
66
    return result;
534
535
  /* Adjust the start offset in case we want to get the N last bytes
536
   * of the stream if the filesize could be determined */
537
1.19k
  if(data->state.resume_from < 0) {
538
308
    if(!fstated) {
539
0
      failf(data, "cannot get the size of file.");
540
0
      return CURLE_READ_ERROR;
541
0
    }
542
308
    data->state.resume_from += (curl_off_t)statbuf.st_size;
543
308
  }
544
545
1.19k
  if(data->state.resume_from > 0) {
546
    /* We check explicitly if we have a start offset, because
547
     * expected_size may be -1 if we do not know how large the file is,
548
     * in which case we should not adjust it. */
549
227
    if(data->state.resume_from <= expected_size)
550
76
      expected_size -= data->state.resume_from;
551
151
    else {
552
151
      failf(data, "failed to resume file:// transfer");
553
151
      return CURLE_BAD_DOWNLOAD_RESUME;
554
151
    }
555
227
  }
556
557
  /* A high water mark has been specified so we obey... */
558
1.04k
  if(data->req.maxdownload > 0)
559
537
    expected_size = data->req.maxdownload;
560
561
1.04k
  if(!fstated || (expected_size <= 0))
562
287
    size_known = FALSE;
563
760
  else
564
760
    size_known = TRUE;
565
566
  /* The following is a shortcut implementation of file reading
567
     this is both more efficient than the former call to download() and
568
     it avoids problems with select() and recv() on file descriptors
569
     in Winsock */
570
1.04k
  if(size_known)
571
760
    Curl_pgrsSetDownloadSize(data, expected_size);
572
573
1.04k
  if(data->state.resume_from) {
574
358
    if(!S_ISDIR(statbuf.st_mode)) {
575
#if defined(__AMIGA__) || defined(__MINGW32CE__)
576
      if(data->state.resume_from !=
577
          lseek(fd, (off_t)data->state.resume_from, SEEK_SET))
578
#else
579
127
      if(data->state.resume_from !=
580
127
          lseek(fd, data->state.resume_from, SEEK_SET))
581
51
#endif
582
51
        return CURLE_BAD_DOWNLOAD_RESUME;
583
127
    }
584
231
    else {
585
231
      return CURLE_BAD_DOWNLOAD_RESUME;
586
231
    }
587
358
  }
588
589
765
  result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
590
765
  if(result)
591
0
    goto out;
592
593
765
  if(!S_ISDIR(statbuf.st_mode)) {
594
17.8k
    while(!result) {
595
17.8k
      ssize_t nread;
596
      /* Do not fill a whole buffer if we want less than all data */
597
17.8k
      size_t bytestoread;
598
599
17.8k
      if(size_known) {
600
17.7k
        bytestoread = (expected_size < (curl_off_t)(xfer_blen-1)) ?
601
17.2k
          curlx_sotouz(expected_size) : (xfer_blen-1);
602
17.7k
      }
603
88
      else
604
88
        bytestoread = xfer_blen-1;
605
606
17.8k
      nread = read(fd, xfer_buf, bytestoread);
607
608
17.8k
      if(nread > 0)
609
17.5k
        xfer_buf[nread] = 0;
610
611
17.8k
      if(nread <= 0 || (size_known && (expected_size == 0)))
612
279
        break;
613
614
17.5k
      if(size_known)
615
17.4k
        expected_size -= nread;
616
617
17.5k
      result = Curl_client_write(data, CLIENTWRITE_BODY, xfer_buf, nread);
618
17.5k
      if(result)
619
27
        goto out;
620
621
17.5k
      if(Curl_pgrsUpdate(data))
622
0
        result = CURLE_ABORTED_BY_CALLBACK;
623
17.5k
      else
624
17.5k
        result = Curl_speedcheck(data, curlx_now());
625
17.5k
      if(result)
626
0
        goto out;
627
17.5k
    }
628
306
  }
629
459
  else {
630
459
#ifdef HAVE_OPENDIR
631
459
    DIR *dir = opendir(file->path);
632
459
    struct dirent *entry;
633
634
459
    if(!dir) {
635
0
      result = CURLE_READ_ERROR;
636
0
      goto out;
637
0
    }
638
459
    else {
639
30.4k
      while((entry = readdir(dir))) {
640
29.9k
        if(entry->d_name[0] != '.') {
641
28.6k
          result = Curl_client_write(data, CLIENTWRITE_BODY,
642
28.6k
                   entry->d_name, strlen(entry->d_name));
643
28.6k
          if(result)
644
19
            break;
645
28.6k
          result = Curl_client_write(data, CLIENTWRITE_BODY, "\n", 1);
646
28.6k
          if(result)
647
3
            break;
648
28.6k
        }
649
29.9k
      }
650
459
      closedir(dir);
651
459
    }
652
#else
653
    failf(data, "Directory listing not yet implemented on this platform.");
654
    result = CURLE_READ_ERROR;
655
#endif
656
459
  }
657
658
738
  if(Curl_pgrsUpdate(data))
659
0
    result = CURLE_ABORTED_BY_CALLBACK;
660
661
765
out:
662
765
  Curl_multi_xfer_buf_release(data, xfer_buf);
663
765
  return result;
664
738
}
665
666
#endif