hadris-udf 1.0.0

A rust implementation of the UDF (Universal Disk Format) filesystem.
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
//! UDF image modification support.
//!
//! This module provides the ability to append files to existing UDF images
//! and mark files for deletion. It uses incremental writing where new
//! metadata chains to the previous version.
//!
//! # Incremental Writing Approach
//!
//! UDF supports incremental modifications through:
//! - VDS chaining via `predecessor_vds_location` field
//! - LVID (Logical Volume Integrity Descriptor) tracks integrity state
//! - VAT (Virtual Allocation Table) for write-once media
//!
//! # Example
//!
//! ```rust,ignore
//! use hadris_udf::modify::UdfModifier;
//!
//! let file = std::fs::OpenOptions::new()
//!     .read(true).write(true)
//!     .open("image.udf")?;
//!
//! let mut modifier = UdfModifier::open(file)?;
//! modifier.append_file("new_file.txt", b"Hello, world!".to_vec());
//! modifier.commit()?;
//! ```

use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use super::super::{Read, Seek, SeekFrom, Write};
use hadris_common::types::extent::{Extent, FileType};
use hadris_common::types::layout::{AllocationMap, DirectoryLayout, FileLayout};
use hadris_io as io;

use super::descriptor::AnchorVolumeDescriptorPointer;
use crate::{AVDP_LOCATION, SECTOR_SIZE, UdfError, UdfRevision};

/// Operations that can be performed on a UDF image.
#[derive(Debug, Clone)]
pub enum ModifyOp {
    /// Add a new file to the image.
    AppendFile {
        /// Path within the UDF (e.g., "docs/readme.txt")
        path: String,
        /// File contents
        data: FileData,
    },
    /// Create a new directory.
    CreateDir {
        /// Path of the directory to create
        path: String,
    },
    /// Mark a file as deleted.
    Delete {
        /// Path of the file to delete
        path: String,
    },
    /// Replace a file's content.
    Replace {
        /// Path of the file to replace
        path: String,
        /// New file contents
        data: FileData,
    },
}

/// File data for modification operations.
#[derive(Debug, Clone)]
pub enum FileData {
    /// In-memory buffer.
    Buffer(Vec<u8>),
    /// Path to a file on the filesystem.
    #[cfg(feature = "std")]
    Path(std::path::PathBuf),
}

impl From<Vec<u8>> for FileData {
    fn from(data: Vec<u8>) -> Self {
        FileData::Buffer(data)
    }
}

impl From<&[u8]> for FileData {
    fn from(data: &[u8]) -> Self {
        FileData::Buffer(data.to_vec())
    }
}

#[cfg(feature = "std")]
impl From<std::path::PathBuf> for FileData {
    fn from(path: std::path::PathBuf) -> Self {
        FileData::Path(path)
    }
}

impl FileData {
    /// Returns the size of the file data.
    pub fn size(&self) -> io::Result<u64> {
        match self {
            FileData::Buffer(data) => Ok(data.len() as u64),
            #[cfg(feature = "std")]
            FileData::Path(path) => {
                let metadata = std::fs::metadata(path)?;
                Ok(metadata.len())
            }
        }
    }

    /// Reads the file data into a buffer.
    pub fn read_all(&self) -> io::Result<Vec<u8>> {
        match self {
            FileData::Buffer(data) => Ok(data.clone()),
            #[cfg(feature = "std")]
            FileData::Path(path) => std::fs::read(path).map_err(Into::into),
        }
    }
}

/// Error type for UDF modification operations.
#[derive(Debug, thiserror::Error)]
pub enum UdfModifyError {
    /// I/O error.
    #[error(transparent)]
    Io(#[from] io::Error),
    /// UDF error.
    #[error(transparent)]
    Udf(#[from] UdfError),
    /// File not found.
    #[error("file not found: {0}")]
    FileNotFound(String),
    /// Path already exists.
    #[error("path already exists: {0}")]
    PathExists(String),
    /// Not enough space.
    #[error("not enough space to allocate {0} bytes")]
    NotEnoughSpace(u64),
    /// Invalid path.
    #[error("invalid path: {0}")]
    InvalidPath(String),
}

/// Result type for UDF modification operations.
pub type UdfModifyResult<T> = Result<T, UdfModifyError>;

/// Options for UDF modification.
#[derive(Debug, Clone)]
pub struct UdfModifyOptions {
    /// Volume name (if changing).
    pub volume_name: Option<String>,
}

impl Default for UdfModifyOptions {
    fn default() -> Self {
        Self { volume_name: None }
    }
}

/// Modifier for UDF images.
///
/// This struct provides methods to append files, create directories,
/// and mark files for deletion in an existing UDF image.
pub struct UdfModifier<RW: Read + Write + Seek> {
    /// The underlying reader/writer.
    inner: RW,
    /// Parsed from existing image.
    existing_layout: DirectoryLayout,
    /// Tracks used sectors.
    #[allow(dead_code)]
    allocation_map: AllocationMap,
    /// Pending operations.
    pending_ops: Vec<ModifyOp>,
    /// Options for modification.
    #[allow(dead_code)]
    options: UdfModifyOptions,
    /// UDF revision.
    #[allow(dead_code)]
    revision: UdfRevision,
    /// Partition start sector.
    #[allow(dead_code)]
    partition_start: u32,
    /// Partition length.
    #[allow(dead_code)]
    partition_length: u32,
    /// Next unique ID for new files.
    #[allow(dead_code)]
    next_unique_id: u64,
    /// Current end of the image.
    end_sector: u32,
}

impl<RW: Read + Write + Seek> UdfModifier<RW> {
    /// Opens an existing UDF image for modification.
    pub fn open(inner: RW) -> UdfModifyResult<Self> {
        Self::open_with_options(inner, UdfModifyOptions::default())
    }

    /// Opens an existing UDF image for modification with custom options.
    pub fn open_with_options(mut inner: RW, options: UdfModifyOptions) -> UdfModifyResult<Self> {
        // Read AVDP to get VDS location
        inner.seek(SeekFrom::Start(AVDP_LOCATION as u64 * SECTOR_SIZE as u64))?;
        let mut avdp_buf = [0u8; SECTOR_SIZE];
        inner.read_exact(&mut avdp_buf)?;
        let avdp: &AnchorVolumeDescriptorPointer = bytemuck::from_bytes(&avdp_buf[..512]);

        let _vds_location = avdp.main_vds_extent.location;

        // For simplicity, use default partition values
        // A full implementation would parse all VDS descriptors
        let partition_start = 270u32;
        let partition_length = 1000u32;

        // Build a minimal layout - full implementation would parse FSD and root directory
        let existing_layout = DirectoryLayout::root();

        // Build allocation map
        let total_sectors = partition_start + partition_length;
        let allocation_map = AllocationMap::new(total_sectors);

        let end_sector = partition_start + partition_length;

        Ok(Self {
            inner,
            existing_layout,
            allocation_map,
            pending_ops: Vec::new(),
            options,
            revision: UdfRevision::V1_02,
            partition_start,
            partition_length,
            next_unique_id: 16,
            end_sector,
        })
    }

    /// Queues a modification operation.
    pub fn queue(&mut self, op: ModifyOp) {
        self.pending_ops.push(op);
    }

    /// Appends a file to the image (convenience method).
    pub fn append_file(&mut self, path: &str, data: impl Into<FileData>) {
        self.queue(ModifyOp::AppendFile {
            path: path.to_string(),
            data: data.into(),
        });
    }

    /// Creates a directory (convenience method).
    pub fn create_dir(&mut self, path: &str) {
        self.queue(ModifyOp::CreateDir {
            path: path.to_string(),
        });
    }

    /// Marks a file for deletion (convenience method).
    pub fn delete(&mut self, path: &str) {
        self.queue(ModifyOp::Delete {
            path: path.to_string(),
        });
    }

    /// Replaces a file's content (convenience method).
    pub fn replace(&mut self, path: &str, data: impl Into<FileData>) {
        self.queue(ModifyOp::Replace {
            path: path.to_string(),
            data: data.into(),
        });
    }

    /// Returns the current layout.
    pub fn layout(&self) -> &DirectoryLayout {
        &self.existing_layout
    }

    /// Commits all pending changes.
    pub fn commit(mut self) -> UdfModifyResult<()> {
        if self.pending_ops.is_empty() {
            return Ok(());
        }

        // 1. Apply pending ops to layout
        let new_layout = self.apply_ops()?;

        // 2. Write new file data
        let file_extents = self.write_new_data(&new_layout)?;

        // 3. Update UDF metadata
        self.write_new_metadata(&new_layout, file_extents)?;

        Ok(())
    }

    /// Applies pending operations to create a new layout.
    fn apply_ops(&mut self) -> UdfModifyResult<DirectoryLayout> {
        let mut layout = self.existing_layout.clone();

        for op in &self.pending_ops {
            match op {
                ModifyOp::AppendFile { path, data } => {
                    if layout.find_file(path).is_some() {
                        return Err(UdfModifyError::PathExists(path.clone()));
                    }

                    let (dir_path, filename) = Self::split_path(path)?;
                    let dir = if dir_path.is_empty() {
                        &mut layout
                    } else {
                        layout.get_or_create_dir(&dir_path)
                    };

                    let size = data.size()?;
                    let file = FileLayout::new(filename, Extent::new(0, size))
                        .with_type(FileType::RegularFile);
                    dir.add_file(file);
                }
                ModifyOp::CreateDir { path } => {
                    layout.get_or_create_dir(path);
                }
                ModifyOp::Delete { path } => {
                    if layout.remove_file(path).is_none() {
                        return Err(UdfModifyError::FileNotFound(path.clone()));
                    }
                }
                ModifyOp::Replace { path, data } => {
                    let file = layout
                        .find_file_mut(path)
                        .ok_or_else(|| UdfModifyError::FileNotFound(path.clone()))?;

                    let size = data.size()?;
                    file.extent = Extent::new(0, size);
                }
            }
        }

        Ok(layout)
    }

    /// Writes new file data.
    fn write_new_data(
        &mut self,
        _layout: &DirectoryLayout,
    ) -> UdfModifyResult<BTreeMap<String, Extent>> {
        let mut file_extents = BTreeMap::new();

        // Start writing after current end
        let mut current_sector = self.end_sector;

        for op in &self.pending_ops {
            match op {
                ModifyOp::AppendFile { path, data } | ModifyOp::Replace { path, data } => {
                    let size = data.size()?;
                    if size == 0 {
                        file_extents.insert(path.clone(), Extent::new(0, 0));
                        continue;
                    }

                    let extent = Extent::new(current_sector, size);
                    file_extents.insert(path.clone(), extent);

                    // Write data
                    self.inner
                        .seek(SeekFrom::Start(current_sector as u64 * SECTOR_SIZE as u64))?;
                    let content = data.read_all()?;
                    self.inner.write_all(&content)?;

                    // Update current sector
                    current_sector += extent.sector_count(SECTOR_SIZE as u32);
                }
                _ => {}
            }
        }

        // Pad to sector boundary
        let pos = self.inner.stream_position()?;
        let remainder = pos % SECTOR_SIZE as u64;
        if remainder != 0 {
            let padding = SECTOR_SIZE as u64 - remainder;
            let zeros = alloc::vec![0u8; padding as usize];
            self.inner.write_all(&zeros)?;
        }

        self.end_sector = current_sector;

        Ok(file_extents)
    }

    /// Writes new metadata (placeholder - full implementation would update VDS).
    fn write_new_metadata(
        &mut self,
        _layout: &DirectoryLayout,
        _file_extents: BTreeMap<String, Extent>,
    ) -> UdfModifyResult<()> {
        // Note: Full implementation would:
        // 1. Create new File Entries for new files
        // 2. Update root directory FIDs
        // 3. Update LVID with new integrity info
        // 4. Optionally chain new VDS

        // For now, this is a placeholder that logs the intent
        // The actual UDF metadata update is complex and would require
        // rewriting the root directory and potentially VDS

        Ok(())
    }

    /// Splits a path into (directory, filename).
    fn split_path(path: &str) -> UdfModifyResult<(String, String)> {
        let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        if parts.is_empty() {
            return Err(UdfModifyError::InvalidPath(path.to_string()));
        }

        let filename = parts.last().unwrap().to_string();
        let dir_path = if parts.len() > 1 {
            parts[..parts.len() - 1].join("/")
        } else {
            String::new()
        };

        Ok((dir_path, filename))
    }
}

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

    #[test]
    fn test_split_path() {
        let (dir, file) = UdfModifier::<std::io::Cursor<Vec<u8>>>::split_path("test.txt").unwrap();
        assert_eq!(dir, "");
        assert_eq!(file, "test.txt");

        let (dir, file) =
            UdfModifier::<std::io::Cursor<Vec<u8>>>::split_path("docs/readme.txt").unwrap();
        assert_eq!(dir, "docs");
        assert_eq!(file, "readme.txt");
    }

    #[test]
    fn test_file_data() {
        let data = FileData::from(vec![1, 2, 3, 4]);
        assert_eq!(data.size().unwrap(), 4);
        assert_eq!(data.read_all().unwrap(), vec![1, 2, 3, 4]);
    }
}