我已经创建了一个JNI封装现在访问直接在C-API的扩展属性。它是一个开源的Java Maven项目和缴费上GitHub/xattrj
仅供参考,我在这里发表了有趣的来源作品。有关最新消息,请参阅上述项目页面。
Xattrj.java
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();
}
}
}
org_securityvision_xattrj_Xattrj.cpp
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "org_securityvision_xattrj_Xattrj.h"
#include <sys/xattr.h>
/**
* 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->GetStringUTFChars(jfilePath, 0);
const char *attrName= env->GetStringUTFChars(jattrName, 0);
const char *attrValue=env->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->GetStringUTFChars(jfilePath, 0);
const char *attrName= env->GetStringUTFChars(jattrName, 0);
// get size of needed buffer
int bufferLength = getxattr(filePath, attrName, NULL, 0, 0, 0);
if(bufferLength > 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 > 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->NewStringUTF(value);
}
}
return jvalue;
}
现在这困扰了我相当多的把事情的工作makefile文件:
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 [email protected]
$(OBJECTS): $(SOURCES)
mkdir -p $(OBJECTS_DIR)
$(CC) $(CFLAGS) $< -o [email protected]
clean:
rm -rf $(OBJECTS_DIR) $(EXECUTABLE)
不错,很高兴看到你工作。只是对你的代码的一些评论......你为什么在第二次调用getxattr()时硬编码255作为缓冲区大小?您已经动态确定了属性值的大小。其次,如果这是为了设置和检索任意属性(而不是特定于应用程序),你不能把所有XATTR值作为字符串。他们可以并经常包含任意的二进制数据;例如,使用Safari下载文件并检查其xattrs。 – jatoben 2013-05-03 17:26:57
我打算创建seaprate方法来支持二进制数据。缓冲区大小来自官方的OS X例子,我只是用同样的方法。我会修改你的建议。 BTW:我在C/C++的经验是相当有限的,因此任何其他的建议高度赞赏:)比如,我想因为我使用C++ string.h中我的C-字符串的处理可以简化... – IsNull 2013-05-03 17:43:59