littlefs-rust-core 0.1.0

Port of LittleFS to unsafe rust
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
//! File operations. Per lfs.c lfs_file_opencfg_, lfs_file_close_, lfs_file_sync_, etc.

use crate::bd::bd::{lfs_bd_read, lfs_cache_drop, lfs_cache_zero};
use crate::dir::traverse::lfs_dir_getread;
use crate::dir::LfsMdir;
use crate::file::ctz::lfs_ctz_find;
use crate::file::LfsFile;
use crate::lfs_info::LfsFileConfig;
use crate::lfs_type::lfs_open_flags::{
    LFS_F_DIRTY, LFS_F_ERRED, LFS_F_INLINE, LFS_F_READING, LFS_F_WRITING, LFS_O_RDONLY,
};
use crate::lfs_type::lfs_type::LFS_TYPE_INLINESTRUCT;
use crate::tag::lfs_mktag;
use crate::types::LFS_BLOCK_INLINE;
use crate::types::{lfs_block_t, lfs_off_t, lfs_size_t};
use crate::util::lfs_min;

/// Per lfs.c lfs_file_opencfg_ (lines 3065-3236)
///
/// C:
/// ```c
/// static int lfs_file_opencfg_(lfs_t *lfs, lfs_file_t *file,
///         const char *path, int flags,
///         const struct lfs_file_config *cfg) {
/// #ifndef LFS_READONLY
///     // deorphan if we haven't yet, needed at most once after poweron
///     if ((flags & LFS_O_WRONLY) == LFS_O_WRONLY) {
///         int err = lfs_fs_forceconsistency(lfs);
///         if (err) {
///             return err;
///         }
///     }
/// #else
///     LFS_ASSERT((flags & LFS_O_RDONLY) == LFS_O_RDONLY);
/// #endif
///
///     // setup simple file details
///     int err;
///     file->cfg = cfg;
///     file->flags = flags;
///     file->pos = 0;
///     file->off = 0;
///     file->cache.buffer = NULL;
///
///     // allocate entry for file if it doesn't exist
///     lfs_stag_t tag = lfs_dir_find(lfs, &file->m, &path, &file->id);
///     if (tag < 0 && !(tag == LFS_ERR_NOENT && lfs_path_islast(path))) {
///         err = tag;
///         goto cleanup;
///     }
///
///     // get id, add to list of mdirs to catch update changes
///     file->type = LFS_TYPE_REG;
///     lfs_mlist_append(lfs, (struct lfs_mlist *)file);
///
/// #ifdef LFS_READONLY
///     if (tag == LFS_ERR_NOENT) {
///         err = LFS_ERR_NOENT;
///         goto cleanup;
/// #else
///     if (tag == LFS_ERR_NOENT) {
///         if (!(flags & LFS_O_CREAT)) {
///             err = LFS_ERR_NOENT;
///             goto cleanup;
///         }
///
///         // don't allow trailing slashes
///         if (lfs_path_isdir(path)) {
///             err = LFS_ERR_NOTDIR;
///             goto cleanup;
///         }
///
///         // check that name fits
///         lfs_size_t nlen = lfs_path_namelen(path);
///         if (nlen > lfs->name_max) {
///             err = LFS_ERR_NAMETOOLONG;
///             goto cleanup;
///         }
///
///         // get next slot and create entry to remember name
///         err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
///                 {LFS_MKTAG(LFS_TYPE_CREATE, file->id, 0), NULL},
///                 {LFS_MKTAG(LFS_TYPE_REG, file->id, nlen), path},
///                 {LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0), NULL}));
///
///         // it may happen that the file name doesn't fit in the metadata blocks, e.g., a 256 byte file name will
///         // not fit in a 128 byte block.
///         err = (err == LFS_ERR_NOSPC) ? LFS_ERR_NAMETOOLONG : err;
///         if (err) {
///             goto cleanup;
///         }
///
///         tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, 0);
///     } else if (flags & LFS_O_EXCL) {
///         err = LFS_ERR_EXIST;
///         goto cleanup;
/// #endif
///     } else if (lfs_tag_type3(tag) != LFS_TYPE_REG) {
///         err = LFS_ERR_ISDIR;
///         goto cleanup;
/// #ifndef LFS_READONLY
///     } else if (flags & LFS_O_TRUNC) {
///         // truncate if requested
///         tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0);
///         file->flags |= LFS_F_DIRTY;
/// #endif
///     } else {
///         // try to load what's on disk, if it's inlined we'll fix it later
///         tag = lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x700, 0x3ff, 0),
///                 LFS_MKTAG(LFS_TYPE_STRUCT, file->id, 8), &file->ctz);
///         if (tag < 0) {
///             err = tag;
///             goto cleanup;
///         }
///         lfs_ctz_fromle32(&file->ctz);
///     }
///
///     // fetch attrs
///     for (unsigned i = 0; i < file->cfg->attr_count; i++) {
///         // if opened for read / read-write operations
///         if ((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY) {
///             lfs_stag_t res = lfs_dir_get(lfs, &file->m,
///                     LFS_MKTAG(0x7ff, 0x3ff, 0),
///                     LFS_MKTAG(LFS_TYPE_USERATTR + file->cfg->attrs[i].type,
///                         file->id, file->cfg->attrs[i].size),
///                         file->cfg->attrs[i].buffer);
///             if (res < 0 && res != LFS_ERR_NOENT) {
///                 err = res;
///                 goto cleanup;
///             }
///         }
///
/// #ifndef LFS_READONLY
///         // if opened for write / read-write operations
///         if ((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY) {
///             if (file->cfg->attrs[i].size > lfs->attr_max) {
///                 err = LFS_ERR_NOSPC;
///                 goto cleanup;
///             }
///
///             file->flags |= LFS_F_DIRTY;
///         }
/// #endif
///     }
///
///     // allocate buffer if needed
///     if (file->cfg->buffer) {
///         file->cache.buffer = file->cfg->buffer;
///     } else {
///         file->cache.buffer = lfs_malloc(lfs->cfg->cache_size);
///         if (!file->cache.buffer) {
///             err = LFS_ERR_NOMEM;
///             goto cleanup;
///         }
///     }
///
///     // zero to avoid information leak
///     lfs_cache_zero(lfs, &file->cache);
///
///     if (lfs_tag_type3(tag) == LFS_TYPE_INLINESTRUCT) {
///         // load inline files
///         file->ctz.head = LFS_BLOCK_INLINE;
///         file->ctz.size = lfs_tag_size(tag);
///         file->flags |= LFS_F_INLINE;
///         file->cache.block = file->ctz.head;
///         file->cache.off = 0;
///         file->cache.size = lfs->cfg->cache_size;
///
///         // don't always read (may be new/trunc file)
///         if (file->ctz.size > 0) {
///             lfs_stag_t res = lfs_dir_get(lfs, &file->m,
///                     LFS_MKTAG(0x700, 0x3ff, 0),
///                     LFS_MKTAG(LFS_TYPE_STRUCT, file->id,
///                         lfs_min(file->cache.size, 0x3fe)),
///                     file->cache.buffer);
///             if (res < 0) {
///                 err = res;
///                 goto cleanup;
///             }
///         }
///     }
///
///     return 0;
///
/// cleanup:
///     // clean up lingering resources
/// #ifndef LFS_READONLY
///     file->flags |= LFS_F_ERRED;
/// #endif
///     lfs_file_close_(lfs, file);
///     return err;
/// }
/// ```
pub fn lfs_file_opencfg_(
    lfs: *mut crate::fs::Lfs,
    file: *mut LfsFile,
    path: *const i8,
    flags: i32,
    cfg: *const LfsFileConfig,
) -> i32 {
    use crate::block_alloc::alloc::lfs_alloc_ckpoint;
    use crate::dir::find::lfs_dir_find;
    use crate::dir::lfs_mlist::lfs_mlist_append;
    use crate::dir::traverse::lfs_dir_get;
    use crate::error::{
        LFS_ERR_EXIST, LFS_ERR_ISDIR, LFS_ERR_NAMETOOLONG, LFS_ERR_NOENT, LFS_ERR_NOMEM,
        LFS_ERR_NOSPC,
    };
    use crate::file::lfs_ctz::lfs_ctz_fromle32;
    use crate::fs::superblock::lfs_fs_forceconsistency;
    use crate::lfs_info::LfsAttr;
    use crate::lfs_type::lfs_open_flags::{LFS_O_CREAT, LFS_O_EXCL, LFS_O_TRUNC, LFS_O_WRONLY};
    use crate::lfs_type::lfs_type::{LFS_TYPE_CREATE, LFS_TYPE_REG, LFS_TYPE_USERATTR};
    use crate::tag::{lfs_mktag, lfs_tag_size, lfs_tag_type3};
    use crate::types::LFS_BLOCK_INLINE;
    use crate::util::{
        lfs_min, lfs_path_isdir, lfs_path_islast, lfs_path_namelen, lfs_path_slice_from_cstr,
    };

    let path_u8 = path as *const u8;
    unsafe {
        if (flags & 2) != 0 {
            let err = lfs_fs_forceconsistency(lfs);
            if err != 0 {
                return crate::lfs_pass_err!(err);
            }
        }

        let file_ref = &mut *file;
        file_ref.cfg = cfg;
        file_ref.flags = flags as u32;
        file_ref.pos = 0;
        file_ref.off = 0;
        file_ref.cache.buffer = core::ptr::null_mut();

        let mut path_ptr = path_u8;
        let mut tag = lfs_dir_find(lfs, &mut file_ref.m, &mut path_ptr, &mut file_ref.id);
        if tag < 0 && !(tag == LFS_ERR_NOENT && lfs_path_islast(lfs_path_slice_from_cstr(path_ptr)))
        {
            let err = tag;
            lfs_file_close_(lfs, file);
            return crate::lfs_pass_err!(err);
        }

        file_ref.type_ = LFS_TYPE_REG as u8;
        lfs_mlist_append(lfs, file as *mut crate::dir::LfsMlist);

        if tag == LFS_ERR_NOENT {
            if (flags & LFS_O_CREAT) == 0 {
                lfs_file_close_(lfs, file);
                return crate::lfs_err!(LFS_ERR_NOENT);
            }
            if lfs_path_isdir(lfs_path_slice_from_cstr(path_ptr)) {
                lfs_file_close_(lfs, file);
                return crate::error::LFS_ERR_NOTDIR;
            }
            let nlen = lfs_path_namelen(lfs_path_slice_from_cstr(path_ptr));
            if nlen > (*lfs).name_max {
                lfs_file_close_(lfs, file);
                return crate::lfs_err!(LFS_ERR_NAMETOOLONG);
            }
            unsafe { lfs_alloc_ckpoint(lfs) };
            let attrs = [
                crate::tag::lfs_mattr {
                    tag: lfs_mktag(LFS_TYPE_CREATE, file_ref.id as u32, 0),
                    buffer: core::ptr::null(),
                },
                crate::tag::lfs_mattr {
                    tag: lfs_mktag(LFS_TYPE_REG, file_ref.id as u32, nlen),
                    buffer: path_ptr as *const core::ffi::c_void,
                },
                crate::tag::lfs_mattr {
                    tag: lfs_mktag(LFS_TYPE_INLINESTRUCT, file_ref.id as u32, 0),
                    buffer: core::ptr::null(),
                },
            ];
            let err = crate::dir::commit::lfs_dir_commit(
                lfs,
                &mut file_ref.m,
                attrs.as_ptr() as *const _,
                3,
            );
            let err = if err == crate::error::LFS_ERR_NOSPC {
                LFS_ERR_NAMETOOLONG
            } else {
                err
            };
            if err != 0 {
                lfs_file_close_(lfs, file);
                return crate::lfs_pass_err!(err);
            }
        } else if (flags & LFS_O_EXCL) != 0 {
            lfs_file_close_(lfs, file);
            return crate::lfs_err!(LFS_ERR_EXIST);
        } else if u32::from(lfs_tag_type3(tag as u32)) != LFS_TYPE_REG {
            lfs_file_close_(lfs, file);
            return crate::lfs_err!(LFS_ERR_ISDIR);
        } else if (flags & LFS_O_TRUNC) != 0 {
            // C: lfs.c:100-104 — truncate if requested
            tag = lfs_mktag(LFS_TYPE_INLINESTRUCT, file_ref.id as u32, 0) as i32;
            file_ref.flags |= LFS_F_DIRTY as u32;
        } else {
            // C: tag = lfs_dir_get(...) — overwrite tag with STRUCT tag for later use
            let struct_tag = lfs_dir_get(
                lfs,
                &file_ref.m as *const _,
                lfs_mktag(0x700, 0x3ff, 0),
                lfs_mktag(
                    crate::lfs_type::lfs_type::LFS_TYPE_STRUCT,
                    file_ref.id as u32,
                    8,
                ),
                &mut file_ref.ctz as *mut _ as *mut core::ffi::c_void,
            );
            if struct_tag < 0 {
                lfs_file_close_(lfs, file);
                return struct_tag;
            }
            tag = struct_tag;
            lfs_ctz_fromle32(&mut file_ref.ctz);
        }

        // C: lfs.c:3162-3187 — fetch attrs
        if !cfg.is_null() && (*cfg).attr_count > 0 && !(*cfg).attrs.is_null() {
            let attr_count = (*cfg).attr_count as usize;
            for i in 0..attr_count {
                let attr = &*(*cfg).attrs.add(i);
                if (file_ref.flags as i32 & LFS_O_RDONLY) == LFS_O_RDONLY {
                    let res = lfs_dir_get(
                        lfs,
                        &file_ref.m as *const _,
                        lfs_mktag(0x7ff, 0x3ff, 0),
                        lfs_mktag(
                            LFS_TYPE_USERATTR + attr.type_ as u32,
                            file_ref.id as u32,
                            attr.size,
                        ),
                        attr.buffer,
                    );
                    if res < 0 && res != LFS_ERR_NOENT {
                        lfs_file_close_(lfs, file);
                        return res;
                    }
                }
                if (file_ref.flags as i32 & LFS_O_WRONLY) == LFS_O_WRONLY {
                    if attr.size > (*lfs).attr_max {
                        lfs_file_close_(lfs, file);
                        return crate::lfs_err!(LFS_ERR_NOSPC);
                    }
                    file_ref.flags |= LFS_F_DIRTY as u32;
                }
            }
        }

        if !cfg.is_null() && !(*cfg).buffer.is_null() {
            file_ref.cache.buffer = (*cfg).buffer as *mut u8;
        } else {
            #[cfg(feature = "alloc")]
            {
                file_ref.cache.buffer = crate::lfs_alloc_module::lfs_malloc(
                    (*lfs).cfg.as_ref().expect("cfg").cache_size,
                );
            }
            #[cfg(not(feature = "alloc"))]
            {
                lfs_file_close_(lfs, file);
                return crate::lfs_err!(LFS_ERR_NOMEM);
            }
            if file_ref.cache.buffer.is_null() {
                lfs_file_close_(lfs, file);
                return crate::lfs_err!(LFS_ERR_NOMEM);
            }
        }

        lfs_cache_zero(lfs as *const crate::fs::Lfs, &mut file_ref.cache);

        let tag_val = if tag == LFS_ERR_NOENT {
            lfs_mktag(LFS_TYPE_INLINESTRUCT, 0, 0) as i32
        } else {
            tag
        };
        if u32::from(lfs_tag_type3(tag_val as u32)) == LFS_TYPE_INLINESTRUCT {
            file_ref.ctz.head = LFS_BLOCK_INLINE;
            file_ref.ctz.size = if tag_val == LFS_ERR_NOENT {
                0
            } else {
                lfs_tag_size(tag_val as u32)
            };
            file_ref.flags |= LFS_F_INLINE as u32;
            file_ref.cache.block = file_ref.ctz.head;
            file_ref.cache.off = 0;
            file_ref.cache.size = (*lfs).cfg.as_ref().expect("cfg").cache_size;
            if file_ref.ctz.size > 0 {
                let res = lfs_dir_get(
                    lfs,
                    &file_ref.m as *const _,
                    lfs_mktag(0x700, 0x3ff, 0),
                    lfs_mktag(
                        crate::lfs_type::lfs_type::LFS_TYPE_STRUCT,
                        file_ref.id as u32,
                        lfs_min(file_ref.cache.size, 0x3fe),
                    ),
                    file_ref.cache.buffer as *mut core::ffi::c_void,
                );
                if res < 0 {
                    lfs_file_close_(lfs, file);
                    return res;
                }
            }
        }
    }
    0
}

/// Per lfs.c lfs_file_open_ (lines 3238-3244)
///
/// C: Wrapper that calls opencfg with default config.
/// Static defaults for lfs_file_open (no opencfg). C uses the same;
/// a stack-local would make file.cfg a dangling pointer after return.
static LFS_FILE_DEFAULTS: LfsFileConfig = LfsFileConfig {
    buffer: core::ptr::null_mut(),
    attrs: core::ptr::null_mut(),
    attr_count: 0,
};

pub fn lfs_file_open_(
    lfs: *mut crate::fs::Lfs,
    file: *mut LfsFile,
    path: *const i8,
    flags: i32,
) -> i32 {
    lfs_file_opencfg_(lfs, file, path, flags, &LFS_FILE_DEFAULTS)
}

/// Per lfs.c lfs_file_close_ (lines 3246-3264)
///
/// Translation docs: Sync if dirty, remove from mlist, free cache buffer if we allocated it.
///
/// C: lfs.c:3246-3264
pub fn lfs_file_close_(lfs: *mut crate::fs::Lfs, file: *mut LfsFile) -> i32 {
    use crate::dir::lfs_mlist::lfs_mlist_remove;

    let err = lfs_file_sync_(lfs, file);
    if err != 0 {
        return crate::lfs_pass_err!(err);
    }

    unsafe {
        lfs_mlist_remove(lfs, file as *mut crate::dir::LfsMlist);

        let cfg = (*file).cfg;
        if !cfg.is_null() && (*cfg).buffer.is_null() {
            #[cfg(feature = "alloc")]
            {
                crate::lfs_alloc_module::lfs_free(
                    (*file).cache.buffer,
                    (*lfs).cfg.as_ref().expect("cfg").cache_size,
                );
            }
        }
    }

    err
}

/// Translation docs: Relocates file data into a new block. For inline reads via
/// lfs_dir_getread; for CTZ via lfs_bd_read. Writes with lfs_bd_prog. Retries on LFS_ERR_CORRUPT.
///
/// Per lfs.c lfs_file_relocate (lines 3266-3335)
///
/// C:
/// ```c
/// static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
///     while (true) {
///         // just relocate what exists into new block
///         lfs_block_t nblock;
///         int err = lfs_alloc(lfs, &nblock);
///         if (err) {
///             return err;
///         }
///
///         err = lfs_bd_erase(lfs, nblock);
///         if (err) {
///             if (err == LFS_ERR_CORRUPT) {
///                 goto relocate;
///             }
///             return err;
///         }
///
///         // either read from dirty cache or disk
///         for (lfs_off_t i = 0; i < file->off; i++) {
///             uint8_t data;
///             if (file->flags & LFS_F_INLINE) {
///                 err = lfs_dir_getread(lfs, &file->m,
///                         // note we evict inline files before they can be dirty
///                         NULL, &file->cache, file->off-i,
///                         LFS_MKTAG(0xfff, 0x1ff, 0),
///                         LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0),
///                         i, &data, 1);
///                 if (err) {
///                     return err;
///                 }
///             } else {
///                 err = lfs_bd_read(lfs,
///                         &file->cache, &lfs->rcache, file->off-i,
///                         file->block, i, &data, 1);
///                 if (err) {
///                     return err;
///                 }
///             }
///
///             err = lfs_bd_prog(lfs,
///                     &lfs->pcache, &lfs->rcache, true,
///                     nblock, i, &data, 1);
///             if (err) {
///                 if (err == LFS_ERR_CORRUPT) {
///                     goto relocate;
///                 }
///                 return err;
///             }
///         }
///
///         // copy over new state of file
///         memcpy(file->cache.buffer, lfs->pcache.buffer, lfs->cfg->cache_size);
///         file->cache.block = lfs->pcache.block;
///         file->cache.off = lfs->pcache.off;
///         file->cache.size = lfs->pcache.size;
///         lfs_cache_zero(lfs, &lfs->pcache);
///
///         file->block = nblock;
///         file->flags |= LFS_F_WRITING;
///         return 0;
///
/// relocate:
///         LFS_DEBUG("Bad block at 0x%"PRIx32, nblock);
///
///         // just clear cache and try a new block
///         lfs_cache_drop(lfs, &lfs->pcache);
///     }
/// }
/// ```
pub fn lfs_file_relocate(lfs: *mut crate::fs::Lfs, file: *mut LfsFile) -> i32 {
    use crate::bd::bd::{lfs_bd_erase, lfs_bd_prog, lfs_bd_read, lfs_cache_drop, lfs_cache_zero};
    use crate::block_alloc::alloc::{lfs_alloc, lfs_alloc_lookahead};
    use crate::error::LFS_ERR_CORRUPT;

    'relocate: loop {
        unsafe {
            let mut nblock: lfs_block_t = 0;
            let err = lfs_alloc(lfs, &mut nblock);
            if err != 0 {
                return crate::lfs_pass_err!(err);
            }

            let err = lfs_bd_erase(lfs as *const crate::fs::Lfs, nblock);
            if err != 0 {
                if err == LFS_ERR_CORRUPT {
                    lfs_alloc_lookahead(lfs, nblock);
                    lfs_cache_drop(lfs, &mut (*lfs).pcache as *mut _);
                    continue 'relocate;
                }
                return crate::lfs_pass_err!(err);
            }

            let file_ref = &mut *file;
            let lfs_ref = &mut *lfs;

            for i in 0..file_ref.off {
                let mut data: u8 = 0;
                let err = if (file_ref.flags as i32 & LFS_F_INLINE) != 0 {
                    let gtag = lfs_mktag(LFS_TYPE_INLINESTRUCT, file_ref.id as u32, 0);
                    lfs_dir_getread(
                        lfs,
                        &file_ref.m,
                        core::ptr::null(),
                        &mut file_ref.cache,
                        file_ref.off - i,
                        lfs_mktag(0xfff, 0x1ff, 0),
                        gtag,
                        i,
                        &mut data as *mut u8 as *mut core::ffi::c_void,
                        1,
                    )
                } else {
                    lfs_bd_read(
                        lfs,
                        &file_ref.cache,
                        &mut lfs_ref.rcache,
                        file_ref.off - i,
                        file_ref.block,
                        i,
                        &mut data,
                        1,
                    )
                };
                if err != 0 {
                    return crate::lfs_pass_err!(err);
                }

                let err = lfs_bd_prog(
                    lfs as *const crate::fs::Lfs,
                    &mut lfs_ref.pcache,
                    &mut lfs_ref.rcache,
                    true,
                    nblock,
                    i,
                    &data,
                    1,
                );
                if err != 0 {
                    if err == LFS_ERR_CORRUPT {
                        lfs_alloc_lookahead(lfs, nblock);
                        lfs_cache_drop(lfs, &mut (*lfs).pcache as *mut _);
                        continue 'relocate;
                    }
                    return crate::lfs_pass_err!(err);
                }
            }

            {
                let lfs_ref = &mut *lfs;
                let pcache = &lfs_ref.pcache;
                let file_ref = &mut *file;
                if !file_ref.cache.buffer.is_null() && !pcache.buffer.is_null() {
                    let cache_size = lfs_ref.cfg.as_ref().expect("cfg").cache_size as usize;
                    core::ptr::copy_nonoverlapping(
                        pcache.buffer,
                        file_ref.cache.buffer,
                        cache_size,
                    );
                }
                file_ref.cache.block = pcache.block;
                file_ref.cache.off = pcache.off;
                file_ref.cache.size = pcache.size;
                file_ref.block = nblock;
                file_ref.flags |= LFS_F_WRITING as u32;
            }
            lfs_cache_zero(lfs, &mut (*lfs).pcache as *mut _);
            return 0;
        }
    }
}

/// Translation docs: Converts an inline file to CTZ when it exceeds inline_max.
/// Sets off=pos, alloc ckpoint, relocate, clears LFS_F_INLINE.
///
/// Per lfs.c lfs_file_outline (lines 3337-3348)
///
/// C:
/// ```c
/// static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) {
///     file->off = file->pos;
///     unsafe { lfs_alloc_ckpoint(lfs) };
///     int err = lfs_file_relocate(lfs, file);
///     if (err) {
///         return err;
///     }
///
///     file->flags &= ~LFS_F_INLINE;
///     return 0;
/// }
/// ```
pub fn lfs_file_outline(lfs: *mut crate::fs::Lfs, file: *mut LfsFile) -> i32 {
    use crate::block_alloc::alloc::lfs_alloc_ckpoint;

    unsafe {
        let file_ref = &mut *file;
        file_ref.off = file_ref.pos;
    }
    unsafe { lfs_alloc_ckpoint(lfs) };
    let err = lfs_file_relocate(lfs, file);
    if err != 0 {
        return crate::lfs_pass_err!(err);
    }
    unsafe {
        (*file).flags &= !LFS_F_INLINE as u32;
    }
    0
}

/// Per lfs.c lfs_file_flush (lines 3350-3429)
///
/// C:
/// ```c
/// static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
///     if (file->flags & LFS_F_READING) {
///         if (!(file->flags & LFS_F_INLINE)) {
///             lfs_cache_drop(lfs, &file->cache);
///         }
///         file->flags &= ~LFS_F_READING;
///     }
///
/// #ifndef LFS_READONLY
///     if (file->flags & LFS_F_WRITING) {
///         lfs_off_t pos = file->pos;
///
///         if (!(file->flags & LFS_F_INLINE)) {
///             // copy over anything after current branch
///             lfs_file_t orig = {
///                 .ctz.head = file->ctz.head,
///                 .ctz.size = file->ctz.size,
///                 .flags = LFS_O_RDONLY,
///                 .pos = file->pos,
///                 .cache = lfs->rcache,
///             };
///             lfs_cache_drop(lfs, &lfs->rcache);
///
///             while (file->pos < file->ctz.size) {
///                 // copy over a byte at a time, leave it up to caching
///                 // to make this efficient
///                 uint8_t data;
///                 lfs_ssize_t res = lfs_file_flushedread(lfs, &orig, &data, 1);
///                 if (res < 0) {
///                     return res;
///                 }
///
///                 res = lfs_file_flushedwrite(lfs, file, &data, 1);
///                 if (res < 0) {
///                     return res;
///                 }
///
///                 // keep our reference to the rcache in sync
///                 if (lfs->rcache.block != LFS_BLOCK_NULL) {
///                     lfs_cache_drop(lfs, &orig.cache);
///                     lfs_cache_drop(lfs, &lfs->rcache);
///                 }
///             }
///
///             // write out what we have
///             while (true) {
///                 int err = lfs_bd_flush(lfs, &file->cache, &lfs->rcache, true);
///                 if (err) {
///                     if (err == LFS_ERR_CORRUPT) {
///                         goto relocate;
///                     }
///                     return err;
///                 }
///
///                 break;
///
/// relocate:
///                 LFS_DEBUG("Bad block at 0x%"PRIx32, file->block);
///                 err = lfs_file_relocate(lfs, file);
///                 if (err) {
///                     return err;
///                 }
///             }
///         } else {
///             file->pos = lfs_max(file->pos, file->ctz.size);
///         }
///
///         // actual file updates
///         file->ctz.head = file->block;
///         file->ctz.size = file->pos;
///         file->flags &= ~LFS_F_WRITING;
///         file->flags |= LFS_F_DIRTY;
///
///         file->pos = pos;
///     }
/// #endif
///
///     return 0;
/// }
/// ```
pub fn lfs_file_flush(lfs: *const core::ffi::c_void, file: *mut LfsFile) -> i32 {
    use crate::bd::bd::lfs_bd_flush;
    use crate::error::LFS_ERR_CORRUPT;
    use crate::util::lfs_max;

    unsafe {
        let lfs = lfs as *mut crate::fs::Lfs;
        let file_ref = &mut *file;
        if (file_ref.flags as i32 & LFS_F_READING) != 0 {
            if (file_ref.flags as i32 & LFS_F_INLINE) == 0 {
                lfs_cache_drop(lfs as *const crate::fs::Lfs, &mut file_ref.cache);
            }
            file_ref.flags &= !(LFS_F_READING as u32);
        }

        if (file_ref.flags as i32 & LFS_F_WRITING) != 0 {
            let pos = file_ref.pos;
            if (file_ref.flags as i32 & LFS_F_INLINE) != 0 {
                file_ref.pos = lfs_max(pos, file_ref.ctz.size);
            } else {
                let lfs_ref = &mut *lfs;
                let mut orig = LfsFile {
                    next: core::ptr::null_mut(),
                    id: file_ref.id,
                    type_: file_ref.type_,
                    m: core::mem::zeroed(),
                    ctz: crate::file::lfs_ctz::LfsCtz {
                        head: file_ref.ctz.head,
                        size: file_ref.ctz.size,
                    },
                    flags: LFS_O_RDONLY as u32,
                    pos: file_ref.pos,
                    block: 0,
                    off: 0,
                    cache: core::ptr::read(&lfs_ref.rcache),
                    cfg: core::ptr::null(),
                };
                lfs_cache_drop(lfs as *const crate::fs::Lfs, &mut (*lfs).rcache);

                #[allow(clippy::while_immutable_condition)] // file.pos updated by flushedwrite
                while (*file).pos < (*file).ctz.size {
                    let mut data: u8 = 0;
                    let res = lfs_file_flushedread(
                        lfs,
                        &mut orig,
                        &mut data as *mut u8 as *mut core::ffi::c_void,
                        1,
                    );
                    if res < 0 {
                        return res as i32;
                    }
                    let res = lfs_file_flushedwrite(
                        lfs,
                        file,
                        &data as *const u8 as *const core::ffi::c_void,
                        1,
                    );
                    if res < 0 {
                        return res as i32;
                    }
                    if (*lfs).rcache.block != crate::types::LFS_BLOCK_NULL {
                        lfs_cache_drop(lfs as *const crate::fs::Lfs, &mut orig.cache);
                        lfs_cache_drop(lfs as *const crate::fs::Lfs, &mut (*lfs).rcache);
                    }
                }

                'flush: loop {
                    let err = lfs_bd_flush(lfs, &mut (*file).cache, &mut (*lfs).rcache, true);
                    if err != 0 {
                        if err == LFS_ERR_CORRUPT {
                            let err = lfs_file_relocate(lfs, file);
                            if err != 0 {
                                return crate::lfs_pass_err!(err);
                            }
                            continue 'flush;
                        }
                        return crate::lfs_pass_err!(err);
                    }
                    break;
                }
            }
            file_ref.ctz.head = file_ref.block;
            file_ref.ctz.size = file_ref.pos;
            file_ref.flags &= !(LFS_F_WRITING as u32);
            file_ref.flags |= LFS_F_DIRTY as u32;
            file_ref.pos = pos;
        }
    }
    0
}

/// Per lfs.c lfs_file_sync_ (lines 3431-3490)
///
/// C:
/// ```c
/// static int lfs_file_sync_(lfs_t *lfs, lfs_file_t *file) {
///     if (file->flags & LFS_F_ERRED) {
///         // it's not safe to do anything if our file errored
///         return 0;
///     }
///
///     int err = lfs_file_flush(lfs, file);
///     if (err) {
///         file->flags |= LFS_F_ERRED;
///         return err;
///     }
///
///     if ((file->flags & LFS_F_DIRTY) &&
///             !lfs_pair_isnull(file->m.pair)) {
///         // before we commit metadata, we need sync the disk to make sure
///         // data writes don't complete after metadata writes
///         if (!(file->flags & LFS_F_INLINE)) {
///             err = lfs_bd_sync(lfs, &lfs->pcache, &lfs->rcache, false);
///             if (err) {
///                 return err;
///             }
///         }
///
///         // update dir entry
///         uint16_t type;
///         const void *buffer;
///         lfs_size_t size;
///         struct lfs_ctz ctz;
///         if (file->flags & LFS_F_INLINE) {
///             // inline the whole file
///             type = LFS_TYPE_INLINESTRUCT;
///             buffer = file->cache.buffer;
///             size = file->ctz.size;
///         } else {
///             // update the ctz reference
///             type = LFS_TYPE_CTZSTRUCT;
///             // copy ctz so alloc will work during a relocate
///             ctz = file->ctz;
///             lfs_ctz_tole32(&ctz);
///             buffer = &ctz;
///             size = sizeof(ctz);
///         }
///
///         // commit file data and attributes
///         err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
///                 {LFS_MKTAG(type, file->id, size), buffer},
///                 {LFS_MKTAG(LFS_FROM_USERATTRS, file->id,
///                     file->cfg->attr_count), file->cfg->attrs}));
///         if (err) {
///             file->flags |= LFS_F_ERRED;
///             return err;
///         }
///
///         file->flags &= ~LFS_F_DIRTY;
///     }
///
///     return 0;
/// }
/// ```
pub fn lfs_file_sync_(lfs: *mut crate::fs::Lfs, file: *mut LfsFile) -> i32 {
    use crate::dir::commit::lfs_dir_commit;
    use crate::fs::superblock::lfs_fs_deorphan;
    use crate::tag::lfs_mktag;
    use crate::types::LFS_BLOCK_INLINE;
    use crate::util::lfs_pair_isnull;

    unsafe {
        let file_ref = &mut *file;
        if (file_ref.flags as i32 & 0x080000) != 0 {
            return 0;
        }

        let err = lfs_file_flush(lfs as *const core::ffi::c_void, file);
        if err != 0 {
            file_ref.flags |= 0x080000;
            return crate::lfs_pass_err!(err);
        }

        if (file_ref.flags as i32 & 0x010000) != 0 && !lfs_pair_isnull(&file_ref.m.pair) {
            let lfs_ref = &*lfs;
            let cfg = lfs_ref.cfg.as_ref().expect("cfg");

            if (file_ref.flags as i32 & LFS_F_INLINE) == 0 {
                let err =
                    crate::bd::bd::lfs_bd_sync(lfs, &mut (*lfs).pcache, &mut (*lfs).rcache, false);
                if err != 0 {
                    return crate::lfs_pass_err!(err);
                }
            }

            // C: copy ctz so alloc will work during a relocate
            // Must live through lfs_dir_commit — declared outside the if/else
            let mut ctz = file_ref.ctz;
            let (type_, buffer, size) = if (file_ref.flags as i32 & LFS_F_INLINE) != 0 {
                (
                    LFS_TYPE_INLINESTRUCT,
                    file_ref.cache.buffer as *const core::ffi::c_void,
                    file_ref.ctz.size,
                )
            } else {
                crate::file::lfs_ctz::lfs_ctz_tole32(&mut ctz);
                (
                    crate::lfs_type::lfs_type::LFS_TYPE_CTZSTRUCT,
                    &ctz as *const _ as *const core::ffi::c_void,
                    core::mem::size_of::<crate::file::LfsCtz>() as u32,
                )
            };

            let attrs = [
                crate::tag::lfs_mattr {
                    tag: lfs_mktag(type_, file_ref.id as u32, size),
                    buffer,
                },
                crate::tag::lfs_mattr {
                    tag: lfs_mktag(
                        crate::lfs_type::lfs_type::LFS_FROM_USERATTRS,
                        file_ref.id as u32,
                        file_ref.cfg.as_ref().map_or(0, |c| c.attr_count),
                    ) as u32,
                    buffer: file_ref.cfg.as_ref().map_or(core::ptr::null(), |c| c.attrs)
                        as *const core::ffi::c_void,
                },
            ];
            let err = lfs_dir_commit(lfs, &mut file_ref.m, attrs.as_ptr() as *const _, 2);
            if err != 0 {
                file_ref.flags |= 0x080000;
                return crate::lfs_pass_err!(err);
            }
            file_ref.flags &= !0x010000;
        }
    }
    0
}

/// Per lfs.c lfs_file_flushedread (lines 3492-3551)
///
/// Translation docs: Read file data. Handles inline and CTZ files.
/// Uses file cache for block caching; dir_getread for inline, bd_read for CTZ.
///
/// C: lfs.c:3493-3551
pub fn lfs_file_flushedread(
    lfs: *mut crate::fs::Lfs,
    file: *mut LfsFile,
    buffer: *mut core::ffi::c_void,
    size: lfs_size_t,
) -> crate::types::lfs_ssize_t {
    if buffer.is_null() {
        return 0;
    }
    let data = buffer as *mut u8;

    unsafe {
        let file_ref = &mut *file;
        let lfs_ref = &*lfs;
        let cfg = lfs_ref.cfg.as_ref().expect("cfg");
        let block_size = cfg.block_size;

        if file_ref.pos >= file_ref.ctz.size {
            return 0;
        }

        let size = lfs_min(size, file_ref.ctz.size - file_ref.pos);
        let mut nsize = size;

        let mut data = data;
        while nsize > 0 {
            if (file_ref.flags as i32 & LFS_F_READING) == 0 || file_ref.off == block_size {
                if (file_ref.flags as i32 & LFS_F_INLINE) == 0 {
                    let err = lfs_ctz_find(
                        lfs,
                        core::ptr::null(),
                        &mut file_ref.cache,
                        file_ref.ctz.head,
                        file_ref.ctz.size,
                        file_ref.pos,
                        &mut file_ref.block,
                        &mut file_ref.off,
                    );
                    if err != 0 {
                        return err as crate::types::lfs_ssize_t;
                    }
                } else {
                    file_ref.block = LFS_BLOCK_INLINE;
                    file_ref.off = file_ref.pos;
                }
                file_ref.flags |= LFS_F_READING as u32;
            }

            let diff = lfs_min(nsize, block_size - file_ref.off);
            if (file_ref.flags as i32 & LFS_F_INLINE) != 0 {
                let gtag = lfs_mktag(LFS_TYPE_INLINESTRUCT, file_ref.id as u32, 0);
                let err = lfs_dir_getread(
                    lfs,
                    &file_ref.m,
                    core::ptr::null(),
                    &mut file_ref.cache,
                    block_size,
                    lfs_mktag(0xfff, 0x1ff, 0),
                    gtag,
                    file_ref.off,
                    data as *mut core::ffi::c_void,
                    diff,
                );
                if err != 0 {
                    return err as crate::types::lfs_ssize_t;
                }
            } else {
                let err = lfs_bd_read(
                    lfs,
                    core::ptr::null(),
                    &mut file_ref.cache,
                    block_size,
                    file_ref.block,
                    file_ref.off,
                    data,
                    diff,
                );
                if err != 0 {
                    return err as crate::types::lfs_ssize_t;
                }
            }

            file_ref.pos += diff;
            file_ref.off += diff;
            data = data.add(diff as usize);
            nsize -= diff;
        }

        size as crate::types::lfs_ssize_t
    }
}

/// Per lfs.c lfs_file_read_ (lines 3553-3570)
///
/// Translation docs: Read file. Asserts RDONLY; flushes pending writes if any; delegates to flushedread.
///
/// C: lfs.c:3553-3570
pub fn lfs_file_read_(
    lfs: *mut crate::fs::Lfs,
    file: *mut LfsFile,
    buffer: *mut core::ffi::c_void,
    size: lfs_size_t,
) -> crate::types::lfs_ssize_t {
    crate::lfs_assert!((unsafe { (*file).flags as i32 } & LFS_O_RDONLY) == LFS_O_RDONLY);

    unsafe {
        if ((*file).flags as i32 & LFS_F_WRITING) != 0 {
            let err = lfs_file_flush(lfs as *const core::ffi::c_void, file);
            if err != 0 {
                return err as crate::types::lfs_ssize_t;
            }
        }
    }

    lfs_file_flushedread(lfs, file, buffer, size)
}

/// Translation docs: Writes file data. Outlines inline files that exceed inline_max.
/// For CTZ: ctz_find when extending, ctz_extend for new blocks, relocate on CORRUPT.
///
/// Per lfs.c lfs_file_flushedwrite (lines 3572-3654)
///
/// C:
/// ```c
/// static lfs_ssize_t lfs_file_flushedwrite(lfs_t *lfs, lfs_file_t *file,
///         const void *buffer, lfs_size_t size) {
///     const uint8_t *data = buffer;
///     lfs_size_t nsize = size;
///
///     if ((file->flags & LFS_F_INLINE) &&
///             lfs_max(file->pos+nsize, file->ctz.size) > lfs->inline_max) {
///         // inline file doesn't fit anymore
///         int err = lfs_file_outline(lfs, file);
///         if (err) {
///             file->flags |= LFS_F_ERRED;
///             return err;
///         }
///     }
///
///     while (nsize > 0) {
///         // check if we need a new block
///         if (!(file->flags & LFS_F_WRITING) ||
///                 file->off == lfs->cfg->block_size) {
///             if (!(file->flags & LFS_F_INLINE)) {
///                 if (!(file->flags & LFS_F_WRITING) && file->pos > 0) {
///                     // find out which block we're extending from
///                     int err = lfs_ctz_find(lfs, NULL, &file->cache,
///                             file->ctz.head, file->ctz.size,
///                             file->pos-1, &file->block, &(lfs_off_t){0});
///                     if (err) {
///                         file->flags |= LFS_F_ERRED;
///                         return err;
///                     }
///
///                     // mark cache as dirty since we may have read data into it
///                     lfs_cache_zero(lfs, &file->cache);
///                 }
///
///                 // extend file with new blocks
///                 unsafe { lfs_alloc_ckpoint(lfs) };
///                 int err = lfs_ctz_extend(lfs, &file->cache, &lfs->rcache,
///                         file->block, file->pos,
///                         &file->block, &file->off);
///                 if (err) {
///                     file->flags |= LFS_F_ERRED;
///                     return err;
///                 }
///             } else {
///                 file->block = LFS_BLOCK_INLINE;
///                 file->off = file->pos;
///             }
///
///             file->flags |= LFS_F_WRITING;
///         }
///
///         // program as much as we can in current block
///         lfs_size_t diff = lfs_min(nsize, lfs->cfg->block_size - file->off);
///         while (true) {
///             int err = lfs_bd_prog(lfs, &file->cache, &lfs->rcache, true,
///                     file->block, file->off, data, diff);
///             if (err) {
///                 if (err == LFS_ERR_CORRUPT) {
///                     goto relocate;
///                 }
///                 file->flags |= LFS_F_ERRED;
///                 return err;
///             }
///
///             break;
/// relocate:
///             err = lfs_file_relocate(lfs, file);
///             if (err) {
///                 file->flags |= LFS_F_ERRED;
///                 return err;
///             }
///         }
///
///         file->pos += diff;
///         file->off += diff;
///         data += diff;
///         nsize -= diff;
///
///         unsafe { lfs_alloc_ckpoint(lfs) };
///     }
///
///     return size;
/// }
/// ```
pub fn lfs_file_flushedwrite(
    lfs: *mut crate::fs::Lfs,
    file: *mut LfsFile,
    buffer: *const core::ffi::c_void,
    size: lfs_size_t,
) -> crate::types::lfs_ssize_t {
    use crate::bd::bd::{lfs_bd_prog, lfs_cache_zero};
    use crate::block_alloc::alloc::lfs_alloc_ckpoint;
    use crate::error::LFS_ERR_CORRUPT;
    use crate::file::ctz::{lfs_ctz_extend, lfs_ctz_find};

    if buffer.is_null() {
        return 0;
    }
    let data = buffer as *const u8;

    unsafe {
        let file_ref = &mut *file;
        let lfs_ref = &*lfs;
        let cfg = lfs_ref.cfg.as_ref().expect("cfg");
        let block_size = cfg.block_size;
        let mut nsize = size;

        if (file_ref.flags as i32 & LFS_F_INLINE) != 0
            && crate::util::lfs_max(file_ref.pos + nsize, file_ref.ctz.size) > lfs_ref.inline_max
        {
            let err = lfs_file_outline(lfs, file);
            if err != 0 {
                file_ref.flags |= LFS_F_ERRED as u32;
                return err as crate::types::lfs_ssize_t;
            }
        }

        let mut data = data;
        while nsize > 0 {
            if (file_ref.flags as i32 & LFS_F_WRITING) == 0 || file_ref.off == block_size {
                if (file_ref.flags as i32 & LFS_F_INLINE) != 0 {
                    file_ref.block = LFS_BLOCK_INLINE;
                    file_ref.off = file_ref.pos;
                } else {
                    if (file_ref.flags as i32 & LFS_F_WRITING) == 0 && file_ref.pos > 0 {
                        let mut block_off: lfs_off_t = 0;
                        let err = lfs_ctz_find(
                            lfs,
                            core::ptr::null(),
                            &mut (*lfs).rcache,
                            file_ref.ctz.head,
                            file_ref.ctz.size,
                            file_ref.pos - 1,
                            &mut file_ref.block,
                            &mut block_off,
                        );
                        if err != 0 {
                            file_ref.flags |= LFS_F_ERRED as u32;
                            return err as crate::types::lfs_ssize_t;
                        }
                        lfs_cache_zero(lfs, &mut file_ref.cache as *mut _);
                    }
                    unsafe { lfs_alloc_ckpoint(lfs) };
                    let err = lfs_ctz_extend(
                        lfs,
                        &mut (*file).cache,
                        &mut (*lfs).rcache,
                        (*file).block,
                        (*file).pos,
                        &mut (*file).block,
                        &mut (*file).off,
                    );
                    if err != 0 {
                        file_ref.flags |= LFS_F_ERRED as u32;
                        return err as crate::types::lfs_ssize_t;
                    }
                }
                file_ref.flags |= LFS_F_WRITING as u32;
            }

            let diff = lfs_min(nsize, block_size - file_ref.off);
            'prog: loop {
                let err = lfs_bd_prog(
                    lfs,
                    &mut file_ref.cache,
                    &mut (*lfs).rcache,
                    true,
                    file_ref.block,
                    file_ref.off,
                    data,
                    diff,
                );
                if err != 0 {
                    if err == LFS_ERR_CORRUPT {
                        let err = lfs_file_relocate(lfs, file);
                        if err != 0 {
                            file_ref.flags |= LFS_F_ERRED as u32;
                            return err as crate::types::lfs_ssize_t;
                        }
                        continue 'prog;
                    }
                    file_ref.flags |= LFS_F_ERRED as u32;
                    return err as crate::types::lfs_ssize_t;
                }
                break;
            }

            file_ref.pos += diff;
            file_ref.off += diff;
            data = data.add(diff as usize);
            nsize -= diff;

            unsafe { lfs_alloc_ckpoint(lfs) };
        }
        size as crate::types::lfs_ssize_t
    }
}

/// Per lfs.c lfs_file_write_ (lines 3656-3698)
///
/// C:
/// ```c
/// static lfs_ssize_t lfs_file_write_(lfs_t *lfs, lfs_file_t *file,
///         const void *buffer, lfs_size_t size) {
///     LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY);
///
///     if (file->flags & LFS_F_READING) {
///         // drop any reads
///         int err = lfs_file_flush(lfs, file);
///         if (err) {
///             return err;
///         }
///     }
///
///     if ((file->flags & LFS_O_APPEND) && file->pos < file->ctz.size) {
///         file->pos = file->ctz.size;
///     }
///
///     if (file->pos + size > lfs->file_max) {
///         // Larger than file limit?
///         return LFS_ERR_FBIG;
///     }
///
///     if (!(file->flags & LFS_F_WRITING) && file->pos > file->ctz.size) {
///         // fill with zeros
///         lfs_off_t pos = file->pos;
///         file->pos = file->ctz.size;
///
///         while (file->pos < pos) {
///             lfs_ssize_t res = lfs_file_flushedwrite(lfs, file, &(uint8_t){0}, 1);
///             if (res < 0) {
///                 return res;
///             }
///         }
///     }
///
///     lfs_ssize_t nsize = lfs_file_flushedwrite(lfs, file, buffer, size);
///     if (nsize < 0) {
///         return nsize;
///     }
///
///     file->flags &= ~LFS_F_ERRED;
///     return nsize;
/// }
/// ```
pub fn lfs_file_write_(
    lfs: *mut crate::fs::Lfs,
    file: *mut LfsFile,
    buffer: *const core::ffi::c_void,
    size: lfs_size_t,
) -> crate::types::lfs_ssize_t {
    crate::lfs_assert!((unsafe { (*file).flags as i32 } & 2) == 2);

    unsafe {
        if ((*file).flags as i32 & LFS_F_READING) != 0 {
            let err = lfs_file_flush(lfs as *const core::ffi::c_void, file);
            if err != 0 {
                return err as crate::types::lfs_ssize_t;
            }
        }
        if ((*file).flags as i32 & 0x0800) != 0 && (*file).pos < (*file).ctz.size {
            (*file).pos = (*file).ctz.size;
        }
        if (*file).pos + size > (*lfs).file_max {
            return crate::error::LFS_ERR_FBIG as crate::types::lfs_ssize_t;
        }

        // C: lfs.c:3677-3688 — zero-fill gap when writing past end of file
        if ((*file).flags as i32 & LFS_F_WRITING) == 0 && (*file).pos > (*file).ctz.size {
            let pos = (*file).pos;
            (*file).pos = (*file).ctz.size;
            let zero: u8 = 0;
            #[allow(clippy::while_immutable_condition)] // pos mutated via raw ptr in flushedwrite
            while (*file).pos < pos {
                let res = lfs_file_flushedwrite(
                    lfs,
                    file,
                    &zero as *const u8 as *const core::ffi::c_void,
                    1,
                );
                if res < 0 {
                    return res;
                }
            }
        }

        let nsize = lfs_file_flushedwrite(lfs, file, buffer, size);
        if nsize >= 0 {
            (*file).flags &= !0x080000;
        }
        nsize
    }
}

/// Per lfs.c lfs_file_seek_ (lines 3700-3751)
///
/// Translation docs: Seek to new position. SEEK_SET, SEEK_CUR, SEEK_END.
/// May avoid flush if new pos is in current cache (reading path).
///
/// C: lfs.c:3700-3751
pub fn lfs_file_seek_(
    lfs: *mut crate::fs::Lfs,
    file: *mut LfsFile,
    off: crate::types::lfs_soff_t,
    whence: i32,
) -> crate::types::lfs_soff_t {
    use crate::error::LFS_ERR_INVAL;
    use crate::file::ctz::lfs_ctz_index;
    use crate::lfs_type::lfs_whence_flags::{LFS_SEEK_CUR, LFS_SEEK_END, LFS_SEEK_SET};

    unsafe {
        let lfs_ref = &*lfs;
        let file_ref = &mut *file;
        let file_max = lfs_ref.file_max;
        let block_size = lfs_ref.cfg.as_ref().expect("cfg").block_size;

        let mut npos = file_ref.pos;
        if whence == LFS_SEEK_SET {
            npos = off as lfs_off_t;
        } else if whence == LFS_SEEK_CUR {
            npos = (file_ref.pos as i64 + off as i64) as lfs_off_t;
        } else if whence == LFS_SEEK_END {
            npos = (lfs_file_size_(lfs as *const core::ffi::c_void, file) as i64 + off as i64)
                as lfs_off_t;
        }

        if npos > file_max {
            return crate::lfs_err!(LFS_ERR_INVAL as crate::types::lfs_soff_t);
        }

        if file_ref.pos == npos {
            return npos as crate::types::lfs_soff_t;
        }

        if (file_ref.flags as i32 & LFS_F_READING) != 0 && file_ref.off != block_size {
            let mut opos = file_ref.pos;
            let mut npos_off = npos;
            let oindex = lfs_ctz_index(lfs as *const crate::fs::Lfs, &mut opos);
            let nindex = lfs_ctz_index(lfs as *const crate::fs::Lfs, &mut npos_off);
            if oindex == nindex
                && npos_off >= file_ref.cache.off
                && npos_off < file_ref.cache.off + file_ref.cache.size
            {
                file_ref.pos = npos;
                file_ref.off = npos_off;
                return npos as crate::types::lfs_soff_t;
            }
        }

        let err = lfs_file_flush(lfs as *const core::ffi::c_void, file);
        if err != 0 {
            return err as crate::types::lfs_soff_t;
        }

        (*file).pos = npos;
        npos as crate::types::lfs_soff_t
    }
}

/// Translation docs: Truncates file to size. Shrink: revert to inline if size <= inline_max,
/// else flush and update CTZ head/size. Grow: seek end, write zeros. Restores pos.
///
/// Per lfs.c lfs_file_truncate_ (lines 3753-3838)
///
/// C:
/// ```c
/// static int lfs_file_truncate_(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
///     LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY);
///
///     if (size > LFS_FILE_MAX) {
///         return LFS_ERR_INVAL;
///     }
///
///     lfs_off_t pos = file->pos;
///     lfs_off_t oldsize = lfs_file_size_(lfs, file);
///     if (size < oldsize) {
///         // revert to inline file?
///         if (size <= lfs->inline_max) {
///             // flush+seek to head
///             lfs_soff_t res = lfs_file_seek_(lfs, file, 0, LFS_SEEK_SET);
///             if (res < 0) {
///                 return (int)res;
///             }
///
///             // read our data into rcache temporarily
///             lfs_cache_drop(lfs, &lfs->rcache);
///             res = lfs_file_flushedread(lfs, file,
///                     lfs->rcache.buffer, size);
///             if (res < 0) {
///                 return (int)res;
///             }
///
///             file->ctz.head = LFS_BLOCK_INLINE;
///             file->ctz.size = size;
///             file->flags |= LFS_F_DIRTY | LFS_F_READING | LFS_F_INLINE;
///             file->cache.block = file->ctz.head;
///             file->cache.off = 0;
///             file->cache.size = lfs->cfg->cache_size;
///             memcpy(file->cache.buffer, lfs->rcache.buffer, size);
///
///         } else {
///             // need to flush since directly changing metadata
///             int err = lfs_file_flush(lfs, file);
///             if (err) {
///                 return err;
///             }
///
///             // lookup new head in ctz skip list
///             err = lfs_ctz_find(lfs, NULL, &file->cache,
///                     file->ctz.head, file->ctz.size,
///                     size-1, &file->block, &(lfs_off_t){0});
///             if (err) {
///                 return err;
///             }
///
///             // need to set pos/block/off consistently so seeking back to
///             // the old position does not get confused
///             file->pos = size;
///             file->ctz.head = file->block;
///             file->ctz.size = size;
///             file->flags |= LFS_F_DIRTY | LFS_F_READING;
///         }
///     } else if (size > oldsize) {
///         // flush+seek if not already at end
///         lfs_soff_t res = lfs_file_seek_(lfs, file, 0, LFS_SEEK_END);
///         if (res < 0) {
///             return (int)res;
///         }
///
///         // fill with zeros
///         while (file->pos < size) {
///             res = lfs_file_write_(lfs, file, &(uint8_t){0}, 1);
///             if (res < 0) {
///                 return (int)res;
///             }
///         }
///     }
///
///     // restore pos
///     lfs_soff_t res = lfs_file_seek_(lfs, file, pos, LFS_SEEK_SET);
///     if (res < 0) {
///       return (int)res;
///     }
///
///     return 0;
/// }
/// #endif
/// ```
pub fn lfs_file_truncate_(lfs: *mut crate::fs::Lfs, file: *mut LfsFile, size: lfs_off_t) -> i32 {
    use crate::error::LFS_ERR_INVAL;
    use crate::file::ctz::lfs_ctz_find;
    use crate::lfs_type::lfs_whence_flags::{LFS_SEEK_END, LFS_SEEK_SET};

    crate::lfs_assert!((unsafe { (*file).flags as i32 } & 2) == 2);

    unsafe {
        let lfs_ref = &*lfs;
        let file_ref = &mut *file;
        if size > lfs_ref.file_max {
            return crate::lfs_err!(LFS_ERR_INVAL);
        }

        let pos = file_ref.pos;
        let oldsize = lfs_file_size_(lfs as *const core::ffi::c_void, file) as lfs_off_t;

        if size < oldsize {
            if size <= lfs_ref.inline_max {
                // C: lfs.c:3762-3786 — revert to inline
                let res = lfs_file_seek_(lfs, file, 0, LFS_SEEK_SET);
                if res < 0 {
                    return res as i32;
                }

                // Read existing data from CTZ blocks into rcache temporarily
                crate::bd::bd::lfs_cache_drop(lfs, &mut (*lfs).rcache as *mut _);
                let res = lfs_file_flushedread(
                    lfs,
                    file,
                    (*lfs).rcache.buffer as *mut core::ffi::c_void,
                    size,
                );
                if res < 0 {
                    return res as i32;
                }

                file_ref.ctz.head = LFS_BLOCK_INLINE;
                file_ref.ctz.size = size;
                file_ref.flags |= (LFS_F_DIRTY | LFS_F_READING | LFS_F_INLINE) as u32;
                file_ref.cache.block = file_ref.ctz.head;
                file_ref.cache.off = 0;
                file_ref.cache.size = lfs_ref.cfg.as_ref().expect("cfg").cache_size;

                // Copy data from rcache into file cache
                core::ptr::copy_nonoverlapping(
                    (*lfs).rcache.buffer,
                    file_ref.cache.buffer,
                    size as usize,
                );
            } else {
                // C: lfs.c:3787-3806 — shrink CTZ
                let err = lfs_file_flush(lfs as *const core::ffi::c_void, file);
                if err != 0 {
                    return crate::lfs_pass_err!(err);
                }

                let mut off_zero: lfs_off_t = 0;
                let err = lfs_ctz_find(
                    lfs,
                    core::ptr::null(),
                    &mut file_ref.cache,
                    file_ref.ctz.head,
                    file_ref.ctz.size,
                    size.saturating_sub(1),
                    &mut file_ref.block,
                    &mut off_zero,
                );
                if err != 0 {
                    return crate::lfs_pass_err!(err);
                }

                file_ref.pos = size;
                file_ref.ctz.head = file_ref.block;
                file_ref.ctz.size = size;
                file_ref.flags |= (LFS_F_DIRTY | LFS_F_READING) as u32;
            }
        } else if size > oldsize {
            // C: lfs.c:3807-3818 — grow
            let res = lfs_file_seek_(lfs, file, 0, LFS_SEEK_END);
            if res < 0 {
                return res as i32;
            }

            let mut zero = 0u8;
            #[allow(clippy::while_immutable_condition)] // file.pos updated by lfs_file_write_
            while file_ref.pos < size {
                let res =
                    lfs_file_write_(lfs, file, &zero as *const u8 as *const core::ffi::c_void, 1);
                if res < 0 {
                    return res as i32;
                }
            }
        }

        let res = lfs_file_seek_(lfs, file, pos as i32, LFS_SEEK_SET);
        if res < 0 {
            return res as i32;
        }
    }
    0
}

/// Per lfs.c lfs_file_tell_ (lines 3835-3838)
///
/// Translation docs: Returns the current file position.
///
/// C:
/// ```c
/// static lfs_soff_t lfs_file_tell_(lfs_t *lfs, lfs_file_t *file) {
///     (void)lfs;
///     return file->pos;
/// }
/// ```
pub fn lfs_file_tell_(
    _lfs: *const core::ffi::c_void,
    file: *const LfsFile,
) -> crate::types::lfs_soff_t {
    unsafe { (*file).pos as crate::types::lfs_soff_t }
}

/// Per lfs.c lfs_file_rewind_ (lines 3840-3850)
///
/// Translation docs: Seek to start of file.
///
/// C: lfs.c:3840-3850
pub fn lfs_file_rewind_(lfs: *mut crate::fs::Lfs, file: *mut LfsFile) -> i32 {
    let res = lfs_file_seek_(
        lfs,
        file,
        0,
        crate::lfs_type::lfs_whence_flags::LFS_SEEK_SET,
    );
    if res < 0 {
        return res as i32;
    }
    0
}

/// Per lfs.c lfs_file_size_ (lines 3849-3858)
///
/// C:
/// ```c
/// static lfs_soff_t lfs_file_size_(lfs_t *lfs, lfs_file_t *file) {
///     (void)lfs;
/// #ifndef LFS_READONLY
///     if (file->flags & LFS_F_WRITING) {
///         return lfs_max(file->pos, file->ctz.size);
///     }
/// #endif
///     return file->ctz.size;
/// }
/// ```
pub fn lfs_file_size_(
    _lfs: *const core::ffi::c_void,
    file: *const LfsFile,
) -> crate::types::lfs_soff_t {
    unsafe {
        if ((*file).flags as i32 & LFS_F_WRITING) != 0 {
            return crate::util::lfs_max((*file).pos, (*file).ctz.size) as crate::types::lfs_soff_t;
        }
        (*file).ctz.size as crate::types::lfs_soff_t
    }
}