Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JNA wrapper for <sys/xattr.h> #338

Merged
merged 9 commits into from
Jun 20, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/mac/XAttr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright (c) 2014 Reinhard Pointner, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.mac;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

/**
* JNA wrapper for <sys/xattr.h>
*
*/
interface XAttr extends Library {

// load from current image
XAttr INSTANCE = (XAttr) Native.loadLibrary(null, XAttr.class);

// see /usr/include/sys/xattr.h
int XATTR_NOFOLLOW = 0x0001;
int XATTR_CREATE = 0x0002;
int XATTR_REPLACE = 0x0004;
int XATTR_NOSECURITY = 0x0008;
int XATTR_NODEFAULT = 0x0010;
int XATTR_SHOWCOMPRESSION = 0x0020;
int XATTR_MAXNAMELEN = 127;
String XATTR_FINDERINFO_NAME = "com.apple.FinderInfo";
String XATTR_RESOURCEFORK_NAME = "com.apple.ResourceFork";

// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/getxattr.2.html
long getxattr(String path, String name, Pointer value, long size, int position, int options);

// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/setxattr.2.html
int setxattr(String path, String name, Pointer value, long size, int position, int options);

// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/removexattr.2.html
int removexattr(String path, String name, int options);

// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/listxattr.2.html
long listxattr(String path, Pointer namebuff, long size, int options);

}
100 changes: 100 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/mac/XAttrUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Copyright (c) 2014 Reinhard Pointner, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.mac;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;

public class XAttrUtil {

public static List<String> listXAttr(String path) {
// get required buffer size
long bufferLength = XAttr.INSTANCE.listxattr(path, Pointer.NULL, 0, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer just null instead of Pointer.NULL. I should probably remove the latter definition altogether, or at least deprecate it.


if (bufferLength < 0)
return null;

if (bufferLength == 0)
return new ArrayList<String>(0);

Memory valueBuffer = new Memory(bufferLength);
long valueLength = XAttr.INSTANCE.listxattr(path, valueBuffer, bufferLength, 0);

if (valueLength < 0)
return null;

return decodeStringSequence(valueBuffer.getByteBuffer(0, valueLength));
}

public static String getXAttr(String path, String name) {
// get required buffer size
long bufferLength = XAttr.INSTANCE.getxattr(path, name, Pointer.NULL, 0, 0, 0);

if (bufferLength < 0)
return null;

Memory valueBuffer = new Memory(bufferLength);
long valueLength = XAttr.INSTANCE.getxattr(path, name, valueBuffer, bufferLength, 0, 0);

if (valueLength < 0)
return null;

return decodeString(valueBuffer.getByteBuffer(0, valueLength - 1));
}

public static int setXAttr(String path, String name, String value) {
Memory valueBuffer = encodeString(value);
return XAttr.INSTANCE.setxattr(path, name, valueBuffer, valueBuffer.size(), 0, 0);
}

public static int removeXAttr(String path, String name) {
return XAttr.INSTANCE.removexattr(path, name, 0);
}

protected static Memory encodeString(String s) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be some overlap here with Native.toString(byte[]) and Pointer.setString()/setWideString().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I wanted a Pointer to a NULL-terminated UTF-8 String I'd use Pointer.setString("XYZ", "UTF-8")? In the docs it doesn't explicitly state it will be NULL-terminated. Also String.length() isn't necessarily the byte size once encoded, so will this automatically allocate the necessary memory as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointer.setString() does NUL-terminate, but since you need to allocate space here as well, that function doesn't help much. The class NativeString actually does what you want, but it's package-protected.

I think your code is fine as is.

// create NULL-terminated UTF-8 String
byte[] bb = s.getBytes(Charset.forName("UTF-8"));
Memory valueBuffer = new Memory(bb.length + 1);
valueBuffer.write(0, bb, 0, bb.length);
valueBuffer.setByte(valueBuffer.size() - 1, (byte) 0);
return valueBuffer;
}

protected static String decodeString(ByteBuffer bb) {
return Charset.forName("UTF-8").decode(bb).toString();
}

protected static List<String> decodeStringSequence(ByteBuffer bb) {
List<String> names = new ArrayList<String>();

bb.mark(); // first key starts from here
while (bb.hasRemaining()) {
if (bb.get() == 0) {
ByteBuffer nameBuffer = (ByteBuffer) bb.duplicate().limit(bb.position() - 1).reset();
if (nameBuffer.hasRemaining()) {
names.add(decodeString(nameBuffer));
}
bb.mark(); // next key starts from here
}
}

return names;
}

}
117 changes: 117 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/mac/XAttrUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* Copyright (c) 2014 Reinhard Pointner, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.mac;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

public class XAttrUtilTest extends TestCase {

String testPath;

protected void setUp() throws Exception {
testPath = File.createTempFile("xattr-test", ".txt").getAbsolutePath();
assertTrue(new File(testPath).exists());
}

protected void tearDown() throws Exception {
new File(testPath).delete();
assertFalse(new File(testPath).exists());
}

public void testListXAttr() {
// no xattr initially
List<String> keys = XAttrUtil.listXAttr(testPath);
assertEquals(0, keys.size());

// set multiple xattr
String[] names = new String[] { "Java", "Native", "Access" };
for (int i = 0; i < names.length; i++) {
// set xattr
XAttrUtil.setXAttr(testPath, names[i], names[i]);

// check if new xattr is listed
keys = XAttrUtil.listXAttr(testPath);
assertEquals(i + 1, keys.size());
assertTrue(keys.contains(names[i]));
}
}

public void testGetXAttr() {
String value = XAttrUtil.getXAttr(testPath, "JNA");
assertNull(value);

XAttrUtil.setXAttr(testPath, "JNA", "Java Native Access");
value = XAttrUtil.getXAttr(testPath, "JNA");

assertEquals(Arrays.toString("Java Native Access".getBytes()), Arrays.toString(value.getBytes()));
}

public void testSetXAttr() {
String value = XAttrUtil.getXAttr(testPath, "JNA");
assertNull(value);

XAttrUtil.setXAttr(testPath, "JNA", "Java Native Access");
value = XAttrUtil.getXAttr(testPath, "JNA");
assertEquals("Java Native Access", value);

XAttrUtil.setXAttr(testPath, "JNA", "is nice");
value = XAttrUtil.getXAttr(testPath, "JNA");
assertEquals("is nice", value);
}

public void testRemoveXAttr() {
XAttrUtil.setXAttr(testPath, "JNA", "Java Native Access");
assertEquals("[JNA]", XAttrUtil.listXAttr(testPath).toString());

// remove xattr
XAttrUtil.removeXAttr(testPath, "JNA");

assertEquals("[]", XAttrUtil.listXAttr(testPath).toString());
}

public void testUnicode() {
String[] names = new String[] { "中文", "にほんご", "Österreichisch", "Française", "Português" };
for (int i = 0; i < names.length; i++) {
// set xattr
XAttrUtil.setXAttr(testPath, names[i], names[i]);

// check if new xattr is listed
List<String> keys = XAttrUtil.listXAttr(testPath);
assertEquals(i + 1, keys.size());
assertTrue(keys.contains(names[i]));

String value = XAttrUtil.getXAttr(testPath, names[i]);
assertEquals(names[i], value);
}
}

public void testLargeData() {
StringBuilder name = new StringBuilder();
while (name.length() < XAttr.XATTR_MAXNAMELEN) {
name.append('X');
}

StringBuilder data = new StringBuilder();
while (data.length() < 4 * 1024 * 1024) {
data.append('X');
}

XAttrUtil.setXAttr(testPath, name.toString(), data.toString());
String value = XAttrUtil.getXAttr(testPath, name.toString());
assertEquals(data.toString(), value.toString());
}
}