Skip to content

Commit

Permalink
fix creating tar on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
amr-crabnebula committed Nov 29, 2023
1 parent 1f055d1 commit f42482e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
2 changes: 1 addition & 1 deletion bindings/updater/nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ export class Update {
timeout?: number
download(onChunk?: (chunkLength: number, contentLength: number | null) => void, onDownloadFinished?: () => void): Promise<Array<number>>
install(bytes: Array<number>): Promise<void>
downloadAndInstall(onChunk?: (chunkLength: number, contentLength: number | null) => void, onDownloadFinished?: () => void): Promise<void>
downloadAndInstall(onChunk?: (chunkLength: number, contentLength?: number) => void, onDownloadFinished?: () => void): Promise<void>
}
2 changes: 1 addition & 1 deletion bindings/updater/nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl Update {
}

#[napi(
ts_args_type = "onChunk?: (chunkLength: number, contentLength: number | null) => void, onDownloadFinished?: () => void"
ts_args_type = "onChunk?: (chunkLength: number, contentLength?: number) => void, onDownloadFinished?: () => void"
)]
pub async fn download_and_install(
&self,
Expand Down
33 changes: 32 additions & 1 deletion crates/packager/src/package/deb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ fn tar_and_gzip_dir<P: AsRef<Path>>(src_dir: P) -> crate::Result<PathBuf> {
let dest_path = src_dir.with_extension("tar.gz");
let dest_file = util::create_file(&dest_path)?;
let gzip_encoder = libflate::gzip::Encoder::new(dest_file)?;
let gzip_encoder = util::create_tar_from_dir(src_dir, gzip_encoder)?;
let gzip_encoder = create_tar_from_dir(src_dir, gzip_encoder)?;
let mut dest_file = gzip_encoder.finish().into_result()?;
dest_file.flush()?;
Ok(dest_path)
Expand Down Expand Up @@ -402,3 +402,34 @@ pub(crate) fn package(ctx: &Context) -> crate::Result<Vec<PathBuf>> {
)?;
Ok(vec![deb_path])
}

fn create_tar_from_dir<P: AsRef<Path>, W: Write>(src_dir: P, dest_file: W) -> crate::Result<W> {
let src_dir = src_dir.as_ref();
let mut tar_builder = tar::Builder::new(dest_file);
for entry in walkdir::WalkDir::new(src_dir) {
let entry = entry?;
let src_path = entry.path();
if src_path == src_dir {
continue;
}
let dest_path = src_path.strip_prefix(src_dir)?;
if entry.file_type().is_dir() {
let stat = std::fs::metadata(src_path)?;
let mut header = tar::Header::new_gnu();
header.set_metadata(&stat);
header.set_uid(0);
header.set_gid(0);
tar_builder.append_data(&mut header, dest_path, &mut std::io::empty())?;
} else {
let mut src_file = std::fs::File::open(src_path)?;
let stat = src_file.metadata()?;
let mut header = tar::Header::new_gnu();
header.set_metadata(&stat);
header.set_uid(0);
header.set_gid(0);
tar_builder.append_data(&mut header, dest_path, &mut src_file)?;
}
}
let dest_file = tar_builder.into_inner()?;
Ok(dest_file)
}
34 changes: 7 additions & 27 deletions crates/packager/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,31 +385,11 @@ fn make_icns_image(img: image::DynamicImage) -> std::io::Result<icns::Image> {
/// Writes a tar file to the given writer containing the given directory.
pub fn create_tar_from_dir<P: AsRef<Path>, W: Write>(src_dir: P, dest_file: W) -> crate::Result<W> {
let src_dir = src_dir.as_ref();
let mut tar_builder = tar::Builder::new(dest_file);
for entry in walkdir::WalkDir::new(src_dir) {
let entry = entry?;
let src_path = entry.path();
if src_path == src_dir {
continue;
}
let dest_path = src_path.strip_prefix(src_dir)?;
if entry.file_type().is_dir() {
let stat = std::fs::metadata(src_path)?;
let mut header = tar::Header::new_gnu();
header.set_metadata(&stat);
header.set_uid(0);
header.set_gid(0);
tar_builder.append_data(&mut header, dest_path, &mut std::io::empty())?;
} else {
let mut src_file = std::fs::File::open(src_path)?;
let stat = src_file.metadata()?;
let mut header = tar::Header::new_gnu();
header.set_metadata(&stat);
header.set_uid(0);
header.set_gid(0);
tar_builder.append_data(&mut header, dest_path, &mut src_file)?;
}
}
let dest_file = tar_builder.into_inner()?;
Ok(dest_file)
let filename = src_dir
.file_name()
.ok_or_else(|| crate::Error::FailedToExtractFilename(src_dir.to_path_buf()))?;
let mut builder = tar::Builder::new(dest_file);
builder.follow_symlinks(false);
builder.append_dir_all(filename, src_dir)?;
builder.into_inner().map_err(Into::into)
}

0 comments on commit f42482e

Please sign in to comment.