How do i set extended user attributes on android files? -


is there way android app retrieve , set extended user attributes of files? there way use java.nio.file.files on android? there way use setfattr , getfattr dalvik app? know android use ext4 file system, guess should possible. suggestions?

the android java library , bionic c library not support it. have use native code linux syscalls that.

here sample code started, tested on android 4.2 , 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 not on (emulated) external storage uses sdcardfs filesystem or other kernel functions disable features not supported fat filesystem such symlinks , extended attributes. arguably because external storage can accessed connecting device pc , users expects copying files , forth preserves information.

so works:

file datafile = new file(getfilesdir(),"test"); datafile.createnewfile(); xattrnative.setxattr(datafile.getpath(), "user.testkey", "testvalue"); 

while throws ioexception error message: "operation not supported on transport endpoint":

file externalstoragefile = new file(getexternalfilesdir(null),"test"); externalstoragefile.createnewfile(); xattrnative.setxattr(externalstoragefile.getpath(), "user.testkey", "testvalue"); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -