use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixUtils method writeSingle.
@SuppressWarnings("unused")
static void writeSingle(FileDescriptor fd, int b, boolean append) throws IOException {
SignedWord n;
int handle = Util_java_io_FileDescriptor.getFD(fd);
if (handle == -1) {
throw new IOException("Stream Closed");
}
CCharPointer bufPtr = StackValue.get(SizeOf.get(CCharPointer.class));
bufPtr.write((byte) b);
// the append parameter is disregarded
n = write(handle, bufPtr, WordFactory.unsigned(1));
if (n.equal(-1)) {
throw new IOException(lastErrorString("Write error"));
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class JavaMainWrapper method getCRuntimeArgumentBlockLength.
/**
* Argv is an array of C strings (i.e. array of pointers to characters). Each entry points to a
* different C string corresponding to a program argument. The program argument strings
* themselves are located in a contiguous block of memory followed by the environment variables
* key-value strings:
*
* <pre>
* <argument_0>\n<argument_1>\n...<argument_n>\n
* <env_key_value_1>\n<env_key_value_2>\n...<env_key_value_n>\n
* </pre>
*
* @return maximum length of C chars that can be stored in the program argument part of the
* contiguous memory block without writing into the environment variables part.
*/
public static long getCRuntimeArgumentBlockLength() {
VMError.guarantee(argv.notEqual(WordFactory.zero()) && argc > 0, "Requires JavaMainWrapper.run(int, CCharPointerPointer) entry point!");
CCharPointer firstArgPos = argv.read(0);
if (argvLength.equal(WordFactory.zero())) {
// Get char* to last program argument
CCharPointer lastArgPos = argv.read(argc - 1);
// Determine the length of the last program argument
UnsignedWord lastArgLength = SubstrateUtil.strlen(lastArgPos);
// Determine maximum C string length that can be stored in the program argument part
argvLength = WordFactory.unsigned(lastArgPos.rawValue()).add(lastArgLength).subtract(WordFactory.unsigned(firstArgPos.rawValue()));
}
return argvLength.rawValue();
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class JavaUtilZipSubstitutions method setDictionary.
// Inflater.c-Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
@Substitute
private static void setDictionary(long addr, byte[] b, int off, int len) {
try (PinnedObject pinned = PinnedObject.create(b)) {
CCharPointer bufPlusOff = pinned.addressOfArrayElement(off);
int res = ZLib.inflateSetDictionary(WordFactory.pointer(addr), bufPlusOff, len);
if (res == ZLib.Z_OK()) {
return;
} else if (res == ZLib.Z_STREAM_ERROR() || res == ZLib.Z_DATA_ERROR()) {
throw new IllegalArgumentException();
} else {
throw new InternalError();
}
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixJavaIOSubstitutions method canonicalize0.
@Substitute
private String canonicalize0(String path) throws IOException {
final int maxPathLen = MAXPATHLEN();
if (path.length() > maxPathLen) {
throw new IOException("Bad pathname");
}
CCharPointer resolved = StackValue.get(maxPathLen);
try (CCharPointerHolder pathPin = CTypeConversion.toCString(path)) {
CCharPointer pathPtr = pathPin.get();
if (realpath(pathPtr, resolved).notEqual(WordFactory.zero())) {
// that worked, so return it
return PosixUtils.collapse(CTypeConversion.toJavaString(resolved));
}
}
// something's bogus in the original path, so remove names from the end
// until either some subpath works or we run out of names
String resolvedPart = path;
String unresolvedPart = "";
CCharPointer r = WordFactory.nullPointer();
int lastSep = resolvedPart.lastIndexOf('/');
while (lastSep != -1) {
// prepend the last part of the resolved part to the unresolved part
unresolvedPart = resolvedPart.substring(lastSep) + unresolvedPart;
// remove the last part from the resolved part
resolvedPart = lastSep == 0 ? "" : resolvedPart.substring(0, lastSep);
lastSep = resolvedPart.lastIndexOf('/');
if (lastSep == -1) {
break;
}
try (CCharPointerHolder pathPin = CTypeConversion.toCString(resolvedPart)) {
CCharPointer resolvedPartPtr = pathPin.get();
r = realpath(resolvedPartPtr, resolved);
}
int errno = errno();
if (r.notEqual(WordFactory.zero())) {
// the subpath has a canonical path
break;
} else if (errno == ENOENT() || errno == ENOTDIR() || errno == EACCES()) {
// Other I/O problems cause an error return.
continue;
} else {
throw new IOException(lastErrorString("Bad pathname"));
}
}
if (r.notEqual(WordFactory.zero())) {
// append unresolved subpath to resolved subpath
String rs = CTypeConversion.toJavaString(r);
if (rs.length() + 1 + unresolvedPart.length() > maxPathLen) {
throw new IOException(lastErrorString("Bad pathname"));
}
return PosixUtils.collapse(rs + "/" + unresolvedPart);
} else {
// nothing resolved, so just return the original path
return PosixUtils.collapse(path);
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixJavaIOSubstitutions method setLastModifiedTime.
@Substitute
public boolean setLastModifiedTime(File f, long time) {
stat stat = StackValue.get(SizeOf.get(stat.class));
try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
CCharPointer pathPtr = pathPin.get();
if (stat(pathPtr, stat) == 0) {
timeval timeval = StackValue.get(2, SizeOf.get(timeval.class));
// preserve access time
timeval access = timeval.addressOf(0);
access.set_tv_sec(stat.st_atime());
access.set_tv_usec(0L);
// change last-modified time
timeval last = timeval.addressOf(1);
last.set_tv_sec(time / 1000);
last.set_tv_usec((time % 1000) * 1000);
if (utimes(pathPtr, timeval) == 0) {
return true;
}
}
}
return false;
}
Aggregations