Skip to content

Commit

Permalink
Merge pull request #14 from functionland/ls-troubleshooting
Browse files Browse the repository at this point in the history
Ls troubleshooting
  • Loading branch information
ehsan6sha authored Dec 17, 2022
2 parents 45eb86e + bf6ba7b commit b7fdfb2
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 77 deletions.
9 changes: 5 additions & 4 deletions appmock/src/androidTest/java/land/fx/app/WNFSTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ class WNFSTest {
//assertTrue(isNewFileCreated)
file.writeBytes(testContent)


/*
try {
val config_err = writeFileFromPath(client, config.cid, config.private_ref, "root/testfrompath.txt", "file://"+pathString+"/test.txt")
Log.d("AppMock", "config_err writeFile. config_err="+config_err)
} catch (e: Exception) {
assertNotNull("config should not be null", e)
Log.d("AppMock", "config_err Error catched "+e.message);
}

*/

config = writeFileFromPath(client, config.cid, config.private_ref, "root/testfrompath.txt", pathString+"/test.txt")
Log.d("AppMock", "config writeFile. cid="+config.cid+" & private_ref="+config.private_ref)
Expand All @@ -129,11 +129,12 @@ class WNFSTest {
assertNotNull("contentfrompathtopath should not be null", contentfrompathtopath)
val readcontent: ByteArray = File(contentfrompathtopath).readBytes()
assert(readcontent contentEquals "Hello, World!".toByteArray())
Log.d("AppMock", "readFileFromPathOfReadTo. content="+readcontent.toString())
Log.d("AppMock", "readFileFromPathOfReadTo. content="+String(readcontent))

config = rm(client, config.cid, config.private_ref, "root/testfrompath.txt")
val content2 = readFile(client, config.cid, config.private_ref, "root/testfrompath.txt")
assertNull(content2)
Log.d("AppMock", "rm. content="+String(content2))
assert(content2 contentEquals "".toByteArray())


config = writeFile(client, config.cid, config.private_ref, "root/test.txt", "Hello, World!".toByteArray())
Expand Down
56 changes: 36 additions & 20 deletions dep/wnfsutils/src/private_forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,27 @@ impl<'a> PrivateDirectoryHelper<'a> {
}
}

pub async fn read_file_to_path(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String], filename: &String) -> String {
pub async fn read_file_to_path(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String], filename: &String) -> Result<String, String> {
let file_content_res = self.read_file(forest, root_dir, path_segments).await;
if file_content_res.is_some() {
self.write_byte_vec_to_file(filename, file_content_res.unwrap());
filename.to_string()
if file_content_res.is_ok() {
self.write_byte_vec_to_file(filename, file_content_res.ok().unwrap());
Ok(filename.to_string())
} else {
trace!("wnfsError occured in read_file_to_path: ");
return "".to_string();
trace!("wnfsError occured in read_file_to_path: {:?}", file_content_res.as_ref().err().unwrap().to_string());
Err(file_content_res.err().unwrap().to_string())
}
}

pub async fn read_file(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Option<Vec<u8>> {
let result = root_dir
pub async fn read_file(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Result<Vec<u8>, String> {
let res = root_dir
.read(path_segments, true, forest, &mut self.store)
.await;
match result {
Ok(res) => Some(res.result),
Err(_) => None
if res.is_ok() {
let PrivateOpResult { result, .. } = res.ok().unwrap();
Ok(result)
} else {
trace!("wnfsError occured in read_file: {:?} ", res.as_ref().err().unwrap());
Err(res.err().unwrap().to_string())
}
}

Expand All @@ -365,13 +368,26 @@ impl<'a> PrivateDirectoryHelper<'a> {
}


pub async fn rm(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> (Cid, PrivateRef) {
let PrivateOpResult { forest, root_dir, .. } = root_dir
pub async fn rm(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Result<(Cid, PrivateRef), String> {
let result = root_dir
.rm(path_segments, true, forest, &mut self.store,&mut self.rng)
.await
.unwrap();

(self.update_forest(forest).await.unwrap(), root_dir.header.get_private_ref())
.await;
if result.is_ok() {
let PrivateOpResult { forest, root_dir, .. } = result
.ok()
.unwrap();
let update_res = self.update_forest(forest).await;
if update_res.is_ok() {
Ok((update_res.ok().unwrap(), root_dir.header.get_private_ref()))
} else {
trace!("wnfsError occured in rm update_res: {:?}", update_res.as_ref().err().unwrap());
Err(update_res.err().unwrap().to_string())
}
} else {
trace!("wnfsError occured in rm result: {:?}", result.as_ref().err().unwrap());
Err(result.err().unwrap().to_string())
}

}

pub async fn ls_files(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Result<Vec<(String, Metadata)>, String> {
Expand Down Expand Up @@ -442,14 +458,14 @@ impl<'a> PrivateDirectoryHelper<'a> {
return runtime.block_on(self.write_file(forest, root_dir, path_segments, content));
}

pub fn synced_read_file_to_path(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String], filename: &String) -> String
pub fn synced_read_file_to_path(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String], filename: &String) -> Result<String, String>
{
let runtime =
tokio::runtime::Runtime::new().expect("Unable to create a runtime");
return runtime.block_on(self.read_file_to_path(forest, root_dir, path_segments, filename));
}

pub fn synced_read_file(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Option<Vec<u8>>
pub fn synced_read_file(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Result<Vec<u8>, String>
{
let runtime =
tokio::runtime::Runtime::new().expect("Unable to create a runtime");
Expand All @@ -463,7 +479,7 @@ impl<'a> PrivateDirectoryHelper<'a> {
return runtime.block_on(self.mkdir(forest, root_dir, path_segments));
}

pub fn synced_rm(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> (Cid, PrivateRef)
pub fn synced_rm(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Result<(Cid, PrivateRef), String>
{
let runtime =
tokio::runtime::Runtime::new().expect("Unable to create a runtime");
Expand Down
2 changes: 1 addition & 1 deletion jitpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ before_install:
- git lfs pull
install:
- FILE="-Dfile=lib/build/outputs/aar/lib-release.aar"
- mvn install:install-file $FILE -DgroupId=com.group.module -DartifactId=wnfs-android -Dversion=1.3.2 -Dpackaging=aar -DgeneratePom=true
- mvn install:install-file $FILE -DgroupId=com.group.module -DartifactId=wnfs-android -Dversion=1.3.3 -Dpackaging=aar -DgeneratePom=true
21 changes: 18 additions & 3 deletions lib/src/main/java/land/fx/wnfslib/Fs.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package land.fx.wnfslib;

import android.util.Log;

import androidx.annotation.NonNull;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedList;
Expand Down Expand Up @@ -29,6 +33,7 @@ public final class Fs {

private static native byte[] readFileNative(Datastore datastore, String cid, String privateRef, String path);

@NonNull
public static String createPrivateForest(Datastore datastore) throws Exception {
try {
String res = createPrivateForestNative(datastore);
Expand All @@ -43,6 +48,7 @@ public static String createPrivateForest(Datastore datastore) throws Exception {
}
}

@NonNull
public static String getPrivateRef(Datastore datastore, byte[] wnfsKey, String cid) throws Exception {
try {
String res = getPrivateRefNative(datastore, wnfsKey, cid);
Expand All @@ -57,6 +63,7 @@ public static String getPrivateRef(Datastore datastore, byte[] wnfsKey, String c
}
}

@NonNull
public static Config createRootDir(Datastore datastore, String cid, byte[] wnfsKey) throws Exception {
try {
Config res = createRootDirNative(datastore, cid, wnfsKey);
Expand All @@ -71,6 +78,7 @@ public static Config createRootDir(Datastore datastore, String cid, byte[] wnfsK
}
}

@NonNull
public static Config writeFileFromPath(Datastore datastore, String cid, String privateRef, String path, String filename) throws Exception {
try {
Config res = writeFileFromPathNative(datastore, cid, privateRef, path, filename);
Expand All @@ -85,6 +93,7 @@ public static Config writeFileFromPath(Datastore datastore, String cid, String p
}
}

@NonNull
public static Config writeFile(Datastore datastore, String cid, String privateRef, String path, byte[] content) throws Exception {
try {
Config res = writeFileNative(datastore, cid, privateRef, path, content);
Expand All @@ -99,10 +108,13 @@ public static Config writeFile(Datastore datastore, String cid, String privateRe
}
}

@NonNull
public static String ls(Datastore datastore, String cid, String privateRef, String path) throws Exception {
try {
JSONArray output = new JSONArray();
Log.d("wnfs", "JSONArray is reached");
byte[] lsResult = lsNative(datastore, cid, privateRef, path);
Log.d("wnfs", "lsResult is reached");
byte[] rowSeparatorPattern = {33, 33, 33}; //!!!
byte[] itemSeparatorPattern = {63, 63, 63}; //???
List<byte[]> rows = split(rowSeparatorPattern, lsResult);
Expand All @@ -111,7 +123,7 @@ public static String ls(Datastore datastore, String cid, String privateRef, Stri
List<byte[]> rowDetails = split(itemSeparatorPattern, element);
if (!rowDetails.isEmpty()) {
String name = new String(rowDetails.get(0), StandardCharsets.UTF_8);
if(name != null && !name.isEmpty()) {
if(!name.isEmpty()) {
obj.put("name", name);
if(rowDetails.size() >= 2) {
String creation = new String(rowDetails.get(1), StandardCharsets.UTF_8);
Expand Down Expand Up @@ -140,6 +152,7 @@ public static String ls(Datastore datastore, String cid, String privateRef, Stri
}
}

@NonNull
public static Config mkdir(Datastore datastore, String cid, String privateRef, String path) throws Exception {
try {
Config res = mkdirNative(datastore, cid, privateRef, path);
Expand All @@ -158,6 +171,7 @@ public static Config rm(Datastore datastore, String cid, String privateRef, Stri
return rmNative(datastore, cid, privateRef, path);
}

@NonNull
public static String readFileToPath(Datastore datastore, String cid, String privateRef, String path, String filename) throws Exception {
try{
String res = readFileToPathNative(datastore, cid, privateRef, path, filename);
Expand All @@ -178,7 +192,7 @@ public static byte[] readFile(Datastore datastore, String cid, String privateRef

public static native void initRustLogger();

private static boolean isMatch(byte[] pattern, byte[] input, int pos) {
private static boolean isMatch(@NonNull byte[] pattern, byte[] input, int pos) {
for(int i=0; i< pattern.length; i++) {
if(pattern[i] != input[pos+i]) {
return false;
Expand All @@ -187,7 +201,8 @@ private static boolean isMatch(byte[] pattern, byte[] input, int pos) {
return true;
}

private static List<byte[]> split(byte[] pattern, byte[] input) {
@NonNull
private static List<byte[]> split(byte[] pattern, @NonNull byte[] input) {
List<byte[]> l = new LinkedList<>();
int blockStart = 0;
for(int i=0; i<input.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.group.module</groupId>
<artifactId>wnfs-android</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</project>
Loading

0 comments on commit b7fdfb2

Please sign in to comment.