forked from apple/containerization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXT4+Formatter.swift
More file actions
1341 lines (1276 loc) · 63.4 KB
/
EXT4+Formatter.swift
File metadata and controls
1341 lines (1276 loc) · 63.4 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
//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
// swiftlint: disable discouraged_direct_init shorthand_operator syntactic_sugar
import ContainerizationOS
import Foundation
import SystemPackage
extension EXT4 {
/// The `EXT4.Formatter` class provides methods to format a block device with the ext4 filesystem.
/// It allows customization of block size and maximum disk size.
public class Formatter {
private let blockSize: UInt32
private var size: UInt64
private let groupDescriptorSize: UInt32 = 32
private var blocksPerGroup: UInt32 {
blockSize * 8
}
private var maxInodesPerGroup: UInt32 {
blockSize * 8 // limited by inode bitmap
}
private var groupsPerDescriptorBlock: UInt32 {
blockSize / groupDescriptorSize
}
private var blockCount: UInt32 {
((size - 1) / blockSize) + 1
}
private var groupCount: UInt32 {
(blockCount - 1) / blocksPerGroup + 1
}
private var groupDescriptorBlocks: UInt32 {
((groupCount - 1) / groupsPerDescriptorBlock + 1) * 32
}
/// Initializes an ext4 filesystem formatter.
///
/// This constructor creates an instance of the ext4 formatter designed to format a block device
/// with the ext4 filesystem. The formatter takes the path to the destination block device and
/// the desired block size of the filesystem as parameters.
///
/// - Parameters:
/// - devicePath: The path to the block device where the ext4 filesystem will be created.
/// - blockSize: The block size of the ext4 filesystem, specified in bytes. Common values are
/// 4096 (4KB) or 1024 (1KB). Default is 4096 (4KB)
/// - minDiskSize: The minimum disk size required for the formatted filesystem.
///
/// - Note: This ext4 formatter is designed for creating block devices out of container images and does not support all the
/// features and options available in the full ext4 filesystem implementation. It focuses
/// on the core functionality required for formatting a block device with ext4.
///
/// - Important: Ensure that the destination block device is accessible and has sufficient permissions
/// for formatting. The formatting process will erase all existing data on the device.
public init(_ devicePath: FilePath, blockSize: UInt32 = 4096, minDiskSize: UInt64 = 256.kib()) throws {
/// The constructor performs the following steps:
///
/// 1. Creates the first 10 inodes:
/// - Inode 2 is reserved for the root directory ('/').
/// - Inodes 1 and 3-10 are reserved for other special purposes.
///
/// 2. Marks inode 11 as the first inode available for consumption by files, directories, sockets,
/// FIFOs, etc.
///
/// 3. Initializes a directory tree with the root directory pointing to inode 2.
///
/// 4. Moves the file descriptor to the start of the block where file metadata and data can be
/// written, which is located past the filesystem superblocks and group descriptor blocks.
///
/// 5. Creates a "/lost+found" directory to satisfy the requirements of e2fsck (ext2/3/4 filesystem
/// checker).
if !FileManager.default.fileExists(atPath: devicePath.description) {
FileManager.default.createFile(atPath: devicePath.description, contents: nil)
}
guard let fileHandle = FileHandle(forWritingTo: devicePath) else {
throw Error.notFound(devicePath)
}
self.handle = fileHandle
self.blockSize = blockSize
self.size = minDiskSize
// make this a 0 byte file
guard ftruncate(self.handle.fileDescriptor, 0) == 0 else {
throw Error.cannotTruncateFile(devicePath)
}
// make it a sparse file
guard lseek(self.handle.fileDescriptor, off_t(self.size - 1), 0) == self.size - 1 else {
throw Error.cannotCreateSparseFile(devicePath)
}
let zero: [UInt8] = [0]
try self.handle.write(contentsOf: zero)
// step #1
self.inodes = [
Ptr<Inode>.allocate(capacity: 1), // defective block inode
{
let root = Inode.Root()
let rootPtr = Ptr<Inode>.allocate(capacity: 1)
rootPtr.initialize(to: root)
return rootPtr
}(),
]
// reserved inodes
for _ in 2..<EXT4.FirstInode - 1 {
inodes.append(Ptr<Inode>.allocate(capacity: 1))
}
// step #2
self.tree = FileTree(EXT4.RootInode, "/")
// skip past the superblock and block descriptor table
try self.seek(block: self.groupDescriptorBlocks + 1)
// lost+found directory is required for e2fsck to pass
try self.create(path: FilePath("/lost+found"), mode: Inode.Mode(.S_IFDIR, 0o700))
}
// Creates a hard link at the path specified by `link` that points to the same file or directory as the path specified by `target`.
//
// A hard link is a directory entry that points to the same inode as another directory entry. It allows multiple paths to refer to the same file on the file system.
//
// - `link`: The path at which to create the new hard link.
// - `target`: The path of the existing file or directory to which the hard link should point.
//
// Throws an error if `target` path does not exist, or `target` is a directory.
public func link(
link: FilePath,
target: FilePath
) throws {
// ensure that target exists
guard let targetPtr = self.tree.lookup(path: target) else {
throw Error.notFound(target)
}
let targetNode = targetPtr.pointee
let targetInodePtr = self.inodes[Int(targetNode.inode) - 1]
var targetInode = targetInodePtr.pointee
// ensure that target is not a directory since hardlinks cannot be
// created to directories
if targetInode.mode.isDir() {
throw Error.cannotCreateHardlinksToDirTarget(link)
}
targetInode.linksCount += 1
targetInodePtr.initialize(to: targetInode)
let parentPath: FilePath = link.dir
if self.tree.lookup(path: link) != nil {
try self.unlink(path: link)
}
guard let parentTreeNodePtr = self.tree.lookup(path: parentPath) else {
throw Error.notFound(parentPath)
}
let parentTreeNode = parentTreeNodePtr.pointee
let parentInodePtr = self.inodes[Int(parentTreeNode.inode) - 1]
let parentInode = parentInodePtr.pointee
guard parentInode.linksCount < EXT4.MaxLinks else {
throw Error.maximumLinksExceeded(parentPath)
}
let linkTreeNodePtr = Ptr<FileTree.FileTreeNode>.allocate(capacity: 1)
let linkTreeNode = FileTree.FileTreeNode(
inode: InodeNumber(2), // this field is ignored, using 2 so array operations dont panic
name: link.base,
parent: parentTreeNodePtr,
children: [],
blocks: nil,
link: targetNode.inode
)
linkTreeNodePtr.initialize(to: linkTreeNode)
parentTreeNode.children.append(linkTreeNodePtr)
parentTreeNodePtr.initialize(to: parentTreeNode)
}
// Deletes the file or directory at the specified path from the filesystem.
//
// It performs the following actions
// - set link count of the file's inode to 0
// - recursively set link count to 0 for its children
// - free the inode
// - free data blocks
// - remove directory entry
//
// - `path`: The `FilePath` specifying the path of the file or directory to delete.
public func unlink(path: FilePath, directoryWhiteout: Bool = false) throws {
guard let pathPtr = self.tree.lookup(path: path) else {
// We are being asked to unlink something that does not exist. Ignore
return
}
let pathNode = pathPtr.pointee
let inodeNumber = Int(pathNode.inode) - 1
let pathInodePtr = self.inodes[inodeNumber]
var pathInode = pathInodePtr.pointee
if directoryWhiteout && !pathInode.mode.isDir() {
throw Error.notDirectory(path)
}
for childPtr in pathNode.children {
try self.unlink(path: path.join(childPtr.pointee.name))
}
guard !directoryWhiteout else {
return
}
if let parentNodePtr = self.tree.lookup(path: path.dir) {
let parentNode = parentNodePtr.pointee
let parentInodePtr = self.inodes[Int(parentNode.inode) - 1]
var parentInode = parentInodePtr.pointee
if pathInode.mode.isDir() {
if parentInode.linksCount > 2 {
parentInode.linksCount -= 1
}
}
parentInodePtr.initialize(to: parentInode)
parentNode.children.removeAll { childPtr in
childPtr.pointee.name == path.base
}
parentNodePtr.initialize(to: parentNode)
}
if let hardlink = pathNode.link {
// the file we are deleting is a hardlink, decrement the link count
let linkedInodePtr = self.inodes[Int(hardlink - 1)]
var linkedInode = linkedInodePtr.pointee
if linkedInode.linksCount > 2 {
linkedInode.linksCount -= 1
linkedInodePtr.initialize(to: linkedInode)
}
}
guard inodeNumber > FirstInode else {
// Free the inodes and the blocks related to the inode only if its valid
return
}
if let blocks = pathNode.blocks {
if !(blocks.start == blocks.end) {
self.deletedBlocks.append((start: blocks.start, end: blocks.end))
}
}
for block in pathNode.additionalBlocks ?? [] {
self.deletedBlocks.append((start: block.start, end: block.end))
}
let now = Date().fs()
pathInode = Inode()
pathInode.dtime = now.lo
pathInodePtr.initialize(to: pathInode)
}
// Creates a file, directory, or symlink at the specified path, recursively creating parent directories if they don't already exist.
//
// - Parameters:
// - path: The FilePath representing the path where the file, directory, or symlink should be created.
// - link: An optional FilePath representing the target path for a symlink. If `nil`, a regular file or directory will be created. Preceding '/' should be omitted
// - mode: The permissions to set for the created file, directory, or symlink.
// - buf: An `InputStream` object providing the contents for the created file. Ignored when creating directories or symlinks.
//
// - Note:
// - This function recursively creates parent directories if they don't already exist. The `uid` and `gid` of the created parent directories are set to the values of their parent's `uid` and `gid`.
// - It is expected that the user sets the permissions explicitly later
// - This function only supports creating files, directories, and symlinks. Attempting to create other types of file system objects will result in an error.
// - In case of symlinks, the preceding '/' should be omitted
//
// - Example usage:
// ```swift
// let formatter = EXT4.Formatter(devicePath: "ext4.img")
// // create a directory
// try formatter.create(path: FilePath("/dir"),
// mode: EXT4.Inode.Mode(.S_IFDIR, 0o700))
//
// // create a file
// let inputStream = InputStream(data: "data".data(using: .utf8)!)
// inputStream.open()
// try formatter.create(path: FilePath("/dir/file"),
// mode: EXT4.Inode.Mode(.S_IFREG, 0o755), buf: inputStream)
// inputStream.close()
//
// // create a symlink
// try formatter.create(path: FilePath("/symlink"), link: "/dir/file",
// mode: EXT4.Inode.Mode(.S_IFLNK, 0o700))
// ```
public func create(
path: FilePath,
link: FilePath? = nil, // to create symbolic links
mode: UInt16,
ts: FileTimestamps = FileTimestamps(),
buf: InputStream? = nil,
uid: UInt32? = nil,
gid: UInt32? = nil,
xattrs: [String: Data]? = nil,
recursion: Bool = false
) throws {
if let nodePtr = self.tree.lookup(path: path) {
let node = nodePtr.pointee
let inodePtr = self.inodes[Int(node.inode) - 1]
let inode = inodePtr.pointee
// Allowed replace
// -----------------------------
//
// Original Type File Directory Symlink
// ----------------------------------------------
// File | ✔ | ✘ | ✔
// Directory | ✘ | ✔ | ✔
// Symlink | ✔ | ✘ | ✔
if mode.isDir() {
if !inode.mode.isDir() {
guard inode.mode.isLink() else {
throw Error.notDirectory(path)
}
}
// mkdir -p
if path.base == node.name {
guard !recursion else {
return
}
// create a new tree node to replace this one
var inode = inode
inode.mode = mode
if let uid {
inode.uid = uid.lo
inode.uidHigh = uid.hi
}
if let gid {
inode.gid = gid.lo
inode.gidHigh = gid.hi
}
inodePtr.initialize(to: inode)
return
}
} else if let _ = node.link { // ok to overwrite links
try self.unlink(path: path)
} else { // file can only be overwritten by another file
if inode.mode.isDir() {
guard mode.isLink() else { // unless it is a link, then it can be replaced by a dir
throw Error.notFile(path)
}
}
try self.unlink(path: path)
}
}
// create all predecessors recursively
let parentPath: FilePath = path.dir
try self.create(path: parentPath, mode: Inode.Mode(.S_IFDIR, 0o755), recursion: true)
guard let parentTreeNodePtr = self.tree.lookup(path: parentPath) else {
throw Error.notFound(parentPath)
}
let parentTreeNode = parentTreeNodePtr.pointee
let parentInodePtr = self.inodes[Int(parentTreeNode.inode) - 1]
var parentInode = parentInodePtr.pointee
guard parentInode.linksCount < EXT4.MaxLinks else {
throw Error.maximumLinksExceeded(parentPath)
}
let childInodePtr = Ptr<Inode>.allocate(capacity: 1)
var childInode = Inode()
var startBlock: UInt32 = 0
var endBlock: UInt32 = 0
defer { // update metadata
childInodePtr.initialize(to: childInode)
parentInodePtr.initialize(to: parentInode)
self.inodes.append(childInodePtr)
let childTreeNodePtr = Ptr<FileTree.FileTreeNode>.allocate(capacity: 1)
let childTreeNode = FileTree.FileTreeNode(
inode: InodeNumber(self.inodes.count),
name: path.base,
parent: parentTreeNodePtr,
children: [],
blocks: (startBlock, endBlock)
)
childTreeNodePtr.initialize(to: childTreeNode)
parentTreeNode.children.append(childTreeNodePtr)
parentTreeNodePtr.initialize(to: parentTreeNode)
}
childInode.mode = mode
// uid,gid
if let uid {
childInode.uid = UInt16(uid & 0xffff)
childInode.uidHigh = UInt16((uid >> 16) & 0xffff)
} else {
childInode.uid = parentInode.uid
childInode.uidHigh = parentInode.uidHigh
}
if let gid {
childInode.gid = UInt16(gid & 0xffff)
childInode.gidHigh = UInt16((gid >> 16) & 0xffff)
} else {
childInode.gid = parentInode.gid
childInode.gidHigh = parentInode.gidHigh
}
if let xattrs, !xattrs.isEmpty {
var state = FileXattrsState(
inode: UInt32(self.inodes.count), inodeXattrCapacity: EXT4.InodeExtraSize, blockCapacity: blockSize)
try state.add(ExtendedAttribute(name: "system.data", value: []))
for (s, d) in xattrs {
let attribute = ExtendedAttribute(name: s, value: [UInt8](d))
try state.add(attribute)
}
if !state.inlineAttributes.isEmpty {
var buffer: [UInt8] = .init(repeating: 0, count: Int(EXT4.InodeExtraSize))
try state.writeInlineAttributes(buffer: &buffer)
childInode.inlineXattrs = (
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7],
buffer[8],
buffer[9],
buffer[10], buffer[11], buffer[12], buffer[13], buffer[14], buffer[15], buffer[16], buffer[17],
buffer[18],
buffer[19],
buffer[20], buffer[21], buffer[22], buffer[23], buffer[24], buffer[25], buffer[26], buffer[27],
buffer[28],
buffer[29],
buffer[30], buffer[31], buffer[32], buffer[33], buffer[34], buffer[35], buffer[36], buffer[37],
buffer[38],
buffer[39],
buffer[40], buffer[41], buffer[42], buffer[43], buffer[44], buffer[45], buffer[46], buffer[47],
buffer[48],
buffer[49],
buffer[50], buffer[51], buffer[52], buffer[53], buffer[54], buffer[55], buffer[56], buffer[57],
buffer[58],
buffer[59],
buffer[60], buffer[61], buffer[62], buffer[63], buffer[64], buffer[65], buffer[66], buffer[67],
buffer[68],
buffer[69],
buffer[70], buffer[71], buffer[72], buffer[73], buffer[74], buffer[75], buffer[76], buffer[77],
buffer[78],
buffer[79],
buffer[80], buffer[81], buffer[82], buffer[83], buffer[84], buffer[85], buffer[86], buffer[87],
buffer[88],
buffer[89],
buffer[90], buffer[91], buffer[92], buffer[93], buffer[94], buffer[95]
)
childInode.flags |= InodeFlag.inlineData.rawValue
}
if !state.blockAttributes.isEmpty {
var buffer: [UInt8] = .init(repeating: 0, count: Int(blockSize))
try state.writeBlockAttributes(buffer: &buffer)
if self.pos % self.blockSize != 0 {
try self.seek(block: self.currentBlock + 1)
}
childInode.xattrBlockLow = self.currentBlock
try self.handle.write(contentsOf: buffer)
childInode.blocksLow += 1
}
}
childInode.atime = ts.accessLo
childInode.atimeExtra = ts.accessHi
// ctime is the last time the inode was changed which is now
childInode.ctime = ts.nowLo
childInode.ctimeExtra = ts.nowHi
childInode.mtime = ts.modificationLo
childInode.mtimeExtra = ts.modificationHi
childInode.crtime = ts.creationLo
childInode.crtimeExtra = ts.creationHi
childInode.linksCount = 1
childInode.extraIsize = UInt16(EXT4.ExtraIsize)
// flags
childInode.flags = InodeFlag.hugeFile.rawValue
// size check
var size: UInt64 = 0
// align with block boundary
if self.pos % self.blockSize != 0 {
try self.seek(block: self.currentBlock + 1)
}
// dir
if childInode.mode.isDir() {
childInode.linksCount += 1
parentInode.linksCount += 1
// to pass e2fsck, the convention is to sort children
// before committing to disk. Therefore, we are deferring
// writing dentries until commit() is called
return
}
// symbolic link
if let link {
startBlock = self.currentBlock
let linkPath = link.bytes
if linkPath.count < 60 {
size += UInt64(linkPath.count)
var blockData: [UInt8] = .init(repeating: 0, count: 60)
for i in 0..<linkPath.count {
blockData[i] = linkPath[i]
}
childInode.block = (
blockData[0], blockData[1], blockData[2], blockData[3], blockData[4], blockData[5],
blockData[6],
blockData[7], blockData[8], blockData[9],
blockData[10], blockData[11], blockData[12], blockData[13], blockData[14], blockData[15],
blockData[16],
blockData[17], blockData[18], blockData[19],
blockData[20], blockData[21], blockData[22], blockData[23], blockData[24], blockData[25],
blockData[26],
blockData[27], blockData[28], blockData[29],
blockData[30], blockData[31], blockData[32], blockData[33], blockData[34], blockData[35],
blockData[36],
blockData[37], blockData[38], blockData[39],
blockData[40], blockData[41], blockData[42], blockData[43], blockData[44], blockData[45],
blockData[46],
blockData[47], blockData[48], blockData[49],
blockData[50], blockData[51], blockData[52], blockData[53], blockData[54], blockData[55],
blockData[56],
blockData[57], blockData[58], blockData[59]
)
} else {
try linkPath.withUnsafeBytes { buffer in
try withUnsafeLittleEndianBuffer(of: buffer) { b in
try self.handle.write(contentsOf: b)
}
size += UInt64(buffer.count)
}
}
if self.pos % self.blockSize != 0 {
try self.seek(block: self.currentBlock + 1)
}
endBlock = self.currentBlock
childInode.sizeLow = size.lo
childInode.sizeHigh = size.hi
childInode.mode |= 0o777
childInode.flags = 0
if linkPath.count < 60 {
childInode.blocksLow = 0
} else {
childInode = try self.writeExtents(childInode, (startBlock, endBlock))
childInode.blocksLow = 8
}
return
}
// regular file
if mode.isReg() {
startBlock = self.currentBlock
if let buf { // in case of empty files, this will be nil
let tempBuf = Ptr<UInt8>.allocate(capacity: Int(self.blockSize))
defer { tempBuf.deallocate() }
while case let block = buf.read(tempBuf.underlying, maxLength: Int(self.blockSize)), block > 0 {
size += UInt64(block)
if size > EXT4.MaxFileSize {
throw Error.fileTooBig(size)
}
let data = UnsafeRawBufferPointer(start: tempBuf.underlying, count: block)
try withUnsafeLittleEndianBuffer(of: data) { b in
try self.handle.write(contentsOf: b)
}
}
}
if self.pos % self.blockSize != 0 {
try self.seek(block: self.currentBlock + 1)
}
endBlock = self.currentBlock
childInode.sizeLow = size.lo
childInode.sizeHigh = size.hi
childInode = try self.writeExtents(childInode, (startBlock, endBlock))
return
}
// FIFO, Socket and other types are not handled
throw Error.unsupportedFiletype
}
public func setOwner(path: FilePath, uid: UInt16? = nil, gid: UInt16? = nil, recursive: Bool = false) throws {
// ensure that target exists
guard let pathPtr = self.tree.lookup(path: path) else {
throw Error.notFound(path)
}
let pathNode = pathPtr.pointee
let pathInodePtr = self.inodes[Int(pathNode.inode) - 1]
var pathInode = pathInodePtr.pointee
if let uid {
pathInode.uid = uid
}
if let gid {
pathInode.gid = gid
}
pathInodePtr.initialize(to: pathInode)
if recursive {
for childPtr in pathNode.children {
let child = childPtr.pointee
try self.setOwner(path: path.join(child.name), uid: uid, gid: gid, recursive: recursive)
}
}
}
// Completes the formatting of an ext4 filesystem after writing the necessary structures.
//
// This function is responsible for finalizing the formatting process of an ext4 filesystem
// after the following structures have been written:
// - Inode table: Contains information about each file and directory in the filesystem.
// - Block bitmap: Tracks the allocation status of each block in the filesystem.
// - Inode bitmap: Tracks the allocation status of each inode in the filesystem.
// - Directory tree: Represents the hierarchical structure of directories and files.
// - Group descriptors: Stores metadata about each block group in the filesystem.
// - Superblock: Contains essential information about the filesystem's configuration.
//
// The function performs any necessary final steps to ensure the integrity and consistency
// of the ext4 filesystem before it can be mounted and used.
public func close() throws {
var breathWiseChildTree: [(parent: Ptr<FileTree.FileTreeNode>?, child: Ptr<FileTree.FileTreeNode>)] = [
(nil, self.tree.root)
]
while !breathWiseChildTree.isEmpty {
let (parent, child) = breathWiseChildTree.removeFirst()
try self.commit(parent, child) // commit directories iteratively
if child.pointee.link != nil {
continue
}
breathWiseChildTree.append(contentsOf: child.pointee.children.map { (child, $0) })
}
let blockGroupSize = optimizeBlockGroupLayout(blocks: self.currentBlock, inodes: UInt32(self.inodes.count))
let inodeTableOffset = try self.commitInodeTable(
blockGroups: blockGroupSize.blockGroups,
inodesPerGroup: blockGroupSize.inodesPerGroup
)
if self.pos % self.blockSize != 0 {
try self.seek(block: self.currentBlock + 1)
}
// write bitmaps and group descriptors
let bitmapOffset = self.currentBlock
let bitmapSize: UInt32 = blockGroupSize.blockGroups * 2 // each group has two bitmaps - for inodes, and for blocks
let dataSize: UInt32 = bitmapOffset + bitmapSize // last data block
var diskSize = dataSize
var minimumDiskSize = (blockGroupSize.blockGroups - 1) * self.blocksPerGroup + 1
if blockGroupSize.blockGroups == 1 {
minimumDiskSize = self.blocksPerGroup // at least 1 block group
}
if diskSize < minimumDiskSize { // for data + metadata
diskSize = minimumDiskSize
}
if self.size < minimumDiskSize {
self.size = UInt64(minimumDiskSize) * self.blockSize
}
// number of blocks needed for group descriptors
let groupDescriptorBlockCount: UInt32 = (blockGroupSize.blockGroups - 1) / self.groupsPerDescriptorBlock + 1
guard groupDescriptorBlockCount <= self.groupDescriptorBlocks else {
throw Error.insufficientSpaceForGroupDescriptorBlocks
}
var totalBlocks: UInt32 = 0
var totalInodes: UInt32 = 0
let inodeTableSizePerGroup: UInt32 = blockGroupSize.inodesPerGroup * EXT4.InodeSize / self.blockSize
var groupDescriptors: [GroupDescriptor] = []
let minGroups = (((self.pos / UInt64(self.blockSize)) - 1) / UInt64(self.blocksPerGroup)) + 1
if self.size < minGroups * blocksPerGroup * blockSize {
self.size = UInt64(minGroups * blocksPerGroup * blockSize)
let pos = self.pos
guard lseek(self.handle.fileDescriptor, off_t(self.size - 1), 0) == self.size - 1 else {
throw Error.cannotResizeFS(self.size)
}
let zero: [UInt8] = [0]
try self.handle.write(contentsOf: zero)
try self.handle.seek(toOffset: pos)
}
let totalGroups = (((self.size / UInt64(self.blockSize)) - 1) / UInt64(self.blocksPerGroup)) + 1
// If the provided disk size is not aligned to a blockgroup boundary, it needs to
// be expanded to the next blockgroup boundary.
// Example:
// Provided disk size: 2 GB + 100MB: 2148 MB
// BlockSize: 4096
// Blockgroup size: 32768 blocks: 128MB
// Number of blocks: 549888
// Number of blockgroups = 549888 / 32768 = 16.78125
// Aligned disk size = 557056 blocks = 17 blockgroups: 2176 MB
if self.size < totalGroups * blocksPerGroup * blockSize {
self.size = UInt64(totalGroups * blocksPerGroup * blockSize)
let pos = self.pos
guard lseek(self.handle.fileDescriptor, off_t(self.size - 1), 0) == self.size - 1 else {
throw Error.cannotResizeFS(self.size)
}
let zero: [UInt8] = [0]
try self.handle.write(contentsOf: zero)
try self.handle.seek(toOffset: pos)
}
for group in 0..<blockGroupSize.blockGroups {
// keep track of directories, inodes and block per blockgroup
var dirs: UInt32 = 0
var inodes: UInt32 = 0
var blocks: UInt32 = 0
// blocks bitmap
var bitmap: [UInt8] = .init(repeating: 0, count: self.blockSize * 2) // 1 for blocks, 1 for inodes
if (group + 1) * UInt32(self.blocksPerGroup) <= dataSize { // fully allocated group
for i in 0..<(self.blockSize) {
bitmap[Int(i)] = 0xff // mark as allocated
}
blocks = UInt32(self.blocksPerGroup)
} else if group * UInt32(self.blocksPerGroup) < dataSize { // partially allocated group
for i in 0..<dataSize - group * UInt32(self.blocksPerGroup) {
bitmap[Int(i / 8)] |= 1 << (i % 8)
blocks += 1
}
}
if group == 0 { // unused group descriptor blocks
// blocks used by group descriptors
let usedGroupDescriptorBlocks = (totalGroups - 1) / self.groupsPerDescriptorBlock + 1
for i in 0...usedGroupDescriptorBlocks {
bitmap[Int(i / 8)] |= 1 << (i % 8)
}
for i in usedGroupDescriptorBlocks + 1...self.groupDescriptorBlocks {
bitmap[Int(i / 8)] &= ~(1 << (i % 8))
blocks -= 1
}
}
// last blockGroup if not aligned with total size should be marked as allocated
let remainingBlocks = diskSize % self.blocksPerGroup
if group == totalGroups - 1 && remainingBlocks != 0 && self.size / self.blockSize < self.blocksPerGroup {
for i in remainingBlocks..<self.blocksPerGroup {
bitmap[Int(i / 8)] |= 1 << (i % 8)
}
if remainingBlocks < self.size / self.blockSize {
for i in remainingBlocks..<self.size / self.blockSize {
bitmap[Int(i / 8)] &= ~(1 << (i % 8))
}
}
}
// mark deleted blocks as free
for block in self.deletedBlocks {
for i in block.start..<block.end where i / self.blocksPerGroup == group {
let j = i % self.blocksPerGroup
blocks -= UInt32((bitmap[Int(j / 8)] >> (j % 8)) & 1)
bitmap[Int(j / 8)] &= ~(1 << (j % 8))
}
}
// inodes bitmap goes into second bitmap block
for i in 0..<blockGroupSize.inodesPerGroup {
let ino = InodeNumber(1 + group * blockGroupSize.inodesPerGroup + i)
if ino > self.inodes.count {
continue
}
let inode = self.inodes[Int(ino) - 1]
if ino > 10 && inode.pointee.linksCount == 0 { // deleted files
continue
}
bitmap[Int(self.blockSize) + Int(i / 8)] |= 1 << (i % 8)
inodes += 1
if inode.pointee.mode.isDir() {
dirs += 1
}
}
for i in (blockGroupSize.inodesPerGroup / 8)..<self.blockSize {
bitmap[Int(self.blockSize) + Int(i)] = 0xff // mark rest of inodes as occupied
}
// write bitmap
try bitmap.withUnsafeBytes { bitmapBytes in
try withUnsafeLittleEndianBuffer(of: bitmapBytes) { b in
try self.handle.write(contentsOf: b)
}
}
var freeBlocks: UInt32 = UInt32(self.blocksPerGroup)
if freeBlocks < blocks {
freeBlocks = 0
} else if self.size / self.blockSize < self.blocksPerGroup {
if blocks < UInt32(self.size / UInt64(self.blockSize)) {
freeBlocks = UInt32(self.size / UInt64(self.blockSize)) - blocks
} else {
freeBlocks = 0
}
} else {
freeBlocks = UInt32(self.blocksPerGroup) - blocks
}
let blockBitmap = UInt64(bitmapOffset + 2 * group)
let inodeBitmap = UInt64(bitmapOffset + 2 * group + 1)
let inodeTable = inodeTableOffset + UInt64(group * inodeTableSizePerGroup)
let freeBlocksCount = UInt32(self.blocksPerGroup - blocks)
let freeInodesCount = UInt32(blockGroupSize.inodesPerGroup - inodes)
groupDescriptors.append(
// low bits
GroupDescriptor(
blockBitmapLow: blockBitmap.lo, // address of block bitmap
inodeBitmapLow: inodeBitmap.lo, // address of inode bitmap
inodeTableLow: inodeTable.lo, // address of inode table for this group
freeBlocksCountLow: freeBlocksCount.lo,
freeInodesCountLow: freeInodesCount.lo,
usedDirsCountLow: dirs.lo,
flags: 0x0000,
excludeBitmapLow: 0x0000_0000,
blockBitmapCsumLow: 0x0000,
inodeBitmapCsumLow: 0x0000,
itableUnusedLow: 0x0000,
checksum: 0x0000
))
totalBlocks += UInt32(blocks)
totalInodes += UInt32(inodes)
}
// Since the bitmaps for unoccupied block groups are the same, there is no need
// to allocate separate memory or storage for each individual bitmap.
var blockBitmap: [UInt8] = .init(repeating: 0, count: Int(self.blocksPerGroup) / 8)
var inodeBitmap: [UInt8] = .init(repeating: 0xff, count: Int(self.blocksPerGroup) / 8)
for i in 0..<inodeTableSizePerGroup + 2 {
blockBitmap[Int(i) / 8] |= 1 << (i % 8)
}
for i in 0..<UInt16(blockGroupSize.inodesPerGroup) {
inodeBitmap[Int(i) / 8] &= ~(1 << (i % 8))
}
for group in blockGroupSize.blockGroups..<totalGroups.lo {
var blocksInGroup = UInt32(self.blocksPerGroup)
if group == totalGroups.lo {
if UInt64(self.size / UInt64(self.blockSize)) < self.blocksPerGroup {
break
}
blocksInGroup = UInt32((self.size / UInt64(self.blockSize)) % UInt64(self.blocksPerGroup))
if blocksInGroup == 0 {
break
}
}
let blockBitmapOffset = UInt64(group * self.blocksPerGroup + inodeTableSizePerGroup)
let inodeBitmapOffset = UInt64(group * self.blocksPerGroup + inodeTableSizePerGroup + 1)
let inodeTableOffset = UInt64(self.blocksPerGroup) * group
let freeBlocksCount = UInt32(blocksInGroup - inodeTableSizePerGroup - 2)
let freeInodesCount = UInt32(blockGroupSize.inodesPerGroup)
groupDescriptors.append(
// low bits
GroupDescriptor(
blockBitmapLow: blockBitmapOffset.lo, // address of block bitmap
inodeBitmapLow: inodeBitmapOffset.lo, // address of inode bitmap
inodeTableLow: inodeTableOffset.lo, // address of inode table for this group
freeBlocksCountLow: freeBlocksCount.lo,
freeInodesCountLow: freeInodesCount.lo,
usedDirsCountLow: 0,
flags: 0x0000,
excludeBitmapLow: 0x0000_0000,
blockBitmapCsumLow: 0x0000,
inodeBitmapCsumLow: 0x0000,
itableUnusedLow: 0x0000,
checksum: 0x0000
))
totalBlocks += (inodeTableSizePerGroup + 2)
try self.seek(block: group * self.blocksPerGroup + inodeTableSizePerGroup)
if group == totalGroups.lo {
var blockBitmapLo: [UInt8] = .init(repeating: 0, count: Int(self.blocksPerGroup) / 8)
for i in blocksInGroup..<UInt32(self.blocksPerGroup) {
blockBitmapLo[Int(i) / 8] |= 1 << (i % 8)
}
for i in 0..<inodeTableSizePerGroup + 2 {
blockBitmapLo[Int(i) / 8] |= 1 << (i % 8)
}
try self.handle.write(contentsOf: blockBitmapLo)
try self.handle.write(contentsOf: inodeBitmap)
continue
}
try self.handle.write(contentsOf: blockBitmap)
try self.handle.write(contentsOf: inodeBitmap)
}
try self.seek(block: 1)
for groupDescriptor in groupDescriptors {
try withUnsafeLittleEndianBytes(of: groupDescriptor) { bytes in
try self.handle.write(contentsOf: bytes)
}
}
// write superblock
try self.seek(block: 0)
try self.handle.write(contentsOf: Array<UInt8>.init(repeating: 0, count: 1024))
let computedInodes = totalGroups * blockGroupSize.inodesPerGroup
var blocksCount = totalGroups * self.blocksPerGroup
while blocksCount < totalBlocks {
blocksCount = UInt64(totalBlocks)
}
let totalFreeBlocks: UInt64
if totalBlocks > blocksCount {
totalFreeBlocks = 0
} else {
totalFreeBlocks = blocksCount - totalBlocks
}
var superblock = SuperBlock()
superblock.inodesCount = computedInodes.lo
superblock.blocksCountLow = blocksCount.lo
superblock.blocksCountHigh = blocksCount.hi
superblock.freeBlocksCountLow = totalFreeBlocks.lo
superblock.freeBlocksCountHigh = totalFreeBlocks.hi
let freeInodesCount = computedInodes.lo - totalInodes
superblock.freeInodesCount = freeInodesCount
superblock.firstDataBlock = 0
superblock.logBlockSize = 2
superblock.logClusterSize = 2
superblock.blocksPerGroup = self.blocksPerGroup
superblock.clustersPerGroup = self.blocksPerGroup
superblock.inodesPerGroup = blockGroupSize.inodesPerGroup
superblock.magic = EXT4.SuperBlockMagic
superblock.state = 1 // cleanly unmounted
superblock.errors = 1 // continue on error
superblock.creatorOS = 3 // freeBSD
superblock.revisionLevel = 1 // dynamic inode sizes
superblock.firstInode = EXT4.FirstInode
superblock.lpfInode = EXT4.LostAndFoundInode
superblock.inodeSize = UInt16(EXT4.InodeSize)
superblock.featureCompat = CompatFeature.sparseSuper2 | CompatFeature.extAttr
superblock.featureIncompat =
IncompatFeature.filetype | IncompatFeature.extents | IncompatFeature.flexBg | IncompatFeature.inlineData
superblock.featureRoCompat =
RoCompatFeature.largeFile | RoCompatFeature.hugeFile | RoCompatFeature.extraIsize
superblock.minExtraIsize = EXT4.ExtraIsize
superblock.wantExtraIsize = EXT4.ExtraIsize
superblock.logGroupsPerFlex = 31
superblock.uuid = UUID().uuid
try withUnsafeLittleEndianBytes(of: superblock) { bytes in
try self.handle.write(contentsOf: bytes)
}
try self.handle.write(contentsOf: Array<UInt8>.init(repeating: 0, count: 2048))
}
// MARK: Private Methods and Properties
private var handle: FileHandle
private var inodes: [Ptr<Inode>]
private var tree: FileTree
private var deletedBlocks: [(start: UInt32, end: UInt32)] = []
private var pos: UInt64 {
guard let offset = try? self.handle.offset() else {
return 0
}
return offset
}
private var currentBlock: UInt32 {
self.pos / self.blockSize
}
private func seek(block: UInt32) throws {
try self.handle.seek(toOffset: UInt64(block) * blockSize)
}
private func commitInodeTable(blockGroups: UInt32, inodesPerGroup: UInt32) throws -> UInt64 {
// inodeTable must go into a new block
if self.pos % blockSize != 0 {
try seek(block: currentBlock + 1)
}
let inodeTableOffset = UInt64(currentBlock)
let inodeSize = MemoryLayout<Inode>.size
// Write InodeTable
for inode in self.inodes {
try withUnsafeLittleEndianBytes(of: inode.pointee) { bytes in
try handle.write(contentsOf: bytes)
}
try self.handle.write(
contentsOf: Array<UInt8>.init(repeating: 0, count: Int(EXT4.InodeSize) - inodeSize))
}
let tableSize: UInt64 = UInt64(EXT4.InodeSize) * blockGroups * inodesPerGroup
let rest = tableSize - uint32(self.inodes.count) * EXT4.InodeSize
let zeroBlock = Array<UInt8>.init(repeating: 0, count: Int(self.blockSize))
for _ in 0..<(rest / self.blockSize) {
try self.handle.write(contentsOf: zeroBlock)
}
try self.handle.write(contentsOf: Array<UInt8>.init(repeating: 0, count: Int(rest % self.blockSize)))
return inodeTableOffset
}
// optimizes the distribution of blockGroups to obtain the lowest number of blockGroups needed to
// represent all the inodes and all the blocks in the FS
private func optimizeBlockGroupLayout(blocks: UInt32, inodes: UInt32) -> (
blockGroups: UInt32, inodesPerGroup: UInt32
) {
// counts the number of blockGroups given a particular inodesPerGroup size
let groupCount: (_ blocks: UInt32, _ inodes: UInt32, _ inodesPerGroup: UInt32) -> UInt32 = {
blocks, inodes, inodesPerGroup in
let inodeBlocksPerGroup: UInt32 = inodesPerGroup * EXT4.InodeSize / self.blockSize
let dataBlocksPerGroup: UInt32 = self.blocksPerGroup - inodeBlocksPerGroup - 2 // save room for the bitmaps
// Increase the block count to ensure there are enough groups for all the inodes.
let minBlocks: UInt32 = (inodes - 1) / inodesPerGroup * dataBlocksPerGroup + 1
var updatedBlocks = blocks
if blocks < minBlocks {
updatedBlocks = minBlocks
}
return (updatedBlocks + dataBlocksPerGroup - 1) / dataBlocksPerGroup
}
var groups: UInt32 = UInt32.max
var inodesPerGroup: UInt32 = 0
let inc = Int(self.blockSize * 512) / Int(EXT4.InodeSize) // inodesPerGroup
// minimizes the number of blockGroups needed to its lowest value
for ipg in stride(from: inc, through: Int(self.maxInodesPerGroup), by: inc) {
let g = groupCount(blocks, inodes, UInt32(ipg))
if g < groups {
groups = g
inodesPerGroup = UInt32(ipg)
}
}