Skip to content

Commit

Permalink
feat: Call ftw() to calculate size
Browse files Browse the repository at this point in the history
Change-Id: I9a25ad24cc17f31318ebfc7f0826dcfb31aee61f
  • Loading branch information
XayahSuSuSu committed Sep 28, 2024
1 parent ffdbaa1 commit bfa2c0c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions source/core/rootservice/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ android {
buildFeatures {
aidl = true
}

externalNativeBuild {
cmake {
path("src/main/cpp/CMakeLists.txt")
version = "3.22.1"
}
}
}

dependencies {
Expand Down
15 changes: 15 additions & 0 deletions source/core/rootservice/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.22.1)

project("nativelib")

add_library(${CMAKE_PROJECT_NAME} SHARED
nativelib.cpp
)

target_link_libraries(${CMAKE_PROJECT_NAME}
android
log
)

target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -O3 -ffunction-sections -fdata-sections -ffile-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=/src)
target_link_libraries(${CMAKE_PROJECT_NAME} -s -flto -Wl,--gc-sections -Wl,--build-id=none -Wl,--hash-style=both)
20 changes: 20 additions & 0 deletions source/core/rootservice/src/main/cpp/nativelib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <jni.h>
#include <string>
#include <ftw.h>

namespace NativeNS {
thread_local size_t total_size{0};

int on_walking(const char *path, const struct stat *p_stat, int flag) {
total_size += p_stat->st_size;
return 0;
}
}

extern "C" JNIEXPORT jlong JNICALL
Java_com_xayah_core_rootservice_util_NativeLib_calculateSize(JNIEnv *env, jobject, jstring path) {
NativeNS::total_size = 0;
const char *p_path = env->GetStringUTFChars(path, JNI_FALSE);
ftw(p_path, &NativeNS::on_walking, 1024);
return NativeNS::total_size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.xayah.core.rootservice.parcelables.StatFsParcelable
import com.xayah.core.rootservice.parcelables.StorageStatsParcelable
import com.xayah.core.rootservice.util.ExceptionUtil.tryOn
import com.xayah.core.rootservice.util.ExceptionUtil.tryWithBoolean
import com.xayah.core.rootservice.util.NativeLib
import com.xayah.core.rootservice.util.SsaidUtil
import com.xayah.core.util.FileUtil
import com.xayah.core.util.HashUtil
Expand Down Expand Up @@ -180,7 +181,7 @@ internal class RemoteRootServiceImpl : IRemoteRootService.Stub() {
}

override fun calculateSize(path: String): Long = synchronized(lock) {
FileUtil.calculateSize(path = path)
NativeLib.calculateSize(path)
}

override fun clearEmptyDirectoriesRecursively(path: String) = synchronized(lock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.pm.UserInfo
import android.os.IBinder
import android.os.Parcel
import android.os.ParcelFileDescriptor
import android.os.Process
import android.os.RemoteException
import android.os.UserHandle
import com.google.gson.reflect.TypeToken
Expand Down Expand Up @@ -56,6 +57,11 @@ class RemoteRootService(private val context: Context) {
private fun log(msg: () -> String) = LogUtil.log { "RemoteRootService" to msg() }

class RemoteRootService : RootService() {
init {
if (Process.myUid() == 0)
System.loadLibrary("nativelib")
}

override fun onBind(intent: Intent): IBinder = RemoteRootServiceImpl()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xayah.core.rootservice.util

object NativeLib {
external fun calculateSize(path: String): Long
}

0 comments on commit bfa2c0c

Please sign in to comment.