Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have created a JNI wrapper for accessing the extended attributes now directly over the C-API. It is a open source Java Maven project and avaiable on <a href="https://github.com/IsNull/xattrj" rel="nofollow">GitHub/xattrj</a></p> <p>For reference, I post the interesting source pieces here. For the latest sources, please refer to the above project page.</p> <p><strong>Xattrj.java</strong></p> <pre><code>public class Xattrj { /** * Write the extended attribute to the given file * @param file * @param attrKey * @param attrValue */ public void writeAttribute(File file, String attrKey, String attrValue){ writeAttribute(file.getAbsolutePath(), attrKey, attrValue); } /** * Read the extended attribute from the given file * @param file * @param attrKey * @return */ public String readAttribute(File file, String attrKey){ return readAttribute(file.getAbsolutePath(), attrKey); } /** * Write the extended attribute to the given file * @param file * @param attrKey * @param attrValue */ private native void writeAttribute(String file, String attrKey, String attrValue); /** * Read the extended attribute from the given file * @param file * @param attrKey * @return */ private native String readAttribute(String file, String attrKey); static { try { System.out.println("loading xattrj..."); LibraryLoader.loadLibrary("xattrj"); System.out.println("loaded!"); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p><strong>org_securityvision_xattrj_Xattrj.cpp</strong></p> <pre><code>#include &lt;jni.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include "org_securityvision_xattrj_Xattrj.h" #include &lt;sys/xattr.h&gt; /** * writeAttribute * writes the extended attribute * */ JNIEXPORT void JNICALL Java_org_securityvision_xattrj_Xattrj_writeAttribute (JNIEnv *env, jobject jobj, jstring jfilePath, jstring jattrName, jstring jattrValue){ const char *filePath= env-&gt;GetStringUTFChars(jfilePath, 0); const char *attrName= env-&gt;GetStringUTFChars(jattrName, 0); const char *attrValue=env-&gt;GetStringUTFChars(jattrValue,0); int res = setxattr(filePath, attrName, (void *)attrValue, strlen(attrValue), 0, 0); //XATTR_NOFOLLOW != 0 if(res){ // an error occurred, see errno printf("native:writeAttribute: error on write..."); perror(""); } } /** * readAttribute * Reads the extended attribute as string * * If the attribute does not exist (or any other error occurs) * a null string is returned. * * */ JNIEXPORT jstring JNICALL Java_org_securityvision_xattrj_Xattrj_readAttribute (JNIEnv *env, jobject jobj, jstring jfilePath, jstring jattrName){ jstring jvalue = NULL; const char *filePath= env-&gt;GetStringUTFChars(jfilePath, 0); const char *attrName= env-&gt;GetStringUTFChars(jattrName, 0); // get size of needed buffer int bufferLength = getxattr(filePath, attrName, NULL, 0, 0, 0); if(bufferLength &gt; 0){ // make a buffer of sufficient length char *buffer = (char*)malloc(bufferLength); // now actually get the attribute string int s = getxattr(filePath, attrName, buffer, bufferLength, 0, 0); if(s &gt; 0){ // convert the buffer to a null terminated string char *value = (char*)malloc(s+1); *(char*)value = 0; strncat(value, buffer, s); free(buffer); // convert the c-String to a java string jvalue = env-&gt;NewStringUTF(value); } } return jvalue; } </code></pre> <p>Now the makefile which has troubled me quite a bit to get things working:</p> <pre><code>CC=gcc LDFLAGS= -fPIC -bundle CFLAGS= -c -shared -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers -m64 SOURCES_DIR=src/main/c++ OBJECTS_DIR=target/c++ EXECUTABLE=target/classes/libxattrj.dylib SOURCES=$(shell find '$(SOURCES_DIR)' -type f -name '*.cpp') OBJECTS=$(SOURCES:$(SOURCES_DIR)/%.cpp=$(OBJECTS_DIR)/%.o) all: $(EXECUTABLE) $(EXECUTABLE): $(OBJECTS) $(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(OBJECTS): $(SOURCES) mkdir -p $(OBJECTS_DIR) $(CC) $(CFLAGS) $&lt; -o $@ clean: rm -rf $(OBJECTS_DIR) $(EXECUTABLE) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload