-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmsio.c
832 lines (712 loc) · 22.4 KB
/
msio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/***************************************************************************
* I/O handling routines, for files and URLs.
*
* This file is part of the miniSEED Library.
*
* Copyright (c) 2024 Chad Trabant, EarthScope Data Services
*
* Licensed 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
***************************************************************************/
/* Define _LARGEFILE_SOURCE to get ftello/fseeko on some systems (Linux) */
#define _LARGEFILE_SOURCE 1
#include <errno.h>
#include "msio.h"
/* Include libcurl library header if URL supported is requested */
#if defined(LIBMSEED_URL)
#include <curl/curl.h>
/* Control for enabling debugging information */
int libmseed_url_debug = -1;
/* Control for SSL peer and host verification */
long libmseed_ssl_noverify = -1;
/* A global libcurl easy handle for configuration options */
CURL *gCURLeasy = NULL;
/* A global libcurl list of headers */
struct curl_slist *gCURLheaders = NULL;
/* Receving callback parameters */
struct recv_callback_parameters
{
char *buffer;
size_t size;
int is_paused;
};
/* Header callback parameters */
struct header_callback_parameters
{
int64_t *startoffset;
int64_t *endoffset;
};
/*********************************************************************
* Callback fired when recv'ing data using libcurl.
*
* The destination buffer pointer and size in the callback parameters
* are adjusted as data are added.
*
* Returns number of bytes added to the destination buffer.
*********************************************************************/
static size_t
recv_callback (char *buffer, size_t size, size_t num, void *userdata)
{
struct recv_callback_parameters *rcp = (struct recv_callback_parameters *)userdata;
if (!buffer || !userdata)
return 0;
size *= num;
/* Pause connection if passed data does not fit into destination buffer */
if (size > rcp->size)
{
rcp->is_paused = 1;
return CURL_WRITEFUNC_PAUSE;
}
/* Otherwise, copy data to destination buffer */
else
{
memcpy (rcp->buffer, buffer, size);
rcp->buffer += size;
rcp->size -= size;
}
return size;
}
/*********************************************************************
* Callback fired when receiving headers using libcurl.
*
* Returns number of bytes processed for success.
*********************************************************************/
static size_t
header_callback (char *buffer, size_t size, size_t num, void *userdata)
{
struct header_callback_parameters *hcp = (struct header_callback_parameters *)userdata;
char startstr[21] = {0}; /* Maximum of 20 digit value */
char endstr[21] = {0}; /* Maximum of 20 digit value */
int startdigits = 0;
int enddigits = 0;
char *dash = NULL;
char *ptr;
if (!buffer || !userdata)
return 0;
size *= num;
/* Parse and store: "Content-Range: bytes START-END/TOTAL"
* e.g. Content-Range: bytes 512-1023/4096 */
if (size > 22 && strncasecmp (buffer, "Content-Range: bytes", 20) == 0)
{
/* Process each character, starting just afer "bytes" unit */
for (ptr = buffer + 20; *ptr != '\0' && (ptr - buffer) < size; ptr++)
{
/* Skip spaces before start of range */
if (*ptr == ' ' && startdigits == 0)
continue;
/* Digits before dash, part of start */
else if (isdigit (*ptr) && dash == NULL)
startstr[startdigits++] = *ptr;
/* Digits after dash, part of end */
else if (isdigit (*ptr) && dash != NULL)
endstr[enddigits++] = *ptr;
/* If first dash found, store pointer */
else if (*ptr == '-' && dash == NULL)
dash = ptr;
/* Nothing else is part of the range */
else
break;
/* If digit sequences have exceeded limits, not a valid range */
if (startdigits >= sizeof (startstr) || enddigits >= sizeof (endstr))
{
startdigits = 0;
enddigits = 0;
break;
}
}
/* Convert start and end values to numbers if non-zero length */
if (hcp->startoffset && startdigits)
*hcp->startoffset = (int64_t) strtoull (startstr, NULL, 10);
if (hcp->endoffset && enddigits)
*hcp->endoffset = (int64_t) strtoull (endstr, NULL, 10);
}
return size;
}
#endif /* defined(LIBMSEED_URL) */
/***************************************************************************
* msio_fopen:
*
* Determine if requested path is a regular file or a URL and open or
* initialize as appropriate.
*
* The 'mode' argument is only for file-system paths and ignored for
* URLs. If 'mode' is set to NULL, default is 'rb' mode.
*
* If 'startoffset' or 'endoffset' are non-zero they will be used to
* position the stream for reading, either setting the read position
* of a file or requesting a range via HTTP. These will be set to the
* actual range if reported via HTTP, which may be different than
* requested.
*
* Return 0 on success and -1 on error.
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
int
msio_fopen (LMIO *io, const char *path, const char *mode,
int64_t *startoffset, int64_t *endoffset)
{
int knownfile = 0;
if (!io || !path)
return -1;
if (!mode)
mode = "rb";
/* Treat "file://" specifications as local files by removing the scheme */
if (!strncasecmp (path, "file://", 7))
{
path += 7;
knownfile = 1;
}
/* Test for URL scheme via "://" */
if (!knownfile && strstr (path, "://"))
{
#if !defined(LIBMSEED_URL)
(void)endoffset; /* Unused */
ms_log (2, "URL support not included in library for %s\n", path);
return -1;
#else
long response_code;
struct header_callback_parameters hcp;
io->type = LMIO_URL;
/* Check for URL debugging environment variable */
if (libmseed_url_debug < 0)
{
if (getenv ("LIBMSEED_URL_DEBUG"))
libmseed_url_debug = 1;
else
libmseed_url_debug = 0;
}
/* Check for SSL peer/host verify environment variable */
if (libmseed_ssl_noverify < 0)
{
if (getenv ("LIBMSEED_SSL_NOVERIFY"))
libmseed_ssl_noverify = 1;
else
libmseed_ssl_noverify = 0;
}
/* Configure the libcurl easy handle, duplicate global options if present */
io->handle = (gCURLeasy) ? curl_easy_duphandle (gCURLeasy) : curl_easy_init ();
if (io->handle == NULL)
{
ms_log (2, "Cannot initialize CURL handle\n");
return -1;
}
/* URL debug */
if (libmseed_url_debug && curl_easy_setopt (io->handle, CURLOPT_VERBOSE, 1L) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_VERBOSE\n");
return -1;
}
/* SSL peer and host verification */
if (libmseed_ssl_noverify &&
(curl_easy_setopt (io->handle, CURLOPT_SSL_VERIFYPEER, 0L) != CURLE_OK ||
curl_easy_setopt (io->handle, CURLOPT_SSL_VERIFYHOST, 0L) != CURLE_OK))
{
ms_log (2, "Cannot set CURLOPT_SSL_VERIFYPEER and/or CURLOPT_SSL_VERIFYHOST\n");
return -1;
}
/* Set URL */
if (curl_easy_setopt (io->handle, CURLOPT_URL, path) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_URL\n");
return -1;
}
/* Set default User-Agent header, can be overridden via custom header */
if (curl_easy_setopt (io->handle, CURLOPT_USERAGENT,
"libmseed/" LIBMSEED_VERSION " libcurl/" LIBCURL_VERSION) != CURLE_OK)
{
ms_log (2, "Cannot set default CURLOPT_USERAGENT\n");
return -1;
}
/* Disable signals */
if (curl_easy_setopt (io->handle, CURLOPT_NOSIGNAL, 1L) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_NOSIGNAL\n");
return -1;
}
/* Return failure codes on errors */
if (curl_easy_setopt (io->handle, CURLOPT_FAILONERROR, 1L) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_FAILONERROR\n");
return -1;
}
/* Follow HTTP redirects */
if (curl_easy_setopt (io->handle, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_FOLLOWLOCATION\n");
return -1;
}
/* Configure write callback for recv'ed data */
if (curl_easy_setopt (io->handle, CURLOPT_WRITEFUNCTION, recv_callback) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_WRITEFUNCTION\n");
return -1;
}
/* Configure the libcurl multi handle, for use with the asynchronous interface */
if ((io->handle2 = curl_multi_init ()) == NULL)
{
ms_log (2, "Cannot initialize CURL multi handle\n");
return -1;
}
if (curl_multi_add_handle (io->handle2, io->handle) != CURLM_OK)
{
ms_log (2, "Cannot add CURL handle to multi handle\n");
return -1;
}
/* Set byte ranging */
if ((startoffset && *startoffset > 0) || (endoffset && *endoffset > 0))
{
char startstr[21] = {0};
char endstr[21] = {0};
char rangestr[42];
/* Build Range header value.
* If start is undefined set it to zero if end is defined. */
if (*startoffset > 0)
snprintf (startstr, sizeof (startstr), "%" PRId64, *startoffset);
else if (*endoffset > 0)
snprintf (startstr, sizeof (startstr), "0");
if (*endoffset > 0)
snprintf (endstr, sizeof (endstr), "%" PRId64, *endoffset);
snprintf (rangestr, sizeof (rangestr), "%s-%s", startstr, endstr);
/* Set Range header */
if (curl_easy_setopt (io->handle, CURLOPT_RANGE, rangestr) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_RANGE to '%s'\n", rangestr);
return -1;
}
}
/* Set up header callback */
if (startoffset || endoffset)
{
hcp.startoffset = startoffset;
hcp.endoffset = endoffset;
/* Configure header callback */
if (curl_easy_setopt (io->handle, CURLOPT_HEADERFUNCTION, header_callback) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_HEADERFUNCTION\n");
return -1;
}
if (curl_easy_setopt (io->handle, CURLOPT_HEADERDATA, (void *)&hcp) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_HEADERDATA\n");
return -1;
}
}
/* Set custom headers */
if (gCURLheaders && curl_easy_setopt (io->handle, CURLOPT_HTTPHEADER, gCURLheaders) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_HTTPHEADER\n");
return -1;
}
/* Set connection as still running */
io->still_running = 1;
/* Start connection, get status & headers, without consuming any data */
msio_fread (io, NULL, 0);
curl_easy_getinfo (io->handle, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code == 404)
{
ms_log (2, "Cannot open %s: Not Found (404)\n", path);
return -1;
}
else if (response_code >= 400 && response_code < 600)
{
ms_log (2, "Cannot open %s: response code %ld\n", path, response_code);
return -1;
}
#endif /* defined(LIBMSEED_URL) */
}
else
{
io->type = LMIO_FILE;
if ((io->handle = fopen (path, mode)) == NULL)
{
ms_log (2, "Cannot open: %s (%s)\n", path, strerror(errno));
return -1;
}
/* Seek to position if start offset is provided */
if (startoffset && *startoffset > 0)
{
if (lmp_fseek64 (io->handle, *startoffset, SEEK_SET))
{
ms_log (2, "Cannot seek in %s to offset %" PRId64 "\n", path, *startoffset);
return -1;
}
}
}
return 0;
} /* End of msio_fopen() */
/*********************************************************************
* msio_fclose:
*
* Close an IO handle.
*
* Returns 0 on success and negative value on error.
*
* \ref MessageOnError - this function logs a message on error
*********************************************************************/
int
msio_fclose (LMIO *io)
{
int rv;
if (!io)
{
ms_log (2, "%s(): Required input not defined: 'io'\n", __func__);
return -1;
}
if (io->handle == NULL || io->type == LMIO_NULL)
return 0;
if (io->type == LMIO_FILE || io->type == LMIO_FD)
{
rv = fclose (io->handle);
if (rv)
{
ms_log (2, "Error closing file (%s)\n", strerror (errno));
return -1;
}
}
else if (io->type == LMIO_URL)
{
#if !defined(LIBMSEED_URL)
ms_log (2, "URL support not included in library\n");
return -1;
#else
curl_multi_remove_handle (io->handle2, io->handle);
curl_easy_cleanup (io->handle);
curl_multi_cleanup (io->handle2);
#endif
}
io->type = LMIO_NULL;
io->handle = NULL;
io->handle2 = NULL;
return 0;
} /* End of msio_fclose() */
/*********************************************************************
* msio_fread:
*
* Read data from the identified IO handle into the specified buffer.
* Up to the requested 'size' bytes are read.
*
* For URL support, with defined(LIBMSEED_URL), the destination
* receive buffer MUST be at least as big as the curl receive buffer
* (CURLOPT_BUFFERSIZE, which defaults to CURL_MAX_WRITE_SIZE of 16kB)
* or the maximum size of a retrieved object if less than
* CURL_MAX_WRITE_SIZE. The caller must ensure this.
*
* Returns the number of bytes read on success and a negative value on
* error.
*********************************************************************/
size_t
msio_fread (LMIO *io, void *buffer, size_t size)
{
size_t read = 0;
if (!io)
return -1;
if (!buffer && size > 0)
{
ms_log (2, "No buffer specified for size is > 0\n");
return -1;
}
/* Read from regular file stream */
if (io->type == LMIO_FILE || io->type == LMIO_FD)
{
read = fread (buffer, 1, size, io->handle);
}
/* Read from URL stream */
else if (io->type == LMIO_URL)
{
#if !defined(LIBMSEED_URL)
ms_log (2, "URL support not included in library\n");
return -1;
#else
struct recv_callback_parameters rcp;
struct timeval timeout;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
long curl_timeo = -1;
int maxfd = -1;
int rc;
if (!io->still_running)
return 0;
/* Set up destination buffer in write callback parameters */
rcp.buffer = buffer;
rcp.size = size;
if (curl_easy_setopt (io->handle, CURLOPT_WRITEDATA, (void *)&rcp) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_WRITEDATA\n");
return -1;
}
/* Unpause connection */
curl_easy_pause (io->handle, CURLPAUSE_CONT);
rcp.is_paused = 0;
/* Receive data while connection running, destination space available
* and connection is not paused. */
do
{
/* Default timeout for read failure */
timeout.tv_sec = 60;
timeout.tv_usec = 0;
curl_multi_timeout (io->handle2, &curl_timeo);
/* Tailor timeout based on maximum suggested by libcurl */
if (curl_timeo >= 0)
{
timeout.tv_sec = curl_timeo / 1000;
if (timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
FD_ZERO (&fdread);
FD_ZERO (&fdwrite);
FD_ZERO (&fdexcep);
/* Extract descriptors from the multi-handle */
if (curl_multi_fdset (io->handle2, &fdread, &fdwrite, &fdexcep, &maxfd) != CURLM_OK)
{
ms_log (2, "Error with curl_multi_fdset()\n");
return -1;
}
/* libcurl/system needs time to work, sleep 100 milliseconds */
if (maxfd == -1)
{
lmp_nanosleep (100000000);
rc = 0;
}
else
{
rc = select (maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
}
/* Receive data */
if (rc >= 0)
{
curl_multi_perform (io->handle2, &io->still_running);
}
} while (io->still_running > 0 && !rcp.is_paused && (rcp.size > 0 || rcp.buffer == NULL));
read = size - rcp.size;
#endif /* defined(LIBMSEED_URL) */
}
return read;
} /* End of msio_fread() */
/*********************************************************************
* msio_feof:
*
* Test if end-of-stream.
*
* Returns 1 when stream is at end, 0 if not, and -1 on error.
*********************************************************************/
int
msio_feof (LMIO *io)
{
if (!io)
return 0;
if (io->handle == NULL || io->type == LMIO_NULL)
return 0;
if (io->type == LMIO_FILE || io->type == LMIO_FD)
{
if (feof ((FILE *)io->handle))
return 1;
}
else if (io->type == LMIO_URL)
{
#if !defined(LIBMSEED_URL)
ms_log (2, "URL support not included in library\n");
return -1;
#else
/* The still_running flag is only changed by curl_multi_perform()
* and indicates current "transfers in progress". Presumably no data
* are in internal libcurl buffers either. */
if (!io->still_running)
return 1;
#endif
}
return 0;
} /* End of msio_feof() */
/*********************************************************************
* msio_url_useragent:
*
* Set global User-Agent header for URL-based IO.
*
* The header is built as "PROGRAM/VERSION libmseed/version libcurl/version"
* where VERSION is optional.
*
* Returns 0 on succes non-zero otherwise.
*
* \ref MessageOnError - this function logs a message on error
*********************************************************************/
int
msio_url_useragent (const char *program, const char *version)
{
if (!program)
{
ms_log (2, "%s(): Required input not defined: 'program'\n", __func__);
return -1;
}
#if !defined(LIBMSEED_URL)
(void)version; /* Unused */
ms_log (2, "URL support not included in library\n");
return -1;
#else
char header[1024];
/* Build User-Agent header and add internal versions */
snprintf (header, sizeof (header),
"User-Agent: %s%s%s libmseed/" LIBMSEED_VERSION " libcurl/" LIBCURL_VERSION,
program,
(version) ? "/" : "",
(version) ? version : "");
return msio_url_addheader (header);
#endif
return 0;
} /* End of msio_url_useragent() */
/*********************************************************************
* msio_url_userpassword:
*
* Set global user-password credentials for URL-based IO.
*
* Returns 0 on succes non-zero otherwise.
*
* \ref MessageOnError - this function logs a message on error
*********************************************************************/
int
msio_url_userpassword (const char *userpassword)
{
if (!userpassword)
{
ms_log (2, "%s(): Required input not defined: 'userpassword'\n", __func__);
return -1;
}
#if !defined(LIBMSEED_URL)
ms_log (2, "URL support not included in library\n");
return -1;
#else
if (gCURLeasy == NULL && (gCURLeasy = curl_easy_init ()) == NULL)
return -1;
/* Allow any authentication, libcurl will pick the most secure */
if (curl_easy_setopt (gCURLeasy, CURLOPT_HTTPAUTH, CURLAUTH_ANY) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_HTTPAUTH\n");
return -1;
}
if (curl_easy_setopt (gCURLeasy, CURLOPT_USERPWD, userpassword) != CURLE_OK)
{
ms_log (2, "Cannot set CURLOPT_USERPWD\n");
return -1;
}
#endif
return 0;
} /* End of msio_url_userpassword() */
/*********************************************************************
* msio_url_addheader:
*
* Add header to global list for URL-based IO.
*
* Returns 0 on succes non-zero otherwise.
*
* \ref MessageOnError - this function logs a message on error
*********************************************************************/
int
msio_url_addheader (const char *header)
{
if (!header)
{
ms_log (2, "%s(): Required input not defined: 'header'\n", __func__);
return -1;
}
#if !defined(LIBMSEED_URL)
ms_log (2, "URL support not included in library\n");
return -1;
#else
struct curl_slist *slist = NULL;
slist = curl_slist_append (gCURLheaders, header);
if (slist == NULL)
{
ms_log (2, "Error adding header to list: %s\n", header);
return -1;
}
gCURLheaders = slist;
#endif
return 0;
} /* End of msio_url_addheader() */
/*********************************************************************
* msio_url_freeheaders:
*
* Free the global list of headers for URL-based IO.
*********************************************************************/
void
msio_url_freeheaders (void)
{
#if !defined(LIBMSEED_URL)
ms_log (2, "URL support not included in library\n");
return;
#else
if (gCURLheaders != NULL)
{
curl_slist_free_all (gCURLheaders);
gCURLheaders = NULL;
}
#endif
} /* End of msio_url_freeheaders() */
/***************************************************************************
* lmp_ftell64:
*
* Return the current file position for the specified descriptor using
* the system's closest match to the POSIX ftello().
***************************************************************************/
int64_t
lmp_ftell64 (FILE *stream)
{
#if defined(LMP_WIN)
return (int64_t)_ftelli64 (stream);
#else
return (int64_t)ftello (stream);
#endif
} /* End of lmp_ftell64() */
/***************************************************************************
* lmp_fseek64:
*
* Seek to a specific file position for the specified descriptor using
* the system's closest match to the POSIX fseeko().
***************************************************************************/
int
lmp_fseek64 (FILE *stream, int64_t offset, int whence)
{
#if defined(LMP_WIN)
return (int)_fseeki64 (stream, offset, whence);
#else
return (int)fseeko (stream, offset, whence);
#endif
} /* End of lmp_fseeko() */
/***************************************************************************
* @brief Sleep for a specified number of nanoseconds
*
* Sleep for a given number of nanoseconds. Under WIN use SleepEx()
* and is limited to millisecond resolution. For all others use the
* POSIX.4 nanosleep(), which can be interrupted by signals.
*
* @param nanoseconds Nanoseconds to sleep
*
* @return On non-WIN: the remaining nanoseconds are returned if the
* requested interval is interrupted.
***************************************************************************/
uint64_t
lmp_nanosleep (uint64_t nanoseconds)
{
#if defined(LMP_WIN)
/* SleepEx is limited to milliseconds */
SleepEx ((DWORD) (nanoseconds / 1e6), 1);
return 0;
#else
struct timespec treq, trem;
treq.tv_sec = (time_t) (nanoseconds / 1e9);
treq.tv_nsec = (long)(nanoseconds - (uint64_t)treq.tv_sec * 1e9);
nanosleep (&treq, &trem);
return trem.tv_sec * 1e9 + trem.tv_nsec;
#endif
} /* End of lmp_nanosleep() */