-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
868c2e5
JNA wrapper for <sys/xattr.h>
rednoah 48ff7be
* fix class name casing
rednoah c58e860
* fix class name casing
rednoah dd78914
Update CHANGES.md
rednoah 450d22a
Update CHANGES.md
rednoah 6533f85
Update CHANGES.md
rednoah 07326a4
Update CHANGES.md
rednoah 0f08638
* just use null
rednoah 21d7352
* organize imports
rednoah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
contrib/platform/src/com/sun/jna/platform/mac/XAttrUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* 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.List; | ||
|
||
import com.sun.jna.Memory; | ||
|
||
public class XAttrUtil { | ||
|
||
public static List<String> listXAttr(String path) { | ||
// get required buffer size | ||
long bufferLength = XAttr.INSTANCE.listxattr(path, null, 0, 0); | ||
|
||
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, 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) { | ||
// 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
117
contrib/platform/test/com/sun/jna/platform/mac/XAttrUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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[])
andPointer.setString()/setWideString()
.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 classNativeString
actually does what you want, but it's package-protected.I think your code is fine as is.