Skip to content

Commit

Permalink
style: remove identation change
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoaldamav committed Nov 2, 2023
1 parent 6236497 commit c3c6f2b
Showing 1 changed file with 59 additions and 63 deletions.
122 changes: 59 additions & 63 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,94 +7,90 @@ use napi::{bindgen_prelude::AsyncTask, Env, Error, JsNumber, Result, Task};
use std::path::PathBuf;

pub struct AsyncReflink {
src: PathBuf,
dst: PathBuf,
src: PathBuf,
dst: PathBuf,
}

#[napi]
impl Task for AsyncReflink {
type Output = ();
type JsValue = JsNumber;

fn compute(&mut self) -> Result<Self::Output> {
// Convert PathBuf to &str
let src_str = self
.src
.to_str()
.ok_or_else(|| Error::from_reason("Invalid UTF-8 sequence in source path".to_string()))?;
let dst_str = self.dst.to_str().ok_or_else(|| {
Error::from_reason("Invalid UTF-8 sequence in destination path".to_string())
})?;

// Call reflink with string slices
match reflink(src_str, dst_str) {
Ok(_) => Ok(()),
Err(err) => Err(Error::from_reason(format!(
"{}, reflink '{}' -> '{}'",
err.to_string(),
src_str,
dst_str
))),
type Output = ();
type JsValue = JsNumber;

fn compute(&mut self) -> Result<Self::Output> {
let src_str = self.src.to_str().ok_or_else(|| {
Error::from_reason("Invalid UTF-8 sequence in source path".to_string())
})?;

let dst_str = self.dst.to_str().ok_or_else(|| {
Error::from_reason("Invalid UTF-8 sequence in destination path".to_string())
})?;

match reflink(src_str, dst_str) {
Ok(_) => {
Ok(())
},
Err(err) => return Err(Error::from_reason(format!(
"{}, reflink '{}' -> '{}'",
err.to_string(),
self.src.display(),
self.dst.display()
))),
}
}
}

fn resolve(&mut self, env: Env, _: ()) -> Result<Self::JsValue> {
env.create_int32(0)
}
fn resolve(&mut self, env: Env, _: ()) -> Result<Self::JsValue> {
env.create_int32(0)
}
}

// Async version
#[napi(js_name = "reflinkFile")]
pub fn reflink_task(src: String, dst: String) -> AsyncTask<AsyncReflink> {
let src_path = PathBuf::from(src);
let dst_path = PathBuf::from(dst);
AsyncTask::new(AsyncReflink {
src: src_path,
dst: dst_path,
})
let src_path = PathBuf::from(src);
let dst_path = PathBuf::from(dst);
AsyncTask::new(AsyncReflink { src: src_path, dst: dst_path })
}

// Sync version
#[napi(js_name = "reflinkFileSync")]
pub fn reflink_sync(env: Env, src: String, dst: String) -> Result<JsNumber> {
// Call reflink with string slices
match reflink(&src, &dst) {
Ok(_) => Ok(env.create_int32(0)?),
Err(err) => Err(Error::from_reason(format!(
"{}, reflink '{}' -> '{}'",
err.to_string(),
src,
dst
))),
}
match reflink(&src, &dst) {
Ok(_) => Ok(env.create_int32(0)?),
Err(err) => Err(Error::from_reason(format!(
"{}, reflink '{}' -> '{}'",
err.to_string(),
src,
dst
))),
}
}

#[test]
pub fn test_pyc_file() {
let src = "fixtures/sample.pyc";
let dst = "fixtures/sample.pyc.reflink";
let src = "fixtures/sample.pyc";
let dst = "fixtures/sample.pyc.reflink";

let dst_path = std::path::Path::new(dst);
let dst_path = std::path::Path::new(dst);

// Remove the destination file if it already exists
if dst_path.exists() {
std::fs::remove_file(&dst_path).unwrap();
}
// Remove the destination file if it already exists
if dst_path.exists() {
std::fs::remove_file(&dst).unwrap();
}

// Run the reflink operation
let result = reflink(src, dst);
assert!(result.is_ok());
// Run the reflink operation
let result = reflink(&src, &dst);
assert!(result.is_ok());

println!("Reflinked '{}' -> '{}'", src, dst);
println!("Reflinked '{}' -> '{}'", src, dst);

// Further validation: compare the contents of both files to make sure they are identical
let src_contents = std::fs::read(src).expect("Failed to read source file");
let dst_contents = std::fs::read(dst).expect("Failed to read destination file");
// Further validation: compare the contents of both files to make sure they are identical
let src_contents = std::fs::read(&src).expect("Failed to read source file");
let dst_contents = std::fs::read(&dst).expect("Failed to read destination file");

assert_eq!(src_contents, dst_contents);
assert_eq!(src_contents, dst_contents);

// Remove the destination file
std::fs::remove_file(&dst_path).unwrap();
// Remove the destination file
std::fs::remove_file(&dst).unwrap();

println!("File contents match, reflink operation successful")
}
println!("File contents match, reflink operation successful")
}

0 comments on commit c3c6f2b

Please sign in to comment.