Skip to content

Commit

Permalink
Merge pull request #1100 from matthiasblaesing/pr-1095
Browse files Browse the repository at this point in the history
Align behaviour of c.s.j.p.mac.XAttrUtil#setXattr and #getXAttr with CLI tool
  • Loading branch information
matthiasblaesing authored Jun 4, 2019
2 parents 2efd065 + 48812c1 commit 65e43fb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Features

Bug Fixes
---------
* [#1095](https://github.com/java-native-access/jna/pull/1095) Align behaviour of com.sun.jna.platform.macXAttrUtil#setXattr and #getXAttr with CLI tool - [@jrobhoward](https://github.com/jrobhoward), [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1091](https://github.com/java-native-access/jna/issues/1091): Check target number to be greater than zero, before calling `Structure#toArray` in `c.s.j.p.win32.Netapi32Util` - [@trevormagg](https://github.com/trevormaggs), [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 5.3.1
Expand Down
16 changes: 10 additions & 6 deletions contrib/platform/src/com/sun/jna/platform/mac/XAttrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;

import com.sun.jna.Memory;
import com.sun.jna.Native;

public class XAttrUtil {

Expand All @@ -55,19 +56,23 @@ 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)
if (bufferLength < 0) {
return null;
}

if (bufferLength == 0)
if (bufferLength == 0) {
return "";
}

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

if (valueLength < 0)
if (valueLength < 0) {
return null;
}

return decodeString(valueBuffer.getByteBuffer(0, valueLength - 1));
return Native.toString(valueBuffer.getByteArray(0, (int) bufferLength), "UTF-8");
}

public static int setXAttr(String path, String name, String value) {
Expand All @@ -82,9 +87,8 @@ public static int removeXAttr(String path, String name) {
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);
Memory valueBuffer = new Memory(bb.length);
valueBuffer.write(0, bb, 0, bb.length);
valueBuffer.setByte(valueBuffer.size() - 1, (byte) 0);
return valueBuffer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
*/
package com.sun.jna.platform.mac;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import junit.framework.TestCase;

Expand Down Expand Up @@ -136,4 +139,26 @@ public void testLargeData() {
String value = XAttrUtil.getXAttr(testPath, name.toString());
assertEquals(data.toString(), value.toString());
}

public void testReadAlignedCliTool() throws IOException {
XAttrUtil.setXAttr(testPath, "JNA", "Java Native Access");
Process p = Runtime.getRuntime().exec(new String[] {"xattr", "-p", "JNA", testPath});
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
InputStream is = p.getInputStream();
while(((read = is.read(buffer))) > 0) {
baos.write(buffer, 0, read);
}
String resultString = baos.toString("UTF-8");
// Trailing new line is added by command
assertEquals("Java Native Access\n", resultString);
}

public void testWriteAlignedCliTool() throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec(new String[] {"xattr", "-w", "JNA", "Java Native Access", testPath});
assertTrue(p.waitFor(10, TimeUnit.SECONDS));
String resultString = XAttrUtil.getXAttr(testPath, "JNA");
assertEquals("Java Native Access", resultString);
}
}

0 comments on commit 65e43fb

Please sign in to comment.