Skip to content

Commit

Permalink
fix: try fixing copy, again
Browse files Browse the repository at this point in the history
  • Loading branch information
levydsa committed Jan 10, 2025
1 parent 7f92f1b commit 1ea996c
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions libsql-ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,41 @@ fn main() {
build_bundled(&out_dir, &out_path);
}

#[cfg(target_os = "windows")]
fn copy_with_cp(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::copy(src, dst)?; // do a regular file copy on Windows
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
let dst = dst.as_ref();
fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.join(entry.file_name()))?;
}
}
Ok(())
}

/// This ensures that in sandboxed environments, such as Nix, permissions from other sources don't
/// propagate into OUT_DIR. If not present, when trying to rewrite a file, a `Permission denied`
/// error will occur.
fn copy_with_cp(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fn copy_with_cp(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<()> {
let status = Command::new("cp")
.arg("--no-preserve=mode,ownership")
.arg("-R")
.arg(src.as_ref().to_str().unwrap())
.arg(dst.as_ref().to_str().unwrap())
.arg(from.as_ref().to_str().unwrap())
.arg(to.as_ref().to_str().unwrap())
.status()?;

if !status.success() {
Err(io::Error::new(
io::ErrorKind::Other,
"Failed to copy using cp",
))
} else {
Ok(())
if status.success() {
return Ok(());
}

return match fs::copy(from.as_ref(), to.as_ref()) {
Err(err) if err.kind() == io::ErrorKind::InvalidInput => copy_dir_all(from, to),
Ok(_) => Ok(()),
Err(err) => Err(err),
};
}

fn make_amalgamation() {
Expand All @@ -97,6 +107,7 @@ fn make_amalgamation() {
.env("CFLAGS", flags.join(" "))
.output()
.unwrap();

Command::new("make")
.current_dir(SQLITE_DIR)
.output()
Expand All @@ -107,6 +118,7 @@ fn make_amalgamation() {
(BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.c"),
)
.unwrap();

copy_with_cp(
(SQLITE_DIR.as_ref() as &Path).join("sqlite3.h"),
(BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.h"),
Expand Down Expand Up @@ -298,32 +310,31 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
} else {
"bundled/bindings/bindgen.rs"
};

if std::env::var("LIBSQL_DEV").is_ok() {
let header = HeaderLocation::FromPath(format!("{BUNDLED_DIR}/src/sqlite3.h"));
bindings::write_to_out_dir(header, bindgen_rs_path.as_ref());
}

let dir = env!("CARGO_MANIFEST_DIR");
copy_with_cp(format!("{dir}/{bindgen_rs_path}"), out_path).unwrap();

let out_dir = env::var("OUT_DIR").unwrap();

copy_with_cp(
(BUNDLED_DIR.as_ref() as &Path)
.join("src")
.join("sqlite3.c"),
(BUNDLED_DIR.as_ref() as &Path)
.join("SQLite3MultipleCiphers")
.join("src")
.join("sqlite3.c"),
dbg!(format!("{BUNDLED_DIR}/SQLite3MultipleCiphers")),
format!("{out_dir}/sqlite3mc"),
)
.unwrap();

let bundled_dir = env::current_dir()
.unwrap()
.join(BUNDLED_DIR)
.join("SQLite3MultipleCiphers");
let out_dir = env::var("OUT_DIR").unwrap();
copy_with_cp(
PathBuf::from(BUNDLED_DIR).join("src").join("sqlite3.c"),
format!("{out_dir}/sqlite3mc/src/sqlite3.c"),
)
.unwrap();

let bundled_dir = format!("{out_dir}/sqlite3mc");
let sqlite3mc_build_dir = env::current_dir().unwrap().join(out_dir).join("sqlite3mc");
let _ = fs::remove_dir_all(sqlite3mc_build_dir.clone());
fs::create_dir_all(sqlite3mc_build_dir.clone()).unwrap();

let mut cmake_opts: Vec<&str> = vec![];

Expand Down

0 comments on commit 1ea996c

Please sign in to comment.