Search in sources :

Example 1 with CCharPointerHolder

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;
        }
    }
}
Also used : CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 2 with CCharPointerHolder

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;
}
Also used : Statvfs.statvfs(com.oracle.svm.core.posix.headers.Statvfs.statvfs) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 3 with CCharPointerHolder

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;
}
Also used : IOException(java.io.IOException) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 4 with CCharPointerHolder

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;
}
Also used : Stat.stat(com.oracle.svm.core.posix.headers.Stat.stat) Stat.fstat(com.oracle.svm.core.posix.headers.Stat.fstat) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 5 with CCharPointerHolder

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()]);
}
Also used : ArrayList(java.util.ArrayList) PosixOSInterface.lastErrorString(com.oracle.svm.core.posix.PosixOSInterface.lastErrorString) Dirent.direntPointer(com.oracle.svm.core.posix.headers.Dirent.direntPointer) DIR(com.oracle.svm.core.posix.headers.Dirent.DIR) ENOTDIR(com.oracle.svm.core.posix.headers.Errno.ENOTDIR) S_IFDIR(com.oracle.svm.core.posix.headers.Stat.S_IFDIR) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Dirent.dirent(com.oracle.svm.core.posix.headers.Dirent.dirent) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)15 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)15 Substitute (com.oracle.svm.core.annotate.Substitute)8 IOException (java.io.IOException)3 PosixOSInterface.lastErrorString (com.oracle.svm.core.posix.PosixOSInterface.lastErrorString)2 Netdb (com.oracle.svm.core.posix.headers.Netdb)2 Stat.fstat (com.oracle.svm.core.posix.headers.Stat.fstat)2 Stat.stat (com.oracle.svm.core.posix.headers.Stat.stat)2 NoAllocationVerifier (com.oracle.svm.core.heap.NoAllocationVerifier)1 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)1 Dirent.dirent (com.oracle.svm.core.posix.headers.Dirent.dirent)1 Dirent.direntPointer (com.oracle.svm.core.posix.headers.Dirent.direntPointer)1 ENOTDIR (com.oracle.svm.core.posix.headers.Errno.ENOTDIR)1 NetinetIn (com.oracle.svm.core.posix.headers.NetinetIn)1 S_IFDIR (com.oracle.svm.core.posix.headers.Stat.S_IFDIR)1 Statvfs.statvfs (com.oracle.svm.core.posix.headers.Statvfs.statvfs)1 Time.timeval (com.oracle.svm.core.posix.headers.Time.timeval)1 FileNotFoundException (java.io.FileNotFoundException)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1