DroidFS/app/src/main/native/memfile.c

30 lines
842 B
C
Raw Permalink Normal View History

2023-09-06 19:27:41 +02:00
#include <errno.h>
#include <string.h>
2023-08-20 14:56:46 +02:00
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <jni.h>
2023-09-06 19:27:41 +02:00
#include <android/log.h>
const char* LOG_TAG = "MemFile";
void log_err(const char* function) {
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "%s(): %s", function, strerror(errno));
}
2023-08-20 14:56:46 +02:00
JNIEXPORT jint JNICALL
Java_sushi_hardcore_droidfs_MemFile_00024Companion_createMemFile(JNIEnv *env, jobject thiz, jstring jname,
jlong size) {
2023-09-06 19:27:41 +02:00
const char* name = (*env)->GetStringUTFChars(env, jname, NULL);
2023-08-20 14:56:46 +02:00
int fd = syscall(SYS_memfd_create, name, MFD_CLOEXEC);
2023-09-06 19:27:41 +02:00
if (fd < 0) {
log_err("memfd_create");
return fd;
}
2023-08-20 14:56:46 +02:00
if (ftruncate64(fd, size) == -1) {
2023-09-06 19:27:41 +02:00
log_err("ftruncate64");
2023-08-20 14:56:46 +02:00
close(fd);
return -1;
}
return fd;
}