-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
1751 lines (1601 loc) · 44.6 KB
/
driver.c
File metadata and controls
1751 lines (1601 loc) · 44.6 KB
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
/*******************************************************************************/
/* © Université de Lille, The Pip Development Team (2015-2025) */
/* Copyright (C) 2020-2025 Orange */
/* */
/* This software is a computer program whose purpose is to run a filesystem */
/* with in-place execution and memory isolation. */
/* */
/* This software is governed by the CeCILL license under French law and */
/* abiding by the rules of distribution of free software. You can use, */
/* modify and/ or redistribute the software under the terms of the CeCILL */
/* license as circulated by CEA, CNRS and INRIA at the following URL */
/* "http://www.cecill.info". */
/* */
/* As a counterpart to the access to the source code and rights to copy, */
/* modify and redistribute granted by the license, users are provided only */
/* with a limited warranty and the software's author, the holder of the */
/* economic rights, and the successive licensors have only limited */
/* liability. */
/* */
/* In this respect, the user's attention is drawn to the risks associated */
/* with loading, using, modifying and/or developing or reproducing the */
/* software by the user in light of its specific status of free software, */
/* that may mean that it is complicated to manipulate, and that also */
/* therefore means that it is reserved for developers and experienced */
/* professionals having in-depth computer knowledge. Users are therefore */
/* encouraged to load and test the software's suitability as regards their */
/* requirements in conditions enabling the security of their systems and/or */
/* data to be ensured and, more generally, to use and operate it in the */
/* same conditions as regards security. */
/* */
/* The fact that you are presently reading this means that you have had */
/* knowledge of the CeCILL license and that you accept its terms. */
/*******************************************************************************/
/*
* The following define is required in order to use strnlen(3)
* since glibc 2.10. Refer to the SYNOPSIS section of the
* strnlen(3) manual and the feature_test_macros(7) manual for
* more information
*/
#define _POSIX_C_SOURCE 200809L
/*
* libc includes
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
/*
* xipfs includes
*/
#include "include/buffer.h"
#include "include/desc.h"
#include "include/errno.h"
#include "include/file.h"
#include "include/flash.h"
#include "include/fs.h"
#include "include/path.h"
#include "include/xipfs.h"
/*
* Macro definitions
*/
/**
* @internal
*
* @def ST_NOSUID
*
* @brief The set-user-ID and set-group-ID bits are ignored by
* xipfs_execv(3) for executable files on this filesystem
*/
#define ST_NOSUID (2)
/**
* @internal
*
* @def MAX
*
* @brief Returns the maximum between x and y
*
* @param x The first variable to be compared
*
* @param y The second variable to be compared
*/
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
/**
* @internal
*
* @def UNUSED
*
* @brief The most versatile macro for disabling warnings
* related to unused variables
*
* @param x The unused variable name
*/
#define UNUSED(x) ((void)(x))
/*
* Helper functions
*/
/**
* @internal
*
* @pre base must be a pointer that references an accessible
* memory region
*
* @pre path must be a pointer that references a path which is
* accessible, null-terminated, starts with a slash, normalized,
* and shorter than XIPFS_PATH_MAX
*
* @brief Copy the base name component of path, into the memory
* region pointed to by base
*
* @param base A pointer to a memory region that respects the
* preconditions for storing the base name component
*
* @param path A pointer to a path that respects the
* preconditions
*/
static void
basename(char *base, const char *path)
{
const char *ptr, *start, *end;
size_t len, i;
assert(base != NULL);
assert(path != NULL);
assert(path[0] == '/');
if (path[1] == '\0') {
base[0] = '/';
base[1] = '\0';
return;
}
len = strnlen(path, XIPFS_PATH_MAX);
assert(len < XIPFS_PATH_MAX);
ptr = path + len - 1;
if (ptr[0] == '/') {
/* skip the trailing slash if the
* path ends with one */
ptr--;
}
end = ptr;
while (ptr > path && *ptr != '/') {
/* skip all characters that are not
* slashes */
ptr--;
}
/* skip the slash */
start = ptr + 1;
for (i = 0; start + i <= end; i++) {
/* copy the characters of the base
* name component until end */
base[i] = start[i];
}
base[i] = '\0';
}
/**
* @internal
*
* @pre xipfs_mp must be a pointer that references a memory
* region containing an xipfs mount point structure which is
* accessible and valid
*
* @pre xipfs_filp must be a pointer that references an
* accessible memory region
*
* @brief Remove a file by flushing the read/write buffer,
* consolidating the file system, and updating the internal
* xipfs file addresses of all open VFS file descriptor
* structures
*
* @param xipfs_mp A pointer to a memory region containing an
* xipfs mount point structure
*
* @param xipfs_filp A pointer to a memory region containing
* the accessible xipfs file structure of the xipfs file to
* remove
*
* @return Returns zero if the function succeeds or a negative
* value otherwise
*/
static int
sync_remove_file(xipfs_mount_t *mp, xipfs_file_t *filp)
{
xipfs_file_position_t reserved;
assert(mp != NULL);
assert(filp != NULL);
if (xipfs_buffer_flush() < 0) {
return -1;
}
reserved = filp->reserved;
if (xipfs_fs_remove(filp) < 0) {
return -1;
}
xipfs_desc_update(mp, filp, reserved);
return 0;
}
/**
* @internal
*
* @brief Checks if the xipfs mount point structure passed as
* an argument is valid
*
* @param xipfs_mp A pointer to a memory region containing an
* xipfs mount point structure
*
* @return Returns zero if the xipfs mount point structure
* passed as an argument is valid or a negative value otherwise
*/
static int
xipfs_mp_check(const xipfs_mount_t *mp)
{
unsigned int page;
if (mp == NULL) {
return -EFAULT;
}
if (mp->magic != XIPFS_MAGIC) {
return -EINVAL;
}
if ( (mp->mount_path == NULL) ||
(mp->mount_path[0] == '\0') ||
((mp->mount_path[0] == '/') && mp->mount_path[1] == '\0') ) {
return -EINVAL;
}
if (!xipfs_flash_in(mp->page_addr)) {
return -EINVAL;
}
if (mp->page_num == 0) {
return -EINVAL;
}
#if (XIPFS_NVM_NUMOF <= 0)
#error "XIPFS_NVM_NUMOF <= 0"
#endif
#if (XIPFS_NVM_PAGE_SIZE <= 0)
#error "XIPFS_NVM_PAGE_SIZE <= 0"
#endif
#if (! (XIPFS_FILE_POSITION_MAX > (0U)) )
#error "XIPFS_FILE_POSITION_MAX <= 0"
#endif
const size_t filesystem_max_pages_count = (size_t)XIPFS_FILE_POSITION_MAX / (size_t)XIPFS_NVM_PAGE_SIZE;
if (filesystem_max_pages_count == 0) {
return -EINVAL;
}
if (filesystem_max_pages_count < (size_t)XIPFS_NVM_NUMOF) {
return -EINVAL;
}
if (mp->page_num > (size_t)XIPFS_NVM_NUMOF) {
return -EINVAL;
}
/* Given that :
* - mp->page_num is a size_t,
* - xipfs_nvm_page returns an unsigned int,
* - both xipfs_fs_free_pages and xipfs_fs_get_page_number return an int,
* - (size_t)INT_MAX < (size_t)UINT_MAX <= SIZE_MAX,
* just ensure that mountpoint's page_num is less than INT_MAX to be consistent.
*/
if (mp->page_num > (size_t)INT_MAX) {
return -EINVAL;
}
/*
* Because :
* - mp->page_num is a size_t,
* - both xipfs_fs_free_pages and xipfs_fs_get_page_number return an int,
* - (size_t)INT_MAX < (size_t)UINT_MAX <= SIZE_MAX,
* let's check that xipfs_nvm_page does not return values greater than INT_MAX.
*/
page = xipfs_nvm_page(mp->page_addr);
if (page > (unsigned int)INT_MAX) {
return -EINVAL;
}
/* Check that the nvm range defined by the mountpoint is in the maximum
* pages count supported by the NVM.
*/
if (((unsigned int)XIPFS_NVM_NUMOF - page) < mp->page_num) {
return -EINVAL;
}
if (mp->mutex == NULL) {
return -EINVAL;
}
if (mp->execution_mutex == NULL) {
return -EINVAL;
}
return 0;
}
/**
* @internal
*
* @pre should be call after xipfs_mp_check
*/
static int
xipfs_file_desc_check(const xipfs_mount_t *mp, const xipfs_file_desc_t *descp)
{
uintptr_t start, end, filp;
assert(mp != NULL);
start = (uintptr_t)mp->page_addr;
end = start + mp->page_num * XIPFS_NVM_PAGE_SIZE;
if (descp == NULL) {
return -EFAULT;
}
if (descp->filp == NULL) {
return -EINVAL;
}
filp = (uintptr_t)descp->filp;
if (!(filp >= start && filp < end)) {
return -EINVAL;
}
/*
* Since xipfs_file_position_t is a signed type, ensure that
* the position is equal to or greater than zero.
*/
if (descp->pos < 0) {
return -EINVAL;
}
if (!((descp->flags & O_CREAT) == O_CREAT ||
(descp->flags & O_EXCL) == O_EXCL ||
(descp->flags & O_WRONLY) == O_WRONLY ||
(descp->flags & O_RDONLY) == O_RDONLY ||
(descp->flags & O_RDWR) == O_RDWR ||
(descp->flags & O_APPEND) == O_APPEND))
{
return -EINVAL;
}
return 0;
}
/*
* Operations on open files
*/
int
xipfs_close(xipfs_mount_t *mp, xipfs_file_desc_t *descp)
{
xipfs_file_position_t size;
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if ((uintptr_t)descp->filp == (uintptr_t)xipfs_infos_file) {
/* nothing to do */
return 0;
}
if ((ret = xipfs_file_desc_check(mp, descp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_tracked(descp)) < 0) {
return ret;
}
if ((size = xipfs_file_get_size(descp->filp)) < 0) {
return -EIO;
}
if (size < descp->pos) {
/* synchronise file size */
if (xipfs_file_set_size(descp->filp, descp->pos) < 0) {
return -EIO;
}
}
if ((ret = xipfs_file_desc_untrack(descp)) < 0) {
return ret;
}
return 0;
}
int
xipfs_fstat(xipfs_mount_t *mp, xipfs_file_desc_t *descp,
struct stat *buf)
{
off_t size, reserved;
int ret;
if (buf == NULL) {
return -EFAULT;
}
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if ( (descp != NULL) &&
((uintptr_t)descp->filp == (uintptr_t)xipfs_infos_file) ) {
/* cannot fstat(2) */
return -EBADF;
}
if ((ret = xipfs_file_desc_check(mp, descp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_tracked(descp)) < 0) {
return ret;
}
if ((size = (off_t)xipfs_file_get_size(descp->filp)) < 0) {
return -EIO;
}
if ((reserved = (off_t)xipfs_file_get_reserved(descp->filp)) < 0) {
return -EIO;
}
(void)memset(buf, 0, sizeof(*buf));
buf->st_dev = (dev_t)(uintptr_t)mp;
buf->st_ino = (ino_t)(uintptr_t)descp->filp;
buf->st_mode = S_IFREG;
buf->st_nlink = 1;
buf->st_size = MAX(size, (off_t)descp->pos);
buf->st_blksize = XIPFS_NVM_PAGE_SIZE;
buf->st_blocks = reserved / XIPFS_NVM_PAGE_SIZE;
return 0;
}
off_t
xipfs_lseek(xipfs_mount_t *mp, xipfs_file_desc_t *descp,
off_t off, int whence)
{
off_t max_pos, new_pos, size;
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_check(mp, descp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_tracked(descp)) < 0) {
return -EBADF;
}
/*
* No matter what architecture is used at compile time, both off_t and xipfs_file_position_t
* are signed types. Furthermore, sizeof(xipfs_file_position_t) <= sizeof(off_t).
* That means that values retrieved from xipfs_file_* can't overflow off_t.
* Then maxpos is necessary :
* - greater than or equal to 0,
* - less than or equal to XIPFS_FILE_POSITION_MAX.
*/
if ((uintptr_t)descp->filp != (uintptr_t)xipfs_infos_file) {
if ((max_pos = (off_t)xipfs_file_get_max_pos(descp->filp)) < 0) {
return -EIO;
}
if ((size = (off_t)xipfs_file_get_size(descp->filp)) < 0) {
return -EIO;
}
} else {
max_pos = (off_t)sizeof(xipfs_mount_t);
size = (off_t)sizeof(xipfs_mount_t);
}
assert(size <= max_pos);
assert(descp->pos <= max_pos);
switch (whence) {
case SEEK_SET:
if ( (off < 0) || (off >= max_pos) ) {
return -EINVAL;
}
new_pos = off;
break;
case SEEK_CUR:
if ( off < 0 ) {
if (off < -((off_t)descp->pos)) {
return -EINVAL;
}
} else {
const off_t remaining_bytes = max_pos - (off_t)descp->pos;
if ( remaining_bytes < off ) {
return -EINVAL;
}
}
new_pos = descp->pos + off;
break;
case SEEK_END:
new_pos = MAX((off_t)descp->pos, size);
if ( (off > 0) || (off < -new_pos) ) {
return -EINVAL;
}
new_pos += off;
break;
default:
return -EINVAL;
}
if (((off_t)descp->pos) > size && new_pos < ((off_t)descp->pos)) {
/* synchronise file size */
if (xipfs_file_set_size(descp->filp, descp->pos) < 0) {
return -EIO;
}
}
descp->pos = (xipfs_file_position_t)new_pos;
return new_pos;
}
int
xipfs_open(xipfs_mount_t *mp, xipfs_file_desc_t *descp,
const char *name, int flags, mode_t mode)
{
char buf[XIPFS_PATH_MAX];
xipfs_path_t xipath;
xipfs_file_t *filp;
size_t len;
xipfs_file_position_t pos;
int ret;
/* mode bits are ignored */
UNUSED(mode);
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if (descp == NULL) {
return -EFAULT;
}
if (name == NULL) {
return -EFAULT;
}
/* only these flags are supported */
#define XIPFS_SUPPORTED_FLAGS \
( \
O_CREAT | O_EXCL | O_APPEND \
)
const int flags_acc_mode = flags & O_ACCMODE;
const int flags_without_acc_mode = flags & (~O_ACCMODE);
switch(flags_acc_mode) {
case O_RDONLY :
/* fallthrough */
case O_WRONLY :
/* fallthrough */
case O_RDWR :
if ( ( (flags_without_acc_mode & ~XIPFS_SUPPORTED_FLAGS) != 0 )
&& ( (flags_without_acc_mode & XIPFS_SUPPORTED_FLAGS) == 0 ) )
return -EINVAL;
break;
default :
// Should not happen
return -EINVAL;
}
#undef XIPFS_SUPPORTED_FLAGS
len = strnlen(name, XIPFS_PATH_MAX);
if (len == XIPFS_PATH_MAX) {
return -ENAMETOOLONG;
}
/* virtual file handling */
basename(buf, name);
if (strncmp(buf, ".xipfs_infos", XIPFS_PATH_MAX) == 0) {
if ((flags & O_CREAT) == O_CREAT &&
(flags & O_EXCL) == O_EXCL) {
return -EEXIST;
}
if ((flags & O_WRONLY) == O_WRONLY ||
(flags & O_APPEND) == O_APPEND ||
(flags & O_RDWR) == O_RDWR) {
return -EACCES;
}
descp->filp = (void *)xipfs_infos_file;
descp->flags = flags;
descp->pos = 0;
return 0;
}
if (xipfs_path_new(mp, &xipath, name) < 0) {
return -EIO;
}
switch (xipath.info) {
case XIPFS_PATH_EXISTS_AS_FILE:
if ((flags & O_CREAT) == O_CREAT &&
(flags & O_EXCL) == O_EXCL) {
return -EEXIST;
}
filp = xipath.witness;
break;
case XIPFS_PATH_EXISTS_AS_EMPTY_DIR:
case XIPFS_PATH_EXISTS_AS_NONEMPTY_DIR:
return -EISDIR;
case XIPFS_PATH_INVALID_BECAUSE_NOT_DIRS:
return -ENOTDIR;
case XIPFS_PATH_INVALID_BECAUSE_NOT_FOUND:
return -ENOENT;
case XIPFS_PATH_CREATABLE:
if ((flags & O_CREAT) != O_CREAT) {
return -ENOENT;
}
if (xipath.path[xipath.len-1] == '/') {
return -EISDIR;
}
if (xipath.witness != NULL && !(xipath.dirname[0] == '/' &&
xipath.dirname[1] == '\0')) {
if (strcmp(xipath.witness->path, xipath.dirname) == 0) {
if (sync_remove_file(mp, xipath.witness) < 0) {
return -EIO;
}
}
}
if ((filp = xipfs_fs_new_file(mp, name, 0, 0)) == NULL) {
/* file creation failed */
if (xipfs_errno == XIPFS_ENOSPACE ||
xipfs_errno == XIPFS_EFULL) {
return -EDQUOT;
}
return -EIO;
}
break;
default:
return -EIO;
}
if ((flags & O_APPEND) == O_APPEND) {
if ((pos = xipfs_file_get_size(filp)) < 0) {
return -EIO;
}
} else {
pos = 0;
}
if ((ret = xipfs_file_desc_track(descp)) < 0) {
return ret;
}
descp->filp = filp;
descp->flags = flags;
descp->pos = pos;
return 0;
}
ssize_t
xipfs_read(xipfs_mount_t *mp, xipfs_file_desc_t *descp,
void *dest, size_t nbytes)
{
xipfs_file_position_t size;
size_t i;
int ret;
/** Special case : virtual file
* This code is used to retrieve the xipfs_mount_t where it is not provided
*/
if ( (descp != NULL) && ((uintptr_t)descp->filp == (uintptr_t)xipfs_infos_file) ) {
if (nbytes != sizeof(xipfs_mount_t)) {
return -EINVAL;
}
if ((nbytes > 0) && (descp->pos >= (xipfs_file_position_t)sizeof(xipfs_mount_t))) {
return -EIO;
}
for (i = 0; i < nbytes && i < sizeof(xipfs_mount_t); i++) {
((char *)dest)[i] = ((char *)mp)[i];
}
descp->pos = sizeof(xipfs_mount_t);
return i;
}
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_check(mp, descp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_tracked(descp)) < 0) {
return ret;
}
if (dest == NULL) {
return -EFAULT;
}
if (nbytes > XIPFS_FILE_POSITION_MAX_AS_SIZE_T) {
return -EINVAL;
}
switch(descp->flags & O_ACCMODE) {
case O_RDONLY :
/* fallthrough */
case O_RDWR :
break;
default :
return -EACCES;
}
if ((size = xipfs_file_get_size(descp->filp)) < 0) {
return -EIO;
}
if ((nbytes > 0) && (descp->pos >= size)) {
return -EIO;
}
for (i = 0; i < nbytes && descp->pos < size; i++) {
if (xipfs_file_read_8(descp->filp, descp->pos,
&((char *)dest)[i]) < 0) {
return -EIO;
}
descp->pos++;
}
return i;
}
ssize_t
xipfs_write(xipfs_mount_t *mp, xipfs_file_desc_t *descp,
const void *src, size_t nbytes)
{
xipfs_file_position_t max_pos;
size_t i;
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_check(mp, descp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_tracked(descp)) < 0) {
return -EBADF;
}
if (src == NULL) {
return -EFAULT;
}
if (nbytes > XIPFS_FILE_POSITION_MAX_AS_SIZE_T) {
return -EINVAL;
}
if (((descp->flags & O_WRONLY) != O_WRONLY) &&
((descp->flags & O_RDWR) != O_RDWR)) {
return -EACCES;
}
if ((uintptr_t)descp->filp == (uintptr_t)xipfs_infos_file) {
/* cannot write(2) */
return -EBADF;
}
if ((max_pos = xipfs_file_get_max_pos(descp->filp)) < 0) {
return -EIO;
}
if ((nbytes > 0) && (descp->pos >= max_pos)) {
return -EDQUOT;
}
for (i = 0; i < nbytes && descp->pos < max_pos; i++) {
if (xipfs_file_write_8(descp->filp, descp->pos,
((const char *)src)[i]) < 0) {
return -EIO;
}
descp->pos++;
}
return i;
}
int
xipfs_fsync(xipfs_mount_t *mp, xipfs_file_desc_t *descp,
off_t pos)
{
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_check(mp, descp)) < 0) {
return ret;
}
if ((ret = xipfs_file_desc_tracked(descp)) < 0) {
return ret;
}
if (((descp->flags & O_WRONLY) != O_WRONLY) &&
((descp->flags & O_RDWR) != O_RDWR)) {
return -EACCES;
}
if ( (pos < 0) || (pos > XIPFS_FILE_POSITION_MAX_AS_OFF_T) ) {
return -EINVAL;
}
if (xipfs_file_set_size(descp->filp, (xipfs_file_position_t)pos) < 0) {
return -EIO;
}
return 0;
}
/*
* Operations on open directories
*/
int
xipfs_opendir(xipfs_mount_t *mp, xipfs_dir_desc_t *descp,
const char *dirname)
{
xipfs_path_t xipath;
xipfs_file_t *headp;
size_t len;
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if (descp == NULL) {
return -EFAULT;
}
if (dirname == NULL) {
return -EFAULT;
}
if (dirname[0] == '\0') {
return -ENOENT;
}
len = strnlen(dirname, XIPFS_PATH_MAX);
if (len == XIPFS_PATH_MAX) {
return -ENAMETOOLONG;
}
xipfs_errno = XIPFS_OK;
headp = xipfs_fs_head(mp);
if (xipfs_errno != XIPFS_OK) {
return -EIO;
}
if ( (headp == NULL) && (dirname[0] == '/' && dirname[1] == '\0') ) {
/* this file system is empty, not an error
* the root of the file system is always present
*/
if ((ret = xipfs_dir_desc_track(descp)) < 0) {
return ret;
}
descp->dirname[0] = '/';
descp->dirname[1] = '\0';
descp->filp = headp;
return 0;
}
if (xipfs_path_new(mp, &xipath, dirname) < 0) {
return -EIO;
}
switch (xipath.info) {
case XIPFS_PATH_EXISTS_AS_FILE:
case XIPFS_PATH_INVALID_BECAUSE_NOT_DIRS:
return -ENOTDIR;
case XIPFS_PATH_EXISTS_AS_EMPTY_DIR:
case XIPFS_PATH_EXISTS_AS_NONEMPTY_DIR:
break;
case XIPFS_PATH_INVALID_BECAUSE_NOT_FOUND:
case XIPFS_PATH_CREATABLE:
case XIPFS_PATH_UNDEFINED:
return -ENOENT;
default:
return -EIO;
}
if ((ret = xipfs_dir_desc_track(descp)) < 0) {
return ret;
}
/* it is safe to use strcpy(3) here */
(void)strcpy(descp->dirname, dirname);
descp->filp = headp;
len = xipath.len;
if (descp->dirname[len-1] != '/') {
if (len+1 == XIPFS_PATH_MAX) {
return -ENAMETOOLONG;
}
/* ensure dirname ends with a slash to indicate it's a
* directory */
descp->dirname[len] = '/';
descp->dirname[len+1] = '\0';
}
return 0;
}
int
xipfs_readdir(xipfs_mount_t *mp, xipfs_dir_desc_t *descp,
xipfs_dirent_t *direntp)
{
size_t i, j;
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
/* TODO check descp integrity */
if (descp == NULL) {
return -EFAULT;
}
if (direntp == NULL) {
return -EFAULT;
}
if ((ret = xipfs_dir_desc_tracked(descp)) < 0) {
return ret;
}
xipfs_errno = XIPFS_OK;
while (descp->filp != NULL) {
i = 0;
while (i < XIPFS_PATH_MAX) {
if (descp->filp->path[i] != descp->dirname[i]) {
break;
}
if (descp->dirname[i] == '\0') {
break;
}
if (descp->filp->path[i] == '\0') {
break;
}
i++;
}
if (i == XIPFS_PATH_MAX) {
return -ENAMETOOLONG;
}
if (descp->dirname[i] == '\0') {
if (descp->filp->path[i] == '/') {
/* skip first slash */
i++;
}
j = i;
while (j < XIPFS_PATH_MAX) {
if (descp->filp->path[j] == '\0') {
direntp->dirname[j-i] = '\0';
break;
}
if (descp->filp->path[j] == '/') {
direntp->dirname[j-i] = '/';
direntp->dirname[j-i+1] = '\0';
break;
}
direntp->dirname[j-i] = descp->filp->path[j];
j++;
}
if (j == XIPFS_PATH_MAX) {
return -ENAMETOOLONG;
}
/* set the next file to the structure */
if ((descp->filp = xipfs_fs_next(descp->filp)) == NULL) {
if (xipfs_errno != XIPFS_OK) {
return -EIO;
}
}
/* entry was updated */
return 1;
}
descp->filp = xipfs_fs_next(descp->filp);
}
if (xipfs_errno != XIPFS_OK) {
return -EIO;
}
/* end of the directory */
return 0;
}
int
xipfs_closedir(xipfs_mount_t *mp, xipfs_dir_desc_t *descp)
{
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
/* XXX check descp integrity */
if (descp == NULL) {
return -EFAULT;
}
if ((ret = xipfs_dir_desc_tracked(descp)) < 0) {
return ret;
}
(void)memset(descp, 0, sizeof(*descp));
if ((ret = xipfs_dir_desc_untrack(descp)) < 0) {
return ret;
}
return 0;
}
/*
* Operations on mounted file systems
*/
int
xipfs_format(xipfs_mount_t *mp)
{
int ret;
if ((ret = xipfs_mp_check(mp)) < 0) {
return ret;
}
if (xipfs_fs_format(mp) < 0) {
return -EIO;
}
if ((ret = xipfs_desc_untrack_all(mp)) < 0) {
return ret;
}
return 0;
}