Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java
32288 views
/*1* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.security.AccessController;28import java.security.PrivilegedAction;2930/**31* Unix system and library calls.32*/3334class UnixNativeDispatcher {35protected UnixNativeDispatcher() { }3637// returns a NativeBuffer containing the given path38static NativeBuffer copyToNativeBuffer(UnixPath path) {39byte[] cstr = path.getByteArrayForSysCalls();40int size = cstr.length + 1;41NativeBuffer buffer = NativeBuffers.getNativeBufferFromCache(size);42if (buffer == null) {43buffer = NativeBuffers.allocNativeBuffer(size);44} else {45// buffer already contains the path46if (buffer.owner() == path)47return buffer;48}49NativeBuffers.copyCStringToNativeBuffer(cstr, buffer);50buffer.setOwner(path);51return buffer;52}5354/**55* char *getcwd(char *buf, size_t size);56*/57static native byte[] getcwd();5859/**60* int dup(int filedes)61*/62static native int dup(int filedes) throws UnixException;6364/**65* int open(const char* path, int oflag, mode_t mode)66*/67static int open(UnixPath path, int flags, int mode) throws UnixException {68NativeBuffer buffer = copyToNativeBuffer(path);69try {70return open0(buffer.address(), flags, mode);71} finally {72buffer.release();73}74}75private static native int open0(long pathAddress, int flags, int mode)76throws UnixException;7778/**79* int openat(int dfd, const char* path, int oflag, mode_t mode)80*/81static int openat(int dfd, byte[] path, int flags, int mode) throws UnixException {82NativeBuffer buffer = NativeBuffers.asNativeBuffer(path);83try {84return openat0(dfd, buffer.address(), flags, mode);85} finally {86buffer.release();87}88}89private static native int openat0(int dfd, long pathAddress, int flags, int mode)90throws UnixException;9192/**93* close(int filedes)94*/95static native void close(int fd);9697/**98* FILE* fopen(const char *filename, const char* mode);99*/100static long fopen(UnixPath filename, String mode) throws UnixException {101NativeBuffer pathBuffer = copyToNativeBuffer(filename);102NativeBuffer modeBuffer = NativeBuffers.asNativeBuffer(Util.toBytes(mode));103try {104return fopen0(pathBuffer.address(), modeBuffer.address());105} finally {106modeBuffer.release();107pathBuffer.release();108}109}110private static native long fopen0(long pathAddress, long modeAddress)111throws UnixException;112113/**114* fclose(FILE* stream)115*/116static native void fclose(long stream) throws UnixException;117118/**119* void rewind(FILE* stream);120*/121static native void rewind(long stream) throws UnixException;122123/**124* link(const char* existing, const char* new)125*/126static void link(UnixPath existing, UnixPath newfile) throws UnixException {127NativeBuffer existingBuffer = copyToNativeBuffer(existing);128NativeBuffer newBuffer = copyToNativeBuffer(newfile);129try {130link0(existingBuffer.address(), newBuffer.address());131} finally {132newBuffer.release();133existingBuffer.release();134}135}136private static native void link0(long existingAddress, long newAddress)137throws UnixException;138139/**140* unlink(const char* path)141*/142static void unlink(UnixPath path) throws UnixException {143NativeBuffer buffer = copyToNativeBuffer(path);144try {145unlink0(buffer.address());146} finally {147buffer.release();148}149}150private static native void unlink0(long pathAddress) throws UnixException;151152/**153* unlinkat(int dfd, const char* path, int flag)154*/155static void unlinkat(int dfd, byte[] path, int flag) throws UnixException {156NativeBuffer buffer = NativeBuffers.asNativeBuffer(path);157try {158unlinkat0(dfd, buffer.address(), flag);159} finally {160buffer.release();161}162}163private static native void unlinkat0(int dfd, long pathAddress, int flag)164throws UnixException;165166/**167* mknod(const char* path, mode_t mode, dev_t dev)168*/169static void mknod(UnixPath path, int mode, long dev) throws UnixException {170NativeBuffer buffer = copyToNativeBuffer(path);171try {172mknod0(buffer.address(), mode, dev);173} finally {174buffer.release();175}176}177private static native void mknod0(long pathAddress, int mode, long dev)178throws UnixException;179180/**181* rename(const char* old, const char* new)182*/183static void rename(UnixPath from, UnixPath to) throws UnixException {184NativeBuffer fromBuffer = copyToNativeBuffer(from);185NativeBuffer toBuffer = copyToNativeBuffer(to);186try {187rename0(fromBuffer.address(), toBuffer.address());188} finally {189toBuffer.release();190fromBuffer.release();191}192}193private static native void rename0(long fromAddress, long toAddress)194throws UnixException;195196/**197* renameat(int fromfd, const char* old, int tofd, const char* new)198*/199static void renameat(int fromfd, byte[] from, int tofd, byte[] to) throws UnixException {200NativeBuffer fromBuffer = NativeBuffers.asNativeBuffer(from);201NativeBuffer toBuffer = NativeBuffers.asNativeBuffer(to);202try {203renameat0(fromfd, fromBuffer.address(), tofd, toBuffer.address());204} finally {205toBuffer.release();206fromBuffer.release();207}208}209private static native void renameat0(int fromfd, long fromAddress, int tofd, long toAddress)210throws UnixException;211212/**213* mkdir(const char* path, mode_t mode)214*/215static void mkdir(UnixPath path, int mode) throws UnixException {216NativeBuffer buffer = copyToNativeBuffer(path);217try {218mkdir0(buffer.address(), mode);219} finally {220buffer.release();221}222}223private static native void mkdir0(long pathAddress, int mode) throws UnixException;224225/**226* rmdir(const char* path)227*/228static void rmdir(UnixPath path) throws UnixException {229NativeBuffer buffer = copyToNativeBuffer(path);230try {231rmdir0(buffer.address());232} finally {233buffer.release();234}235}236private static native void rmdir0(long pathAddress) throws UnixException;237238/**239* readlink(const char* path, char* buf, size_t bufsize)240*241* @return link target242*/243static byte[] readlink(UnixPath path) throws UnixException {244NativeBuffer buffer = copyToNativeBuffer(path);245try {246return readlink0(buffer.address());247} finally {248buffer.release();249}250}251private static native byte[] readlink0(long pathAddress) throws UnixException;252253/**254* realpath(const char* path, char* resolved_name)255*256* @return resolved path257*/258static byte[] realpath(UnixPath path) throws UnixException {259NativeBuffer buffer = copyToNativeBuffer(path);260try {261return realpath0(buffer.address());262} finally {263buffer.release();264}265}266private static native byte[] realpath0(long pathAddress) throws UnixException;267268/**269* symlink(const char* name1, const char* name2)270*/271static void symlink(byte[] name1, UnixPath name2) throws UnixException {272NativeBuffer targetBuffer = NativeBuffers.asNativeBuffer(name1);273NativeBuffer linkBuffer = copyToNativeBuffer(name2);274try {275symlink0(targetBuffer.address(), linkBuffer.address());276} finally {277linkBuffer.release();278targetBuffer.release();279}280}281private static native void symlink0(long name1, long name2)282throws UnixException;283284/**285* stat(const char* path, struct stat* buf)286*/287static void stat(UnixPath path, UnixFileAttributes attrs) throws UnixException {288NativeBuffer buffer = copyToNativeBuffer(path);289try {290stat0(buffer.address(), attrs);291} finally {292buffer.release();293}294}295private static native void stat0(long pathAddress, UnixFileAttributes attrs)296throws UnixException;297298/**299* lstat(const char* path, struct stat* buf)300*/301static void lstat(UnixPath path, UnixFileAttributes attrs) throws UnixException {302NativeBuffer buffer = copyToNativeBuffer(path);303try {304lstat0(buffer.address(), attrs);305} finally {306buffer.release();307}308}309private static native void lstat0(long pathAddress, UnixFileAttributes attrs)310throws UnixException;311312/**313* fstat(int filedes, struct stat* buf)314*/315static native void fstat(int fd, UnixFileAttributes attrs) throws UnixException;316317/**318* fstatat(int filedes,const char* path, struct stat* buf, int flag)319*/320static void fstatat(int dfd, byte[] path, int flag, UnixFileAttributes attrs)321throws UnixException322{323NativeBuffer buffer = NativeBuffers.asNativeBuffer(path);324try {325fstatat0(dfd, buffer.address(), flag, attrs);326} finally {327buffer.release();328}329}330private static native void fstatat0(int dfd, long pathAddress, int flag,331UnixFileAttributes attrs) throws UnixException;332333/**334* chown(const char* path, uid_t owner, gid_t group)335*/336static void chown(UnixPath path, int uid, int gid) throws UnixException {337NativeBuffer buffer = copyToNativeBuffer(path);338try {339chown0(buffer.address(), uid, gid);340} finally {341buffer.release();342}343}344private static native void chown0(long pathAddress, int uid, int gid)345throws UnixException;346347/**348* lchown(const char* path, uid_t owner, gid_t group)349*/350static void lchown(UnixPath path, int uid, int gid) throws UnixException {351NativeBuffer buffer = copyToNativeBuffer(path);352try {353lchown0(buffer.address(), uid, gid);354} finally {355buffer.release();356}357}358private static native void lchown0(long pathAddress, int uid, int gid)359throws UnixException;360361/**362* fchown(int filedes, uid_t owner, gid_t group)363*/364static native void fchown(int fd, int uid, int gid) throws UnixException;365366/**367* chmod(const char* path, mode_t mode)368*/369static void chmod(UnixPath path, int mode) throws UnixException {370NativeBuffer buffer = copyToNativeBuffer(path);371try {372chmod0(buffer.address(), mode);373} finally {374buffer.release();375}376}377private static native void chmod0(long pathAddress, int mode)378throws UnixException;379380/**381* fchmod(int fildes, mode_t mode)382*/383static native void fchmod(int fd, int mode) throws UnixException;384385/**386* utimes(conar char* path, const struct timeval times[2])387*/388static void utimes(UnixPath path, long times0, long times1)389throws UnixException390{391NativeBuffer buffer = copyToNativeBuffer(path);392try {393utimes0(buffer.address(), times0, times1);394} finally {395buffer.release();396}397}398private static native void utimes0(long pathAddress, long times0, long times1)399throws UnixException;400401/**402* futimes(int fildes,, const struct timeval times[2])403*/404static native void futimes(int fd, long times0, long times1) throws UnixException;405406/**407* DIR *opendir(const char* dirname)408*/409static long opendir(UnixPath path) throws UnixException {410NativeBuffer buffer = copyToNativeBuffer(path);411try {412return opendir0(buffer.address());413} finally {414buffer.release();415}416}417private static native long opendir0(long pathAddress) throws UnixException;418419/**420* DIR* fdopendir(int filedes)421*/422static native long fdopendir(int dfd) throws UnixException;423424425/**426* closedir(DIR* dirp)427*/428static native void closedir(long dir) throws UnixException;429430/**431* struct dirent* readdir(DIR *dirp)432*433* @return dirent->d_name434*/435static native byte[] readdir(long dir) throws UnixException;436437/**438* size_t read(int fildes, void* buf, size_t nbyte)439*/440static native int read(int fildes, long buf, int nbyte) throws UnixException;441442/**443* size_t writeint fildes, void* buf, size_t nbyte)444*/445static native int write(int fildes, long buf, int nbyte) throws UnixException;446447/**448* access(const char* path, int amode);449*/450static void access(UnixPath path, int amode) throws UnixException {451NativeBuffer buffer = copyToNativeBuffer(path);452try {453access0(buffer.address(), amode);454} finally {455buffer.release();456}457}458private static native void access0(long pathAddress, int amode) throws UnixException;459460/**461* struct passwd *getpwuid(uid_t uid);462*463* @return passwd->pw_name464*/465static native byte[] getpwuid(int uid) throws UnixException;466467/**468* struct group *getgrgid(gid_t gid);469*470* @return group->gr_name471*/472static native byte[] getgrgid(int gid) throws UnixException;473474/**475* struct passwd *getpwnam(const char *name);476*477* @return passwd->pw_uid478*/479static int getpwnam(String name) throws UnixException {480NativeBuffer buffer = NativeBuffers.asNativeBuffer(Util.toBytes(name));481try {482return getpwnam0(buffer.address());483} finally {484buffer.release();485}486}487private static native int getpwnam0(long nameAddress) throws UnixException;488489/**490* struct group *getgrnam(const char *name);491*492* @return group->gr_name493*/494static int getgrnam(String name) throws UnixException {495NativeBuffer buffer = NativeBuffers.asNativeBuffer(Util.toBytes(name));496try {497return getgrnam0(buffer.address());498} finally {499buffer.release();500}501}502private static native int getgrnam0(long nameAddress) throws UnixException;503504/**505* statvfs(const char* path, struct statvfs *buf)506*/507static void statvfs(UnixPath path, UnixFileStoreAttributes attrs)508throws UnixException509{510NativeBuffer buffer = copyToNativeBuffer(path);511try {512statvfs0(buffer.address(), attrs);513} finally {514buffer.release();515}516}517private static native void statvfs0(long pathAddress, UnixFileStoreAttributes attrs)518throws UnixException;519520/**521* long int pathconf(const char *path, int name);522*/523static long pathconf(UnixPath path, int name) throws UnixException {524NativeBuffer buffer = copyToNativeBuffer(path);525try {526return pathconf0(buffer.address(), name);527} finally {528buffer.release();529}530}531private static native long pathconf0(long pathAddress, int name)532throws UnixException;533534/**535* long fpathconf(int fildes, int name);536*/537static native long fpathconf(int filedes, int name) throws UnixException;538539/**540* char* strerror(int errnum)541*/542static native byte[] strerror(int errnum);543544/**545* Capabilities546*/547private static final int SUPPORTS_OPENAT = 1 << 1; // syscalls548private static final int SUPPORTS_FUTIMES = 1 << 2;549private static final int SUPPORTS_BIRTHTIME = 1 << 16; // other features550private static final int capabilities;551552/**553* Supports openat and other *at calls.554*/555static boolean openatSupported() {556return (capabilities & SUPPORTS_OPENAT) != 0;557}558559/**560* Supports futimes or futimesat561*/562static boolean futimesSupported() {563return (capabilities & SUPPORTS_FUTIMES) != 0;564}565566/**567* Supports file birth (creation) time attribute568*/569static boolean birthtimeSupported() {570return (capabilities & SUPPORTS_BIRTHTIME) != 0;571}572573private static native int init();574static {575AccessController.doPrivileged(new PrivilegedAction<Void>() {576public Void run() {577System.loadLibrary("nio");578return null;579}});580capabilities = init();581}582}583584585