Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i set extended user attributes on android files?

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?

like image 352
pismakron Avatar asked Jul 22 '13 09:07

pismakron


People also ask

How are extended attributes stored?

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.”

What is attr file in Android?

The Attr interface represents an attribute in an Element object.

What is the role of extended file attributes in a filesystem?

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).

What are tools attributes in Android Studio?

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).

What are android file permissions?

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.

How to access external storage on Android devices?

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”.

Why does Android support multiple user profiles?

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?


Video Answer


1 Answers

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");
like image 142
HHK Avatar answered Oct 20 '22 00:10

HHK