alexcrichton / tar-rs

Tar file reading/writing for Rust
https://docs.rs/tar
Apache License 2.0
616 stars 178 forks source link

mtime is incorrectly read as 0 #349

Open konstin opened 7 months ago

konstin commented 7 months ago

For certain tar file, the tar command on ubuntu 23.10 and tarfile in the python 3.11 standard library read the correct mtime, while tar-rs claims it's 0. Example: https://files.pythonhosted.org/packages/c0/3f/d7af728f075fb08564c5949a9c95e44352e23dee646869fa104a3b2060a3/tomli-2.0.1.tar.gz

tar-rs prints 0:

use flate2::read::GzDecoder;
use std::fs::File;
use std::path::Path;
use tar::Archive;

fn main() {
    let mut archive = Archive::new(GzDecoder::new(File::open("tomli-2.0.1.tar.gz").unwrap()));
    let entry = archive
        .entries()
        .unwrap()
        .map(|entry| entry.unwrap())
        .find(|entry| {
            entry.path().unwrap().as_ref() == Path::new("tomli-2.0.1/src/tomli/__init__.py")
        })
        .unwrap();
    println!("{}", entry.header().mtime().unwrap());
}

python prints 1644317623.2803335:

import tarfile

if __name__ == '__main__':
    archive = tarfile.open("tomli-2.0.1.tar.gz")
    init_py = archive.getmember("tomli-2.0.1/src/tomli/__init__.py")
    print(init_py.mtime)

tar sets the same timestamp:

$ tar xf tomli-2.0.1.tar.gz
$ ls -al --full-time tomli-2.0.1/src/tomli/__init__.py
-rw-r--r-- 1 konsti konsti 396 2022-02-08 11:53:43.280333500 +0100 tomli-2.0.1/src/tomli/__init__.py

This creates a problem because the timstamps are by default inferred to unix time 1 (~1970), while python std's zipfile library does not support timestamps before 1980.