hadris-common 1.0.3

Common types and functions used by Hadris
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
//! File and directory layout types for metadata-only writing.
//!
//! These types require the `alloc` feature for heap allocation.

extern crate alloc;

use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

use super::extent::{Extent, FileType, Timestamps};

/// File layout with pre-calculated extent (for metadata-only writing).
///
/// This structure describes where a file's data is located on disk
/// without containing the actual data. Used for operations that only
/// need to write/update filesystem metadata.
#[derive(Debug, Clone)]
pub struct FileLayout {
    /// File name (without path).
    pub name: String,
    /// Location and size of file data on disk.
    pub extent: Extent,
    /// Type of the file entry.
    pub file_type: FileType,
    /// File timestamps.
    pub timestamps: Timestamps,
    /// Filesystem-specific attribute flags.
    pub attributes: u32,
    /// Optional symlink target (only valid if file_type is Symlink).
    pub symlink_target: Option<String>,
}

impl FileLayout {
    /// Creates a new file layout.
    pub fn new(name: impl Into<String>, extent: Extent) -> Self {
        Self {
            name: name.into(),
            extent,
            file_type: FileType::RegularFile,
            timestamps: Timestamps::default(),
            attributes: 0,
            symlink_target: None,
        }
    }

    /// Sets the file type.
    pub fn with_type(mut self, file_type: FileType) -> Self {
        self.file_type = file_type;
        self
    }

    /// Sets the timestamps.
    pub fn with_timestamps(mut self, timestamps: Timestamps) -> Self {
        self.timestamps = timestamps;
        self
    }

    /// Sets the attributes.
    pub fn with_attributes(mut self, attributes: u32) -> Self {
        self.attributes = attributes;
        self
    }

    /// Sets the symlink target (only meaningful for symlinks).
    pub fn with_symlink_target(mut self, target: impl Into<String>) -> Self {
        self.symlink_target = Some(target.into());
        self
    }

    /// Returns the file size in bytes.
    #[inline]
    pub fn size(&self) -> u64 {
        self.extent.length
    }
}

/// Directory layout (tree of files).
///
/// Represents a complete directory tree with pre-calculated extents
/// for all files. Used for metadata-only writing operations.
#[derive(Debug, Clone, Default)]
pub struct DirectoryLayout {
    /// Directory name (empty string for root).
    pub name: String,
    /// Files in this directory.
    pub files: Vec<FileLayout>,
    /// Subdirectories.
    pub subdirs: Vec<DirectoryLayout>,
    /// Directory timestamps.
    pub timestamps: Timestamps,
    /// Filesystem-specific attribute flags.
    pub attributes: u32,
    /// Extent for the directory entry itself (if applicable).
    pub extent: Option<Extent>,
}

impl DirectoryLayout {
    /// Creates a new empty directory layout.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            files: Vec::new(),
            subdirs: Vec::new(),
            timestamps: Timestamps::default(),
            attributes: 0,
            extent: None,
        }
    }

    /// Creates a root directory layout (empty name).
    pub fn root() -> Self {
        Self::new("")
    }

    /// Adds a file to this directory.
    pub fn add_file(&mut self, file: FileLayout) {
        self.files.push(file);
    }

    /// Adds a subdirectory to this directory.
    pub fn add_subdir(&mut self, subdir: DirectoryLayout) {
        self.subdirs.push(subdir);
    }

    /// Sets the timestamps.
    pub fn with_timestamps(mut self, timestamps: Timestamps) -> Self {
        self.timestamps = timestamps;
        self
    }

    /// Sets the extent for this directory entry.
    pub fn with_extent(mut self, extent: Extent) -> Self {
        self.extent = Some(extent);
        self
    }

    /// Returns the total number of files in this directory (not recursive).
    #[inline]
    pub fn file_count(&self) -> usize {
        self.files.len()
    }

    /// Returns the total number of subdirectories in this directory (not recursive).
    #[inline]
    pub fn subdir_count(&self) -> usize {
        self.subdirs.len()
    }

    /// Returns the total number of entries (files + subdirs).
    #[inline]
    pub fn entry_count(&self) -> usize {
        self.files.len() + self.subdirs.len()
    }

    /// Returns an iterator over all files (recursive, depth-first).
    pub fn iter_files(&self) -> impl Iterator<Item = (&str, &FileLayout)> {
        FileIterator::new(self)
    }

    /// Finds a file by path (e.g., "docs/readme.txt").
    pub fn find_file(&self, path: &str) -> Option<&FileLayout> {
        let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        self.find_file_parts(&parts)
    }

    /// Finds a file by path parts.
    fn find_file_parts(&self, parts: &[&str]) -> Option<&FileLayout> {
        if parts.is_empty() {
            return None;
        }

        if parts.len() == 1 {
            // Looking for a file in this directory
            self.files.iter().find(|f| f.name == parts[0])
        } else {
            // Looking in a subdirectory
            self.subdirs
                .iter()
                .find(|d| d.name == parts[0])
                .and_then(|d| d.find_file_parts(&parts[1..]))
        }
    }

    /// Finds a mutable file reference by path.
    pub fn find_file_mut(&mut self, path: &str) -> Option<&mut FileLayout> {
        let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        self.find_file_parts_mut(&parts)
    }

    /// Finds a mutable file reference by path parts.
    fn find_file_parts_mut(&mut self, parts: &[&str]) -> Option<&mut FileLayout> {
        if parts.is_empty() {
            return None;
        }

        if parts.len() == 1 {
            self.files.iter_mut().find(|f| f.name == parts[0])
        } else {
            self.subdirs
                .iter_mut()
                .find(|d| d.name == parts[0])
                .and_then(|d| d.find_file_parts_mut(&parts[1..]))
        }
    }

    /// Finds or creates a subdirectory by path.
    pub fn get_or_create_dir(&mut self, path: &str) -> &mut DirectoryLayout {
        let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        self.get_or_create_dir_parts(&parts)
    }

    /// Finds or creates a subdirectory by path parts.
    fn get_or_create_dir_parts(&mut self, parts: &[&str]) -> &mut DirectoryLayout {
        if parts.is_empty() {
            return self;
        }

        let name = parts[0];

        // Find or create the subdirectory
        let idx = self.subdirs.iter().position(|d| d.name == name);
        let idx = match idx {
            Some(i) => i,
            None => {
                self.subdirs.push(DirectoryLayout::new(name));
                self.subdirs.len() - 1
            }
        };

        self.subdirs[idx].get_or_create_dir_parts(&parts[1..])
    }

    /// Removes a file by path. Returns the removed file if found.
    pub fn remove_file(&mut self, path: &str) -> Option<FileLayout> {
        let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        self.remove_file_parts(&parts)
    }

    /// Removes a file by path parts.
    fn remove_file_parts(&mut self, parts: &[&str]) -> Option<FileLayout> {
        if parts.is_empty() {
            return None;
        }

        if parts.len() == 1 {
            let idx = self.files.iter().position(|f| f.name == parts[0])?;
            Some(self.files.remove(idx))
        } else {
            self.subdirs
                .iter_mut()
                .find(|d| d.name == parts[0])
                .and_then(|d| d.remove_file_parts(&parts[1..]))
        }
    }
}

/// Iterator over all files in a directory tree (depth-first).
struct FileIterator<'a> {
    stack: Vec<(&'a str, &'a DirectoryLayout, usize, usize)>,
    path_prefix: String,
}

impl<'a> FileIterator<'a> {
    fn new(root: &'a DirectoryLayout) -> Self {
        Self {
            stack: vec![("", root, 0, 0)],
            path_prefix: String::new(),
        }
    }
}

impl<'a> Iterator for FileIterator<'a> {
    type Item = (&'a str, &'a FileLayout);

    fn next(&mut self) -> Option<Self::Item> {
        while let Some((name, dir, file_idx, subdir_idx)) = self.stack.pop() {
            // Update path prefix when entering a new directory
            if !name.is_empty() {
                if !self.path_prefix.is_empty() {
                    self.path_prefix.push('/');
                }
                self.path_prefix.push_str(name);
            }

            // Return files first
            if file_idx < dir.files.len() {
                // Push state for next file
                self.stack.push((name, dir, file_idx + 1, subdir_idx));
                let file = &dir.files[file_idx];
                // We can't easily return the full path here without allocation
                // so we return just the file name
                return Some((&file.name, file));
            }

            // Then recurse into subdirectories
            if subdir_idx < dir.subdirs.len() {
                // Push state for next subdir
                self.stack.push((name, dir, file_idx, subdir_idx + 1));
                let subdir = &dir.subdirs[subdir_idx];
                self.stack.push((&subdir.name, subdir, 0, 0));
                continue;
            }

            // Pop path segment when leaving directory
            if !name.is_empty() {
                if let Some(idx) = self.path_prefix.rfind('/') {
                    self.path_prefix.truncate(idx);
                } else {
                    self.path_prefix.clear();
                }
            }
        }
        None
    }
}

/// Tracks which sectors are allocated in an image.
///
/// Uses a bitmap to efficiently track sector usage, supporting
/// allocation and deallocation operations.
#[derive(Debug, Clone)]
pub struct AllocationMap {
    /// Bitmap of used sectors (1 = used, 0 = free).
    bitmap: Vec<u8>,
    /// Total sectors in image.
    total_sectors: u32,
    /// Next free sector hint for faster allocation.
    next_free: u32,
}

impl AllocationMap {
    /// Creates a new allocation map for the given number of sectors.
    pub fn new(total_sectors: u32) -> Self {
        let bitmap_size = (total_sectors as usize).div_ceil(8);
        Self {
            bitmap: alloc::vec![0u8; bitmap_size],
            total_sectors,
            next_free: 0,
        }
    }

    /// Creates an allocation map from a list of existing used extents.
    pub fn from_existing(used_extents: &[Extent], total_sectors: u32, sector_size: u32) -> Self {
        let mut map = Self::new(total_sectors);
        for extent in used_extents {
            map.mark_used(*extent, sector_size);
        }
        map
    }

    /// Allocates a contiguous region of the given size.
    ///
    /// Returns `None` if there isn't enough contiguous free space.
    pub fn allocate(&mut self, size_bytes: u64, sector_size: u32) -> Option<Extent> {
        if size_bytes == 0 {
            return Some(Extent::new(self.next_free, 0));
        }

        let sectors_needed = size_bytes.div_ceil(sector_size as u64) as u32;

        // Start searching from next_free hint
        let mut start = self.next_free;
        let mut consecutive = 0u32;
        let mut found_start = start;

        while start + consecutive < self.total_sectors {
            let current = start + consecutive;
            if self.is_free(current) {
                if consecutive == 0 {
                    found_start = current;
                }
                consecutive += 1;
                if consecutive >= sectors_needed {
                    // Found enough space
                    let extent = Extent::new(found_start, size_bytes);
                    self.mark_used(extent, sector_size);
                    return Some(extent);
                }
            } else {
                // Reset search
                consecutive = 0;
                start = current + 1;
                found_start = start;
            }
        }

        // Try from beginning if we started after 0
        if self.next_free > 0 {
            start = 0;
            consecutive = 0;
            found_start = 0;

            while start + consecutive < self.next_free {
                let current = start + consecutive;
                if self.is_free(current) {
                    if consecutive == 0 {
                        found_start = current;
                    }
                    consecutive += 1;
                    if consecutive >= sectors_needed {
                        let extent = Extent::new(found_start, size_bytes);
                        self.mark_used(extent, sector_size);
                        return Some(extent);
                    }
                } else {
                    consecutive = 0;
                    start = current + 1;
                    found_start = start;
                }
            }
        }

        None
    }

    /// Marks the given extent as used.
    pub fn mark_used(&mut self, extent: Extent, sector_size: u32) {
        let end = extent.end_sector(sector_size);
        for sector in extent.sector..end {
            self.set_bit(sector, true);
        }
        // Update next_free hint
        if extent.sector == self.next_free {
            self.next_free = end;
            // Skip any used sectors
            while self.next_free < self.total_sectors && !self.is_free(self.next_free) {
                self.next_free += 1;
            }
        }
    }

    /// Marks the given extent as free.
    pub fn mark_free(&mut self, extent: Extent, sector_size: u32) {
        let end = extent.end_sector(sector_size);
        for sector in extent.sector..end {
            self.set_bit(sector, false);
        }
        // Update next_free hint if this freed earlier sectors
        if extent.sector < self.next_free {
            self.next_free = extent.sector;
        }
    }

    /// Checks if a sector is free.
    #[inline]
    pub fn is_free(&self, sector: u32) -> bool {
        if sector >= self.total_sectors {
            return false;
        }
        let byte_idx = sector as usize / 8;
        let bit_idx = sector % 8;
        (self.bitmap[byte_idx] & (1 << bit_idx)) == 0
    }

    /// Checks if a sector is used.
    #[inline]
    pub fn is_used(&self, sector: u32) -> bool {
        !self.is_free(sector)
    }

    /// Returns the total number of sectors.
    #[inline]
    pub fn total_sectors(&self) -> u32 {
        self.total_sectors
    }

    /// Returns the number of free sectors.
    pub fn free_sectors(&self) -> u32 {
        let mut count = 0u32;
        for sector in 0..self.total_sectors {
            if self.is_free(sector) {
                count += 1;
            }
        }
        count
    }

    /// Returns the number of used sectors.
    #[inline]
    pub fn used_sectors(&self) -> u32 {
        self.total_sectors - self.free_sectors()
    }

    /// Sets or clears a bit in the bitmap.
    #[inline]
    fn set_bit(&mut self, sector: u32, used: bool) {
        if sector >= self.total_sectors {
            return;
        }
        let byte_idx = sector as usize / 8;
        let bit_idx = sector % 8;
        if used {
            self.bitmap[byte_idx] |= 1 << bit_idx;
        } else {
            self.bitmap[byte_idx] &= !(1 << bit_idx);
        }
    }

    /// Reserves the first N sectors (typically for metadata).
    pub fn reserve_initial(&mut self, sectors: u32, sector_size: u32) {
        let extent = Extent::new(0, sectors as u64 * sector_size as u64);
        self.mark_used(extent, sector_size);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_file_layout() {
        let file = FileLayout::new("test.txt", Extent::new(100, 1024))
            .with_type(FileType::RegularFile)
            .with_attributes(0x20);

        assert_eq!(file.name, "test.txt");
        assert_eq!(file.size(), 1024);
        assert_eq!(file.extent.sector, 100);
    }

    #[test]
    fn test_directory_layout() {
        let mut root = DirectoryLayout::root();
        root.add_file(FileLayout::new("file1.txt", Extent::new(100, 1024)));

        let mut subdir = DirectoryLayout::new("docs");
        subdir.add_file(FileLayout::new("readme.md", Extent::new(200, 512)));
        root.add_subdir(subdir);

        assert_eq!(root.file_count(), 1);
        assert_eq!(root.subdir_count(), 1);

        let file = root.find_file("file1.txt");
        assert!(file.is_some());
        assert_eq!(file.unwrap().name, "file1.txt");

        let nested = root.find_file("docs/readme.md");
        assert!(nested.is_some());
        assert_eq!(nested.unwrap().name, "readme.md");
    }

    #[test]
    fn test_get_or_create_dir() {
        let mut root = DirectoryLayout::root();
        let dir = root.get_or_create_dir("docs/api/v1");

        assert_eq!(dir.name, "v1");
        assert_eq!(root.subdirs[0].name, "docs");
        assert_eq!(root.subdirs[0].subdirs[0].name, "api");
        assert_eq!(root.subdirs[0].subdirs[0].subdirs[0].name, "v1");
    }

    #[test]
    fn test_remove_file() {
        let mut root = DirectoryLayout::root();
        root.add_file(FileLayout::new("test.txt", Extent::new(100, 1024)));

        let removed = root.remove_file("test.txt");
        assert!(removed.is_some());
        assert_eq!(removed.unwrap().name, "test.txt");
        assert_eq!(root.file_count(), 0);
    }

    #[test]
    fn test_allocation_map() {
        let mut map = AllocationMap::new(100);
        assert_eq!(map.total_sectors(), 100);
        assert_eq!(map.free_sectors(), 100);

        // Allocate 10 sectors (20480 bytes with 2048-byte sectors)
        let extent = map.allocate(20480, 2048).unwrap();
        assert_eq!(extent.sector, 0);
        assert_eq!(extent.sector_count(2048), 10);
        assert_eq!(map.free_sectors(), 90);

        // Allocate more
        let extent2 = map.allocate(4096, 2048).unwrap();
        assert_eq!(extent2.sector, 10);

        // Free the first allocation
        map.mark_free(extent, 2048);
        assert_eq!(map.free_sectors(), 98);

        // New allocation should reuse freed space
        let extent3 = map.allocate(2048, 2048).unwrap();
        assert_eq!(extent3.sector, 0);
    }

    #[test]
    fn test_allocation_map_reserve() {
        let mut map = AllocationMap::new(100);
        map.reserve_initial(16, 2048); // Reserve sectors 0-15

        let extent = map.allocate(2048, 2048).unwrap();
        assert_eq!(extent.sector, 16); // Should start after reserved area
    }
}