From 6807f184f68ba6905cc1b0d99f709bc7d32d4b55 Mon Sep 17 00:00:00 2001 From: "Eric B. Ridge" Date: Tue, 9 Nov 2021 14:21:40 -0700 Subject: [PATCH] fix bugs with file content filtering --- cargo-pgx/src/commands/install.rs | 39 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/cargo-pgx/src/commands/install.rs b/cargo-pgx/src/commands/install.rs index f4bd68447..d53a80c76 100644 --- a/cargo-pgx/src/commands/install.rs +++ b/cargo-pgx/src/commands/install.rs @@ -38,14 +38,14 @@ pub(crate) fn install_extension( let mut dest = base_directory.clone(); dest.push(&extdir); dest.push(&control_file); - copy_file(control_file, dest, "control file"); + copy_file(&control_file, &dest, "control file", true); } { let mut dest = base_directory.clone(); dest.push(&pkgdir); dest.push(format!("{}.so", extname)); - copy_file(shlibpath, dest, "shared library"); + copy_file(&shlibpath, &dest, "shared library", false); } if !no_schema || !get_target_sql_file(&extdir, &base_directory).exists() { @@ -64,7 +64,7 @@ pub(crate) fn install_extension( Ok(()) } -fn copy_file(src: PathBuf, dest: PathBuf, msg: &str) { +fn copy_file(src: &PathBuf, dest: &PathBuf, msg: &str, do_filter: bool) { if !dest.parent().unwrap().exists() { handle_result!( std::fs::create_dir_all(dest.parent().unwrap()), @@ -82,17 +82,24 @@ fn copy_file(src: PathBuf, dest: PathBuf, msg: &str) { format_display_path(&dest) ); - // we want to filter the contents of each sql file - let input = handle_result!( - std::fs::read_to_string(&src), - format!("failed to read `{}`", src.display()) - ); - let input = filter_contents(input); + if do_filter { + // we want to filter the contents of the file we're to copy + let input = handle_result!( + std::fs::read_to_string(&src), + format!("failed to read `{}`", src.display()) + ); + let input = filter_contents(input); - handle_result!( - std::fs::write(&src, &input), - format!("failed writing `{}` to `{}`", src.display(), dest.display()) - ); + handle_result!( + std::fs::write(&dest, &input), + format!("failed writing `{}` to `{}`", src.display(), dest.display()) + ); + } else { + handle_result!( + std::fs::copy(&src, &dest), + format!("failed copying `{}` to `{}`", src.display(), dest.display()) + ); + } } pub(crate) fn build_extension(major_version: u16, is_release: bool, additional_features: &[&str]) { @@ -167,9 +174,7 @@ fn copy_sql_files( false, true, )?; - let written = std::fs::read_to_string(&dest).unwrap(); - let written = filter_contents(written); - std::fs::write(&dest, written).unwrap(); + copy_file(&dest, &dest, "extension schema file", true); // now copy all the version upgrade files too if let Ok(dir) = std::fs::read_dir("sql/") { @@ -182,7 +187,7 @@ fn copy_sql_files( dest.push(extdir); dest.push(filename); - copy_file(sql.path(), dest, "extension schema file"); + copy_file(&sql.path(), &dest, "extension schema upgrade file", true); } } }