Is there a way for my android app to retrieve and set extended user attributes of files? Is there a way to use java.nio.file.Files
on android? Is there any way to use setfattr
and getfattr
from my dalvik app? I know that android use the ext4 file system, so i guess it should be possible. Any suggestions?
Extended attributes are arbitrary metadata stored with a file, but separate from the file system attributes (such as modification time or file size). The metadata is often a null-terminated UTF-8 string, but can also be arbitrary binary data.”
The Attr interface represents an attribute in an Element object.
Extended file attributes are file system features that enable users to associate computer files with metadata not interpreted by the filesystem, whereas regular attributes have a purpose strictly defined by the filesystem (such as permissions or records of creation and modification times).
Tools attributes reference. Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources).
Understanding Android File Permissions On any UNIX or Linux based file system, every single file and folder stored on the hard drive has a set of permissions associated with it. These permissions which are also called attributes, determine the level of accessibility/permission given to a user or a group of users.
Now open the root file explorer app from the app drawer. Here you will see a list of directories and files. If you copied the file to the internal storage on your device, you can find it by opening the “sdcard” directory. To access the external storage, tap on “storage/extSdcard”.
If you share an Android device with other people, it can be rough to keep your account separate from theirs. Fortunately, Android supports multiple user profiles, allowing users to share devices without fear of encroaching on each other. What Are User Profiles on Android?
The Android Java library and the bionic C library do not support it. So you have to use native code with Linux syscalls for that.
Here is some sample code to get you started, tested on Android 4.2 and Android 4.4.
XAttrNative.java
package com.appfour.example;
import java.io.IOException;
public class XAttrNative {
static {
System.loadLibrary("xattr");
}
public static native void setxattr(String path, String key, String value) throws IOException;
}
xattr.c
#include <string.h>
#include <jni.h>
#include <asm/unistd.h>
#include <errno.h>
void Java_com_appfour_example_XAttrNative_setxattr(JNIEnv* env, jclass clazz,
jstring path, jstring key, jstring value) {
char* pathChars = (*env)->GetStringUTFChars(env, path, NULL);
char* keyChars = (*env)->GetStringUTFChars(env, key, NULL);
char* valueChars = (*env)->GetStringUTFChars(env, value, NULL);
int res = syscall(__NR_setxattr, pathChars, keyChars, valueChars,
strlen(valueChars), 0);
if (res != 0) {
jclass exClass = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exClass, strerror(errno));
}
(*env)->ReleaseStringUTFChars(env, path, pathChars);
(*env)->ReleaseStringUTFChars(env, key, keyChars);
(*env)->ReleaseStringUTFChars(env, value, valueChars);
}
This works fine on internal storage but not on (emulated) external storage which uses the sdcardfs filesystem or other kernel functions to disable features not supported by the FAT filesystem such as symlinks and extended attributes. Arguably they do this because external storage can be accessed by connecting the device to a PC and the users expects that copying files back and forth preserves all information.
So this works:
File dataFile = new File(getFilesDir(),"test");
dataFile.createNewFile();
XAttrNative.setxattr(dataFile.getPath(), "user.testkey", "testvalue");
while this throws IOException
with the error message: "Operation not supported on transport endpoint":
File externalStorageFile = new File(getExternalFilesDir(null),"test");
externalStorageFile.createNewFile();
XAttrNative.setxattr(externalStorageFile.getPath(), "user.testkey", "testvalue");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With