Skip to content

Commit da54c1b

Browse files
committed
fix: try fixing copy, again
1 parent 5e20bd3 commit da54c1b

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

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"),
@@ -410,32 +422,31 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
410422
} else {
411423
"bundled/bindings/bindgen.rs"
412424
};
425+
413426
if std::env::var("LIBSQL_DEV").is_ok() {
414427
let header = HeaderLocation::FromPath(format!("{BUNDLED_DIR}/src/sqlite3.h"));
415428
bindings::write_to_out_dir(header, bindgen_rs_path.as_ref());
416429
}
430+
417431
let dir = env!("CARGO_MANIFEST_DIR");
418432
copy_with_cp(format!("{dir}/{bindgen_rs_path}"), out_path).unwrap();
419433

434+
let out_dir = env::var("OUT_DIR").unwrap();
435+
420436
copy_with_cp(
421-
(BUNDLED_DIR.as_ref() as &Path)
422-
.join("src")
423-
.join("sqlite3.c"),
424-
(BUNDLED_DIR.as_ref() as &Path)
425-
.join("SQLite3MultipleCiphers")
426-
.join("src")
427-
.join("sqlite3.c"),
437+
dbg!(format!("{BUNDLED_DIR}/SQLite3MultipleCiphers")),
438+
format!("{out_dir}/sqlite3mc"),
428439
)
429440
.unwrap();
430441

431-
let bundled_dir = env::current_dir()
432-
.unwrap()
433-
.join(BUNDLED_DIR)
434-
.join("SQLite3MultipleCiphers");
435-
let out_dir = env::var("OUT_DIR").unwrap();
442+
copy_with_cp(
443+
PathBuf::from(BUNDLED_DIR).join("src").join("sqlite3.c"),
444+
format!("{out_dir}/sqlite3mc/src/sqlite3.c"),
445+
)
446+
.unwrap();
447+
448+
let bundled_dir = format!("{out_dir}/sqlite3mc");
436449
let sqlite3mc_build_dir = env::current_dir().unwrap().join(out_dir).join("sqlite3mc");
437-
let _ = fs::remove_dir_all(sqlite3mc_build_dir.clone());
438-
fs::create_dir_all(sqlite3mc_build_dir.clone()).unwrap();
439450

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

0 commit comments

Comments
 (0)