-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathefs.c
1763 lines (1489 loc) · 33.3 KB
/
efs.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
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "efs.h"
#include "endian.h"
#include "err.h"
#include "progname.h"
struct efs_sb efstoh(struct efs_sb efs)
{
struct efs_sb out = {0,};
out.fs_size = be32toh(efs.fs_size);
out.fs_firstcg = be32toh(efs.fs_firstcg);
out.fs_cgfsize = be32toh(efs.fs_cgfsize);
out.fs_cgisize = be16toh(efs.fs_cgisize);
out.fs_sectors = be16toh(efs.fs_sectors);
out.fs_heads = be16toh(efs.fs_heads);
out.fs_ncg = be16toh(efs.fs_ncg);
out.fs_dirty = be16toh(efs.fs_dirty);
out.__pad = be16toh(efs.__pad);
out.fs_time = be32toh(efs.fs_time);
out.fs_magic = be32toh(efs.fs_magic);
memcpy(&out.fs_fname, &efs.fs_fname, sizeof(efs.fs_fname));
memcpy(&out.fs_fpack, &efs.fs_fpack, sizeof(efs.fs_fpack));
out.fs_bmsize = be32toh(efs.fs_bmsize);
out.fs_tfree = be32toh(efs.fs_tfree);
out.fs_tinode = be32toh(efs.fs_tinode);
out.fs_bmblock = be32toh(efs.fs_bmblock);
out.fs_replsb = be32toh(efs.fs_replsb);
out.fs_lastialloc = be32toh(efs.fs_lastialloc);
memcpy(&out.fs_spare, &efs.fs_spare, sizeof(efs.fs_spare));
out.fs_checksum = be32toh(efs.fs_checksum);
return out;
}
uint32_t efs_extent_get_bn(struct efs_extent extent)
{
uint8_t buf[8];
uint32_t out = 0;
memcpy(buf, &extent, sizeof(buf));
out = 256*256*buf[1] + 256*buf[2] + buf[3];
return out;
}
uint32_t efs_extent_get_offset(struct efs_extent extent)
{
uint8_t buf[8];
uint32_t out = 0;
memcpy(buf, &extent, sizeof(buf));
out = 256*256*buf[5] + 256*buf[6] + buf[7];
return out;
}
struct efs_dinode efs_dinodetoh(struct efs_dinode inode)
{
size_t i;
struct efs_dinode out = {0,};
out.di_mode = be16toh(inode.di_mode);
out.di_nlink = be16toh(inode.di_nlink);
out.di_uid = be16toh(inode.di_uid);
out.di_gid = be16toh(inode.di_gid);
out.di_size = be32toh(inode.di_size);
out.di_atime = be32toh(inode.di_atime);
out.di_mtime = be32toh(inode.di_mtime);
out.di_ctime = be32toh(inode.di_ctime);
out.di_gen = be32toh(inode.di_gen);
out.di_numextents = be16toh(inode.di_numextents);
out.di_version = inode.di_version;
out.di_spare = inode.di_spare;
#if 0
printf("numextents: %u\n", out.di_numextents);
#endif
switch (out.di_mode & IFMT) {
case IFCHR:
case IFBLK:
out.di_u.di_dev.ndev = be32toh(inode.di_u.di_dev.ndev);
out.di_u.di_dev.odev = be16toh(inode.di_u.di_dev.odev);
break;
default:
for (i = 0; i < EFS_DIRECTEXTENTS; i++) {
out.di_u.di_extents[i] = inode.di_u.di_extents[i];
}
break;
}
return out;
}
efs_err_t efs_get_blocks(efs_t *ctx, void *buf, size_t firstlbn, size_t nblks)
{
__label__ out_error, out_ok;
int rc;
efs_err_t erc;
#if 0
printf("efs_get_blocks(%p, %p, %zu, %zu);\n", ctx, buf, firstlbn, nblks);
#endif
rc = fsseek(ctx->fs, BLKSIZ * firstlbn, SEEK_SET);
if (rc == -1) {
erc = EFS_ERR_READFAIL;
goto out_error;
}
#if 0
printf("fsread(%p, %u, %lu, %p);\n", buf, BLKSIZ, nblks, ctx->fs);
#endif
rc = fsread(buf, BLKSIZ, nblks, ctx->fs);
#if 0
printf("fsread: returning %d\n", rc);
hexdump(buf, BLKSIZ * nblks);
#endif
if (rc != nblks) {
erc = EFS_ERR_READFAIL;
goto out_error;
}
goto out_ok;
out_ok:
return EFS_ERR_OK;
out_error:
return erc;
}
efs_err_t efs_open(efs_t **ctx, fileslice_t *fs)
{
__label__ out_error;
efs_err_t erc;
int rc;
/* Allocate efs context */
*ctx = calloc(1, sizeof(efs_t));
if (!*ctx) {
erc = EFS_ERR_NOMEM;
goto out_error;
}
(*ctx)->fs = fs;
/* Read superblock */
rc = efs_get_blocks(*ctx, &(*ctx)->sb, 1, 1);
if (rc != EFS_ERR_OK) {
erc = rc;
goto out_error;
}
/* Validate superblock magic */
if (!IS_EFS_MAGIC(be32toh((*ctx)->sb.fs_magic))) {
erc = EFS_ERR_SBMAGIC;
goto out_error;
}
/* Convert superblock to native endianness */
(*ctx)->sb = efstoh((*ctx)->sb);
return EFS_ERR_OK;
out_error:
if (*ctx) free(*ctx);
*ctx = NULL;
return erc;
}
void efs_close(efs_t *ctx)
{
if (!ctx) return;
if (ctx->dvh)
dvh_close(ctx->dvh);
fsclose(ctx->fs);
free(ctx);
}
struct efs_ino_inf_s {
size_t bb;
unsigned slot;
};
size_t itobb(efs_t *ctx, efs_ino_t ino)
{
return EFS_ITOBB(&ctx->sb, ino);
}
struct efs_ino_inf_s efs_get_inode_info(efs_t *ctx, efs_ino_t ino)
{
struct efs_ino_inf_s out = {0,};
out.bb = ctx->sb.fs_firstcg;
out.bb = itobb(ctx, ino);
out.slot = ino & EFS_INOPBBMASK;
return out;
}
struct efs_dinode efs_get_inode(efs_t *ctx, unsigned ino)
{
struct efs_dinode inodes[4];
struct efs_ino_inf_s info;
info = efs_get_inode_info(ctx, ino);
efs_get_blocks(ctx, &inodes, info.bb, 1);
inodes[info.slot] = efs_dinodetoh(inodes[info.slot]);
#if 0
hexdump(&inodes[off], sizeof(*inodes));
#endif
return inodes[info.slot];
}
efs_ino_t efs_find_entry(efs_t *efs, const char *name)
{
/* TODO */
return 2;
}
/*
* Directory functions.
*/
int compar(const void *a, const void *b)
{
struct efs_dirent *de_a, *de_b;
de_a = (struct efs_dirent *)a;
de_b = (struct efs_dirent *)b;
return strcmp(de_a->d_name, de_b->d_name);
}
efs_dir_t *efs_opendir(efs_t *efs, const char *dirname)
{
__label__ out_ok, out_error;
efs_dir_t *dirp;
size_t nel = 0;
struct efs_dirent *de;
dirp = calloc(sizeof(*dirp), 1);
if (!dirp) goto out_error;
dirp->ino = efs_namei(efs, dirname);
#if 0
printf("--ino: %u\n", dirp->ino);
#endif
if (dirp->ino == -1) goto out_error;
dirp->dirent = _efs_read_dirblks(efs, dirp->ino);
if (!dirp->dirent)
goto out_error;
dirp->_dirent_memobj = dirp->dirent;
for (de = dirp->dirent; de->d_ino; de++) {
nel++;
}
qsort(dirp->dirent, nel, sizeof(struct efs_dirent), compar);
goto out_ok;
out_ok:
return dirp;
out_error:
free(dirp);
return NULL;
}
int efs_closedir(efs_dir_t *dirp)
{
free(dirp->_dirent_memobj);
free(dirp);
return 0;
}
struct efs_dirent *efs_readdir(efs_dir_t *dirp)
{
if (dirp->dirent->d_ino)
return dirp->dirent ++;
else
return NULL;
}
void efs_rewinddir(efs_dir_t *dirp)
{
dirp->dirent = dirp->_dirent_memobj;
}
int efs_stati(efs_t *ctx, efs_ino_t ino, struct efs_stat *statbuf)
{
struct efs_dinode dinode;
dinode = efs_get_inode(ctx, ino);
statbuf->st_ino = ino;
statbuf->st_mode = dinode.di_mode;
statbuf->st_nlink = dinode.di_nlink;
statbuf->st_uid = dinode.di_uid;
statbuf->st_gid = dinode.di_gid;
statbuf->st_size = dinode.di_size;
switch (statbuf->st_mode & IFMT) {
case IFCHR:
case IFBLK:
statbuf->st_major = (dinode.di_u.di_dev.odev & 0xff00) >> 8;
statbuf->st_minor = (dinode.di_u.di_dev.odev & 0x00ff);
break;
}
statbuf->st_atimespec.tv_sec = dinode.di_atime;
statbuf->st_atimespec.tv_nsec = 0;
statbuf->st_mtimespec.tv_sec = dinode.di_mtime;
statbuf->st_mtimespec.tv_nsec = 0;
statbuf->st_ctimespec.tv_sec = dinode.di_ctime;
statbuf->st_ctimespec.tv_nsec = 0;
return 0;
}
int efs_stat(efs_t *ctx, const char *pathname, struct efs_stat *statbuf)
{
efs_ino_t ino;
ino = efs_namei(ctx, pathname);
if (ino == -1) return -1;
return efs_stati(ctx, ino, statbuf);
}
int efs_fstat(efs_file_t *file, struct efs_stat *statbuf)
{
return efs_stati(file->ctx, file->ino, statbuf);
}
char *mkpath(char *path, char *name)
{
int rc;
char *out;
if (!path || !name)
return NULL;
if (strlen(path) == 0) {
out = strdup(name);
} else {
size_t stringsize;
stringsize = strlen(path) + strlen("/") + strlen(name) + 1;
out = malloc(stringsize);
if (!out)
err(1, "in malloc");
rc = snprintf(out, stringsize, "%s/%s", path, name);
if (rc >= stringsize)
errx(1, "in snprintf");
out[stringsize - 1] = '\0';
}
return out;
}
int efs_nftw(
efs_t *efs,
const char *dirpath,
int (*fn)(const char *fpath, const struct efs_stat *sb)
) {
int rc;
queue_t q;
struct qent_s *qe;
q = queue_init();
if (!q)
return -1;
queue_add_head(q, strdup(dirpath));
while ((qe = queue_dequeue(q))) {
efs_dir_t *dirp;
queue_t dirq;
struct efs_dirent *de;
dirp = efs_opendir(efs, qe->path);
if (!dirp)
errx(1, "couldn't open directory: '%s'", qe->path);
dirq = queue_init();
if (!dirq)
err(1, "in queue_init");
while ((de = efs_readdir(dirp))) {
__label__ nextfile;
struct efs_stat sb;
char *path;
rc = efs_stati(efs, de->d_ino, &sb);
if (rc == -1)
err(1, "couldn't get stat for '%s'", de->d_name);
path = mkpath(qe->path, de->d_name);
if (!path)
goto nextfile;
if ((sb.st_mode & IFMT) == IFDIR) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
goto nextfile;
}
queue_add_head(dirq, strdup(path));
}
if (fn) {
rc = fn(path, &sb);
if (rc != 0) {
/* TODO: stop walk */
}
} else {
printf("%s\n", path);
}
nextfile:
free(path);
}
efs_closedir(dirp);
free(qe->path);
free(qe);
queue_add_queue_head(q, dirq);
}
queue_free(q);
return 0;
}
const char *efs_strerror(efs_err_t e)
{
switch (e) {
case EFS_ERR_OK:
return "no error";
case EFS_ERR_INVAL:
return "invalid argument";
case EFS_ERR_NOENT:
return "no such file or directory";
case EFS_ERR_NOMEM:
return "out of memory";
case EFS_ERR_READFAIL:
return "read error";
case EFS_ERR_NOPAR:
return "requested partition not found";
case EFS_ERR_NOVH:
return "volume header not found";
case EFS_ERR_BADVH:
return "volume header checksum failure";
case EFS_ERR_SBMAGIC:
return "superblock not found";
case EFS_ERR_PARTYPE:
return "unrecognized partition type";
case EFS_ERR_BADPAR:
return "bad partition";
case EFS_ERR_IS_BSD:
return "RISCos format is not supported";
case EFS_ERR_IS_ISO9660:
return "ISO9660 format is not supported";
case EFS_ERR_IS_XFS:
return "XFS format is not supported";
default:
return "unknown error";
}
}
void vwarnefs(efs_err_t e, const char *fmt, va_list args)
{
fprintf(stderr, "%s: ", __progname);
if (fmt) {
vfprintf(stderr, fmt, args);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", efs_strerror(e));
}
void verrefs(int eval, efs_err_t e, const char *fmt, va_list args)
{
vwarnefs(e, fmt, args);
exit(eval);
}
void warnefs(efs_err_t e, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnefs(e, fmt, ap);
va_end(ap);
}
void errefs(int eval, efs_err_t e, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrefs(eval, e, fmt, ap);
va_end(ap);
}
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a>b?b:a)
const char *_getfirstpathpart(const char *name);
struct efs_extent *_efs_get_extents(efs_t *ctx, struct efs_dinode *dinode);
struct efs_extent *_efs_find_extent(struct efs_extent *exs, unsigned numextents, size_t pos);
efs_ino_t _efs_nameiat(efs_t *ctx, efs_ino_t ino, const char *name);
efs_file_t *_efs_file_openi(efs_t *ctx, efs_ino_t ino);
struct efs_extent *_efs_get_extents(efs_t *ctx, struct efs_dinode *dinode)
{
__label__ out_error, out_ok;
efs_err_t erc;
struct efs_extent *out = NULL;
unsigned numextents;
unsigned last;
size_t i;
numextents = dinode->di_numextents;
#if 0
printf("numextents: %4x\n", numextents);
#endif
out = calloc(numextents, sizeof(struct efs_extent));
if (!out)
goto out_error;
if (numextents == 0)
goto out_ok;
if (numextents <= EFS_DIRECTEXTENTS) {
/* direct extents */
unsigned i;
for (i = 0; i < numextents; i++) {
out[i] = dinode->di_u.di_extents[i];
}
} else {
/* indirect extents */
void *buf = NULL;
unsigned numindirect;
unsigned nobbs;
unsigned bbcursor = 0;
unsigned i;
numindirect = efs_extent_get_offset(dinode->di_u.di_extents[0]);
#if 0
printf("numindirect: %4x\n", numindirect);
#endif
if (numindirect > EFS_DIRECTEXTENTS)
errx(1, "invalid number of indirect extents (fs corrupt)");
/* validate number of BBs is at or under limit */
nobbs = 0;
for (i = 0; i < numindirect; i++) {
struct efs_extent ex;
ex = dinode->di_u.di_extents[i];
nobbs += ex.ex_length;
}
if (nobbs > EFS_MAXINDIRBBS) {
goto out_error;
}
buf = calloc(nobbs, BLKSIZ);
if (!buf) err(1, "in calloc");
for (i = 0; i < numindirect; i++) {
struct efs_extent ex;
ex = dinode->di_u.di_extents[i];
#if 0
printf("bbcursor: %4x (%d)\n", bbcursor, i);
#endif
erc = efs_get_blocks(
ctx,
buf + (BLKSIZ * bbcursor),
efs_extent_get_bn(ex),
ex.ex_length
);
if (erc != EFS_ERR_OK)
errefs(1, erc, "while fetching indir extent");
#if 0
hexdump(buf, BLKSIZ * nobbs);
#endif
bbcursor += ex.ex_length;
}
for (i = 0; i < numextents; i++) {
struct efs_extent *exs;
exs = (struct efs_extent *)buf;
out[i] = exs[i];
}
free(buf);
}
#if 0
/* sort extents */
int a(const struct efs_extent *a, const struct efs_extent *b) {
unsigned ao = efs_extent_get_offset(*a);
unsigned bo = efs_extent_get_offset(*b);
if (ao < bo)
return -1;
if (ao > bo)
return 1;
return 0;
}
qsort(out, numextents, sizeof(*out), a);
#endif
#if 0
/* print extent table */
for (size_t i = 0; i < numextents; i++) {
uint32_t offset, bn;
offset = efs_extent_get_offset(out[i]);
bn = efs_extent_get_bn(out[i]);
printf("%4zx %2x %6x %2x %6x\n",
i,
out[i].ex_magic,
bn,
out[i].ex_length,
offset
);
}
#endif
#if 1
/* assert that extents are in ascending order */
last = efs_extent_get_offset(out[0]);
for (i = 1; i < numextents; i++) {
unsigned suspect;
suspect = efs_extent_get_offset(out[i]);
if (last >= suspect) {
errx(1, "unsorted extents (%zu: %x >= %x)", i, last, suspect);
}
last = suspect;
}
#endif
goto out_ok;
out_ok:
return out;
out_error:
free(out);
out = NULL;
return NULL;
}
struct efs_extent *_efs_find_extent(struct efs_extent *exs, unsigned numextents, size_t pos)
{
unsigned i;
for (i = 0; i < numextents; i++) {
struct efs_extent *ex;
ex = &exs[i];
/* too low? */
if ((efs_extent_get_offset(*ex) * BLKSIZ) > pos)
continue;
/* too high? */
if (((efs_extent_get_offset(*ex) + ex->ex_length) * BLKSIZ) <= pos)
continue;
return ex;
}
return NULL;
}
unsigned _efs_nbytes_this_extent(
struct efs_extent *ex,
unsigned pos,
unsigned nbytes
) {
__label__ out_error;
size_t start, end;
unsigned bytes_left_in_extent;
start = BLKSIZ * efs_extent_get_offset(*ex);
end = BLKSIZ * (efs_extent_get_offset(*ex) + ex->ex_length);
/* assert position is within extent */
if (pos < start)
goto out_error;
if (pos > end)
goto out_error;
/* move pos to be relative to start of extent */
pos -= start;
bytes_left_in_extent = end - start - pos;
return MIN(bytes_left_in_extent, nbytes);
out_error:
return 0;
}
unsigned _efs_nbytes_firstbn(
struct efs_extent *ex,
unsigned pos
) {
unsigned blkOff, newpos;
newpos = pos - (BLKSIZ * efs_extent_get_offset(*ex));
blkOff = newpos / BLKSIZ;
return blkOff;
}
size_t efs_fread_blocks(
void *ptr,
size_t lbn,
size_t numblocks,
efs_file_t *file
) {
efs_err_t erc;
struct efs_extent *ex;
unsigned done;
if (!ptr) {
return 0;
}
#if 0
printf("efs_fread_blocks: ptr: %p, lbn: %zu, numblocks: %zu, file: %p\n",
ptr,
lbn,
numblocks,
file
);
#endif
if ((numblocks==1) && (file->blocknum == lbn)) {
memcpy(ptr, file->blockbuf, BLKSIZ);
return 1;
}
done = 0;
while (done < numblocks) {
unsigned offset_in_extent;
unsigned blocks_this_extent;
ex = _efs_find_extent(file->exs, file->numextents, lbn * BLKSIZ);
if (!ex) errx(1, "in efs_fread_blocks");
offset_in_extent = lbn - efs_extent_get_offset(*ex);
blocks_this_extent = MIN(numblocks, ex->ex_length - offset_in_extent);
#if 0
printf("blocks_this_extent: %u\n", blocks_this_extent);
#endif
#if 0
printf("before: %p %u\n", ptr, blocks_this_extent);
hexdump(ptr, blocks_this_extent * BLKSIZ);
#endif
erc = efs_get_blocks(file->ctx, ptr, efs_extent_get_bn(*ex) + offset_in_extent, blocks_this_extent);
if (erc) errefs(1, erc, "in efs_fread_blocks");
#if 0
printf("after:\n");
hexdump(ptr, blocks_this_extent * BLKSIZ);
#endif
ptr += blocks_this_extent * BLKSIZ;
done += blocks_this_extent;
}
if (numblocks == 1) {
file->blocknum = lbn;
memcpy(file->blockbuf, ptr - BLKSIZ, BLKSIZ);
}
#if 0
printf("efs_fread_blocks returning: %zu\n", numblocks);
#endif
return numblocks;
}
size_t _efs_fread_aux(
void *ptr,
size_t size,
efs_file_t *file
) {
#if 0
printf("_efs_fread_aux: ptr: %16p, size: %8zx, file: %p\n",
ptr,
size,
file
);
#endif
if (!size) return 0;
/* start with a partial block? */
if (file->pos % BLKSIZ) {
unsigned start, len, blknum;
size_t rc;
uint8_t *buf = calloc(BLKSIZ, 1);
start = file->pos % BLKSIZ;
len = MIN(size, (BLKSIZ - start));
blknum = file->pos / BLKSIZ;
rc = efs_fread_blocks(buf, blknum, 1, file);
if (rc != 1) return 0;
memcpy(ptr, &buf[start], len);
file->pos += len;
ptr += len;
size -= len;
free(buf);
buf = NULL;
}
/* whole blocks? */
if (!(file->pos % BLKSIZ) && (size >= BLKSIZ)) {
unsigned startblk, nblks;
size_t rc;
startblk = file->pos / BLKSIZ;
nblks = size / BLKSIZ;
#if 0
printf("file->pos: %u, size: %lu\n", file->pos, size);
printf("nblks: %u, startblk: %u\n", nblks, startblk);
#endif
rc = efs_fread_blocks(ptr, startblk, nblks, file);
if (rc != nblks) return 0;
file->pos += nblks * BLKSIZ;
ptr += nblks * BLKSIZ;
size -= nblks * BLKSIZ;
}
/* end with a partial block? */
if (!(file->pos % BLKSIZ) && size > 0 && size < BLKSIZ) {
size_t rc;
uint8_t *buf = calloc(BLKSIZ, 1);
unsigned blknum;
blknum = file->pos / BLKSIZ;
rc = efs_fread_blocks(buf, blknum, 1, file);
if (rc != 1) return 0;
memcpy(ptr, buf, size);
file->pos += size;
ptr += size;
free(buf);
buf = NULL;
}
return 1;
}
size_t efs_fread(
void *ptr,
size_t size,
size_t nmemb,
efs_file_t *file
) {
size_t out = 0;
int i;
#if 0
if (size > 0xc080c1a2bf5e9032ULL)
printf("efs_fread: ptr: %16p, size: %8zx, nmemb: %8zx, file: %p\n",
ptr,
size,
nmemb,
file
);
#endif
for (i = 0; i < nmemb; i++) {
size_t rc;
rc = _efs_fread_aux(ptr, size, file);
ptr += size;
if (rc == 1) out++;
else break;
}
#if 0
printf("efs_fread: returning %zu\n", out);
#endif
return out;
}
efs_file_t *efs_fopenat(
efs_t *ctx,
efs_dir_t *dirp,
const char *path
) {
__label__ out_ok, out_error;
efs_ino_t ino;
efs_file_t *out = NULL;
ino = _efs_nameiat(ctx, dirp->ino, path);
if (ino == (efs_ino_t)(-1)) {
errno = ENOENT;
goto out_error;
}
out = _efs_file_openi(ctx, ino);
if (!out) goto out_error;
goto out_ok;
out_ok:
errno = 0;
return out;
out_error:
free(out);
return NULL;
}
efs_file_t *efs_fopen(
efs_t *ctx,
const char *path
) {
efs_dir_t dir;
dir.ino = EFS_BLK_ROOTINO;
return efs_fopenat(ctx, &dir, path);
}
efs_file_t *_efs_file_openi(efs_t *ctx, efs_ino_t ino)
{
__label__ out_error, out_ok;
efs_file_t *out = NULL;
if (!ctx) goto out_error;
out = calloc(sizeof(efs_file_t), 1);
if (!out) goto out_error;
out->ctx = ctx;
out->ino = ino;
out->pos = 0;
out->eof = false;
out->error = false;
out->dinode = efs_get_inode(ctx, ino);
out->nbytes = out->dinode.di_size;
out->blocknum = -1;
/* validate inode */
if (out->dinode.di_version != 0)
goto out_error;
if (out->dinode.di_nlink == 0)
goto out_error;
switch (out->dinode.di_mode & IFMT) {
case IFREG:
case IFDIR:
case IFLNK:
break;
default:
goto out_error;
}
out->numextents = out->dinode.di_numextents;
out->exs = _efs_get_extents(ctx, &(out->dinode));
if (!out->exs)
goto out_error;
goto out_ok;
out_ok:
return out;
out_error:
free(out);
return NULL;
}
int efs_fclose(efs_file_t *file)
{
free(file->exs);
free(file);
return 0;
}
int efs_fseek(efs_file_t *file, long offset, int whence)
{
long newpos;
if (!file) {
errno = EBADF;
return -1;
}
switch (file->dinode.di_mode & IFMT) {
case IFIFO:
case IFSOCK:
errno = ESPIPE;
return -1;
default:
break;
}
switch (whence) {
case SEEK_SET:
newpos = 0;
break;
case SEEK_CUR:
newpos = file->pos;
break;
case SEEK_END:
newpos = file->dinode.di_size -1;
break;
default:
errno = EINVAL;
return -1;
}
newpos += offset;
if (offset < 0) {
errno = EINVAL;
return -1;
}