Skip to content

Commit 1ea996c

Browse files
committed
fix: try fixing copy, again
1 parent 7f92f1b commit 1ea996c

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
@@ -56,31 +56,41 @@ fn main() {
5656
build_bundled(&out_dir, &out_path);
5757
}
5858

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

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

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

8696
fn make_amalgamation() {
@@ -97,6 +107,7 @@ fn make_amalgamation() {
97107
.env("CFLAGS", flags.join(" "))
98108
.output()
99109
.unwrap();
110+
100111
Command::new("make")
101112
.current_dir(SQLITE_DIR)
102113
.output()
@@ -107,6 +118,7 @@ fn make_amalgamation() {
107118
(BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.c"),
108119
)
109120
.unwrap();
121+
110122
copy_with_cp(
111123
(SQLITE_DIR.as_ref() as &Path).join("sqlite3.h"),
112124
(BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.h"),
@@ -298,32 +310,31 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
298310
} else {
299311
"bundled/bindings/bindgen.rs"
300312
};
313+
301314
if std::env::var("LIBSQL_DEV").is_ok() {
302315
let header = HeaderLocation::FromPath(format!("{BUNDLED_DIR}/src/sqlite3.h"));
303316
bindings::write_to_out_dir(header, bindgen_rs_path.as_ref());
304317
}
318+
305319
let dir = env!("CARGO_MANIFEST_DIR");
306320
copy_with_cp(format!("{dir}/{bindgen_rs_path}"), out_path).unwrap();
307321

322+
let out_dir = env::var("OUT_DIR").unwrap();
323+
308324
copy_with_cp(
309-
(BUNDLED_DIR.as_ref() as &Path)
310-
.join("src")
311-
.join("sqlite3.c"),
312-
(BUNDLED_DIR.as_ref() as &Path)
313-
.join("SQLite3MultipleCiphers")
314-
.join("src")
315-
.join("sqlite3.c"),
325+
dbg!(format!("{BUNDLED_DIR}/SQLite3MultipleCiphers")),
326+
format!("{out_dir}/sqlite3mc"),
316327
)
317328
.unwrap();
318329

319-
let bundled_dir = env::current_dir()
320-
.unwrap()
321-
.join(BUNDLED_DIR)
322-
.join("SQLite3MultipleCiphers");
323-
let out_dir = env::var("OUT_DIR").unwrap();
330+
copy_with_cp(
331+
PathBuf::from(BUNDLED_DIR).join("src").join("sqlite3.c"),
332+
format!("{out_dir}/sqlite3mc/src/sqlite3.c"),
333+
)
334+
.unwrap();
335+
336+
let bundled_dir = format!("{out_dir}/sqlite3mc");
324337
let sqlite3mc_build_dir = env::current_dir().unwrap().join(out_dir).join("sqlite3mc");
325-
let _ = fs::remove_dir_all(sqlite3mc_build_dir.clone());
326-
fs::create_dir_all(sqlite3mc_build_dir.clone()).unwrap();
327338

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

0 commit comments

Comments
 (0)