Skip to content

Commit 6610598

Browse files
committed
fix: try fixing copy, again
1 parent 778dacc commit 6610598

1 file changed

Lines changed: 38 additions & 27 deletions

File tree

libsql-ffi/build.rs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,41 @@ fn main() {
5757
build_bundled(&out_dir, &out_path);
5858
}
5959

60-
#[cfg(target_os = "windows")]
61-
fn copy_with_cp(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
62-
fs::copy(src, dst)?; // do a regular file copy on Windows
60+
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
61+
let dst = dst.as_ref();
62+
fs::create_dir_all(dst)?;
63+
for entry in fs::read_dir(src)? {
64+
let entry = entry?;
65+
let ty = entry.file_type()?;
66+
if ty.is_dir() {
67+
copy_dir_all(entry.path(), dst.join(entry.file_name()))?;
68+
} else {
69+
fs::copy(entry.path(), dst.join(entry.file_name()))?;
70+
}
71+
}
6372
Ok(())
6473
}
6574

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

77-
if !status.success() {
78-
Err(io::Error::new(
79-
io::ErrorKind::Other,
80-
"Failed to copy using cp",
81-
))
82-
} else {
83-
Ok(())
86+
if status.success() {
87+
return Ok(());
8488
}
89+
90+
return match fs::copy(from.as_ref(), to.as_ref()) {
91+
Err(err) if err.kind() == io::ErrorKind::InvalidInput => copy_dir_all(from, to),
92+
Ok(_) => Ok(()),
93+
Err(err) => Err(err),
94+
};
8595
}
8696

8797
fn make_amalgamation() {
@@ -98,6 +108,7 @@ fn make_amalgamation() {
98108
.env("CFLAGS", flags.join(" "))
99109
.output()
100110
.unwrap();
111+
101112
Command::new("make")
102113
.current_dir(SQLITE_DIR)
103114
.output()
@@ -108,6 +119,7 @@ fn make_amalgamation() {
108119
(BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.c"),
109120
)
110121
.unwrap();
122+
111123
copy_with_cp(
112124
(SQLITE_DIR.as_ref() as &Path).join("sqlite3.h"),
113125
(BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.h"),
@@ -406,32 +418,31 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
406418
} else {
407419
"bundled/bindings/bindgen.rs"
408420
};
421+
409422
if std::env::var("LIBSQL_DEV").is_ok() {
410423
let header = HeaderLocation::FromPath(format!("{BUNDLED_DIR}/src/sqlite3.h"));
411424
bindings::write_to_out_dir(header, bindgen_rs_path.as_ref());
412425
}
426+
413427
let dir = env!("CARGO_MANIFEST_DIR");
414428
copy_with_cp(format!("{dir}/{bindgen_rs_path}"), out_path).unwrap();
415429

430+
let out_dir = env::var("OUT_DIR").unwrap();
431+
416432
copy_with_cp(
417-
(BUNDLED_DIR.as_ref() as &Path)
418-
.join("src")
419-
.join("sqlite3.c"),
420-
(BUNDLED_DIR.as_ref() as &Path)
421-
.join("SQLite3MultipleCiphers")
422-
.join("src")
423-
.join("sqlite3.c"),
433+
dbg!(format!("{BUNDLED_DIR}/SQLite3MultipleCiphers")),
434+
format!("{out_dir}/sqlite3mc"),
424435
)
425436
.unwrap();
426437

427-
let bundled_dir = env::current_dir()
428-
.unwrap()
429-
.join(BUNDLED_DIR)
430-
.join("SQLite3MultipleCiphers");
431-
let out_dir = env::var("OUT_DIR").unwrap();
438+
copy_with_cp(
439+
PathBuf::from(BUNDLED_DIR).join("src").join("sqlite3.c"),
440+
format!("{out_dir}/sqlite3mc/src/sqlite3.c"),
441+
)
442+
.unwrap();
443+
444+
let bundled_dir = format!("{out_dir}/sqlite3mc");
432445
let sqlite3mc_build_dir = env::current_dir().unwrap().join(out_dir).join("sqlite3mc");
433-
let _ = fs::remove_dir_all(sqlite3mc_build_dir.clone());
434-
fs::create_dir_all(sqlite3mc_build_dir.clone()).unwrap();
435446

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

0 commit comments

Comments
 (0)