/
efile_drv.c
3242 lines (2932 loc) · 82.8 KB
/
efile_drv.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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2010. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Purpose: Provides file and directory operations.
*
* This file is generic, and does the work of decoding the commands
* and encoding the responses. System-specific functions are found in
* the unix_efile.c and win_efile.c files.
*/
/* Operations */
#define FILE_OPEN 1 /* Essential for startup */
#define FILE_READ 2
#define FILE_LSEEK 3
#define FILE_WRITE 4
#define FILE_FSTAT 5 /* Essential for startup */
#define FILE_PWD 6 /* Essential for startup */
#define FILE_READDIR 7 /* Essential for startup */
#define FILE_CHDIR 8
#define FILE_FSYNC 9
#define FILE_MKDIR 10
#define FILE_DELETE 11
#define FILE_RENAME 12
#define FILE_RMDIR 13
#define FILE_TRUNCATE 14
#define FILE_READ_FILE 15 /* Essential for startup */
#define FILE_WRITE_INFO 16
#define FILE_LSTAT 19
#define FILE_READLINK 20
#define FILE_LINK 21
#define FILE_SYMLINK 22
#define FILE_CLOSE 23
#define FILE_PWRITEV 24
#define FILE_PREADV 25
#define FILE_SETOPT 26
#define FILE_IPREAD 27
#define FILE_ALTNAME 28
#define FILE_READ_LINE 29
#define FILE_FDATASYNC 30
#define FILE_FADVISE 31
/* Return codes */
#define FILE_RESP_OK 0
#define FILE_RESP_ERROR 1
#define FILE_RESP_DATA 2
#define FILE_RESP_NUMBER 3
#define FILE_RESP_INFO 4
#define FILE_RESP_NUMERR 5
#define FILE_RESP_LDATA 6
#define FILE_RESP_N2DATA 7
#define FILE_RESP_EOF 8
#define FILE_RESP_FNAME 9
#define FILE_RESP_ALL_DATA 10
/* Options */
#define FILE_OPT_DELAYED_WRITE 0
#define FILE_OPT_READ_AHEAD 1
/* IPREAD variants */
#define IPREAD_S32BU_P32BU 0
/* Limits */
#define FILE_SEGMENT_READ (256*1024)
#define FILE_SEGMENT_WRITE (256*1024)
/* Internal */
/* Set to 1 to test having read_ahead implicitly for read_line */
#define ALWAYS_READ_LINE_AHEAD 0
/* Must not be possible to get from malloc()! */
#define FILE_FD_INVALID ((Sint)(-1))
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include "sys.h"
#include "erl_driver.h"
#include "erl_efile.h"
#include "erl_threads.h"
#include "zlib.h"
#include "gzio.h"
#include <ctype.h>
#include <sys/types.h>
void erl_exit(int n, char *fmt, ...);
static ErlDrvSysInfo sys_info;
/* #define TRACE 1 */
#ifdef TRACE
# define TRACE_C(c) do { putchar(c); fflush(stdout); } while (0)
# define TRACE_S(s) do { fputs((s), stdout); fflush(stdout); } while (0)
# define TRACE_F(args) do { printf args ;fflush(stdout); } while (0)
#else
# define TRACE_C(c) ((void)(0))
# define TRACE_S(s) ((void)(0))
# define TRACE_F(args) ((void)(0))
#endif
#ifdef USE_THREADS
#define IF_THRDS if (sys_info.async_threads > 0)
#ifdef HARDDEBUG /* HARDDEBUG in io.c is expected too */
#define TRACE_DRIVER fprintf(stderr, "Efile: ")
#else
#define TRACE_DRIVER
#endif
#define MUTEX_INIT(m, p) do { IF_THRDS { TRACE_DRIVER; (m = driver_pdl_create(p)); } } while (0)
#define MUTEX_LOCK(m) do { IF_THRDS { TRACE_DRIVER; driver_pdl_lock(m); } } while (0)
#define MUTEX_UNLOCK(m) do { IF_THRDS { TRACE_DRIVER; driver_pdl_unlock(m); } } while (0)
#else
#define MUTEX_INIT(m, p)
#define MUTEX_LOCK(m)
#define MUTEX_UNLOCK(m)
#endif
#if 0
/* Experimental, for forcing all file operations to use the same thread. */
static unsigned file_fixed_key = 1;
# define KEY(desc) (&file_fixed_key)
#else
# define KEY(desc) (&(desc)->key)
#endif
#ifdef FILENAMES_16BIT
# define FILENAME_BYTELEN(Str) filename_len_16bit(Str)
# define FILENAME_COPY(To,From) filename_cpy_16bit((To),(From))
# define FILENAME_CHARSIZE 2
static int filename_len_16bit(char *str)
{
char *p = str;
while(*p != '\0' || p[1] != '\0') {
p += 2;
}
return (p - str);
}
static void filename_cpy_16bit(char *to, char *from)
{
while(*from != '\0' || from[1] != '\0') {
*to++ = *from++;
*to++ = *from++;
}
*to++ = *from++;
*to++ = *from++;
}
#else
# define FILENAME_BYTELEN(Str) strlen(Str)
# define FILENAME_COPY(To,From) strcpy(To,From)
# define FILENAME_CHARSIZE 1
#endif
#if (MAXPATHLEN+1)*FILENAME_CHARSIZE+1 > BUFSIZ
# define RESBUFSIZE ((MAXPATHLEN+1)*FILENAME_CHARSIZE+1)
#else
# define RESBUFSIZE BUFSIZ
#endif
#define GET_TIME(i, b) \
(i).year = get_int32((b) + 0 * 4); \
(i).month = get_int32((b) + 1 * 4); \
(i).day = get_int32((b) + 2 * 4); \
(i).hour = get_int32((b) + 3 * 4); \
(i).minute = get_int32((b) + 4 * 4); \
(i).second = get_int32((b) + 5 * 4)
#define PUT_TIME(i, b) \
put_int32((i).year, (b) + 0 * 4); \
put_int32((i).month, (b) + 1 * 4); \
put_int32((i).day, (b) + 2 * 4); \
put_int32((i).hour, (b) + 3 * 4); \
put_int32((i).minute,(b) + 4 * 4); \
put_int32((i).second,(b) + 5 * 4)
#if ALWAYS_READ_LINE_AHEAD
#define DEFAULT_LINEBUF_SIZE 2048
#else
#define DEFAULT_LINEBUF_SIZE 512 /* Small, it's usually discarded anyway */
#endif
typedef unsigned char uchar;
static ErlDrvData file_start(ErlDrvPort port, char* command);
static int file_init(void);
static void file_stop(ErlDrvData);
static void file_output(ErlDrvData, char* buf, int len);
static int file_control(ErlDrvData, unsigned int command,
char* buf, int len, char **rbuf, int rlen);
static void file_timeout(ErlDrvData);
static void file_outputv(ErlDrvData, ErlIOVec*);
static void file_async_ready(ErlDrvData, ErlDrvThreadData);
static void file_flush(ErlDrvData);
enum e_timer {timer_idle, timer_again, timer_write};
struct t_data;
typedef struct {
SWord fd;
ErlDrvPort port;
unsigned int key; /* Async queue key */
unsigned flags; /* Original flags from FILE_OPEN. */
void (*invoke)(void *);
struct t_data *d;
void (*free)(void *);
struct t_data *cq_head; /* Queue of incoming commands */
struct t_data *cq_tail; /* -""- */
enum e_timer timer_state;
size_t read_bufsize;
ErlDrvBinary *read_binp;
size_t read_offset;
size_t read_size;
size_t write_bufsize;
unsigned long write_delay;
int write_error;
Efile_error write_errInfo;
ErlDrvPDL q_mtx; /* Mutex for the driver queue, known by the emulator. Also used for
mutual exclusion when accessing field(s) below. */
size_t write_buffered;
} file_descriptor;
static int reply_error(file_descriptor*, Efile_error* errInfo);
struct erl_drv_entry efile_driver_entry = {
file_init,
file_start,
file_stop,
file_output,
NULL,
NULL,
"efile",
NULL,
NULL,
file_control,
file_timeout,
file_outputv,
file_async_ready,
file_flush,
NULL,
NULL,
ERL_DRV_EXTENDED_MARKER,
ERL_DRV_EXTENDED_MAJOR_VERSION,
ERL_DRV_EXTENDED_MINOR_VERSION,
ERL_DRV_FLAG_USE_PORT_LOCKING,
NULL
};
static int thread_short_circuit;
#define DRIVER_ASYNC(level, desc, f_invoke, data, f_free) \
if (thread_short_circuit >= (level)) { \
(*(f_invoke))(data); \
file_async_ready((ErlDrvData)(desc), (data)); \
} else { \
driver_async((desc)->port, KEY(desc), (f_invoke), (data), (f_free)); \
}
struct t_pbuf_spec {
Sint64 offset;
size_t size;
};
struct t_pwritev {
ErlDrvPort port;
ErlDrvPDL q_mtx;
size_t size;
size_t free_size;
unsigned cnt;
unsigned n;
struct t_pbuf_spec specs[1];
};
struct t_preadv {
ErlIOVec eiov;
unsigned n;
unsigned cnt;
size_t size;
Sint64 offsets[1];
};
#define READDIR_BUFSIZE (8*1024)
#if READDIR_BUFSIZE < (FILENAME_CHARSIZE*2*(MAXPATHLEN+1))
# undef READDIR_BUFSIZE
# define READDIR_BUFSIZE (FILENAME_CHARSIZE*2*(MAXPATHLEN+1))
#endif
struct t_readdir_buf {
struct t_readdir_buf *next;
char buf[READDIR_BUFSIZE];
};
struct t_data
{
struct t_data *next;
int command;
int level;
void (*invoke)(void *);
void (*free)(void *);
int again;
int reply;
int result_ok;
Efile_error errInfo;
int flags;
SWord fd;
/**/
Efile_info info;
EFILE_DIR_HANDLE dir_handle; /* Handle to open directory. */
ErlDrvBinary *bin;
int drive;
size_t n;
/*off_t offset;*/
/*size_t bytesRead; Bytes read from the file. */
/**/
union {
struct {
Sint64 offset;
int origin;
Sint64 location;
} lseek;
struct {
ErlDrvPort port;
ErlDrvPDL q_mtx;
size_t size;
size_t free_size;
size_t reply_size;
} writev;
struct t_pwritev pwritev;
struct t_preadv preadv;
struct {
ErlDrvBinary *binp;
size_t bin_offset;
size_t bin_size;
size_t size;
} read;
struct {
ErlDrvBinary *binp; /* in - out */
size_t read_offset; /* in - out */
size_t read_size; /* in - out */
size_t nl_pos; /* out */
short nl_skip; /* out, 0 or 1 */
#if !ALWAYS_READ_LINE_AHEAD
short read_ahead; /* in, bool */
#endif
} read_line;
struct {
ErlDrvBinary *binp;
int size;
int offset;
} read_file;
struct {
struct t_readdir_buf *first_buf;
struct t_readdir_buf *last_buf;
} read_dir;
struct {
Sint64 offset;
Sint64 length;
int advise;
} fadvise;
} c;
char b[1];
};
#define EF_ALLOC(S) driver_alloc((S))
#define EF_REALLOC(P, S) driver_realloc((P), (S))
#define EF_SAFE_ALLOC(S) ef_safe_alloc((S))
#define EF_SAFE_REALLOC(P, S) ef_safe_realloc((P), (S))
#define EF_FREE(P) do { if((P)) driver_free((P)); } while(0)
static void *ef_safe_alloc(Uint s)
{
void *p = EF_ALLOC(s);
if (!p) erl_exit(1, "efile drv: Can't allocate %d bytes of memory\n", s);
return p;
}
#if 0 /* Currently not used */
static void *ef_safe_realloc(void *op, Uint s)
{
void *p = EF_REALLOC(op, s);
if (!p) erl_exit(1, "efile drv: Can't reallocate %d bytes of memory\n", s);
return p;
}
#endif
/*********************************************************************
* ErlIOVec manipulation functions.
*/
/* char EV_CHAR(ErlIOVec *ev, int p, int q) */
#define EV_CHAR_P(ev, p, q) \
(((char *)(ev)->iov[(q)].iov_base) + (p))
/* int EV_GET_CHAR(ErlIOVec *ev, char *p, int *pp, int *qp) */
#define EV_GET_CHAR(ev, p, pp, qp) \
(*(pp)+1 <= (ev)->iov[*(qp)].iov_len \
? (*(p) = *EV_CHAR_P(ev, *(pp), *(qp)), \
*(pp) = ( *(pp)+1 < (ev)->iov[*(qp)].iov_len \
? *(pp)+1 \
: ((*(qp))++, 0)), \
!0) \
: 0)
/* Uint32 EV_UINT32(ErlIOVec *ev, int p, int q)*/
#define EV_UINT32(ev, p, q) \
((Uint32) *(((unsigned char *)(ev)->iov[(q)].iov_base) + (p)))
/* int EV_GET_UINT32(ErlIOVec *ev, Uint32 *p, int *pp, int *qp) */
#define EV_GET_UINT32(ev, p, pp, qp) \
(*(pp)+4 <= (ev)->iov[*(qp)].iov_len \
? (*(p) = (EV_UINT32(ev, *(pp), *(qp)) << 24) \
| (EV_UINT32(ev, *(pp)+1, *(qp)) << 16) \
| (EV_UINT32(ev, *(pp)+2, *(qp)) << 8) \
| (EV_UINT32(ev, *(pp)+3, *(qp))), \
*(pp) = ( *(pp)+4 < (ev)->iov[*(qp)].iov_len \
? *(pp)+4 \
: ((*(qp))++, 0)), \
!0) \
: 0)
/* Uint64 EV_UINT64(ErlIOVec *ev, int p, int q)*/
#define EV_UINT64(ev, p, q) \
((Uint64) *(((unsigned char *)(ev)->iov[(q)].iov_base) + (p)))
/* int EV_GET_UINT64(ErlIOVec *ev, Uint32 *p, int *pp, int *qp) */
#define EV_GET_UINT64(ev, p, pp, qp) \
(*(pp)+8 <= (ev)->iov[*(qp)].iov_len \
? (*(p) = (EV_UINT64(ev, *(pp), *(qp)) << 56) \
| (EV_UINT64(ev, *(pp)+1, *(qp)) << 48) \
| (EV_UINT64(ev, *(pp)+2, *(qp)) << 40) \
| (EV_UINT64(ev, *(pp)+3, *(qp)) << 32) \
| (EV_UINT64(ev, *(pp)+4, *(qp)) << 24) \
| (EV_UINT64(ev, *(pp)+5, *(qp)) << 16) \
| (EV_UINT64(ev, *(pp)+6, *(qp)) << 8) \
| (EV_UINT64(ev, *(pp)+7, *(qp))), \
*(pp) = ( *(pp)+8 < (ev)->iov[*(qp)].iov_len \
? *(pp)+8 \
: ((*(qp))++, 0)), \
!0) \
: 0)
#if 0
static void ev_clear(ErlIOVec *ev) {
ASSERT(ev);
ev->size = 0;
ev->vsize = 0;
ev->iov = NULL;
ev->binv = NULL;
}
/* Assumes that ->iov and ->binv were allocated with sys_alloc().
*/
static void ev_free(ErlIOVec *ev) {
if (! ev) {
return;
}
if (ev->vsize > 0) {
int i;
ASSERT(ev->iov);
ASSERT(ev->binv);
for (i = 0; i < ev->vsize; i++) {
if (ev->binv[i]) {
driver_free_binary(ev->binv[i]);
}
}
EF_FREE(ev->iov);
EF_FREE(ev->binv);
}
}
/* Copy the contents from source to dest.
* Data in binaries is not copied, just the pointers;
* and refc is incremented.
*/
static ErlIOVec *ev_copy(ErlIOVec *dest, ErlIOVec *source) {
int *ip;
ASSERT(dest);
ASSERT(source);
if (source->vsize == 0) {
/* Empty source */
ev_clear(dest);
return dest;
}
/* Allocate ->iov and ->binv */
dest->iov = EF_ALLOC(sizeof(*dest->iov) * source->vsize);
if (! dest->iov) {
return NULL;
}
dest->binv = EF_ALLOC(sizeof(*dest->binv) * source->vsize);
if (! dest->binv) {
EF_FREE(dest->iov);
return NULL;
}
dest->size = source->size;
/* Copy one vector element at the time.
* Use *ip as an alias for dest->vsize to improve readabiliy.
* Keep dest consistent in every iteration by using
* dest->vsize==*ip as loop variable.
*/
for (ip = &dest->vsize, *ip = 0; *ip < source->vsize; (*ip)++) {
if (source->iov[*ip].iov_len == 0) {
/* Empty vector element */
dest->iov[*ip].iov_len = 0;
dest->iov[*ip].iov_base = NULL;
dest->binv[*ip] = NULL;
} else {
/* Non empty vector element */
if (source->binv[*ip]) {
/* Contents in binary - copy pointers and increment refc */
dest->iov[*ip] = source->iov[*ip];
dest->binv[*ip] = source->binv[*ip];
driver_binary_inc_refc(source->binv[*ip]);
} else {
/* Contents not in binary - allocate new binary and copy data */
if (! (dest->binv[*ip] =
driver_alloc_binary(source->iov[*ip].iov_len))) {
goto failed;
}
sys_memcpy(dest->binv[*ip]->orig_bytes,
source->iov[*ip].iov_base,
source->iov[*ip].iov_len);
dest->iov[*ip].iov_base = dest->binv[*ip]->orig_bytes;
dest->iov[*ip].iov_len = source->iov[*ip].iov_len;
}
}
}
return dest;
failed:
ev_free(dest);
return NULL;
}
#endif
/*********************************************************************
* Command queue functions
*/
static void cq_enq(file_descriptor *desc, struct t_data *d) {
ASSERT(d);
if (desc->cq_head) {
ASSERT(desc->cq_tail);
ASSERT(!desc->cq_tail->next);
desc->cq_tail = desc->cq_tail->next = d;
} else {
ASSERT(desc->cq_tail == NULL);
desc->cq_head = desc->cq_tail = d;
}
d->next = NULL;
}
static struct t_data *cq_deq(file_descriptor *desc) {
struct t_data *d = desc->cq_head;
ASSERT(d || (!d && !desc->cq_tail));
if (d) {
ASSERT(!d->next || (d->next && desc->cq_tail != d));
if ((desc->cq_head = d->next) == NULL) {
ASSERT(desc->cq_tail == d);
desc->cq_tail = NULL;
}
}
return d;
}
/*********************************************************************
* Driver entry point -> init
*/
static int
file_init(void)
{
char buf[21]; /* enough to hold any 64-bit integer */
size_t bufsz = sizeof(buf);
thread_short_circuit = (erl_drv_getenv("ERL_EFILE_THREAD_SHORT_CIRCUIT",
buf,
&bufsz) == 0
? atoi(buf)
: 0);
driver_system_info(&sys_info, sizeof(ErlDrvSysInfo));
return 0;
}
/*********************************************************************
* Driver entry point -> start
*/
static ErlDrvData
file_start(ErlDrvPort port, char* command)
{
file_descriptor* desc;
if ((desc = (file_descriptor*) EF_ALLOC(sizeof(file_descriptor)))
== NULL) {
errno = ENOMEM;
return ERL_DRV_ERROR_ERRNO;
}
desc->fd = FILE_FD_INVALID;
desc->port = port;
desc->key = (unsigned int) (UWord) port;
desc->flags = 0;
desc->invoke = NULL;
desc->d = NULL;
desc->free = NULL;
desc->cq_head = NULL;
desc->cq_tail = NULL;
desc->timer_state = timer_idle;
desc->read_bufsize = 0;
desc->read_binp = NULL;
desc->read_offset = 0;
desc->read_size = 0;
desc->write_delay = 0L;
desc->write_bufsize = 0;
desc->write_error = 0;
MUTEX_INIT(desc->q_mtx, port); /* Refc is one, referenced by emulator now */
desc->write_buffered = 0;
return (ErlDrvData) desc;
}
static void free_data(void *data)
{
EF_FREE(data);
}
static void do_close(int flags, SWord fd) {
if (flags & EFILE_COMPRESSED) {
erts_gzclose((gzFile)(fd));
} else {
efile_closefile((int) fd);
}
}
static void invoke_close(void *data)
{
struct t_data *d = (struct t_data *) data;
d->again = 0;
do_close(d->flags, d->fd);
}
/*********************************************************************
* Driver entry point -> stop
*/
static void
file_stop(ErlDrvData e)
{
file_descriptor* desc = (file_descriptor*)e;
TRACE_C('p');
if (desc->fd != FILE_FD_INVALID) {
do_close(desc->flags, desc->fd);
desc->fd = FILE_FD_INVALID;
desc->flags = 0;
}
if (desc->read_binp) {
driver_free_binary(desc->read_binp);
}
EF_FREE(desc);
}
/*
* Sends back an error reply to Erlang.
*/
static void reply_posix_error(file_descriptor *desc, int posix_errno) {
char response[256]; /* Response buffer. */
char* s;
char* t;
/*
* Contents of buffer sent back:
*
* +-----------------------------------------+
* | FILE_RESP_ERROR | Posix error id string |
* +-----------------------------------------+
*/
TRACE_C('E');
response[0] = FILE_RESP_ERROR;
for (s = erl_errno_id(posix_errno), t = response+1; *s; s++, t++)
*t = tolower(*s);
driver_output2(desc->port, response, t-response, NULL, 0);
}
static void reply_Uint_posix_error(file_descriptor *desc, Uint num,
int posix_errno) {
char response[256]; /* Response buffer. */
char* s;
char* t;
/*
* Contents of buffer sent back:
*
* +----------------------------------------------------------------------+
* | FILE_RESP_NUMERR | 64-bit number (big-endian) | Posix error id string |
* +----------------------------------------------------------------------+
*/
TRACE_C('N');
response[0] = FILE_RESP_NUMERR;
#if SIZEOF_VOID_P == 4 || HALFWORD_HEAP
put_int32(0, response+1);
#else
put_int32(num>>32, response+1);
#endif
put_int32((Uint32)num, response+1+4);
for (s = erl_errno_id(posix_errno), t = response+1+4+4; *s; s++, t++)
*t = tolower(*s);
driver_output2(desc->port, response, t-response, NULL, 0);
}
static int reply_error(file_descriptor *desc,
Efile_error *errInfo) /* The error codes. */
{
reply_posix_error(desc, errInfo->posix_errno);
return 0;
}
static int reply_Uint_error(file_descriptor *desc, Uint num,
Efile_error *errInfo) /* The error codes. */
{
reply_Uint_posix_error(desc, num, errInfo->posix_errno);
return 0;
}
static int reply_ok(file_descriptor *desc) {
char c = FILE_RESP_OK;
driver_output2(desc->port, &c, 1, NULL, 0);
return 0;
}
static int reply(file_descriptor *desc, int ok, Efile_error *errInfo) {
if (!ok) {
reply_error(desc, errInfo);
} else {
TRACE_C('K');
reply_ok(desc);
}
return 0;
}
static int reply_Uint(file_descriptor *desc, Uint result) {
char tmp[1+4+4];
/*
* Contents of buffer sent back:
*
* +-----------------------------------------------+
* | FILE_RESP_NUMBER | 64-bit number (big-endian) |
* +-----------------------------------------------+
*/
TRACE_C('R');
tmp[0] = FILE_RESP_NUMBER;
#if SIZEOF_VOID_P == 4 || HALFWORD_HEAP
put_int32(0, tmp+1);
#else
put_int32(result>>32, tmp+1);
#endif
put_int32((Uint32)result, tmp+1+4);
driver_output2(desc->port, tmp, sizeof(tmp), NULL, 0);
return 0;
}
static int reply_Sint64(file_descriptor *desc, Sint64 result) {
char tmp[1+4+4];
/*
* Contents of buffer sent back:
*
* +-----------------------------------------------+
* | FILE_RESP_NUMBER | 64-bit number (big-endian) |
* +-----------------------------------------------+
*/
TRACE_C('R');
tmp[0] = FILE_RESP_NUMBER;
put_int64(result, tmp+1);
driver_output2(desc->port, tmp, sizeof(tmp), NULL, 0);
return 0;
}
#if 0
static void reply_again(file_descriptor *desc) {
char tmp[1];
tmp[0] = FILE_RESP_AGAIN;
driver_output2(desc->port, tmp, sizeof(tmp), NULL, 0);
}
#endif
static void reply_ev(file_descriptor *desc, char response, ErlIOVec *ev) {
char tmp[1];
/* Data arriving at the Erlang process:
* [Response, Binary0, Binary1, .... | BinaryN-1]
*/
tmp[0] = response;
driver_outputv(desc->port, tmp, sizeof(tmp), ev, 0);
}
static void reply_data(file_descriptor *desc,
ErlDrvBinary *binp, size_t offset, size_t len) {
char header[1+4+4];
/* Data arriving at the Erlang process:
* [?FILE_RESP_DATA, 64-bit length (big-endian) | Data]
*/
header[0] = FILE_RESP_DATA;
#if SIZEOF_SIZE_T == 4
put_int32(0, header+1);
#else
put_int32(len>>32, header+1);
#endif
put_int32((Uint32)len, header+1+4);
driver_output_binary(desc->port, header, sizeof(header),
binp, offset, len);
}
static void reply_buf(file_descriptor *desc, char *buf, size_t len) {
char header[1+4+4];
/* Data arriving at the Erlang process:
* [?FILE_RESP_DATA, 64-bit length (big-endian) | Data]
*/
header[0] = FILE_RESP_DATA;
#if SIZEOF_SIZE_T == 4
put_int32(0, header+1);
#else
put_int32(len>>32, header+1);
#endif
put_int32((Uint32)len, header+1+4);
driver_output2(desc->port, header, sizeof(header), buf, len);
}
static int reply_eof(file_descriptor *desc) {
char c = FILE_RESP_EOF;
driver_output2(desc->port, &c, 1, NULL, 0);
return 0;
}
static void invoke_name(void *data, int (*f)(Efile_error *, char *))
{
struct t_data *d = (struct t_data *) data;
char *name = (char *) d->b;
d->again = 0;
d->result_ok = (*f)(&d->errInfo, name);
}
static void invoke_mkdir(void *data)
{
invoke_name(data, efile_mkdir);
}
static void invoke_rmdir(void *data)
{
invoke_name(data, efile_rmdir);
}
static void invoke_delete_file(void *data)
{
invoke_name(data, efile_delete_file);
}
static void invoke_chdir(void *data)
{
invoke_name(data, efile_chdir);
}
static void invoke_fdatasync(void *data)
{
struct t_data *d = (struct t_data *) data;
int fd = (int) d->fd;
d->again = 0;
d->result_ok = efile_fdatasync(&d->errInfo, fd);
}
static void invoke_fsync(void *data)
{
struct t_data *d = (struct t_data *) data;
int fd = (int) d->fd;
d->again = 0;
d->result_ok = efile_fsync(&d->errInfo, fd);
}
static void invoke_truncate(void *data)
{
struct t_data *d = (struct t_data *) data;
int fd = (int) d->fd;
d->again = 0;
d->result_ok = efile_truncate_file(&d->errInfo, &fd, d->flags);
}
static void invoke_read(void *data)
{
struct t_data *d = (struct t_data *) data;
int status, segment;
size_t size, read_size;
segment = d->again && d->c.read.bin_size >= 2*FILE_SEGMENT_READ;
if (segment) {
size = FILE_SEGMENT_READ;
} else {
size = d->c.read.bin_size;
}
read_size = size;
if (d->flags & EFILE_COMPRESSED) {
read_size = erts_gzread((gzFile)d->fd,
d->c.read.binp->orig_bytes + d->c.read.bin_offset,
size);
status = (read_size != -1);
if (!status) {
d->errInfo.posix_errno = EIO;
}
} else {
status = efile_read(&d->errInfo, d->flags, (int) d->fd,
d->c.read.binp->orig_bytes + d->c.read.bin_offset,
size,
&read_size);
}
if ( (d->result_ok = status)) {
ASSERT(read_size <= size);
d->c.read.bin_offset += read_size;
if (read_size < size || !segment) {
d->c.read.bin_size = 0;
d->again = 0;
} else {
d->c.read.bin_size -= read_size;
}
} else {
d->again = 0;
}
}
static void free_read(void *data)
{
struct t_data *d = (struct t_data *) data;
driver_free_binary(d->c.read.binp);
EF_FREE(d);
}