use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixExecutableName method realpath.
/**
* A platform-independent method to canonicalize a path.
*/
protected String realpath(String path) {
/*
* Find the real path to the executable. realpath(3) mallocs a result buffer and returns a
* pointer to it, so I have to free it.
*/
try (CCharPointerHolder pathHolder = CTypeConversion.toCString(path)) {
final CCharPointer realpathPointer = Stdlib.realpath(pathHolder.get(), WordFactory.nullPointer());
if (realpathPointer.isNull()) {
/* Failure to find a real path. */
return null;
} else {
/* Success */
final String result = CTypeConversion.toJavaString(realpathPointer);
LibC.free(realpathPointer);
return result;
}
}
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixJavaIOSubstitutions method getSpace.
@Substitute
public long getSpace(File f, int t) {
statvfs statvfs = StackValue.get(SizeOf.get(statvfs.class));
LibC.memset(statvfs, WordFactory.zero(), WordFactory.unsigned(SizeOf.get(statvfs.class)));
try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
CCharPointer pathPtr = pathPin.get();
if (statvfs(pathPtr, statvfs) == 0) {
final long frsize = statvfs.f_frsize();
if (t == Target_java_io_FileSystem.SPACE_TOTAL) {
return frsize * statvfs.f_blocks();
} else if (t == Target_java_io_FileSystem.SPACE_FREE) {
return frsize * statvfs.f_bfree();
} else if (t == Target_java_io_FileSystem.SPACE_USABLE) {
return frsize * statvfs.f_bavail();
} else {
throw VMError.shouldNotReachHere("illegal space mode");
}
}
}
return 0L;
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixJavaIOSubstitutions method createFileExclusively.
@Substitute
public boolean createFileExclusively(String path) throws IOException {
int fd;
if (path.equals("/")) {
return false;
} else {
try (CCharPointerHolder pathPin = CTypeConversion.toCString(PosixUtils.removeTrailingSlashes(path))) {
CCharPointer pathPtr = pathPin.get();
fd = open(pathPtr, O_RDWR() | O_CREAT(), 0666);
}
}
if (fd < 0) {
if (fd != EEXIST()) {
throw new IOException(lastErrorString(path));
}
} else {
close(fd);
return true;
}
return false;
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixJavaIOSubstitutions method setPermission.
@Substitute
public boolean setPermission(File f, int access, boolean enable, boolean owneronly) {
int amode = 0;
if (access == Target_java_io_FileSystem.ACCESS_READ) {
amode = owneronly ? S_IRUSR() : S_IRUSR() | S_IRGRP() | S_IROTH();
} else if (access == Target_java_io_FileSystem.ACCESS_WRITE) {
amode = owneronly ? S_IWUSR() : S_IWUSR() | S_IWGRP() | S_IWOTH();
} else if (access == Target_java_io_FileSystem.ACCESS_EXECUTE) {
amode = owneronly ? S_IXUSR() : S_IXUSR() | S_IXGRP() | S_IXOTH();
} else {
throw VMError.shouldNotReachHere("illegal access mode");
}
stat stat = StackValue.get(SizeOf.get(stat.class));
try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
CCharPointer pathPtr = pathPin.get();
if (stat(pathPtr, stat) == 0) {
int newMode;
if (enable) {
newMode = stat.st_mode() | amode;
} else {
newMode = stat.st_mode() & ~amode;
}
if (chmod(pathPtr, newMode) >= 0) {
return true;
}
}
}
return false;
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixJavaIOSubstitutions method list.
@Substitute
public String[] list(File f) {
DIR dir;
try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
CCharPointer pathPtr = pathPin.get();
dir = opendir(pathPtr);
}
if (dir.isNull()) {
return null;
}
List<String> entries = new ArrayList<>();
dirent dirent = StackValue.get(SizeOf.get(dirent.class) + PATH_MAX() + 1);
direntPointer resultDirent = StackValue.get(SizeOf.get(direntPointer.class));
while (readdir_r(dir, dirent, resultDirent) == 0 && !resultDirent.read().isNull()) {
String name = CTypeConversion.toJavaString(dirent.d_name());
if (name.equals(".") || name.equals("..")) {
continue;
}
entries.add(name);
}
closedir(dir);
return entries.toArray(new String[entries.size()]);
}
Aggregations