-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
ios.c
1273 lines (1142 loc) · 29.4 KB
/
ios.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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a part of Julia. License is MIT: https://julialang.org/license
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>
#include <wchar.h>
#include <stdio.h> // for printf
#include "dtypes.h"
#ifdef _OS_WINDOWS_
#include <malloc.h>
#include <io.h>
#include <fcntl.h>
#define fileno _fileno
//#define lseek _lseek
#else
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include "utils.h"
#include "utf8.h"
#include "utf8proc.h"
#include "ios.h"
#include "timefuncs.h"
#define MOST_OF(x) ((x) - ((x)>>4))
#ifdef __cplusplus
extern "C" {
#endif
void (*ios_set_io_wait_func)(int) = NULL;
static void set_io_wait_begin(int v)
{
if (__likely(ios_set_io_wait_func)) {
ios_set_io_wait_func(v);
}
}
/* OS-level primitive wrappers */
#if defined(__APPLE__) || defined(_OS_WINDOWS_)
JL_DLLEXPORT void *memrchr(const void *s, int c, size_t n)
{
const unsigned char *src = (unsigned char*)s + n;
unsigned char uc = c;
while (--src >= (unsigned char*)s)
if (*src == uc)
return (void*)src;
return NULL;
}
#else
extern void *memrchr(const void *s, int c, size_t n);
#endif
/*
static int _fd_available(long fd)
{
#ifndef _OS_WINDOWS_
fd_set set;
struct timeval tv = {0, 0};
FD_ZERO(&set);
FD_SET(fd, &set);
return (select(fd+1, &set, NULL, NULL, &tv)!=0);
#else
return 1;
#endif
}
*/
static int _enonfatal(int err)
{
return (err == EAGAIN ||/* err == EINPROGRESS ||*/ err == EINTR /*|| err == EWOULDBLOCK*/); //jwn
}
#define SLEEP_TIME 5//ms
#if defined(__APPLE__)
#define MAXSIZE ((1l << 31) - 1) // OSX cannot handle blocks larger than this
#define LIMIT_IO_SIZE(n) ((n) < MAXSIZE ? (n) : MAXSIZE)
#elif defined(_OS_WINDOWS_)
#define MAXSIZE (0x7fffffff) // Windows read() takes a uint
#define LIMIT_IO_SIZE(n) ((n) < (size_t)MAXSIZE ? (unsigned int)(n) : MAXSIZE)
#else
#define LIMIT_IO_SIZE(n) (n)
#endif
// return error code, #bytes read in *nread
// these wrappers retry operations until success or a fatal error
static int _os_read(long fd, void *buf, size_t n, size_t *nread)
{
ssize_t r;
n = LIMIT_IO_SIZE(n);
while (1) {
set_io_wait_begin(1);
r = read((int)fd, buf, n);
set_io_wait_begin(0);
if (r > -1) {
*nread = (size_t)r;
return 0;
}
// This test is a hack to fix #11481 for Windows 7. Unnecessary for Windows 10.
if (errno == ENOMEM && n > 80) {
n >>= 3;
continue;
}
if (!_enonfatal(errno)) {
*nread = 0;
return errno;
}
sleep_ms(SLEEP_TIME);
}
return 0;
}
static int _os_read_all(long fd, void *buf, size_t n, size_t *nread)
{
size_t got;
*nread = 0;
while (n>0) {
set_io_wait_begin(1);
int err = _os_read(fd, buf, n, &got);
set_io_wait_begin(0);
n -= got;
*nread += got;
buf = (char *)buf + got;
if (err || got==0)
return err;
}
return 0;
}
static int _os_write(long fd, const void *buf, size_t n, size_t *nwritten)
{
ssize_t r;
while (1) {
r = write((int)fd, buf, LIMIT_IO_SIZE(n));
if (r > -1) {
*nwritten = (size_t)r;
return 0;
}
if (!_enonfatal(errno)) {
*nwritten = 0;
return errno;
}
sleep_ms(SLEEP_TIME);
}
return 0;
}
int _os_write_all(long fd, const void *buf, size_t n, size_t *nwritten)
{
size_t wrote;
*nwritten = 0;
while (n>0) {
int err = _os_write(fd, buf, n, &wrote);
n -= wrote;
*nwritten += wrote;
buf = (char *)buf + wrote;
if (err)
return err;
}
return 0;
}
/* internal utility functions */
static char *_buf_realloc(ios_t *s, size_t sz)
{
char *temp;
if ((s->buf==NULL || s->buf==&s->local[0]) && (sz <= IOS_INLSIZE)) {
/* TODO: if we want to allow shrinking, see if the buffer shrank
down to this size, in which case we need to copy. */
s->buf = &s->local[0];
s->maxsize = IOS_INLSIZE;
s->ownbuf = 1;
return s->buf;
}
if (sz <= s->maxsize) return s->buf;
if (s->ownbuf && s->buf != &s->local[0]) {
// if we own the buffer we're free to resize it
temp = (char*)LLT_REALLOC(s->buf, sz);
if (temp == NULL)
return NULL;
}
else {
temp = (char*)LLT_ALLOC(sz);
if (temp == NULL)
return NULL;
s->ownbuf = 1;
if (s->size > 0)
memcpy(temp, s->buf, (size_t)s->size);
}
s->buf = temp;
s->maxsize = sz;
return s->buf;
}
// write a block of data into the buffer at the current position, resizing
// if necessary. returns # written.
static size_t _write_grow(ios_t *s, const char *data, size_t n)
{
size_t amt;
size_t newsize;
if (n == 0)
return 0;
if (s->bpos + n > s->size) {
if (s->bpos + n > s->maxsize) {
/* TODO: here you might want to add a mechanism for limiting
the growth of the stream. */
newsize = (size_t)(s->maxsize ? s->maxsize * 2 : 8);
while (s->bpos + n > newsize)
newsize *= 2;
if (_buf_realloc(s, newsize) == NULL) {
/* no more space; write as much as we can */
amt = (size_t)(s->maxsize - s->bpos);
if (amt > 0) {
memcpy(&s->buf[s->bpos], data, amt);
}
s->bpos += amt;
s->size = s->maxsize;
return amt;
}
}
s->size = s->bpos + n;
}
memcpy(s->buf + s->bpos, data, n);
s->bpos += n;
return n;
}
/* interface functions, low level */
static size_t _ios_read(ios_t *s, char *dest, size_t n, int all)
{
size_t tot = 0;
size_t got, avail;
int didread = 0;
if (s->state == bst_wr) {
ios_seek(s, ios_pos(s));
}
s->state = bst_rd;
while (n > 0) {
avail = (size_t)(s->size - s->bpos);
if (avail > 0) {
size_t ncopy = (avail >= n) ? n : avail;
memcpy(dest, s->buf + s->bpos, ncopy);
s->bpos += ncopy;
if (ncopy >= n)
return tot+ncopy;
}
if (s->bm == bm_mem || s->fd == -1) {
// can't get any more data
if (avail == 0)
s->_eof = 1;
return avail;
}
dest += avail;
n -= avail;
tot += avail;
if (didread && !all) return tot;
ios_flush(s);
s->bpos = s->size = 0;
s->fpos = -1;
if (n > MOST_OF(s->maxsize)) {
// doesn't fit comfortably in buffer; go direct
if (all) {
//result = _os_read_all(s->fd, dest, n, &got);
_os_read_all(s->fd, dest, n, &got);
}
else {
//result = _os_read(s->fd, dest, n, &got);
_os_read(s->fd, dest, n, &got);
}
tot += got;
if (got == 0)
s->_eof = 1;
return tot;
}
else {
// refill buffer
if (_os_read(s->fd, s->buf, (size_t)s->maxsize, &got)) {
s->_eof = 1;
return tot;
}
if (got == 0) {
s->_eof = 1;
return tot;
}
s->size = got;
}
didread = 1;
}
return tot;
}
size_t ios_read(ios_t *s, char *dest, size_t n)
{
return _ios_read(s, dest, n, 0);
}
size_t ios_readall(ios_t *s, char *dest, size_t n)
{
return _ios_read(s, dest, n, 1);
}
size_t ios_readprep(ios_t *s, size_t n)
{
if (s->state == bst_wr && s->bm != bm_mem) {
ios_flush(s);
s->bpos = s->size = 0;
}
size_t space = (size_t)(s->size - s->bpos);
s->state = bst_rd;
if (space >= n || s->bm == bm_mem || s->fd == -1)
return space;
if (s->maxsize < s->bpos+n) {
// it won't fit. grow buffer or move data back.
if (n <= s->maxsize && space <= ((s->maxsize)>>2)) {
if (space)
memmove(s->buf, s->buf+s->bpos, space);
s->size -= s->bpos;
s->bpos = 0;
}
else {
if (_buf_realloc(s, (size_t)(s->bpos + n))==NULL)
return space;
}
}
size_t got;
s->fpos = -1;
int result = _os_read(s->fd, s->buf+s->size, (size_t)(s->maxsize - s->size), &got);
if (result)
return space;
s->size += got;
return (size_t)(s->size - s->bpos);
}
// attempt to fill the buffer. returns the number of bytes available if we
// have read the whole file, or -1 if there might be more data.
ssize_t ios_fillbuf(ios_t *s)
{
size_t nb = s->maxsize - s->bpos;
size_t got = ios_readprep(s, nb);
if (got < nb)
return (ssize_t)got;
return -1;
}
static void _write_update_pos(ios_t *s)
{
if (s->bpos > s->ndirty) s->ndirty = s->bpos;
if (s->bpos > s->size) s->size = s->bpos;
}
// directly copy a buffer to a descriptor
JL_DLLEXPORT size_t ios_write_direct(ios_t *dest, ios_t *src)
{
char *data = src->buf;
size_t n = (size_t)src->size;
size_t nwr;
dest->fpos = -1;
_os_write_all(dest->fd, data, n, &nwr);
return nwr;
}
size_t ios_write(ios_t *s, const char *data, size_t n)
{
if (!s->writable) return 0;
if (n == 0) return 0;
size_t space;
size_t wrote = 0;
if (s->state == bst_rd) {
ios_seek(s, ios_pos(s));
}
s->state = bst_wr;
space = (size_t)(s->maxsize - s->bpos);
if (s->bm == bm_mem) {
wrote = _write_grow(s, data, n);
}
else if (s->bm == bm_none) {
s->fpos = -1;
_os_write_all(s->fd, data, n, &wrote);
return wrote;
}
else if (n <= space) {
if (s->bm == bm_line) {
char *nl;
if ((nl=(char*)memrchr(data, '\n', n)) != NULL) {
size_t linesz = nl-data+1;
s->bm = bm_block;
wrote += ios_write(s, data, linesz);
ios_flush(s);
s->bm = bm_line;
n -= linesz;
data += linesz;
}
}
memcpy(s->buf + s->bpos, data, n);
s->bpos += n;
wrote += n;
}
else {
ios_flush(s);
if (n > MOST_OF(s->maxsize)) {
s->fpos = -1;
_os_write_all(s->fd, data, n, &wrote);
return wrote;
}
return ios_write(s, data, n);
}
_write_update_pos(s);
return wrote;
}
// Returns 0 on success,
// -1 on error which set errno, and
// -2 on error which doesn't set errno.
int64_t ios_seek(ios_t *s, int64_t pos)
{
s->_eof = 0;
if (s->bm == bm_mem) {
if (pos < 0 || pos > s->size)
return -2;
s->bpos = pos;
}
else {
ios_flush(s);
int64_t fdpos = lseek(s->fd, (off_t)pos, SEEK_SET);
if (fdpos == (int64_t)-1)
return fdpos;
s->fpos = fdpos;
s->bpos = s->size = 0;
}
return 0;
}
int64_t ios_seek_end(ios_t *s)
{
s->_eof = 1;
if (s->bm == bm_mem) {
s->bpos = s->size;
}
else {
ios_flush(s);
int64_t fdpos = lseek(s->fd, 0, SEEK_END);
if (fdpos == (int64_t)-1)
return fdpos;
s->fpos = fdpos;
s->bpos = s->size = 0;
}
return 0;
}
// Returns 0 on success,
// -1 on error which set errno, and
// -2 on error which doesn't set errno.
int64_t ios_skip(ios_t *s, int64_t offs)
{
if (offs != 0) {
if (offs > 0) {
if (offs <= (s->size - s->bpos)) {
s->bpos += offs;
return 0;
}
else if (s->bm == bm_mem) {
// TODO: maybe grow buffer
return -2;
}
}
else if (offs < 0) {
if (-offs <= (int64_t)s->bpos) {
s->bpos += offs;
s->_eof = 0;
return 0;
}
else if (s->bm == bm_mem) {
return -2;
}
}
ios_flush(s);
if (s->state == bst_wr)
offs += s->bpos;
else if (s->state == bst_rd)
offs -= (s->size - s->bpos);
int64_t fdpos = lseek(s->fd, (off_t)offs, SEEK_CUR);
if (fdpos == (int64_t)-1)
return fdpos;
s->fpos = fdpos;
s->bpos = s->size = 0;
s->_eof = 0;
}
return 0;
}
int64_t ios_pos(ios_t *s)
{
if (s->bm == bm_mem)
return s->bpos;
int64_t fdpos = s->fpos;
if (fdpos == (int64_t)-1) {
fdpos = lseek(s->fd, 0, SEEK_CUR);
if (fdpos == (int64_t)-1)
return fdpos;
s->fpos = fdpos;
}
if (s->state == bst_wr)
fdpos += s->bpos;
else if (s->state == bst_rd)
fdpos -= (s->size - s->bpos);
return fdpos;
}
int64_t ios_filesize(ios_t *s)
{
int64_t fdpos = s->fpos;
if (fdpos == (int64_t)-1) {
fdpos = lseek(s->fd, 0, SEEK_CUR);
if (fdpos == (int64_t)-1)
return fdpos;
s->fpos = fdpos;
}
off_t sz = lseek(s->fd, 0, SEEK_END);
lseek(s->fd, (off_t)fdpos, SEEK_SET);
return sz;
}
int ios_trunc(ios_t *s, size_t size)
{
if (s->bm == bm_mem) {
if (size == s->size)
return 0;
if (size < s->size) {
if (s->bpos > size)
s->bpos = size;
}
else {
if (_buf_realloc(s, size)==NULL)
return 0;
}
s->size = size;
return 0;
}
else {
ios_flush(s);
if (s->state == bst_rd) {
int64_t p = ios_pos(s);
if (size < p + (s->size - s->bpos))
s->size -= (p + (s->size - s->bpos) - size);
}
#if !defined(_OS_WINDOWS_)
if (ftruncate(s->fd, size) == 0)
#else
if (_chsize_s(s->fd, size) == 0)
#endif
return 0;
}
return 1;
}
int ios_eof(ios_t *s)
{
if (s->state == bst_rd && s->bpos < s->size)
return 0;
if (s->bm == bm_mem)
return (s->_eof ? 1 : 0);
if (s->fd == -1)
return 1;
if (s->_eof)
return 1;
return 0;
/*
if (_fd_available(s->fd))
return 0;
s->_eof = 1;
return 1;
*/
}
int ios_eof_blocking(ios_t *s)
{
if (s->state == bst_rd && s->bpos < s->size)
return 0;
if (s->bm == bm_mem)
return (s->_eof ? 1 : 0);
if (s->fd == -1)
return 1;
if (ios_readprep(s, 1) < 1)
return 1;
return 0;
}
int ios_flush(ios_t *s)
{
if (s->ndirty == 0 || s->bm == bm_mem || s->buf == NULL)
return 0;
if (s->fd == -1)
return -1;
if (s->state == bst_rd) {
if (lseek(s->fd, -(off_t)s->size, SEEK_CUR) == (off_t)-1) {
}
}
size_t nw, ntowrite=s->ndirty;
s->fpos = -1;
int err = _os_write_all(s->fd, s->buf, ntowrite, &nw);
// todo: try recovering from some kinds of errors (e.g. retry)
if (s->state == bst_rd) {
if (lseek(s->fd, (off_t)(s->size - nw), SEEK_CUR) == (off_t)-1) {
}
}
else if (s->state == bst_wr) {
if (s->bpos != nw &&
lseek(s->fd, (off_t)(s->bpos - nw), SEEK_CUR) == (off_t)-1) {
}
// now preserve the invariant that data to write
// begins at the beginning of the buffer, and s->size refers
// to how much valid file data is stored in the buffer.
if (s->size > s->ndirty) {
size_t delta = (size_t)(s->size - s->ndirty);
memmove(s->buf, s->buf + s->ndirty, delta);
}
s->size -= s->ndirty;
s->bpos = 0;
}
s->ndirty = 0;
if (err)
return err;
if (nw < ntowrite)
return -1;
return 0;
}
int ios_close(ios_t *s)
{
int err = ios_flush(s);
if (s->fd != -1 && s->ownfd) {
int err2 = close(s->fd);
if (err2 != 0)
err = err2;
}
s->fd = -1;
if (s->buf!=NULL && s->ownbuf && s->buf!=&s->local[0]) {
LLT_FREE(s->buf);
}
s->buf = NULL;
s->size = s->maxsize = s->bpos = 0;
return err;
}
int ios_isopen(ios_t *s)
{
return s->fd != -1;
}
static void _buf_init(ios_t *s, bufmode_t bm)
{
s->bm = bm;
if (s->bm == bm_mem || s->bm == bm_none) {
s->buf = &s->local[0];
s->maxsize = IOS_INLSIZE;
}
else {
s->buf = NULL;
s->maxsize = 0;
_buf_realloc(s, IOS_BUFSIZE);
}
s->size = s->bpos = 0;
}
char *ios_take_buffer(ios_t *s, size_t *psize)
{
char *buf;
ios_flush(s);
if (s->buf == &s->local[0] || s->buf == NULL || (!s->ownbuf && s->size == s->maxsize)) {
buf = (char*)LLT_ALLOC((size_t)s->size + 1);
if (buf == NULL)
return NULL;
if (s->size)
memcpy(buf, s->buf, (size_t)s->size);
}
else if (s->size == s->maxsize) {
buf = (char*)LLT_REALLOC(s->buf, (size_t)s->size + 1);
if (buf == NULL)
return NULL;
}
else {
buf = s->buf;
}
buf[s->size] = '\0';
*psize = s->size + 1;
/* empty stream and reinitialize */
_buf_init(s, s->bm);
return buf;
}
int ios_setbuf(ios_t *s, char *buf, size_t size, int own)
{
ios_flush(s);
size_t nvalid=0;
nvalid = (size_t)((size < s->size) ? size : s->size);
if (nvalid > 0)
memcpy(buf, s->buf, nvalid);
if (s->bpos > nvalid) {
// truncated
s->bpos = nvalid;
}
s->size = nvalid;
if (s->buf!=NULL && s->ownbuf && s->buf!=&s->local[0]) {
LLT_FREE(s->buf);
}
s->buf = buf;
s->maxsize = size;
s->ownbuf = own;
return 0;
}
int ios_bufmode(ios_t *s, bufmode_t mode)
{
// no fd; can only do mem-only buffering
if (s->fd == -1 && mode != bm_mem)
return -1;
s->bm = mode;
return 0;
}
int ios_get_readable(ios_t *s)
{
return s->readable;
}
int ios_get_writable(ios_t *s)
{
return s->writable;
}
void ios_set_readonly(ios_t *s)
{
if (!s->writable) return;
ios_flush(s);
s->state = bst_none;
s->writable = 0;
}
static size_t ios_copy_(ios_t *to, ios_t *from, size_t nbytes, bool_t all)
{
size_t total = 0, avail;
if (!ios_eof(from)) {
do {
avail = ios_readprep(from, IOS_BUFSIZE/2);
if (avail == 0) {
from->_eof = 1;
break;
}
size_t written, ntowrite;
ntowrite = (avail <= nbytes || all) ? avail : nbytes;
written = ios_write(to, from->buf+from->bpos, ntowrite);
// TODO: should this be +=written instead?
from->bpos += ntowrite;
total += written;
if (!all) {
nbytes -= written;
if (nbytes == 0)
break;
}
if (written < ntowrite)
break;
} while (!ios_eof(from));
}
return total;
}
size_t ios_copy(ios_t *to, ios_t *from, size_t nbytes)
{
return ios_copy_(to, from, nbytes, 0);
}
size_t ios_copyall(ios_t *to, ios_t *from)
{
return ios_copy_(to, from, 0, 1);
}
#define LINE_CHUNK_SIZE 160
size_t ios_copyuntil(ios_t *to, ios_t *from, char delim)
{
size_t total = 0, avail = (size_t)(from->size - from->bpos);
while (!ios_eof(from)) {
if (avail == 0) {
avail = ios_readprep(from, LINE_CHUNK_SIZE);
if (avail == 0)
break;
}
size_t written;
char *pd = (char*)memchr(from->buf+from->bpos, delim, avail);
if (pd == NULL) {
written = ios_write(to, from->buf+from->bpos, avail);
from->bpos += avail;
total += written;
avail = 0;
}
else {
size_t ntowrite = pd - (from->buf+from->bpos) + 1;
written = ios_write(to, from->buf+from->bpos, ntowrite);
from->bpos += ntowrite;
total += written;
return total;
}
}
from->_eof = 1;
return total;
}
size_t ios_nchomp(ios_t *from, size_t ntowrite)
{
assert(ntowrite > 0);
size_t nchomp;
if (ntowrite > 1 && from->buf[from->bpos+ntowrite - 2] == '\r') {
nchomp = 2;
}
else {
nchomp = 1;
}
return nchomp;
}
static void _ios_init(ios_t *s)
{
// put all fields in a sane initial state
s->bm = bm_block;
s->state = bst_none;
s->errcode = 0;
s->buf = NULL;
s->maxsize = 0;
s->size = 0;
s->bpos = 0;
s->ndirty = 0;
s->fpos = -1;
s->lineno = 1;
s->u_colno = 0;
s->fd = -1;
s->ownbuf = 1;
s->ownfd = 0;
s->_eof = 0;
s->readable = 1;
s->writable = 1;
s->rereadable = 0;
}
/* stream object initializers. we do no allocation. */
#if !defined(_OS_WINDOWS_)
/*
* NOTE: we do not handle system call restart in this function,
* please do it manually:
*
* do
* open_cloexec(...)
* while (-1 == fd && _enonfatal(errno))
*/
static int open_cloexec(const char *path, int flags, mode_t mode)
{
#ifdef O_CLOEXEC
static int no_cloexec = 0;
if (!no_cloexec) {
set_io_wait_begin(1);
int fd = open(path, flags | O_CLOEXEC, mode);
set_io_wait_begin(0);
if (fd != -1)
return fd;
if (errno != EINVAL)
return -1;
/* O_CLOEXEC not supported. */
no_cloexec = 1;
}
#endif
set_io_wait_begin(1);
int fd = open(path, flags, mode);
set_io_wait_begin(0);
return fd;
}
#endif
ios_t *ios_file(ios_t *s, const char *fname, int rd, int wr, int create, int trunc)
{
int flags;
int fd;
if (!(rd || wr))
// must specify read and/or write
goto open_file_err;
flags = wr ? (rd ? O_RDWR : O_WRONLY) : O_RDONLY;
if (create) flags |= O_CREAT;
if (trunc) flags |= O_TRUNC;
#if defined(_OS_WINDOWS_)
size_t wlen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
if (!wlen) goto open_file_err;
wchar_t *fname_w = (wchar_t*)alloca(wlen*sizeof(wchar_t));
if (!MultiByteToWideChar(CP_UTF8, 0, fname, -1, fname_w, wlen)) goto open_file_err;
set_io_wait_begin(1);
fd = _wopen(fname_w, flags | O_BINARY | O_NOINHERIT, _S_IREAD | _S_IWRITE);
set_io_wait_begin(0);
#else
// The mode of the created file is (mode & ~umask), which resolves with
// default umask to u=rw,g=r,o=r
do
fd = open_cloexec(fname, flags,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
while (-1 == fd && _enonfatal(errno));
#endif
if (fd == -1)
goto open_file_err;
s = ios_fd(s, fd, 1, 1);
s->fpos = 0;
if (!rd)
s->readable = 0;
if (!wr)
s->writable = 0;
return s;
open_file_err:
s->fd = -1;
return NULL;
}
// Portable ios analogue of mkstemp: modifies fname to replace
// trailing XXXX's with unique ID and returns the file handle s
// for writing and reading.
ios_t *ios_mkstemp(ios_t *s, char *fname)
{
int fd;
// would be better to use a libuv function once it exists (see libuv/libuv#322)
#ifdef _OS_WINDOWS_
size_t wlen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
if (!wlen) goto open_file_err;
wchar_t *fname_w = (wchar_t*)alloca(wlen*sizeof(wchar_t));
if (!MultiByteToWideChar(CP_UTF8, 0, fname, -1, fname_w, wlen) ||
!_wmktemp(fname_w) ||
!WideCharToMultiByte(CP_UTF8, 0, fname_w, -1, fname, strlen(fname)+1,
NULL, NULL))
goto open_file_err;
fd = _wopen(fname_w, O_CREAT|O_TRUNC|O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD | _S_IWRITE);
#else
fd = mkstemp(fname);
#endif
ios_fd(s, fd, 1, 1);
if (fd == -1)
goto open_file_err;
return s;
open_file_err:
s->fd = -1;