/
vfs.cpp
1304 lines (993 loc) · 23.8 KB
/
vfs.cpp
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
/*
* Copyright 2003-2013, Axel Dörfler, axeld@pinc-software.de.
* Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2017, Jessica Hamilton, jessica.l.hamilton@gmail.com.
* Distributed under the terms of the MIT License.
*/
#include <boot/vfs.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>
#include <StorageDefs.h>
#include <AutoDeleter.h>
#include <boot/platform.h>
#include <boot/partitions.h>
#include <boot/stdio.h>
#include <boot/stage2.h>
#include <syscall_utils.h>
#include "package_support.h"
#include "RootFileSystem.h"
#include "file_systems/packagefs/packagefs.h"
using namespace boot;
//#define TRACE_VFS
#ifdef TRACE_VFS
# define TRACE(x) dprintf x
#else
# define TRACE(x) ;
#endif
struct __DIR {
Directory* directory;
void* cookie;
char _direntBuffer[sizeof(dirent) + B_FILE_NAME_LENGTH + 1];
dirent* entry() { return (dirent*)_direntBuffer; }
};
class Descriptor {
public:
Descriptor(Node *node, void *cookie);
~Descriptor();
ssize_t ReadAt(off_t pos, void *buffer, size_t bufferSize);
ssize_t Read(void *buffer, size_t bufferSize);
ssize_t WriteAt(off_t pos, const void *buffer, size_t bufferSize);
ssize_t Write(const void *buffer, size_t bufferSize);
void Stat(struct stat &stat);
status_t Seek(off_t position, int mode);
off_t Offset() const { return fOffset; }
int32 RefCount() const { return fRefCount; }
status_t Acquire();
status_t Release();
Node *GetNode() const { return fNode; }
private:
Node *fNode;
void *fCookie;
off_t fOffset;
int32 fRefCount;
};
#define MAX_VFS_DESCRIPTORS 64
NodeList gBootDevices;
NodeList gPartitions;
RootFileSystem *gRoot;
static Descriptor *sDescriptors[MAX_VFS_DESCRIPTORS];
static Node *sBootDevice;
Node::Node()
:
fRefCount(1)
{
}
Node::~Node()
{
}
status_t
Node::Open(void **_cookie, int mode)
{
TRACE(("%p::Open()\n", this));
return Acquire();
}
status_t
Node::Close(void *cookie)
{
TRACE(("%p::Close()\n", this));
return Release();
}
status_t
Node::ReadLink(char* buffer, size_t bufferSize)
{
return B_BAD_VALUE;
}
status_t
Node::GetName(char *nameBuffer, size_t bufferSize) const
{
return B_ERROR;
}
status_t
Node::GetFileMap(struct file_map_run *runs, int32 *count)
{
return B_ERROR;
}
int32
Node::Type() const
{
return 0;
}
off_t
Node::Size() const
{
return 0LL;
}
ino_t
Node::Inode() const
{
return 0;
}
void
Node::Stat(struct stat& stat)
{
stat.st_mode = Type();
stat.st_size = Size();
stat.st_ino = Inode();
}
status_t
Node::Acquire()
{
fRefCount++;
TRACE(("%p::Acquire(), fRefCount = %" B_PRId32 "\n", this, fRefCount));
return B_OK;
}
status_t
Node::Release()
{
TRACE(("%p::Release(), fRefCount = %" B_PRId32 "\n", this, fRefCount));
if (--fRefCount == 0) {
TRACE(("delete node: %p\n", this));
delete this;
return 1;
}
return B_OK;
}
// #pragma mark -
ConsoleNode::ConsoleNode()
: Node()
{
}
ssize_t
ConsoleNode::Read(void *buffer, size_t bufferSize)
{
return ReadAt(NULL, -1, buffer, bufferSize);
}
ssize_t
ConsoleNode::Write(const void *buffer, size_t bufferSize)
{
return WriteAt(NULL, -1, buffer, bufferSize);
}
// #pragma mark -
Directory::Directory()
: Node()
{
}
ssize_t
Directory::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize)
{
return B_ERROR;
}
ssize_t
Directory::WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize)
{
return B_ERROR;
}
int32
Directory::Type() const
{
return S_IFDIR;
}
Node*
Directory::Lookup(const char* name, bool traverseLinks)
{
Node* node = LookupDontTraverse(name);
if (node == NULL)
return NULL;
if (!traverseLinks || !S_ISLNK(node->Type()))
return node;
// the node is a symbolic link, so we have to resolve the path
char* linkPath = (char*)malloc(B_PATH_NAME_LENGTH);
if (linkPath == NULL) {
node->Release();
return NULL;
}
status_t error = node->ReadLink(linkPath, B_PATH_NAME_LENGTH);
node->Release();
// we don't need this one anymore
if (error != B_OK) {
free(linkPath);
return NULL;
}
// let open_from() do the real work
int fd = open_from(this, linkPath, O_RDONLY);
if (fd < 0) {
free(linkPath);
return NULL;
}
free(linkPath);
node = get_node_from(fd);
if (node != NULL)
node->Acquire();
close(fd);
return node;
}
status_t
Directory::CreateFile(const char *name, mode_t permissions, Node **_node)
{
return EROFS;
}
// #pragma mark -
MemoryDisk::MemoryDisk(const uint8* data, size_t size, const char* name)
: Node(),
fData(data),
fSize(size)
{
strlcpy(fName, name, sizeof(fName));
}
ssize_t
MemoryDisk::ReadAt(void* cookie, off_t pos, void* buffer, size_t bufferSize)
{
if (pos < 0)
return B_BAD_VALUE;
if ((size_t)pos >= fSize)
return 0;
if (pos + bufferSize > fSize)
bufferSize = fSize - pos;
memcpy(buffer, fData + pos, bufferSize);
return bufferSize;
}
ssize_t
MemoryDisk::WriteAt(void* cookie, off_t pos, const void* buffer,
size_t bufferSize)
{
return B_NOT_ALLOWED;
}
off_t
MemoryDisk::Size() const
{
return fSize;
}
status_t
MemoryDisk::GetName(char *nameBuffer, size_t bufferSize) const
{
if (!nameBuffer)
return B_BAD_VALUE;
strlcpy(nameBuffer, fName, bufferSize);
return B_OK;
}
// #pragma mark -
Descriptor::Descriptor(Node *node, void *cookie)
:
fNode(node),
fCookie(cookie),
fOffset(0),
fRefCount(1)
{
}
Descriptor::~Descriptor()
{
}
ssize_t
Descriptor::Read(void *buffer, size_t bufferSize)
{
ssize_t bytesRead = fNode->ReadAt(fCookie, fOffset, buffer, bufferSize);
if (bytesRead > B_OK)
fOffset += bytesRead;
return bytesRead;
}
ssize_t
Descriptor::ReadAt(off_t pos, void *buffer, size_t bufferSize)
{
return fNode->ReadAt(fCookie, pos, buffer, bufferSize);
}
ssize_t
Descriptor::Write(const void *buffer, size_t bufferSize)
{
ssize_t bytesWritten = fNode->WriteAt(fCookie, fOffset, buffer, bufferSize);
if (bytesWritten > B_OK)
fOffset += bytesWritten;
return bytesWritten;
}
ssize_t
Descriptor::WriteAt(off_t pos, const void *buffer, size_t bufferSize)
{
return fNode->WriteAt(fCookie, pos, buffer, bufferSize);
}
void
Descriptor::Stat(struct stat &stat)
{
fNode->Stat(stat);
}
status_t
Descriptor::Seek(off_t position, int mode)
{
off_t newPosition;
switch (mode)
{
case SEEK_SET:
newPosition = position;
break;
case SEEK_CUR:
newPosition = fOffset + position;
break;
case SEEK_END:
{
struct stat st;
Stat(st);
newPosition = st.st_size + position;
break;
}
default:
return B_BAD_VALUE;
}
if (newPosition < 0)
return B_BAD_VALUE;
fOffset = newPosition;
return B_OK;
}
status_t
Descriptor::Acquire()
{
fRefCount++;
return B_OK;
}
status_t
Descriptor::Release()
{
if (--fRefCount == 0) {
status_t status = fNode->Close(fCookie);
if (status != B_OK)
return status;
}
return B_OK;
}
// #pragma mark -
BootVolume::BootVolume()
:
fRootDirectory(NULL),
fSystemDirectory(NULL),
fPackageVolumeInfo(NULL),
fPackageVolumeState(NULL)
{
}
BootVolume::~BootVolume()
{
Unset();
}
status_t
BootVolume::SetTo(Directory* rootDirectory,
PackageVolumeInfo* packageVolumeInfo,
PackageVolumeState* packageVolumeState)
{
Unset();
status_t error = _SetTo(rootDirectory, packageVolumeInfo,
packageVolumeState);
if (error != B_OK)
Unset();
return error;
}
void
BootVolume::Unset()
{
if (fRootDirectory != NULL) {
fRootDirectory->Release();
fRootDirectory = NULL;
}
if (fSystemDirectory != NULL) {
fSystemDirectory->Release();
fSystemDirectory = NULL;
}
if (fPackageVolumeInfo != NULL) {
fPackageVolumeInfo->ReleaseReference();
fPackageVolumeInfo = NULL;
fPackageVolumeState = NULL;
}
}
status_t
BootVolume::_SetTo(Directory* rootDirectory,
PackageVolumeInfo* packageVolumeInfo,
PackageVolumeState* packageVolumeState)
{
Unset();
if (rootDirectory == NULL)
return B_BAD_VALUE;
fRootDirectory = rootDirectory;
fRootDirectory->Acquire();
// find the system directory
Node* systemNode = fRootDirectory->Lookup("system", true);
if (systemNode == NULL || !S_ISDIR(systemNode->Type())) {
if (systemNode != NULL)
systemNode->Release();
Unset();
return B_ENTRY_NOT_FOUND;
}
fSystemDirectory = static_cast<Directory*>(systemNode);
if (packageVolumeInfo == NULL) {
// get a package volume info
BReference<PackageVolumeInfo> packageVolumeInfoReference(
new(std::nothrow) PackageVolumeInfo);
status_t error = packageVolumeInfoReference->SetTo(fSystemDirectory,
"packages");
if (error != B_OK) {
// apparently not packaged
return B_OK;
}
fPackageVolumeInfo = packageVolumeInfoReference.Detach();
} else {
fPackageVolumeInfo = packageVolumeInfo;
fPackageVolumeInfo->AcquireReference();
}
fPackageVolumeState = packageVolumeState != NULL
? packageVolumeState : fPackageVolumeInfo->States().Head();
// try opening the system package
int packageFD = _OpenSystemPackage();
if (packageFD < 0)
return packageFD;
// mount packagefs
Directory* packageRootDirectory;
status_t error = packagefs_mount_file(packageFD, fSystemDirectory,
packageRootDirectory);
close(packageFD);
if (error != B_OK) {
Unset();
return error;
}
fSystemDirectory->Release();
fSystemDirectory = packageRootDirectory;
return B_OK;
}
int
BootVolume::_OpenSystemPackage()
{
// open the packages directory
Node* packagesNode = fSystemDirectory->Lookup("packages", false);
if (packagesNode == NULL)
return -1;
MethodDeleter<Node, status_t, &Node::Release>
packagesNodeReleaser(packagesNode);
if (!S_ISDIR(packagesNode->Type()))
return -1;
Directory* packageDirectory = (Directory*)packagesNode;
// open the system package
return open_from(packageDirectory, fPackageVolumeState->SystemPackage(),
O_RDONLY);
}
// #pragma mark -
status_t
vfs_init(stage2_args *args)
{
gRoot = new(nothrow) RootFileSystem();
if (gRoot == NULL)
return B_NO_MEMORY;
return B_OK;
}
status_t
register_boot_file_system(BootVolume& bootVolume)
{
Directory* rootDirectory = bootVolume.RootDirectory();
gRoot->AddLink("boot", rootDirectory);
Partition *partition;
status_t status = gRoot->GetPartitionFor(rootDirectory, &partition);
if (status != B_OK) {
dprintf("register_boot_file_system(): could not locate boot volume in "
"root!\n");
return status;
}
gBootVolume.SetInt64(BOOT_VOLUME_PARTITION_OFFSET,
partition->offset);
if (bootVolume.IsPackaged()) {
gBootVolume.SetBool(BOOT_VOLUME_PACKAGED, true);
PackageVolumeState* state = bootVolume.GetPackageVolumeState();
if (state->Name() != NULL)
gBootVolume.AddString(BOOT_VOLUME_PACKAGES_STATE, state->Name());
}
Node *device = get_node_from(partition->FD());
if (device == NULL) {
dprintf("register_boot_file_system(): could not get boot device!\n");
return B_ERROR;
}
return platform_register_boot_device(device);
}
/*! Gets the boot device, scans all of its partitions, gets the
boot partition, and mounts its file system.
\param args The stage 2 arguments.
\param _bootVolume On success set to the boot volume.
\return \c B_OK on success, another error code otherwise.
*/
status_t
get_boot_file_system(stage2_args* args, BootVolume& _bootVolume)
{
status_t error = platform_add_boot_device(args, &gBootDevices);
if (error != B_OK)
return error;
NodeIterator iterator = gBootDevices.GetIterator();
while (iterator.HasNext()) {
Node *device = iterator.Next();
error = add_partitions_for(device, false, true);
if (error != B_OK)
continue;
NodeList bootPartitions;
error = platform_get_boot_partitions(args, device, &gPartitions, &bootPartitions);
if (error != B_OK)
continue;
NodeIterator partitionIterator = bootPartitions.GetIterator();
while (partitionIterator.HasNext()) {
Partition *partition = (Partition*)partitionIterator.Next();
Directory *fileSystem;
error = partition->Mount(&fileSystem, true);
if (error != B_OK) {
// this partition doesn't contain any known file system; we
// don't need it anymore
gPartitions.Remove(partition);
delete partition;
continue;
}
// init the BootVolume
error = _bootVolume.SetTo(fileSystem);
if (error != B_OK)
continue;
sBootDevice = device;
return B_OK;
}
}
return B_ERROR;
}
/** Mounts all file systems recognized on the given device by
* calling the add_partitions_for() function on them.
*/
status_t
mount_file_systems(stage2_args *args)
{
// mount other partitions on boot device (if any)
NodeIterator iterator = gPartitions.GetIterator();
Partition *partition = NULL;
while ((partition = (Partition *)iterator.Next()) != NULL) {
// don't scan known partitions again
if (partition->IsFileSystem())
continue;
// remove the partition if it doesn't contain a (known) file system
if (partition->Scan(true) != B_OK && !partition->IsFileSystem()) {
gPartitions.Remove(partition);
delete partition;
}
}
// add all block devices the platform has for us
status_t status = platform_add_block_devices(args, &gBootDevices);
if (status < B_OK)
return status;
iterator = gBootDevices.GetIterator();
Node *device = NULL, *last = NULL;
while ((device = iterator.Next()) != NULL) {
// don't scan former boot device again
if (device == sBootDevice)
continue;
if (add_partitions_for(device, true) == B_OK) {
// ToDo: we can't delete the object here, because it must
// be removed from the list before we know that it was
// deleted.
/* // if the Release() deletes the object, we need to skip it
if (device->Release() > 0) {
list_remove_item(&gBootDevices, device);
device = last;
}
*/
(void)last;
}
last = device;
}
if (gPartitions.IsEmpty())
return B_ENTRY_NOT_FOUND;
#if 0
void *cookie;
if (gRoot->Open(&cookie, O_RDONLY) == B_OK) {
Directory *directory;
while (gRoot->GetNextNode(cookie, (Node **)&directory) == B_OK) {
char name[256];
if (directory->GetName(name, sizeof(name)) == B_OK)
printf(":: %s (%p)\n", name, directory);
void *subCookie;
if (directory->Open(&subCookie, O_RDONLY) == B_OK) {
while (directory->GetNextEntry(subCookie, name, sizeof(name)) == B_OK) {
printf("\t%s\n", name);
}
directory->Close(subCookie);
}
}
gRoot->Close(cookie);
}
#endif
return B_OK;
}
/*! Resolves \a directory + \a path to a node.
Note that \a path will be modified by the function.
*/
static status_t
get_node_for_path(Directory *directory, char *path, Node **_node)
{
directory->Acquire();
// balance Acquire()/Release() calls
while (true) {
Node *nextNode;
char *nextPath;
// walk to find the next path component ("path" will point to a single
// path component), and filter out multiple slashes
for (nextPath = path + 1; nextPath[0] != '\0' && nextPath[0] != '/'; nextPath++);
if (*nextPath == '/') {
*nextPath = '\0';
do
nextPath++;
while (*nextPath == '/');
}
nextNode = directory->Lookup(path, true);
directory->Release();
if (nextNode == NULL)
return B_ENTRY_NOT_FOUND;
path = nextPath;
if (S_ISDIR(nextNode->Type()))
directory = (Directory *)nextNode;
else if (path[0])
return B_NOT_ALLOWED;
// are we done?
if (path[0] == '\0') {
*_node = nextNode;
return B_OK;
}
}
return B_ENTRY_NOT_FOUND;
}
/*! Version of get_node_for_path() not modifying \a path.
*/
static status_t
get_node_for_path(Directory* directory, const char* path, Node** _node)
{
char* mutablePath = strdup(path);
if (mutablePath == NULL)
return B_NO_MEMORY;
MemoryDeleter mutablePathDeleter(mutablePath);
return get_node_for_path(directory, mutablePath, _node);
}
// #pragma mark -
static Descriptor *
get_descriptor(int fd)
{
if (fd < 0 || fd >= MAX_VFS_DESCRIPTORS)
return NULL;
return sDescriptors[fd];
}
static void
free_descriptor(int fd)
{
if (fd >= MAX_VFS_DESCRIPTORS)
return;
delete sDescriptors[fd];
sDescriptors[fd] = NULL;
}
/** Reserves an entry of the descriptor table and
* assigns the given node to it.
*/
int
open_node(Node *node, int mode)
{
if (node == NULL)
return B_ERROR;
// get free descriptor
int fd = 0;
for (; fd < MAX_VFS_DESCRIPTORS; fd++) {
if (sDescriptors[fd] == NULL)
break;
}
if (fd == MAX_VFS_DESCRIPTORS)
return B_ERROR;
TRACE(("got descriptor %d for node %p\n", fd, node));
// we got a free descriptor entry, now try to open the node
void *cookie;
status_t status = node->Open(&cookie, mode);
if (status < B_OK)
return status;
TRACE(("could open node at %p\n", node));
Descriptor *descriptor = new(nothrow) Descriptor(node, cookie);
if (descriptor == NULL)
return B_NO_MEMORY;
sDescriptors[fd] = descriptor;
return fd;
}
int
dup(int fd)
{
Descriptor *descriptor = get_descriptor(fd);
if (descriptor == NULL)
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
descriptor->Acquire();
RETURN_AND_SET_ERRNO(fd);
}
off_t
lseek(int fd, off_t offset, int whence)
{
Descriptor* descriptor = get_descriptor(fd);
if (descriptor == NULL)
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
status_t error = descriptor->Seek(offset, whence);
if (error != B_OK)
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
return descriptor->Offset();
}
int
ftruncate(int fd, off_t newSize)
{
dprintf("ftruncate() not implemented!\n");
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
}
ssize_t
read_pos(int fd, off_t offset, void *buffer, size_t bufferSize)
{
Descriptor *descriptor = get_descriptor(fd);
if (descriptor == NULL)
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
RETURN_AND_SET_ERRNO(descriptor->ReadAt(offset, buffer, bufferSize));
}
ssize_t
pread(int fd, void* buffer, size_t bufferSize, off_t offset)
{
return read_pos(fd, offset, buffer, bufferSize);
}
ssize_t
read(int fd, void *buffer, size_t bufferSize)
{
Descriptor *descriptor = get_descriptor(fd);
if (descriptor == NULL)
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
RETURN_AND_SET_ERRNO(descriptor->Read(buffer, bufferSize));
}
ssize_t
write_pos(int fd, off_t offset, const void *buffer, size_t bufferSize)
{
Descriptor *descriptor = get_descriptor(fd);
if (descriptor == NULL)
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
RETURN_AND_SET_ERRNO(descriptor->WriteAt(offset, buffer, bufferSize));
}
ssize_t
pwrite(int fd, const void* buffer, size_t bufferSize, off_t offset)
{
return write_pos(fd, offset, buffer, bufferSize);
}
ssize_t
write(int fd, const void *buffer, size_t bufferSize)
{
Descriptor *descriptor = get_descriptor(fd);
if (descriptor == NULL)
RETURN_AND_SET_ERRNO(B_FILE_ERROR);
RETURN_AND_SET_ERRNO(descriptor->Write(buffer, bufferSize));