hpkg 1.0.0

A native Rust crate to parse Haiku's binary package and repo formats
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
use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::Write;
use std::mem::size_of;
use std::error::Error;

use flate2::write::ZlibEncoder;
use flate2::Compression;
use zstd::stream::write::Encoder as ZstdEncoder;

use crate::hpkg_common::*;
use crate::package::{PackageHeaderV2, HPKG_FILE_TYPE_FILE, HPKG_FILE_TYPE_DIRECTORY, HPKG_FILE_TYPE_SYMLINK};

// HPKG attribute IDs (from Haiku's PackageAttributes.h) — TOC attributes
const ATTR_DIRECTORY_ENTRY: u16		= 0;
const ATTR_FILE_TYPE: u16			= 1;
const ATTR_FILE_PERMISSIONS: u16	= 2;
const ATTR_FILE_USER: u16			= 3;
const ATTR_FILE_GROUP: u16			= 4;
const ATTR_FILE_MTIME: u16			= 6;
const ATTR_DATA: u16				= 13;
const ATTR_SYMLINK_PATH: u16		= 14;

// Package attribute IDs
const ATTR_PACKAGE_NAME: u16					= 15;
const ATTR_PACKAGE_SUMMARY: u16					= 16;
const ATTR_PACKAGE_DESCRIPTION: u16				= 17;
const ATTR_PACKAGE_VENDOR: u16					= 18;
const ATTR_PACKAGE_PACKAGER: u16				= 19;
const ATTR_PACKAGE_FLAGS: u16					= 20;
const ATTR_PACKAGE_ARCHITECTURE: u16			= 21;
const ATTR_PACKAGE_URL: u16						= 38;
const ATTR_PACKAGE_SOURCE_URL: u16				= 39;
const ATTR_PACKAGE_INSTALL_PATH: u16			= 40;
const ATTR_PACKAGE_BASE_PACKAGE: u16			= 41;

/// A builder for creating HPKG archive files programmatically.
///
/// # Example
///
/// ```no_run
/// use hpkg::package_writer::PackageWriter;
/// use hpkg::hpkg_common::B_HPKG_COMPRESSION_ZLIB;
///
/// let mut writer = PackageWriter::new("/tmp/test.hpkg");
/// writer.set_name("test_package")
///       .set_summary("A test package")
///       .set_vendor("Test")
///       .set_architecture(1)
///       .set_compression(B_HPKG_COMPRESSION_ZLIB);
/// writer.add_file("bin/hello", b"#!/bin/bash\necho hello\n");
/// writer.add_directory("data");
/// writer.finish().unwrap();
/// ```
pub struct PackageWriter {
    path: PathBuf,
    compression: u16,
    chunk_size: u32,
    name: Option<String>,
    summary: Option<String>,
    description: Option<String>,
    vendor: Option<String>,
    packager: Option<String>,
    flags: u32,
    architecture: Option<u64>,
    url: Option<String>,
    source_url: Option<String>,
    install_path: Option<String>,
    base_package: Option<String>,
    entries: Vec<FileEntryToWrite>,
}

/// A file or directory entry to be written into the HPKG archive.
#[derive(Debug, Clone)]
pub struct FileEntryToWrite {
    pub path: String,
    pub file_type: u32,
    pub permissions: u32,
    pub user: Option<String>,
    pub group: Option<String>,
    pub modified_time: Option<u64>,
    pub symlink_target: Option<String>,
    pub data: Vec<u8>,
}

impl FileEntryToWrite {
    pub fn new_file(path: &str, data: &[u8]) -> Self {
        FileEntryToWrite {
            path: path.to_string(),
            file_type: HPKG_FILE_TYPE_FILE,
            permissions: 0o644,
            user: None,
            group: None,
            modified_time: None,
            symlink_target: None,
            data: data.to_vec(),
        }
    }

    pub fn new_directory(path: &str) -> Self {
        FileEntryToWrite {
            path: path.to_string(),
            file_type: HPKG_FILE_TYPE_DIRECTORY,
            permissions: 0o755,
            user: None,
            group: None,
            modified_time: None,
            symlink_target: None,
            data: Vec::new(),
        }
    }

    pub fn new_symlink(path: &str, target: &str) -> Self {
        FileEntryToWrite {
            path: path.to_string(),
            file_type: HPKG_FILE_TYPE_SYMLINK,
            permissions: 0o644,
            user: None,
            group: None,
            modified_time: None,
            symlink_target: Some(target.to_string()),
            data: Vec::new(),
        }
    }
}

impl PackageWriter {
    pub fn new<P: AsRef<Path>>(path: P) -> Self {
        PackageWriter {
            path: path.as_ref().to_path_buf(),
            compression: B_HPKG_COMPRESSION_NONE,
            chunk_size: 65536,
            name: None,
            summary: None,
            description: None,
            vendor: None,
            packager: None,
            flags: 0,
            architecture: None,
            url: None,
            source_url: None,
            install_path: None,
            base_package: None,
            entries: Vec::new(),
        }
    }

    pub fn set_name(&mut self, name: &str) -> &mut Self {
        self.name = Some(name.to_string());
        self
    }

    pub fn set_summary(&mut self, summary: &str) -> &mut Self {
        self.summary = Some(summary.to_string());
        self
    }

    pub fn set_description(&mut self, description: &str) -> &mut Self {
        self.description = Some(description.to_string());
        self
    }

    pub fn set_vendor(&mut self, vendor: &str) -> &mut Self {
        self.vendor = Some(vendor.to_string());
        self
    }

    pub fn set_packager(&mut self, packager: &str) -> &mut Self {
        self.packager = Some(packager.to_string());
        self
    }

    pub fn set_architecture(&mut self, arch: u64) -> &mut Self {
        self.architecture = Some(arch);
        self
    }

    pub fn set_flags(&mut self, flags: u32) -> &mut Self {
        self.flags = flags;
        self
    }

    pub fn set_url(&mut self, url: &str) -> &mut Self {
        self.url = Some(url.to_string());
        self
    }

    pub fn set_source_url(&mut self, url: &str) -> &mut Self {
        self.source_url = Some(url.to_string());
        self
    }

    pub fn set_install_path(&mut self, path: &str) -> &mut Self {
        self.install_path = Some(path.to_string());
        self
    }

    pub fn set_base_package(&mut self, base: &str) -> &mut Self {
        self.base_package = Some(base.to_string());
        self
    }

    pub fn set_compression(&mut self, compression: u16) -> &mut Self {
        self.compression = compression;
        self
    }

    pub fn add_entry(&mut self, entry: FileEntryToWrite) -> &mut Self {
        self.entries.push(entry);
        self
    }

    pub fn add_file(&mut self, path: &str, data: &[u8]) -> &mut Self {
        self.entries.push(FileEntryToWrite::new_file(path, data));
        self
    }

    pub fn add_directory(&mut self, path: &str) -> &mut Self {
        self.entries.push(FileEntryToWrite::new_directory(path));
        self
    }

    pub fn add_symlink(&mut self, path: &str, target: &str) -> &mut Self {
        self.entries.push(FileEntryToWrite::new_symlink(path, target));
        self
    }

    /// Build and write the HPKG archive to the output file.
    pub fn finish(&mut self) -> Result<(), Box<dyn Error>> {
        // 1. Build the directory tree from flat entries
        let mut root = TocNode::new("");
        for entry in &self.entries {
            self.insert_entry_into_tree(&mut root, entry)?;
        }

        // 2. Build file data pool (heap bytes from offset 0)
        //    Also assign data_offset / data_size to leaf TocNodes.
        let mut data_pool: Vec<u8> = Vec::new();
        self.assign_data_offsets(&mut root, &mut data_pool);

        // 3. Serialize TOC section (string table + attribute tree)
        let toc_strings: Vec<u8> = Vec::new();
        let mut toc_tree: Vec<u8> = Vec::new();
        // We write an empty string table (no strings) with inline strings in the tree
        let toc_strings_count: u32 = 0;
        self.serialize_toc_node(&mut root, &mut toc_tree)?;
        let toc_strings_length = toc_strings.len() as u64;
        let toc_length = (toc_strings_length + toc_tree.len() as u64) as u64;

        // 4. Serialize package attributes section
        let attr_strings: Vec<u8> = Vec::new();
        let mut attr_tree: Vec<u8> = Vec::new();
        self.serialize_package_attributes(&mut attr_tree)?;
        let attr_strings_count: u32 = 0;
        let attr_strings_length = attr_strings.len() as u32;
        let attributes_length = (attr_strings_length + attr_tree.len() as u32) as u32;

        // 5. Assemble uncompressed heap: [data_pool][toc_section][attr_section]
        let mut heap = Vec::new();
        heap.extend_from_slice(&data_pool);
        heap.extend_from_slice(&toc_strings);
        heap.extend_from_slice(&toc_tree);
        heap.extend_from_slice(&attr_strings);
        heap.extend_from_slice(&attr_tree);

        let heap_size_uncompressed = heap.len() as u64;

        // 6. Chunk and (if needed) compress the heap
        let chunk_count = (heap_size_uncompressed + self.chunk_size as u64 - 1) / self.chunk_size as u64;
        let mut compressed_chunks: Vec<Vec<u8>> = Vec::new();
        let mut chunk_size_table: Vec<u16> = Vec::new();

        for i in 0..chunk_count {
            let start = (i * self.chunk_size as u64) as usize;
            let end = std::cmp::min(start + self.chunk_size as usize, heap.len());
            let chunk_data = &heap[start..end];

            let compressed = match self.compression {
                B_HPKG_COMPRESSION_NONE => chunk_data.to_vec(),
                B_HPKG_COMPRESSION_ZLIB => {
                    let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
                    encoder.write_all(chunk_data)?;
                    let compressed = encoder.finish()?;
                    if compressed.len() < chunk_data.len() {
                        compressed
                    } else {
                        chunk_data.to_vec()
                    }
                }
                B_HPKG_COMPRESSION_ZSTD => {
                    let mut encoder = ZstdEncoder::new(Vec::new(), 3)?;
                    encoder.write_all(chunk_data)?;
                    let compressed = encoder.finish()?;
                    if compressed.len() < chunk_data.len() {
                        compressed
                    } else {
                        chunk_data.to_vec()
                    }
                }
                _ => return Err(From::from(format!("Unknown compression: {}", self.compression))),
            };
            compressed_chunks.push(compressed);
        }

        // Build chunk size table (for compressed heaps only; N-1 entries)
        let mut heap_size_compressed: u64 = 0;
        if self.compression == B_HPKG_COMPRESSION_NONE {
            heap_size_compressed = heap_size_uncompressed;
        } else {
            for (i, chunk) in compressed_chunks.iter().enumerate() {
                heap_size_compressed += chunk.len() as u64;
                if i < compressed_chunks.len() - 1 {
                    let cookie = (chunk.len() as u16).wrapping_sub(1);
                    chunk_size_table.push(cookie);
                }
            }
            heap_size_compressed += (chunk_size_table.len() * 2) as u64;
        }

        let header_size = size_of::<PackageHeaderV2>() as u16;
        let header = PackageHeaderV2 {
            magic: u32::from_ne_bytes([b'h', b'p', b'k', b'g']),
            header_size: header_size.to_be(),
            version: 2u16.to_be(),
            total_size: (header_size as u64 + heap_size_compressed).to_be(),
            minor_version: 0u16.to_be(),
            heap_compression: self.compression.to_be(),
            heap_chunk_size: self.chunk_size.to_be(),
            heap_size_compressed: heap_size_compressed.to_be(),
            heap_size_uncompressed: heap_size_uncompressed.to_be(),
            attributes_length: attributes_length.to_be(),
            attributes_strings_length: attr_strings_length.to_be(),
            attributes_strings_count: attr_strings_count.to_be(),
            reserved1: 0u32.to_be(),
            toc_length: toc_length.to_be(),
            toc_strings_length: toc_strings_length.to_be(),
            toc_strings_count: (toc_strings_count as u64).to_be(),
        };

        // 7. Write to file
        let mut f = File::create(&self.path)?;

        // Write header (raw bytes)
        let header_bytes = unsafe {
            std::slice::from_raw_parts(
                &header as *const PackageHeaderV2 as *const u8,
                size_of::<PackageHeaderV2>(),
            )
        };
        f.write_all(header_bytes)?;

        // Write compressed chunks
        for chunk in &compressed_chunks {
            f.write_all(chunk)?;
        }

        // Write chunk size table (only for compressed heaps)
        if self.compression != B_HPKG_COMPRESSION_NONE {
            for &cookie in &chunk_size_table {
                f.write_all(&cookie.to_be_bytes())?;
            }
        }

        f.sync_all()?;
        Ok(())
    }

    // -----------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------

    /// Build the directory tree by inserting a flat entry at the correct path.
    fn insert_entry_into_tree(&self, root: &mut TocNode, entry: &FileEntryToWrite) -> Result<(), Box<dyn Error>> {
        let path = entry.path.trim_start_matches('/');
        if path.is_empty() {
            return Err(From::from("Empty entry path".to_string()));
        }
        let components: Vec<&str> = path.split('/').collect();
        let mut node = root;

        for (i, component) in components.iter().enumerate() {
            let is_last = i == components.len() - 1;
            if is_last {
                node = node.get_or_create_child(component, entry);
            } else {
                node = node.get_or_create_dir_child(component);
            }
        }
        Ok(())
    }

    /// Walk the tree in pre-order and assign data pool offsets for file entries.
    fn assign_data_offsets(&self, node: &mut TocNode, pool: &mut Vec<u8>) {
        if node.file_type == HPKG_FILE_TYPE_FILE && !node.data.is_empty() {
            node.data_offset = Some(pool.len());
            node.data_size = Some(node.data.len());
            pool.extend_from_slice(&node.data);
        }
        for child in &mut node.children {
            self.assign_data_offsets(child, pool);
        }
    }

    /// Serialize a single TOC node (DIRECTORY_ENTRY with its children).
    fn serialize_toc_node(&self, node: &TocNode, buf: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
        // Write DIRECTORY_ENTRY tag with the entry name as inline string
        let has_children = !node.children.is_empty()
            || node.file_type != HPKG_FILE_TYPE_DIRECTORY;
        write_attr_tag(buf, ATTR_DIRECTORY_ENTRY, HPKG_ATTR_TYPE_STRING, HPKG_ATTR_ENCODING_STRING_INLINE, has_children);
        write_string_inline(buf, &node.name);

        if has_children {
            // Write metadata attributes
            if node.file_type != HPKG_FILE_TYPE_DIRECTORY {
                write_attr_tag(buf, ATTR_FILE_TYPE, HPKG_ATTR_TYPE_UINT, uint_encoding(node.file_type as u64), false);
                write_int_value(buf, node.file_type as u64, uint_encoding(node.file_type as u64));
            }

            if node.permissions != 0o755 {
                write_attr_tag(buf, ATTR_FILE_PERMISSIONS, HPKG_ATTR_TYPE_UINT, uint_encoding(node.permissions as u64), false);
                write_int_value(buf, node.permissions as u64, uint_encoding(node.permissions as u64));
            }

            if let Some(ref user) = node.user {
                write_attr_tag(buf, ATTR_FILE_USER, HPKG_ATTR_TYPE_STRING, HPKG_ATTR_ENCODING_STRING_INLINE, false);
                write_string_inline(buf, user);
            }

            if let Some(ref group) = node.group {
                write_attr_tag(buf, ATTR_FILE_GROUP, HPKG_ATTR_TYPE_STRING, HPKG_ATTR_ENCODING_STRING_INLINE, false);
                write_string_inline(buf, group);
            }

            if let Some(time) = node.modified_time {
                write_attr_tag(buf, ATTR_FILE_MTIME, HPKG_ATTR_TYPE_UINT, uint_encoding(time), false);
                write_int_value(buf, time, uint_encoding(time));
            }

            // ATTR_DATA or ATTR_SYMLINK_PATH
            if let Some(ref target) = node.symlink_path {
                write_attr_tag(buf, ATTR_SYMLINK_PATH, HPKG_ATTR_TYPE_STRING, HPKG_ATTR_ENCODING_STRING_INLINE, false);
                write_string_inline(buf, target);
            } else if let (Some(offset), Some(size)) = (node.data_offset, node.data_size) {
                if size > 0 {
                    // Use RAW_HEAP encoding for large data, RAW_INLINE for small
                    if size > 8 {
                        write_attr_tag(buf, ATTR_DATA, HPKG_ATTR_TYPE_RAW, HPKG_ATTR_ENCODING_RAW_HEAP, false);
                        write_unsigned_leb128(buf, size as u64);
                        write_unsigned_leb128(buf, offset as u64);
                    } else {
                        write_attr_tag(buf, ATTR_DATA, HPKG_ATTR_TYPE_RAW, HPKG_ATTR_ENCODING_RAW_INLINE, false);
                        write_unsigned_leb128(buf, size as u64);
                        if let Some(ref data) = node.data.as_slice().get(..size) {
                            buf.extend_from_slice(data);
                        }
                    }
                }
            }

            // Sub-entries
            for child in &node.children {
                self.serialize_toc_node(child, buf)?;
            }

            // End-of-children marker
            write_unsigned_leb128(buf, 0);
        }
        Ok(())
    }

    /// Serialize the package attributes section.
    fn serialize_package_attributes(&self, buf: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
        let pairs: Vec<(u16, &str)> = vec![
            (ATTR_PACKAGE_NAME, "name"),
            (ATTR_PACKAGE_SUMMARY, "summary"),
            (ATTR_PACKAGE_DESCRIPTION, "description"),
            (ATTR_PACKAGE_VENDOR, "vendor"),
            (ATTR_PACKAGE_PACKAGER, "packager"),
            (ATTR_PACKAGE_URL, "url"),
            (ATTR_PACKAGE_SOURCE_URL, "source_url"),
            (ATTR_PACKAGE_INSTALL_PATH, "install_path"),
            (ATTR_PACKAGE_BASE_PACKAGE, "base_package"),
        ];

        for (attr_id, field) in pairs {
            let value: Option<&str> = match field {
                "name" => self.name.as_deref(),
                "summary" => self.summary.as_deref(),
                "description" => self.description.as_deref(),
                "vendor" => self.vendor.as_deref(),
                "packager" => self.packager.as_deref(),
                "url" => self.url.as_deref(),
                "source_url" => self.source_url.as_deref(),
                "install_path" => self.install_path.as_deref(),
                "base_package" => self.base_package.as_deref(),
                _ => None,
            };
            if let Some(v) = value {
                write_attr_tag(buf, attr_id, HPKG_ATTR_TYPE_STRING, HPKG_ATTR_ENCODING_STRING_INLINE, false);
                write_string_inline(buf, v);
            }
        }

        // Flags
        if self.flags != 0 {
            write_attr_tag(buf, ATTR_PACKAGE_FLAGS, HPKG_ATTR_TYPE_UINT, uint_encoding(self.flags as u64), false);
            write_int_value(buf, self.flags as u64, uint_encoding(self.flags as u64));
        }

        // Architecture
        if let Some(arch) = self.architecture {
            write_attr_tag(buf, ATTR_PACKAGE_ARCHITECTURE, HPKG_ATTR_TYPE_UINT, uint_encoding(arch), false);
            write_int_value(buf, arch, uint_encoding(arch));
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// TOC tree internal node
// ---------------------------------------------------------------------------
struct TocNode {
    name: String,
    file_type: u32,
    permissions: u32,
    user: Option<String>,
    group: Option<String>,
    modified_time: Option<u64>,
    symlink_path: Option<String>,
    data: Vec<u8>,
    data_offset: Option<usize>,
    data_size: Option<usize>,
    children: Vec<TocNode>,
}

impl TocNode {
    fn new(name: &str) -> Self {
        TocNode {
            name: name.to_string(),
            file_type: HPKG_FILE_TYPE_DIRECTORY,
            permissions: 0o755,
            user: None,
            group: None,
            modified_time: None,
            symlink_path: None,
            data: Vec::new(),
            data_offset: None,
            data_size: None,
            children: Vec::new(),
        }
    }

    fn get_or_create_dir_child(&mut self, name: &str) -> &mut TocNode {
        let idx = self.children.iter().position(|c| c.name == name);
        let idx = idx.unwrap_or_else(|| {
            self.children.push(TocNode::new(name));
            self.children.len() - 1
        });
        &mut self.children[idx]
    }

    fn get_or_create_child(&mut self, name: &str, entry: &FileEntryToWrite) -> &mut TocNode {
        let idx = self.children.iter().position(|c| c.name == name);
        let idx = idx.unwrap_or_else(|| {
            self.children.push(TocNode {
                name: name.to_string(),
                file_type: entry.file_type,
                permissions: entry.permissions,
                user: entry.user.clone(),
                group: entry.group.clone(),
                modified_time: entry.modified_time,
                symlink_path: entry.symlink_target.clone(),
                data: entry.data.clone(),
                data_offset: None,
                data_size: None,
                children: Vec::new(),
            });
            self.children.len() - 1
        });
        &mut self.children[idx]
    }
}

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

    #[test]
    fn test_writer_round_trip_uncompressed() {
        let tmp = std::env::temp_dir().join("test_round_trip_uncompressed.hpkg");
        let _ = std::fs::remove_file(&tmp);

        {
            let mut writer = PackageWriter::new(&tmp);
            writer.set_name("test_pkg")
                  .set_summary("A test package")
                  .set_vendor("Test Vendor")
                  .set_architecture(ARCH_X86_64)
                  .set_compression(B_HPKG_COMPRESSION_NONE)
                  .set_url("https://example.com");
            writer.add_file("bin/hello", b"#!/bin/sh\necho hello\n");
            writer.add_file("data/file.txt", b"Hello, HPKG World!\n");
            writer.add_directory("data/subdir");
            writer.add_symlink("bin/link", "hello");
            writer.finish().expect("write should succeed");
        }

        // Read it back
        let pkg = Package::load(&tmp).expect("should load written package");
        assert_eq!(pkg.name.as_deref(), Some("test_pkg"));
        assert_eq!(pkg.summary.as_deref(), Some("A test package"));
        assert_eq!(pkg.vendor.as_deref(), Some("Test Vendor"));
        assert_eq!(pkg.architecture.as_deref(), Some("x86_64"));
        assert_eq!(pkg.url.as_deref(), Some("https://example.com"));

        // Check files
        let files = pkg.list_files();
        let file_names: Vec<&str> = files.iter().map(|f| f.path.as_str()).collect();
        assert!(file_names.contains(&"bin/hello"), "should contain bin/hello, got {:?}", file_names);
        assert!(file_names.contains(&"data/file.txt"), "should contain data/file.txt");

        // Read file contents
        let hello_data = pkg.read_file("bin/hello").expect("should read bin/hello");
        assert_eq!(hello_data, b"#!/bin/sh\necho hello\n");

        let txt_data = pkg.read_file("data/file.txt").expect("should read data/file.txt");
        assert_eq!(txt_data, b"Hello, HPKG World!\n");

        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn test_writer_round_trip_zstd() {
        let tmp = std::env::temp_dir().join("test_round_trip_zstd.hpkg");
        let _ = std::fs::remove_file(&tmp);

        {
            let mut writer = PackageWriter::new(&tmp);
            writer.set_name("zstd_pkg")
                  .set_summary("Zstd compressed test")
                  .set_vendor("Test")
                  .set_architecture(ARCH_ANY)
                  .set_compression(B_HPKG_COMPRESSION_ZSTD);
            writer.add_file("data.txt", b"Zstd compression test data.\n");
            writer.finish().expect("write should succeed");
        }

        let pkg = Package::load(&tmp).expect("should load zstd-compressed package");
        assert_eq!(pkg.name.as_deref(), Some("zstd_pkg"));
        assert!(pkg.header.unwrap().heap_compression == B_HPKG_COMPRESSION_ZSTD);

        let data = pkg.read_file("data.txt").expect("should read data.txt");
        assert_eq!(data, b"Zstd compression test data.\n");

        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn test_writer_round_trip_zlib() {
        let tmp = std::env::temp_dir().join("test_round_trip_zlib.hpkg");
        let _ = std::fs::remove_file(&tmp);

        {
            let mut writer = PackageWriter::new(&tmp);
            writer.set_name("zlib_pkg")
                  .set_summary("Zlib compressed test")
                  .set_vendor("Test")
                  .set_architecture(ARCH_SOURCE)
                  .set_compression(B_HPKG_COMPRESSION_ZLIB);
            writer.add_file("readme.txt", b"This is a test file.\n");
            writer.finish().expect("write should succeed");
        }

        let pkg = Package::load(&tmp).expect("should load zlib-compressed package");
        assert_eq!(pkg.name.as_deref(), Some("zlib_pkg"));
        assert!(pkg.header.unwrap().heap_compression == B_HPKG_COMPRESSION_ZLIB);

        let data = pkg.read_file("readme.txt").expect("should read readme.txt");
        assert_eq!(data, b"This is a test file.\n");

        let _ = std::fs::remove_file(&tmp);
    }
}