Java UNIX设置扩展属性setxattr
-如何在UNIX中编写用户定义的文件属性? 或 -如何在UNIX中设置setxattr?Java UNIX设置扩展属性setxattr,java,unix,attributes,Java,Unix,Attributes,-如何在UNIX中编写用户定义的文件属性? 或 -如何在UNIX中设置setxattr? 我正在使用我在UNIX服务器上的Windows中制作的java程序。作为其中的一部分,我使用UserDefinedFileAttributeView将扩展文件信息写入文件 我有一个测试,看看文件系统是否支持UserDefinedFileAttributeView: public static String[] supportedFileAttributeViews() { final FileSy
我正在使用我在UNIX服务器上的Windows中制作的java程序。作为其中的一部分,我使用UserDefinedFileAttributeView将扩展文件信息写入文件 我有一个测试,看看文件系统是否支持UserDefinedFileAttributeView:
public static String[] supportedFileAttributeViews() {
final FileSystem defaultFS = FileSystems.getDefault();
return defaultFS.supportedFileAttributeViews()
.toArray(new String[defaultFS.supportedFileAttributeViews().size()]);
}
在UNIX服务器上,这为我提供了:
"basic" "owner" "user" "unix" "dos" "posix"
所以我想可以将UserDefinedFileAttributes写入文件(->“user”)
但如果java写入文件,我会得到一个错误:
java.nio.file.FileSystemException: ....
Error writing extended attribute 'test': Operation not supported
at sun.nio.fs.LinuxUserDefinedFileAttributeView.write
(LinuxUserDefinedFileAttributeView.java:246)
........
我编写UserDefinedFileAttributes的代码:
public static boolean writeCustomMETAfile(String filepath,String name, String value) {
boolean succes = false;
try {
Path file = Paths.get(filepath);
UserDefinedFileAttributeView userView = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);
//userView.write(name, Charset.defaultCharset().encode(value));
final byte[] bytes = value.getBytes("UTF-8");
final ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
writeBuffer.put(bytes);
writeBuffer.flip();
userView.write(name, writeBuffer);
succes = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return succes;
}
我一直在搜索StackOverflow和各种手册(UNIX)以及其他web资源,但现在没有机会正常运行
在UNIX命令行上:
man 2 setxattr
给了我一些信息,我尝试了各种chmod设置
它可能在i节点中吗
An i-node holds most of the metadata for a
Unix file — on classic Unix, it holds all but the
i-node number itself
因此,问题是:
-如何在UNIX中写入UserDefinedFileAttributes/如何在UNIX中设置setxattr?在下面找到一个小片段,它将读取和写入文件上的xattr。 为简单起见,没有适当的异常处理
Path path = Paths.get("/tmp/foobar");
// you should get the filestore for the path
FileStore fileStore = Files.getFileStore(path);
System.out.println("fileStore : " + fileStore);
// check if the filesystem supports xattr
//
// I found out that the reported state might be wrong for ext3/ext4
//
// if you create following properties file it will be correct
// echo "ext4=user_attr" > $JAVA_HOME/lib/fstypes.properties
//
// keep in mind that it's possible that your ext3/ext4 does not support xattr
// might be related to: kernel configuration, mount options, etc.
boolean supportsXattr = fileStore.supportsFileAttributeView(UserDefinedFileAttributeView.class);
System.out.println("supports xattr: " + supportsXattr);
// get the file attribute view
UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
String xattrName = "xattr-foo";
String xattrValue = "dummy-value";
Charset defaultCharset = Charset.defaultCharset();
if (view.list().contains(xattrName)) {
// get the size of the attribute value
int xattrSize = view.size(xattrName);
ByteBuffer buffer = ByteBuffer.allocateDirect(xattrSize);
// read the attribute value
int bytesRead = view.read(xattrName, buffer);
// decode the buffer and print it
buffer.flip();
xattrValue = defaultCharset.decode(buffer).toString();
System.out.println("xattr name : " + xattrName);
System.out.println("xattr value : " + xattrValue);
} else {
// edit: System.out.println to System.out.printf in the next line
System.out.printf("file has no xattr [%s]%n", xattrName);
}
// write the current value back reversed, can be checked next run
// or on command line
String newXattrValue = new StringBuilder(xattrValue).reverse().toString();
view.write(xattrName, defaultCharset.encode(newXattrValue);
编辑我推出了一个完整的示例。它还应该在Windws上的NTFS分区上工作
扩展属性可以在Linux上设置为
setfattr -n user.foo_name -v bar_value /tmp/foobar
并且可以作为
getfattr -n user.foo_name /tmp/foobar
您在这个分区上使用的文件系统是什么?Hi@SubOptimal:TYPE=“ext4”在unix中通过terminal命令:blkid找到您使用的Java版本是什么?还请提及更新版本(例如1.7.0_40)。Hi@SubOptimal:Java版本(在Eclipse-Properties-Java Compiler 1.8中找到,符合J2SE-1.5)终端Java-version->1.8.0_31->SE运行时环境build 1.8.0_31-b13(在windows开发环境中)和UNIX终端上:Java版本“1.7.0_75”Java(TM)SE运行时环境(build 1.7.0_75-b13)Java HotSpot(TM)64位服务器虚拟机(build 24.75-b04,混合模式)Hi@SubOptimal:谢谢。我在Windows上做了一个测试:(支持xattr:true)和UNIX:supports xattr:false文件没有xattr[xattr-foo]。我将尝试将Java更改为1.7,看看是否有效…@irJvV您需要至少运行该代码段两次。在第一次运行中,预期该文件没有xattr
xattr foo
,将在第一次运行中写入。在第二次运行中,应该读取它(假设文件系统真的支持xattr)。稍后我将提供更多细节,包括Linux命令。Hi@SubOptimal:谢谢,我已经运行了多次。我试过Java1.7。我创建了一个函数:public static boolean supportsFileAttributeView(String filepath){…},它返回一个布尔值:fileStore.supportsFileAttributeView(UserDefinedFileAttributeView.class);在UNIX中,返回值为FALSE->我担心的与其说是JAVA,不如说是UNIX。@irJvV看看我更新的答案。我在github上推了一个完整的例子。@irJvV您的UNIX
是什么?可能文件系统没有安装xattr支持,内核是在没有xattr支持的情况下构建的,或者其他一些东西