Skip to content

Commit

Permalink
Add BSD support, fix return types
Browse files Browse the repository at this point in the history
  • Loading branch information
battleblow committed Mar 20, 2021
1 parent d025f52 commit 05dca17
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
#endif
#include <sys/time.h>

#if defined(__linux__) || defined(_ALLBSD_SOURCE)
#if defined(__linux__) || defined(MACOSX)
#include <sys/xattr.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/extattr.h>
#endif

/* For POSIX-compliant getpwuid_r */
Expand Down Expand Up @@ -1245,19 +1247,21 @@ JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fgetxattr0(JNIEnv* env, jclass clazz,
jint fd, jlong nameAddress, jlong valueAddress, jint valueLen)
{
size_t res = -1;
ssize_t res = -1;
const char* name = jlong_to_ptr(nameAddress);
void* value = jlong_to_ptr(valueAddress);

#ifdef __linux__
res = fgetxattr(fd, name, value, valueLen);
#elif _ALLBSD_SOURCE
#elif MACOSX
res = fgetxattr(fd, name, value, valueLen, 0, 0);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
res = extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, value, (size_t)valueLen);
#else
throwUnixException(env, ENOTSUP);
#endif

if (res == (size_t)-1)
if (res == (ssize_t)-1)
throwUnixException(env, errno);
return (jint)res;
}
Expand All @@ -1272,8 +1276,10 @@ Java_sun_nio_fs_UnixNativeDispatcher_fsetxattr0(JNIEnv* env, jclass clazz,

#ifdef __linux__
res = fsetxattr(fd, name, value, valueLen, 0);
#elif _ALLBSD_SOURCE
#elif MACOSX
res = fsetxattr(fd, name, value, valueLen, 0, 0);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
res = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, name, value, (size_t)valueLen);
#else
throwUnixException(env, ENOTSUP);
#endif
Expand All @@ -1291,8 +1297,10 @@ Java_sun_nio_fs_UnixNativeDispatcher_fremovexattr0(JNIEnv* env, jclass clazz,

#ifdef __linux__
res = fremovexattr(fd, name);
#elif _ALLBSD_SOURCE
#elif MACOSX
res = fremovexattr(fd, name, 0);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
res = extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, name);
#else
throwUnixException(env, ENOTSUP);
#endif
Expand All @@ -1305,18 +1313,20 @@ JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_flistxattr(JNIEnv* env, jclass clazz,
jint fd, jlong listAddress, jint size)
{
size_t res = -1;
ssize_t res = -1;
char* list = jlong_to_ptr(listAddress);

#ifdef __linux__
res = flistxattr(fd, list, (size_t)size);
#elif _ALLBSD_SOURCE
#elif MACOSX
res = flistxattr(fd, list, (size_t)size, 0);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
res = extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, list, (size_t)size);

This comment has been minimized.

Copy link
@rednoah

rednoah Aug 22, 2023

Note that this code does not work. Linux flistxattr and extattr_list_fd don't return the same data structure.

"""
extattr_list_file() returns a list of attributes present in the requested
namespace. Each list entry consists of a single byte containing the
length of the attribute name, followed by the attribute name. The attri-
bute name is not terminated by ASCII 0 (nul).
"""

This comment has been minimized.

Copy link
@rednoah

rednoah Aug 22, 2023

The Java side should decode the memory like so:

private static List<String> decodeStringList(ByteBuffer buffer) {
	List<String> list = new ArrayList<String>();

	while (buffer.hasRemaining()) {
		int length = buffer.get() & 0xFF;
		byte[] value = new byte[length];
		buffer.get(value);

		list.add(new String(value, UTF_8));
	}

	return list;
}
#else
throwUnixException(env, ENOTSUP);
#endif

if (res == (size_t)-1)
if (res == (ssize_t)-1)
throwUnixException(env, errno);
return (jint)res;
}
}

0 comments on commit 05dca17

Please sign in to comment.