Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alignment handling broken? #33

Closed
danielocfb opened this issue Apr 23, 2024 · 0 comments
Closed

Alignment handling broken? #33

danielocfb opened this issue Apr 23, 2024 · 0 comments

Comments

@danielocfb
Copy link

I tried upgrading to version 1.1 and it seems as if handling of alignment is broken.

use std::env;
use std::fs::read as read_file;
use std::fs::File;
use std::io::Write as _;
use std::path::Path;

use zip::write::SimpleFileOptions;
use zip::CompressionMethod;
use zip::ZipArchive;
use zip::ZipWriter;

fn main() {
    let page_size = 4096;
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let dst_dir = Path::new(&crate_dir);
    let dst_file = dst_dir.join("test.zip");

    {
        let dst_file = File::options()
            .create(true)
            .truncate(true)
            .read(false)
            .write(true)
            .open(&dst_file)
            .unwrap();

        let options = SimpleFileOptions::default()
            .compression_method(CompressionMethod::Stored)
            .with_alignment(page_size.try_into().unwrap());
        let mut zip = ZipWriter::new(dst_file);
        let contents = read_file("/usr/bin/sleep").unwrap();
        let () = zip.start_file("sleep", options).unwrap();
        let _count = zip.write(&contents).unwrap();
    }

    {
        let dst_file = File::open(dst_file).unwrap();
        let mut zip = ZipArchive::new(dst_file).unwrap();
        let file = zip.by_index(0).unwrap();
        assert_eq!(file.name(), "sleep");
        assert_eq!(file.data_start(), page_size);
    }
}

fails with:

thread 'main' panicked at src/main.rs:41:9:
assertion `left == right` failed
  left: 4092
 right: 4096
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Similar code on 0.6.4 works fine:

use std::env;
use std::fs::read as read_file;
use std::fs::File;
use std::io::Write as _;
use std::path::Path;

use zip::write::FileOptions;
use zip::CompressionMethod;
use zip::ZipArchive;
use zip::ZipWriter;

fn main() {
    let page_size = 4096;
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let dst_dir = Path::new(&crate_dir);
    let dst_file = dst_dir.join("test.zip");

    {
        let dst_file = File::options()
            .create(true)
            .truncate(true)
            .read(false)
            .write(true)
            .open(&dst_file)
            .unwrap();

        let options = FileOptions::default().compression_method(CompressionMethod::Stored);
        let mut zip = ZipWriter::new(dst_file);
        let contents = read_file("/usr/bin/sleep").unwrap();
        let _align = zip
            .start_file_aligned(
                "sleep",
                options,
                page_size.try_into().unwrap(),
            )
            .unwrap();
        let _count = zip.write(&contents).unwrap();
    }

    {
        let dst_file = File::open(dst_file).unwrap();
        let mut zip = ZipArchive::new(dst_file).unwrap();
        let file = zip.by_index(0).unwrap();
        assert_eq!(file.name(), "sleep");
        assert_eq!(file.data_start(), page_size);
    }
}

Am I misunderstanding what is being aligned?

Pr0methean added a commit that referenced this issue Apr 23, 2024
@Pr0methean Pr0methean added the fix merged Fix is merged, but the issue will stay open until it's released. label Apr 24, 2024
@Pr0methean Pr0methean removed the fix merged Fix is merged, but the issue will stay open until it's released. label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants