commit 753cab50edca0cff6db1b03a4085bcb1a98006f5
parent bc343b0cde9a79aec0a932d6f2c05d432e8bb454
Author: julianharty <julianharty@gmail.com>
Date: Mon, 12 Aug 2024 21:55:22 +0100
Various interim modifcations that create files in the app's internal storage.
We want to see if we can provide plugin modules from the app's assets folder.
These need copying into either internal (app) or external (device) storage.
These changes are very much a work-in-progress and only intended to help
debug what would work.
oriole:/data/user/0/org.gnu.gnunet/files $ ls -la
total 46
drwxrwx--x 2 u0_a317 u0_a317 3452 2024-08-12 21:34 .
drwx------ 7 u0_a317 u0_a317 3452 2024-08-12 21:29 ..
-rwxr-xr-x 1 u0_a317 u0_a317 33112 2024-08-12 21:51 libgnunet_plugin_peerstore_sqlite.so
-rw------- 1 u0_a317 u0_a317 24 2024-08-12 21:51 output.bin
Diffstat:
3 files changed, 82 insertions(+), 13 deletions(-)
diff --git a/android_studio/app/src/main/cpp/CMakeLists.txt b/android_studio/app/src/main/cpp/CMakeLists.txt
@@ -26,9 +26,7 @@ set_target_properties(libgnunetutil PROPERTIES IMPORTED_LOCATION
add_library(libgnunetsq SHARED IMPORTED)
set_target_properties(libgnunetsq PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libgnunetsq/lib/${ANDROID_ABI}/libgnunetsq.so)
-add_library(libgnunet_plugin_peerstore_sqlite SHARED IMPORTED)
-set_target_properties(libgnunet_plugin_peerstore_sqlite PROPERTIES IMPORTED_LOCATION
- ${distribution_DIR}/libgnunet_plugin_peerstore_sqlite/lib/${ANDROID_ABI}/libgnunet_plugin_peerstore_sqlite.so)
+
add_library(libgnunethello SHARED IMPORTED)
set_target_properties(libgnunethello PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libgnunethello/lib/${ANDROID_ABI}/libgnunethello.so)
@@ -126,7 +124,7 @@ target_link_libraries(gnunetti
libgnunetutil
libgnunetsq
libgnunethello
- libgnunet_plugin_peerstore_sqlite
+ #libgnunet_plugin_peerstore_sqlite
libgnunet
sqliteX
log)
diff --git a/android_studio/app/src/main/cpp/native-lib.cpp b/android_studio/app/src/main/cpp/native-lib.cpp
@@ -1,5 +1,8 @@
+#include <fstream>
+#include <iostream>
#include <jni.h>
#include <string>
+#include <vector>
#include <android/log.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
@@ -248,7 +251,9 @@ Java_org_gnu_gnunet_MainActivity_stringFromJNI(
LOGD ("Temp file is here: %s", tmp_file.c_str());
LOGD ("current ltdl search path: %s", lt_dlgetsearchpath());
- lt_dlsetsearchpath ("/data/user/0/org.gnu.gnunet/:/data/user/0/org.gnu.gnunet/assets");
+ lt_dlsetsearchpath ("/data/user/0/org.gnu.gnunet/files/");
+
+ LOGD ("current ltdl search path afterwards: %s", lt_dlgetsearchpath());
char *const non_const_ptr = const_cast<char*>(tmp_file.c_str());
char *const argvx[] = {
@@ -269,32 +274,50 @@ Java_org_gnu_gnunet_MainActivity_stringFromJNI(
*/
AAsset *plugin_asset = AAssetManager_open(mgr, "libgnunet_plugin_peerstore_sqlite.so", AASSET_MODE_BUFFER);
- char plugin_buf[AAsset_getLength(plugin_asset)];
- AAsset_read(plugin_asset, plugin_buf, AAsset_getLength(plugin_asset));
+ const off_t &length = AAsset_getLength(plugin_asset);
+ char plugin_buf[length];
+ LOGD("libgnunet_plugin_peerstore_sqlite.so size is %ld bytes", length);
+ AAsset_read(plugin_asset, plugin_buf, length);
enum GNUNET_DISK_AccessPermissions permission = static_cast<GNUNET_DISK_AccessPermissions>(
GNUNET_DISK_PERM_USER_READ
| GNUNET_DISK_PERM_USER_WRITE
| GNUNET_DISK_PERM_USER_EXEC);
-
- struct GNUNET_DISK_FileHandle *fh = GNUNET_DISK_file_open ("/data/user/0/org.gnu.gnunet/libgnunet_plugin_peerstore_sqlite.so",
+
+ // This code fails to get a fileHandle. TODO work out why not. It means the write doesn't happen.
+ struct GNUNET_DISK_FileHandle *fh = GNUNET_DISK_file_open ("/data/user/0/org.gnu.gnunet/files/libgnunet_plugin_peerstore_sqlite_gnunet.so",
GNUNET_DISK_OPEN_WRITE,
permission );
GNUNET_DISK_file_write (fh,
plugin_buf,
- sizeof (plugin_buf));
+ length);
GNUNET_DISK_file_close (fh);
+ // The following code does work; so does the kotlin code that copies the .so file from the assets folder.
+ std::vector<int> data = {1, 2, 3, 4, 5, 6};
+ std::ofstream outfile("/data/user/0/org.gnu.gnunet/files/output.bin", std::ios::binary);
+ if (!outfile) {
+ LOGE("Could not open output.bin for writing");
+ }
+
+ outfile.write(reinterpret_cast<const char*>(data.data()), data.size() * sizeof(int ));
+ if (!outfile) {
+ LOGE("Write failed for output.bin");
+ }
+ outfile.close();
+
+
cfg = GNUNET_CONFIGURATION_create ();
AAsset *asset = AAssetManager_open(mgr, "gnunet.conf", AASSET_MODE_BUFFER);
- char buf[AAsset_getLength(asset)];
+ const off_t &confif_file_size = AAsset_getLength(asset);
+ LOGD("gnunet.conf is %ld bytes", confif_file_size);
+ char buf[confif_file_size];
- AAsset_read(asset, buf, AAsset_getLength(asset));
+ AAsset_read(asset, buf, confif_file_size);
if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, buf, strlen(buf), NULL))
{
LOGE ("Deserialization of configuration failed!");
}
-
AAsset_close(asset);
GNUNET_SERVICE_main (1,
diff --git a/android_studio/app/src/main/java/org/gnu/gnunet/MainActivity.kt b/android_studio/app/src/main/java/org/gnu/gnunet/MainActivity.kt
@@ -3,7 +3,12 @@ package org.gnu.gnunet
import android.content.res.AssetManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
+import android.util.Log
import org.gnu.gnunet.databinding.ActivityMainBinding
+import java.io.File
+import java.io.FileOutputStream
+import java.io.IOException
+import java.io.InputStream
class MainActivity : AppCompatActivity() {
@@ -15,10 +20,53 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
+ val isSuccess = copyFileFromAssetsToInternalStorage("libgnunet_plugin_peerstore_sqlite.so", "libgnunet_plugin_peerstore_sqlite.so")
+ if (isSuccess) {
+ Log.d("GNUNET", "onCreate: file copied successfully")
+ } else {
+ Log.e("GNUNET", "onCreate: file not copied :(")
+ }
+
// Example of a call to a native method
binding.sampleText.text = stringFromJNI(assets)
}
+ private fun copyFileFromAssetsToInternalStorage(fileName: String, outputFileName: String): Boolean {
+ var inputStream: InputStream? = null
+ var outputStream: FileOutputStream? = null
+
+ return try {
+ // Access the file from the assets
+ inputStream = assets.open(fileName)
+
+ // Create the destination file in internal storage
+ val outputFile = File(filesDir, outputFileName)
+ Log.d("GNUNET", outputFile.path)
+ outputStream = FileOutputStream(outputFile)
+
+ // Buffer to hold data during copy
+ val buffer = ByteArray(1024)
+ var length: Int
+
+ // Copy the file contents from assets to the destination file
+ while (inputStream.read(buffer).also { length = it } > 0) {
+ outputStream.write(buffer, 0, length)
+ }
+
+ outputFile.setReadable(true, false)
+ outputFile.setExecutable(true, false)
+ //Success
+ true
+ } catch (e: IOException) {
+ e.printStackTrace()
+
+ // Failure
+ false
+ } finally {
+ inputStream?.close()
+ outputStream?.close()
+ }
+ }
/**
* A native method that is implemented by the 'gnunet' native library,
* which is packaged with this application.