Index: modules/luni/src/main/java/java/io/RandomAccessFile.java =================================================================== --- modules/luni/src/main/java/java/io/RandomAccessFile.java (revision 418697) +++ modules/luni/src/main/java/java/io/RandomAccessFile.java (working copy) @@ -148,7 +148,7 @@ public void close() throws IOException { synchronized (channel) { synchronized (this) { - if(channel.isOpen() && fd.descriptor >= 0){ + if(channel.isOpen() && fd.descriptor != -1){ channel.close(); } fd.descriptor = -1; @@ -201,7 +201,7 @@ } private synchronized void openCheck() throws IOException { - if (fd.descriptor < 0) { + if (fd.descriptor == -1) { throw new IOException(); } } Index: modules/luni/src/main/java/java/io/FileInputStream.java =================================================================== --- modules/luni/src/main/java/java/io/FileInputStream.java (revision 418697) +++ modules/luni/src/main/java/java/io/FileInputStream.java (working copy) @@ -156,7 +156,7 @@ * of but the underlying file has been opened */ synchronized (this) { - if (fd.descriptor >= 0) { + if (fd.descriptor != -1) { fileSystem.close(fd.descriptor); } fd.descriptor = -1; @@ -169,7 +169,7 @@ synchronized (channel) { synchronized (this) { // FIXME: System.in, out, err may not want to be closed? - if (channel.isOpen() && fd.descriptor >= 0) { + if (channel.isOpen() && fd.descriptor != -1) { channel.close(); } fd.descriptor = -1; @@ -334,7 +334,7 @@ } private synchronized void openCheck() throws IOException { - if (fd.descriptor < 0) { + if (fd.descriptor == -1) { throw new IOException(); } } Index: modules/luni/src/main/java/java/io/FileOutputStream.java =================================================================== --- modules/luni/src/main/java/java/io/FileOutputStream.java (revision 418697) +++ modules/luni/src/main/java/java/io/FileOutputStream.java (working copy) @@ -174,7 +174,7 @@ * of but the underlying file has been opened */ synchronized (this) { - if (fd.descriptor >= 0) { + if (fd.descriptor != -1) { fileSystem.close(fd.descriptor); } fd.descriptor = -1; @@ -187,7 +187,7 @@ synchronized (channel) { synchronized (this) { // FIXME: System.in, out, err may not want to be closed? - if (channel.isOpen() && fd.descriptor >= 0) { + if (channel.isOpen() && fd.descriptor != -1) { channel.close(); } fd.descriptor = -1; @@ -303,7 +303,7 @@ } private synchronized void openCheck() throws IOException { - if (fd.descriptor < 0) { + if (fd.descriptor == -1) { throw new IOException(); } } Index: modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java =================================================================== --- modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java (revision 418697) +++ modules/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java (working copy) @@ -219,7 +219,7 @@ throw new NullPointerException(); } long handler = openImpl(fileName, mode); - if (handler < 0) { + if (handler == -1) { throw new FileNotFoundException(); } return handler; Index: native-src/shared/port/hyfile.c =================================================================== --- native-src/shared/port/hyfile.c (revision 0) +++ native-src/shared/port/hyfile.c (revision 0) @@ -0,0 +1,929 @@ +/* Copyright 1991, 2006 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define CDEV_CURRENT_FUNCTION _comment_ +/** + * @file + * @ingroup Port + * @brief file + */ +#undef CDEV_CURRENT_FUNCTION + +#include +#include "hyport.h" +#include "portpriv.h" +#include "hystdarg.h" +#include "portnls.h" +#include "ut_hyprt.h" +#include +#include "apr_arch_file_io.h" +#include "apr_strings.h" +#include "apr_portable.h" +#include +#include +#include + +#define CDEV_CURRENT_FUNCTION _prototypes_private + +static I_32 findError (I_32 errorCode); + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION EsTranslateOpenFlags + +static I_32 +EsTranslateOpenFlags (I_32 flags) +{ + I_32 realFlags = 0; + if (flags & HyOpenAppend) + { + realFlags |= APR_FOPEN_APPEND; + } + if (flags & HyOpenTruncate) + { + realFlags |= APR_TRUNCATE; + } + if (flags & HyOpenCreate) + { + realFlags |= APR_CREATE; + } + if (flags & HyOpenCreateNew) + { + realFlags |= (APR_EXCL | APR_CREATE); + } +#ifdef O_SYNC + if (flags & HyOpenSync) { + realFlags |= APR_XTHREAD; + } +#endif + if (flags & HyOpenRead) + { + if (flags & HyOpenWrite) + { + return (APR_READ | APR_WRITE | realFlags); + } + return (APR_READ | realFlags); + } + if (flags & HyOpenWrite) + { + return (APR_WRITE | realFlags); + } + return -1; +} +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION findError +/** + * @internal + * Determines the proper portable error code to return given a native error code + * + * @param[in] errorCode The error code reported by the OS + * + * @return the (negative) portable error code + */ +static I_32 +findError (I_32 errorCode) +{ + switch (errorCode) + { + case APR_ENAMETOOLONG: + return HYPORT_ERROR_FILE_NAMETOOLONG; + case APR_EACCES: + return HYPORT_ERROR_FILE_NOPERMISSION; + case APR_NOTFOUND: + return HYPORT_ERROR_FILE_NOTFOUND; + case APR_ENOSPC: + return HYPORT_ERROR_FILE_DISKFULL; + case APR_EEXIST: + return HYPORT_ERROR_FILE_EXIST; + case APR_ENOMEM: + return HYPORT_ERROR_FILE_SYSTEMFULL; + default: + return HYPORT_ERROR_FILE_OPFAILED; + } +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_attr +/** + * Determine whether path is a file or directory. + * + * @param[in] portLibrary The port library + * @param[in] path file/path name being queried. + * + * @return EslsFile if a file, EslsDir if a directory, negative portable error code on failure. + */ +I_32 VMCALL +hyfile_attr (struct HyPortLibrary * portLibrary, const char *path) +{ + apr_status_t stat; + apr_finfo_t finfo; + apr_pool_t* pool; + I_32 result = HyIsFile; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ( (stat = apr_stat(&finfo, + path, + APR_FINFO_TYPE, + pool) ) != APR_SUCCESS) { + apr_pool_destroy(pool); + return portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if (finfo.filetype == APR_DIR) { + result = HyIsDir; + } + apr_pool_destroy(pool); + return result; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_close +/** + * Closes a file descriptor. + * + * @param[in] portLibrary The port library + * @param[in] fd The file descriptor. + * + * @return 0 on success, -1 on failure. + * @internal @todo return negative portable return code on failure. + */ +I_32 VMCALL +hyfile_close (struct HyPortLibrary * portLibrary, IDATA fd) +{ + apr_status_t stat; + I_32 result = 0; + apr_pool_t* pool = apr_file_pool_get((apr_file_t *) fd); + if ((stat = apr_file_close((apr_file_t*)fd)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + result = -1; + } + apr_pool_destroy(pool); + return result; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_error_message +/** + * Return an error message describing the last OS error that occurred. The last + * error returned is not thread safe, it may not be related to the operation that + * failed for this thread. + * + * @param[in] portLibrary The port library + * + * @return error message describing the last OS error, may return NULL. + * + * @internal + * @note This function gets the last error code from the OS and then returns + * the corresponding string. It is here as a helper function for JCL. Once hyerror + * is integrated into the port library this function should probably disappear. + */ +const char *VMCALL +hyfile_error_message (struct HyPortLibrary *portLibrary) +{ + return portLibrary->error_last_error_message (portLibrary); +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_findclose +/** + * Close the handle returned from @ref hyfile_findfirst. + * + * @param[in] portLibrary The port library + * @param[in] findhandle Handle returned from @ref hyfile_findfirst. + */ +void VMCALL +hyfile_findclose (struct HyPortLibrary *portLibrary, UDATA findhandle) +{ + apr_status_t stat; + apr_dir_t* thedir = (apr_dir_t*) findhandle; + if ( (stat = apr_dir_close(thedir)) != APR_SUCCESS) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + apr_pool_destroy(thedir->pool); +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_findfirst +/** + * Find the first occurrence of a file identified by path. Answers a handle + * to be used in subsequent calls to @ref hyfile_findnext and @ref hyfile_findclose. + * + * @param[in] portLibrary The port library + * @param[in] path file/path name being queried. + * @param[out] resultbuf filename and path matching path. + * + * @return valid handle on success, -1 on failure. + */ +UDATA VMCALL +hyfile_findfirst (struct HyPortLibrary *portLibrary, const char *path, + char *resultbuf) +{ + apr_dir_t* newdir; + apr_status_t stat; + apr_finfo_t finfo; + apr_pool_t* pool; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ( (stat = apr_dir_open(&newdir, + path, + pool) ) != APR_SUCCESS) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return (UDATA) -1; + } + + if ( (stat = apr_dir_read(&finfo, + APR_FINFO_DIRENT, + newdir) ) != APR_SUCCESS) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_dir_close(newdir); + apr_pool_destroy(pool); + return (UDATA) -1; + } + strcpy (resultbuf, finfo.name); + return (UDATA) newdir; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_findnext +/** + * Find the next filename and path matching a given handle. + * + * @param[in] portLibrary The port library + * @param[in] findhandle handle returned from @ref hyfile_findfirst. + * @param[out] resultbuf next filename and path matching findhandle. + * + * @return 0 on success, -1 on failure or if no matching entries. + * @internal @todo return negative portable return code on failure. + */ +I_32 VMCALL +hyfile_findnext (struct HyPortLibrary * portLibrary, UDATA findhandle, + char *resultbuf) +{ + apr_dir_t* newdir; + apr_status_t stat; + apr_finfo_t finfo; + newdir = (apr_dir_t*) findhandle; + if ( (stat = apr_dir_read(&finfo, + APR_FINFO_DIRENT, + newdir) ) != APR_SUCCESS) + { + + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + return (UDATA) -1; + } + strcpy (resultbuf, finfo.name); + return 0; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_lastmod +/** + * Return the last modification time of the file path in seconds. + * + * @param[in] portLibrary The port library + * @param[in] path file/path name being queried. + * + * @return last modification time on success, -1 on failure. + */ +I_64 VMCALL +hyfile_lastmod (struct HyPortLibrary * portLibrary, const char *path) +{ + apr_status_t stat; + apr_finfo_t finfo; + apr_pool_t* pool; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ( (stat = apr_stat(&finfo, + path, + APR_FINFO_MTIME, + pool) ) != APR_SUCCESS) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + apr_pool_destroy(pool); + return (I_64)(finfo.mtime / 1000000); +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_length +/** + * Answer the length in bytes of the file. + * + * @param[in] portLibrary The port library + * @param[in] path file/path name being queried. + * + * @return Length in bytes of the file on success, negative portable error code on failure + */ +I_64 VMCALL +hyfile_length (struct HyPortLibrary * portLibrary, const char *path) +{ + apr_status_t stat; + apr_finfo_t finfo; + apr_pool_t* pool; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ( (stat = apr_stat(&finfo, + path, + APR_FINFO_SIZE, + pool) ) != APR_SUCCESS) + { + apr_pool_destroy(pool); + return portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + apr_pool_destroy(pool); + return (I_64)finfo.size; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_mkdir +/** + * Create a directory. + * + * @param[in] portLibrary The port library + * @param[in] path Directory to be created. + * + * @return 0 on success, -1 on failure. + * @note Assumes all components of path up to the last directory already exist. + * @internal @todo return negative portable return code on failure. + */ +I_32 VMCALL +hyfile_mkdir (struct HyPortLibrary * portLibrary, const char *path) +{ + apr_status_t stat; + apr_pool_t* pool; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ( (stat = apr_dir_make(path, + APR_OS_DEFAULT, + pool) ) != APR_SUCCESS) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + apr_pool_destroy(pool); + return 0; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_move +/** + * Move the file pathExist to a new name pathNew. + * + * @param[in] portLibrary The port library + * @param[in] pathExist The existing file name. + * @param[in] pathNew The new file name. + * + * @return 0 on success, -1 on failure. + * @internal @todo return negative portable return code on failure. + */ +I_32 VMCALL +hyfile_move (struct HyPortLibrary * portLibrary, const char *pathExist, + const char *pathNew) +{ + apr_status_t stat; + apr_pool_t* pool; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ( (stat = apr_file_rename(pathExist, + pathNew, + pool) ) != APR_SUCCESS) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + apr_pool_destroy(pool); + return 0; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_open +/** + * Convert a pathname into a file descriptor. + * + * @param[in] portLibrary The port library + * @param[in] path Name of the file to be opened. + * @param[in] flags Portable file read/write attributes. + * @param[in] mode Platform file permissions. + * + * @return The file descriptor of the newly opened file, -1 on failure. + */ +IDATA VMCALL +hyfile_open (struct HyPortLibrary * portLibrary, const char *path, I_32 flags, + I_32 mode) +{ + apr_file_t *file_handle; + apr_status_t stat; + apr_pool_t* pool; + I_32 realFlags = EsTranslateOpenFlags (flags); + if (realFlags == -1) + { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(APR_EINVAL), + findError (APR_EINVAL)); + return -1; + } + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + return -1; + } + if ((stat = apr_file_open(&file_handle, path, + realFlags, + APR_FPROT_OS_DEFAULT, + pool)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + return (IDATA)file_handle; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_read +/** + * Read bytes from a file descriptor into a user provided buffer. + * + * @param[in] portLibrary The port library + * @param[in] fd The file descriptor. + * @param[in,out] buf Buffer to read into. + * @param[in] nbytes Size of buffer. + * + * @return The number of bytes read, or -1 on failure. + */ +IDATA VMCALL +hyfile_read (struct HyPortLibrary * portLibrary, IDATA fd, void *buf, + IDATA nbytes) +{ + apr_status_t stat; + apr_size_t result; + + if (nbytes == 0) + { + return 0; + } + + result = (apr_size_t)nbytes; + + if ( (stat = apr_file_read((apr_file_t*)fd, + buf, + &result) ) != APR_SUCCESS && stat != APR_EOF) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + return -1; + } + if (stat == APR_EOF) + { + return -1; + } + + return result; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_seek +/** + * Repositions the offset of the file descriptor to a given offset as per directive whence. + * + * @param[in] portLibrary The port library + * @param[in] fd The file descriptor. + * @param[in] offset The offset in the file to position to. + * @param[in] whence Portable constant describing how to apply the offset. + * + * @return The resulting offset on success, -1 on failure. + * @note whence is one of HySeekSet (seek from beginning of file), + * HySeekCur (seek from current file pointer) or HySeekEnd (seek backwards from + * end of file). + * @internal @note seek operations return -1 on failure. Negative offsets + * can be returned when seeking beyond end of file. + */ +I_64 VMCALL +hyfile_seek (struct HyPortLibrary * portLibrary, IDATA fd, I_64 offset, + I_32 whence) +{ + apr_seek_where_t where; + apr_off_t result; + apr_status_t stat; + result = (apr_off_t) offset; + if ((whence < HySeekSet) || (whence > HySeekEnd)) + { + return -1; + } + if (whence == HySeekSet) + { + where = APR_SET; + } + if (whence == HySeekEnd) + { + where = APR_END; + } + if (whence == HySeekCur) + { + where = APR_CUR; + } + + /* If file offsets are 32 bit, truncate the seek to that range */ + if (sizeof (apr_off_t) < sizeof (I_64)) + { + if (offset > 0x7FFFFFFF) + { + result = 0x7FFFFFFF; + } + else if (offset < -0x7FFFFFFF) + { + result = -0x7FFFFFFF; + } + } + + if ( (stat = apr_file_seek((apr_file_t*)fd, + where, + &result)) != APR_SUCCESS) + { + portLibrary->error_set_last_error(portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + return -1; + } + return result; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_shutdown +/** + * PortLibrary shutdown. + * + * This function is called during shutdown of the portLibrary. Any resources that were created by @ref hyfile_startup + * should be destroyed here. + * + * @param[in] portLibrary The port library + * + * @note Most implementations will be empty. + */ +void VMCALL +hyfile_shutdown (struct HyPortLibrary *portLibrary) +{ +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_startup +/** + * PortLibrary startup. + * + * This function is called during startup of the portLibrary. Any resources that are required for + * the file operations may be created here. All resources created here should be destroyed + * in @ref hyfile_shutdown. + * + * @param[in] portLibrary The port library + * + * @return 0 on success, negative error code on failure. Error code values returned are + * \arg HYPORT_ERROR_STARTUP_FILE + * + * @note Most implementations will simply return success. + */ +I_32 VMCALL +hyfile_startup (struct HyPortLibrary *portLibrary) +{ + return 0; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_sync +/** + * Synchronize a file's state with the state on disk. + * + * @param[in] portLibrary The port library + * @param[in] fd The file descriptor. + * + * @return 0 on success, -1 on failure. + * @internal @todo return negative portable return code on failure. + */ +I_32 VMCALL +hyfile_sync (struct HyPortLibrary * portLibrary, IDATA fd) +{ + apr_status_t stat; + if ((stat = apr_file_flush((apr_file_t*)fd)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + return -1; + } + return 0; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_unlink +/** + * Remove a file from the file system. + * + * @param[in] portLibrary The port library + * @param[in] path file/path name to remove. + * + * @return 0 on success, -1 on failure. + * @internal @todo return negative portable return code on failure. + */ +I_32 VMCALL +hyfile_unlink (struct HyPortLibrary * portLibrary, const char *path) +{ + apr_status_t stat; + apr_pool_t* pool; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ((stat = apr_file_attrs_set(path, + 0, + APR_FILE_ATTR_READONLY, + pool)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + if ((stat = apr_file_remove(path,pool)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + apr_pool_destroy(pool); + return 0; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_unlinkdir +/** + * Remove the trailing directory of the path. If the path is a symbolic link to a directory, remove + * the symbolic link. + * + * @param[in] portLibrary The port library + * @param[in] path directory name being removed. + * + * @return 0 on success, -1 on failure. + * @internal @todo return negative portable return code on failure.. + */ +I_32 VMCALL +hyfile_unlinkdir (struct HyPortLibrary * portLibrary, const char *path) +{ + apr_status_t stat; + apr_pool_t* pool; + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + if ((stat = apr_file_attrs_set(path, + 0, + APR_FILE_ATTR_READONLY, + pool)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + if ((stat = apr_dir_remove(path, pool)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + apr_pool_destroy(pool); + return 0; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_vprintf +/** + * Write to a file. + * + * Writes formatted output to the file referenced by the file descriptor. + * + * @param[in] portLibrary The port library + * @param[in] fd File descriptor to write. + * @param[in] format The format String. + * @param[in] args Variable argument list. + */ +void VMCALL +hyfile_vprintf (struct HyPortLibrary *portLibrary, IDATA fd, + const char *format, va_list args) +{ + char outputBuffer[256]; + + char *allocatedBuffer; + U_32 numberWritten; + va_list copyOfArgs; + + /* Attempt to write output to stack buffer */ + COPY_VA_LIST (copyOfArgs, args); + numberWritten = + portLibrary->str_vprintf (portLibrary, outputBuffer, + sizeof (outputBuffer), format, copyOfArgs); + + /* str_vprintf always null terminates, returns number characters written excluding the null terminator */ + if (sizeof (outputBuffer) > (numberWritten + 1)) + { + /* write out the buffer */ + portLibrary->file_write_text (portLibrary, fd, outputBuffer, + numberWritten); + return; + } + + /* Either the buffer was too small, or it was the exact size. Unfortunately can't tell the difference, + * need to determine the size of the buffer (another call to str_vprintf) then print to the buffer, + * a third call to str_vprintf + */ + COPY_VA_LIST (copyOfArgs, args); + + /* What is size of buffer required ? Does not include the \0 */ + numberWritten = + portLibrary->str_vprintf (portLibrary, NULL, (U_32) (-1), format, + copyOfArgs); + numberWritten += 1; + + allocatedBuffer = + portLibrary->mem_allocate_memory (portLibrary, numberWritten); + if (NULL == allocatedBuffer) + { + portLibrary->nls_printf (portLibrary, HYNLS_ERROR, + HYNLS_PORT_FILE_MEMORY_ALLOCATE_FAILURE); + return; + } + + numberWritten = + portLibrary->str_vprintf (portLibrary, allocatedBuffer, numberWritten, + format, args); + portLibrary->file_write_text (portLibrary, fd, allocatedBuffer, + numberWritten); + portLibrary->mem_free_memory (portLibrary, allocatedBuffer); +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_write +/** + * Write to a file. + * + * Writes up to nbytes from the provided buffer to the file referenced by the file descriptor. + * + * @param[in] portLibrary The port library + * @param[in] fd File descriptor to write. + * @param[in] buf Buffer to be written. + * @param[in] nbytes Size of buffer. + * + * @return Number of bytes written on success, -1 on failure. + * @internal @todo return negative portable return code on failure. + */ +IDATA VMCALL +hyfile_write (struct HyPortLibrary * portLibrary, IDATA fd, const void *buf, + IDATA nbytes) +{ + apr_status_t stat; + apr_size_t result; + apr_file_t* apr_fd; + apr_pool_t* pool = 0; + if (nbytes == 0) { + return 0; + } + result = (apr_size_t) nbytes; + apr_fd = (apr_file_t*)fd; + if (fd == HYPORT_TTY_OUT) { + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + return -1; + } + if ((stat = apr_file_open_stdout(&apr_fd, pool)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + } else if (fd == HYPORT_TTY_ERR) { + if ((stat = apr_pool_create(&pool, NULL)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + return -1; + } + if ((stat = apr_file_open_stderr(&apr_fd,pool)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + apr_pool_destroy(pool); + return -1; + } + } + if ((stat = apr_file_write(apr_fd, buf, &result)) != APR_SUCCESS) { + portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + result = -1; + } + if (pool) { + apr_pool_destroy(pool); + } + return result; +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_printf +/** + * Write to a file. + * + * Writes formatted output to the file referenced by the file descriptor. + * + * @param[in] portLibrary The port library + * @param[in] fd File descriptor to write to + * @param[in] format The format string to be output. + * @param[in] ... arguments for format. + */ +void VMCALL +hyfile_printf (struct HyPortLibrary *portLibrary, IDATA fd, + const char *format, ...) +{ + va_list args; + va_start (args, format); + portLibrary->file_vprintf (portLibrary, fd, format, args); + va_end (args); +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hyfile_set_length +/** + * Set the length of a file to a specified value. + * + * @param[in] portLibrary The port library + * @param[in] fd The file descriptor. + * @param[in] newLength Length to be set + * + * @return 0 on success, negative portable error code on failure + */ +I_32 VMCALL +hyfile_set_length (struct HyPortLibrary *portLibrary, IDATA fd, + I_64 newLength) +{ + apr_status_t stat; + if ((stat = apr_file_trunc((apr_file_t*)fd, newLength)) != APR_SUCCESS) { + return portLibrary->error_set_last_error (portLibrary, APR_TO_OS_ERROR(stat), + findError (stat)); + } + return 0; +} + +#undef CDEV_CURRENT_FUNCTION Index: native-src/shared/port/hyport.c =================================================================== --- native-src/shared/port/hyport.c (revision 418697) +++ native-src/shared/port/hyport.c (working copy) @@ -22,6 +22,7 @@ #include "hyport.h" #include "portpriv.h" #include "hyportpg.h" +#include /** * Initialize the port library. @@ -43,6 +44,8 @@ /* return value of 0 is success */ I_32 rc; + apr_initialize(); + rc = hyport_create_library (portLibrary, version, size); if (rc == 0) Index: native-src/shared/luni/process.c =================================================================== --- native-src/shared/luni/process.c (revision 418697) +++ native-src/shared/luni/process.c (working copy) @@ -18,6 +18,8 @@ #include "jclglob.h" +jfieldID getJavaIoFileDescriptorDescriptorFID (JNIEnv * env); + /** * Create a System Process with the specified * environment and arguments @@ -183,6 +185,33 @@ termProc ((IDATA) pHandle); } +static void +proc_close (JNIEnv * env, jobject recv, jfieldID fdFID) +{ + jobject fd; + jfieldID descriptorFID; + IDATA descriptor; + PORT_ACCESS_FROM_ENV (env); + + descriptorFID = getJavaIoFileDescriptorDescriptorFID (env); + if (NULL == descriptorFID) + { + return; + } + + /* fetch the fd field from the object */ + fd = (*env)->GetObjectField (env, recv, fdFID); + + /* dereference the C pointer from the wrapper object */ + descriptor = (IDATA) getJavaIoFileDescriptorContentsAsPointer (env, fd); + + closeProc(descriptor); + + setJavaIoFileDescriptorContentsAsPointer (env, fd, (void *) -1); + return; +} + + /* Close the input stream*/ void JNICALL Java_org_apache_harmony_luni_internal_process_ProcessInputStream_closeImpl (JNIEnv * env, @@ -190,9 +219,9 @@ { PORT_ACCESS_FROM_ENV (env); - new_ioh_close (env, recv, - JCL_CACHE_GET (env, - FID_org_apache_harmony_luni_internal_process_ProcessInputStream_fd)); + proc_close (env, recv, + JCL_CACHE_GET (env, + FID_org_apache_harmony_luni_internal_process_ProcessInputStream_fd)); } void JNICALL @@ -200,9 +229,9 @@ jobject recv) { PORT_ACCESS_FROM_ENV (env); - new_ioh_close (env, recv, - JCL_CACHE_GET (env, - FID_org_apache_harmony_luni_internal_process_ProcessOutputStream_fd)); + proc_close (env, recv, + JCL_CACHE_GET (env, + FID_org_apache_harmony_luni_internal_process_ProcessOutputStream_fd)); } /* Read nbytes from the receiver */ @@ -213,8 +242,8 @@ jlong handle) { - return (jint) ioh_readbytesImpl (env, recv, buffer, offset, nbytes, - (IDATA) handle); + return (jint) readbytesProc (env, recv, buffer, offset, nbytes, + (IDATA) handle); } @@ -249,7 +278,7 @@ jlong handle) { - ioh_writebytesImpl (env, recv, buffer, offset, nbytes, (IDATA) handle); + writebytesProc (env, recv, buffer, offset, nbytes, (IDATA) handle); } Index: native-src/shared/common/iohelp.c =================================================================== --- native-src/shared/common/iohelp.c (revision 418697) +++ native-src/shared/common/iohelp.c (working copy) @@ -108,224 +108,9 @@ (*env)->ThrowNew(env, exceptionClass, ""); } -/** - * This will write count bytes from buffer starting at offset - */ -void -ioh_writebytesImpl (JNIEnv * env, jobject recv, jbyteArray buffer, - jint offset, jint count, IDATA descriptor) -{ - I_32 result = 0; - jbyte *buf; - PORT_ACCESS_FROM_ENV (env); - jsize len; - char *errorMessage = NULL; -/* TODO: ARRAY PINNING */ -#define INTERNAL_MAX 512 - jbyte internalBuffer[INTERNAL_MAX]; - if (buffer == NULL) - { - throwNPException (env, "buffer is null"); - return; - } - - len = (*env)->GetArrayLength (env, buffer); - - /** - * If offset is negative, or count is negative, or offset+count is greater - * than the length of the array b, then an IndexOutOfBoundsException is thrown. - * Must test offset > len, or len - offset < count to avoid int overflow caused - * by offset + count - */ - if (offset < 0 || count < 0 || offset > len || (len - offset) < count) - { - throwIndexOutOfBoundsException (env); - return; - } - - /* If len or count is zero, just return 0 */ - if (len == 0 || count == 0) - return; - - if (descriptor == -1) - { - throwJavaIoIOExceptionClosed (env); - return; - } - if (count > INTERNAL_MAX) - { - buf = jclmem_allocate_memory (env, count); - } - else - { - buf = internalBuffer; - } - - if (buf == NULL) - { - throwNewOutOfMemoryError (env, ""); - return; - } - ((*env)->GetByteArrayRegion (env, buffer, offset, count, buf)); - - result = hyfile_write (descriptor, buf, count); - - /** - * if there is an error, find the error message before calling free in case - * hymem_free_memory changes the error code - */ - if (result < 0) - errorMessage = ioLookupErrorString (env, result); - - if (buf != internalBuffer) - { - jclmem_free_memory (env, buf); - } -#undef INTERNAL_MAX - - if (result < 0) - throwJavaIoIOException (env, errorMessage); -} - /** - * This will write one byte - */ -void -ioh_writecharImpl (JNIEnv * env, jobject recv, jint c, IDATA descriptor) -{ - I_32 result = 0; - char buf[1]; - PORT_ACCESS_FROM_ENV (env); - - if (descriptor == -1) - { - throwJavaIoIOExceptionClosed (env); - return; - } - - buf[0] = (char) c; - - result = hyfile_write (descriptor, buf, 1); - - if (result < 0) - throwJavaIoIOException (env, ioLookupErrorString (env, result)); -} - -/** - * This will read a single character from the descriptor - */ -jint -ioh_readcharImpl (JNIEnv * env, jobject recv, IDATA descriptor) -{ - I_32 result; - char buf[1]; - PORT_ACCESS_FROM_ENV (env); - - if (descriptor == -1) - { - throwJavaIoIOExceptionClosed (env); - return 0; - } - - if (descriptor == 0) - { - result = hytty_get_chars (buf, 1); - } - else - { - result = hyfile_read (descriptor, buf, 1); - } - - if (result <= 0) - return -1; - - return (jint) buf[0] & 0xFF; -} - -/** - * This will read a up to count bytes into buffer starting at offset - */ -jint -ioh_readbytesImpl (JNIEnv * env, jobject recv, jbyteArray buffer, jint offset, - jint count, IDATA descriptor) -{ - I_32 result; - jsize len; - jbyte *buf; - -/* TODO: ARRAY PINNING */ -#define INTERNAL_MAX 2048 - jbyte internalBuffer[INTERNAL_MAX]; - - PORT_ACCESS_FROM_ENV (env); - - if (buffer == NULL) - { - throwNPException (env, "buffer is null"); - return 0; - } - - len = (*env)->GetArrayLength (env, buffer); - /** - * Throw IndexOutOfBoundsException according to spec. - * Must test offset > len, or len - offset < count to avoid - * int overflow caused by offset + count - */ - if (offset < 0 || count < 0 || offset > len || (len - offset) < count) - { - throwIndexOutOfBoundsException (env); - return 0; - } - /* If len is 0, simply return 0 (even if it is closed) */ - if (len == 0 || count == 0) - return 0; - - if (descriptor == -1) - { - throwJavaIoIOExceptionClosed (env); - return 0; - } - if (len >= INTERNAL_MAX) - { - buf = jclmem_allocate_memory (env, len); - } - else - { - buf = internalBuffer; - } - - if (buf == NULL) - { - throwNewOutOfMemoryError (env, ""); - return 0; - } - /* Must FREE buffer before returning */ - - if (descriptor == 0) - { - /* hytty_get_chars() returns zero on EOF */ - if ((result = hytty_get_chars (buf, count)) == 0) - result = -1; - } - else - { - result = hyfile_read (descriptor, buf, count); - } - if (result > 0) - (*env)->SetByteArrayRegion (env, buffer, offset, result, buf); - - if (buf != internalBuffer) - { - jclmem_free_memory (env, buf); - } -#undef INTERNAL_MAX - - return result; -} - -/** * Throw java.lang.NullPointerException with the message provided * Note: This is not named throwNullPointerException because it conflicts * with a VM function of that same name and this causes problems on @@ -342,75 +127,8 @@ (*env)->ThrowNew(env, exceptionClass, message); } -/** - * This will return the number of chars left in the file - */ -jint -new_ioh_available (JNIEnv * env, jobject recv, jfieldID fdFID) -{ - jobject fd; - I_64 currentPosition, endOfFile; - IDATA descriptor; - PORT_ACCESS_FROM_ENV (env); - /* fetch the fd field from the object */ - fd = (*env)->GetObjectField (env, recv, fdFID); - - /* dereference the C pointer from the wrapper object */ - descriptor = (IDATA) getJavaIoFileDescriptorContentsAsPointer (env, fd); - if (descriptor == -1) - { - throwJavaIoIOExceptionClosed (env); - return -1; - } - /** - * If the descriptor represents StdIn, call the hytty port library. - */ - if (descriptor == 0) - { - return hytty_available (); - } - currentPosition = hyfile_seek (descriptor, 0, HySeekCur); - endOfFile = hyfile_seek (descriptor, 0, HySeekEnd); - hyfile_seek (descriptor, currentPosition, HySeekSet); - return (jint) (endOfFile - currentPosition); -} - /** - * This will close a file descriptor - */ -void -new_ioh_close (JNIEnv * env, jobject recv, jfieldID fdFID) -{ - jobject fd; - jfieldID descriptorFID; - IDATA descriptor; - PORT_ACCESS_FROM_ENV (env); - - descriptorFID = getJavaIoFileDescriptorDescriptorFID (env); - if (NULL == descriptorFID) - { - return; - } - - /* fetch the fd field from the object */ - fd = (*env)->GetObjectField (env, recv, fdFID); - - /* dereference the C pointer from the wrapper object */ - descriptor = (IDATA) getJavaIoFileDescriptorContentsAsPointer (env, fd); - - /* Check for closed file, in, out, and err */ - if (descriptor >= -1 && descriptor <= 2) - { - return; - } - - hyfile_close (descriptor); - setJavaIoFileDescriptorContentsAsPointer (env, fd, (void *) -1); - return; -} - -/** * This will retrieve the 'descriptor' field value from a java.io.FileDescriptor */ void * Index: native-src/win.IA32/apr/makefile =================================================================== --- native-src/win.IA32/apr/makefile (revision 0) +++ native-src/win.IA32/apr/makefile (revision 0) @@ -0,0 +1,40 @@ +# Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +!include <$(HY_HDK)\build\make\defines.mak> + +LIBNAME=$(LIBPATH)hyapr.lib + +HYCFLAGS = -MD -D_DLL -D_MT -DWIN32 -D_WIN32 -Oityb1 -W3 -DAPR_DECLARE_EXPORT /I.\apr-1.2.7\include /I.\apr-1.2.7\include\arch\win32 \ + /I.\apr-1.2.7\include\arch\unix /I.\apr-1.2.7\include\arch + +all: + $(cc) $(cflags) $(HYCFLAGS) apr-1.2.7\user\win32\*.c apr-1.2.7\time\win32\*.c apr-1.2.7\threadproc\win32\*.c \ + apr-1.2.7\tables\*.c apr-1.2.7\strings\*.c apr-1.2.7\shmem\win32\*.c \ + apr-1.2.7\random\unix\*.c apr-1.2.7\passwd\*.c apr-1.2.7\network_io\win32\*.c \ + apr-1.2.7\network_io\unix\multicast.c apr-1.2.7\network_io\unix\sockaddr.c \ + apr-1.2.7\network_io\unix\inet_pton.c apr-1.2.7\network_io\unix\inet_ntop.c \ + apr-1.2.7\mmap\win32\*.c apr-1.2.7\mmap\unix\common.c apr-1.2.7\misc\unix\version.c \ + apr-1.2.7\misc\unix\otherchild.c apr-1.2.7\misc\unix\getopt.c apr-1.2.7\misc\unix\errorcodes.c \ + apr-1.2.7\misc\win32\*.c apr-1.2.7\memory\unix\apr_pools.c apr-1.2.7\locks\win32\*.c \ + apr-1.2.7\file_io\win32\*.c apr-1.2.7\file_io\unix\fileacc.c apr-1.2.7\dso\win32\*.c \ + apr-1.2.7\file_io\unix\copy.c apr-1.2.7\file_io\unix\filepath_util.c apr-1.2.7\file_io\unix\tempdir.c \ + apr-1.2.7\file_io\unix\mktemp.c apr-1.2.7\file_io\unix\fullrw.c apr-1.2.7\atomic\win32\*.c \ + apr-1.2.7\support\unix\waitio.c apr-1.2.7\poll\unix\select.c + + $(implib) /NOLOGO -out:$(LIBNAME) *.obj + +clean: + -del *.obj dist\*.obj + -del $(LIBNAME) Index: native-src/win.IA32/port/hyfile.c =================================================================== --- native-src/win.IA32/port/hyfile.c (revision 418697) +++ native-src/win.IA32/port/hyfile.c (working copy) @@ -1,941 +0,0 @@ -/* Copyright 1991, 2006 The Apache Software Foundation or its licensors, as applicable - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define CDEV_CURRENT_FUNCTION _comment_ -/** - * @file - * @ingroup Port - * @brief file - */ -#undef CDEV_CURRENT_FUNCTION - -#include -#include -#include -#include "hyport.h" -#include "portpriv.h" -#include "hystdarg.h" -#include "portnls.h" -#include "ut_hyprt.h" - -#define CDEV_CURRENT_FUNCTION _prototypes_private - -static I_32 findError (I_32 errorCode); - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION findError -/** - * @internal - * Determines the proper portable error code to return given a native error code - * - * @param[in] errorCode The error code reported by the OS - * - * @return the (negative) portable error code - */ -static I_32 -findError (I_32 errorCode) -{ - switch (errorCode) - { - case ERROR_FILENAME_EXCED_RANGE: - return HYPORT_ERROR_FILE_NAMETOOLONG; - case ERROR_ACCESS_DENIED: - return HYPORT_ERROR_FILE_NOPERMISSION; - case ERROR_FILE_NOT_FOUND: - return HYPORT_ERROR_FILE_NOTFOUND; - case ERROR_DISK_FULL: - return HYPORT_ERROR_FILE_DISKFULL; - case ERROR_FILE_EXISTS: - case ERROR_ALREADY_EXISTS: - return HYPORT_ERROR_FILE_EXIST; - case ERROR_NOT_ENOUGH_MEMORY: - return HYPORT_ERROR_FILE_SYSTEMFULL; - default: - return HYPORT_ERROR_FILE_OPFAILED; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_attr -/** - * Determine whether path is a file or directory. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * - * @return EslsFile if a file, EslsDir if a directory, negative portable error code on failure. - */ -I_32 VMCALL -hyfile_attr (struct HyPortLibrary * portLibrary, const char *path) -{ - DWORD result; - result = GetFileAttributes ((LPCTSTR) path); - if (result == 0xFFFFFFFF) - { - result = GetLastError (); - return portLibrary->error_set_last_error (portLibrary, result, - findError (result)); - } - - if (result & FILE_ATTRIBUTE_DIRECTORY) - { - return HyIsDir; - } - - /* otherwise assume it's a normal file */ - return HyIsFile; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_close -/** - * Closes a file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_close (struct HyPortLibrary * portLibrary, IDATA fd) -{ - if (CloseHandle ((HANDLE) fd)) - { - return 0; - } - else - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_error_message -/** - * Return an error message describing the last OS error that occurred. The last - * error returned is not thread safe, it may not be related to the operation that - * failed for this thread. - * - * @param[in] portLibrary The port library - * - * @return error message describing the last OS error, may return NULL. - * - * @internal - * @note This function gets the last error code from the OS and then returns - * the corresponding string. It is here as a helper function for JCL. Once hyerror - * is integrated into the port library this function should probably disappear. - */ -const char *VMCALL -hyfile_error_message (struct HyPortLibrary *portLibrary) -{ - return portLibrary->error_last_error_message (portLibrary); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_findclose -/** - * Close the handle returned from @ref hyfile_findfirst. - * - * @param[in] portLibrary The port library - * @param[in] findhandle Handle returned from @ref hyfile_findfirst. - */ -void VMCALL -hyfile_findclose (struct HyPortLibrary *portLibrary, UDATA findhandle) -{ - if (0 == FindClose ((HANDLE) findhandle)) - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_findfirst -/** - * Find the first occurrence of a file identified by path. Answers a handle - * to be used in subsequent calls to @ref hyfile_findnext and @ref hyfile_findclose. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * @param[out] resultbuf filename and path matching path. - * - * @return valid handle on success, -1 on failure. - */ -UDATA VMCALL -hyfile_findfirst (struct HyPortLibrary *portLibrary, const char *path, - char *resultbuf) -{ - WIN32_FIND_DATA lpFindFileData; - char newPath[HyMaxPath]; - HANDLE result; - - strcpy (newPath, path); - strcat (newPath, "*"); - - result = FindFirstFile ((LPCTSTR) newPath, &lpFindFileData); - if (result == INVALID_HANDLE_VALUE) - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return (UDATA) - 1; - } - - lstrcpy (resultbuf, lpFindFileData.cFileName); - return (UDATA) result; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_findnext -/** - * Find the next filename and path matching a given handle. - * - * @param[in] portLibrary The port library - * @param[in] findhandle handle returned from @ref hyfile_findfirst. - * @param[out] resultbuf next filename and path matching findhandle. - * - * @return 0 on success, -1 on failure or if no matching entries. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_findnext (struct HyPortLibrary * portLibrary, UDATA findhandle, - char *resultbuf) -{ - WIN32_FIND_DATA lpFindFileData; - if (!FindNextFile ((HANDLE) findhandle, &lpFindFileData)) - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } - - lstrcpy (resultbuf, lpFindFileData.cFileName); - return 0; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_lastmod -/** - * Return the last modification time of the file path in seconds. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * - * @return last modification time on success, -1 on failure. - */ -I_64 VMCALL -hyfile_lastmod (struct HyPortLibrary * portLibrary, const char *path) -{ - WIN32_FIND_DATA myStat; - HANDLE newHandle; - I_64 result, tempResult; - I_32 error; - - newHandle = FindFirstFile (path, &myStat); - if (newHandle == INVALID_HANDLE_VALUE) - { - error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } - - /* - * Search MSDN for 'Converting a time_t Value to a File Time' for following implementation. - */ - tempResult = - ((I_64) myStat.ftLastWriteTime. - dwHighDateTime << (I_64) 32) | (I_64) myStat.ftLastWriteTime. - dwLowDateTime; - - result = (tempResult - 116444736000000000) / 10000000; - - if (0 == FindClose (newHandle)) - { - error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, findError (error)); /* continue */ - } - - return result; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_length -/** - * Answer the length in bytes of the file. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * - * @return Length in bytes of the file on success, negative portable error code on failure - */ -I_64 VMCALL -hyfile_length (struct HyPortLibrary * portLibrary, const char *path) -{ - WIN32_FIND_DATA myStat; - HANDLE newHandle; - I_64 result; - I_32 error; - - newHandle = FindFirstFile (path, &myStat); - if (newHandle == INVALID_HANDLE_VALUE) - { - error = GetLastError (); - return portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - } - - result = ((I_64) myStat.nFileSizeHigh) << 32; - result += (I_64) myStat.nFileSizeLow; - if (0 == FindClose (newHandle)) - { - error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, findError (error)); /* continue */ - } - - return result; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_mkdir -/** - * Create a directory. - * - * @param[in] portLibrary The port library - * @param[in] path Directory to be created. - * - * @return 0 on success, -1 on failure. - * @note Assumes all components of path up to the last directory already exist. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_mkdir (struct HyPortLibrary * portLibrary, const char *path) -{ - if (CreateDirectory (path, 0)) - { - return 0; - } - else - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_move -/** - * Move the file pathExist to a new name pathNew. - * - * @param[in] portLibrary The port library - * @param[in] pathExist The existing file name. - * @param[in] pathNew The new file name. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_move (struct HyPortLibrary * portLibrary, const char *pathExist, - const char *pathNew) -{ - if (MoveFile (pathExist, pathNew)) - { - return 0; - } - else - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_open -/** - * Convert a pathname into a file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] path Name of the file to be opened. - * @param[in] flags Portable file read/write attributes. - * @param[in] mode Platform file permissions. - * - * @return The file descriptor of the newly opened file, -1 on failure. - */ -IDATA VMCALL -hyfile_open (struct HyPortLibrary * portLibrary, const char *path, I_32 flags, - I_32 mode) -{ - DWORD accessMode, shareMode, createMode, flagsAndAttributes; - HANDLE aHandle; - I_32 error; - - Trc_PRT_file_open_Entry (path, flags, mode); - - accessMode = 0; - if (flags & HyOpenRead) - { - accessMode |= GENERIC_READ; - } - if (flags & HyOpenWrite) - { - accessMode |= GENERIC_WRITE; - } - - shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; - - if ((flags & HyOpenCreate) == HyOpenCreate) - { - createMode = OPEN_ALWAYS; - } - else if ((flags & HyOpenCreateNew) == HyOpenCreateNew) - { - createMode = CREATE_NEW; - } - else - { - createMode = OPEN_EXISTING; - } - - flagsAndAttributes = FILE_ATTRIBUTE_NORMAL; - if (flags & HyOpenSync) { - flagsAndAttributes |= FILE_FLAG_WRITE_THROUGH; - } - - aHandle = - CreateFile (path, accessMode, shareMode, NULL, createMode, - flagsAndAttributes, NULL); - if (aHandle == INVALID_HANDLE_VALUE) - { - error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - Trc_PRT_file_open_Exit2 (error, findError (error)); - return -1; - } - - if ((flags & HyOpenTruncate) == HyOpenTruncate) - { - if (0 == CloseHandle (aHandle)) - { - error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, findError (error)); /* continue */ - } - - aHandle = - CreateFile (path, accessMode, shareMode, NULL, TRUNCATE_EXISTING, - flagsAndAttributes, NULL); - if (aHandle == INVALID_HANDLE_VALUE) - { - error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - Trc_PRT_file_open_Exit3 (error, findError (error)); - return -1; - } - } - - if (flags & HyOpenAppend) - { - portLibrary->file_seek (portLibrary, (IDATA) aHandle, 0, HySeekEnd); - } - - Trc_PRT_file_open_Exit (aHandle); - return ((IDATA) aHandle); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_read -/** - * Read bytes from a file descriptor into a user provided buffer. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * @param[in,out] buf Buffer to read into. - * @param[in] nbytes Size of buffer. - * - * @return The number of bytes read, or -1 on failure. - */ -IDATA VMCALL -hyfile_read (struct HyPortLibrary * portLibrary, IDATA fd, void *buf, - IDATA nbytes) -{ - DWORD bytesRead; - - if (nbytes == 0) - { - return 0; - } - - if (!ReadFile ((HANDLE) fd, buf, nbytes, &bytesRead, NULL)) - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } - if (bytesRead == 0) - { - return -1; - } - - return bytesRead; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_seek -/** - * Repositions the offset of the file descriptor to a given offset as per directive whence. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * @param[in] offset The offset in the file to position to. - * @param[in] whence Portable constant describing how to apply the offset. - * - * @return The resulting offset on success, -1 on failure. - * @note whence is one of HySeekSet (seek from beginning of file), - * HySeekCur (seek from current file pointer) or HySeekEnd (seek backwards from - * end of file). - * @internal @note seek operations return -1 on failure. Negative offsets - * can be returned when seeking beyond end of file. - */ -I_64 VMCALL -hyfile_seek (struct HyPortLibrary * portLibrary, IDATA fd, I_64 offset, - I_32 whence) -{ - DWORD moveMethod, moveResult; - DWORD lowerOffset, upperOffset; - I_64 result; - I_32 error; - - lowerOffset = (DWORD) (offset & 0xFFFFFFFF); - upperOffset = (DWORD) ((offset >> 32) & 0x7FFFFFFF); - - if ((whence < HySeekSet) || (whence > HySeekEnd)) - { - return -1; - } - if (whence == HySeekSet) - { - moveMethod = FILE_BEGIN; - } - if (whence == HySeekEnd) - { - moveMethod = FILE_END; - } - if (whence == HySeekCur) - { - moveMethod = FILE_CURRENT; - } - moveResult = - SetFilePointer ((HANDLE) fd, lowerOffset, &upperOffset, moveMethod); - if (-1 == moveResult) - { - error = GetLastError (); - if (error != NO_ERROR) - { - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } - } - - result = (I_64) upperOffset << 32; - result |= moveResult; - - return result; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_shutdown -/** - * PortLibrary shutdown. - * - * This function is called during shutdown of the portLibrary. Any resources that were created by @ref hyfile_startup - * should be destroyed here. - * - * @param[in] portLibrary The port library - * - * @note Most implementations will be empty. - */ -void VMCALL -hyfile_shutdown (struct HyPortLibrary *portLibrary) -{ - /* empty */ -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_startup -/** - * PortLibrary startup. - * - * This function is called during startup of the portLibrary. Any resources that are required for - * the file operations may be created here. All resources created here should be destroyed - * in @ref hyfile_shutdown. - * - * @param[in] portLibrary The port library - * - * @return 0 on success, negative error code on failure. Error code values returned are - * \arg HYPORT_ERROR_STARTUP_FILE - * - * @note Most implementations will simply return success. - */ -I_32 VMCALL -hyfile_startup (struct HyPortLibrary *portLibrary) -{ - return 0; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_sync -/** - * Synchronize a file's state with the state on disk. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_sync (struct HyPortLibrary * portLibrary, IDATA fd) -{ - if (FlushFileBuffers ((HANDLE) fd)) - { - return 0; - } - else - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_unlink -/** - * Remove a file from the file system. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name to remove. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_unlink (struct HyPortLibrary * portLibrary, const char *path) -{ - /* should be able to delete read-only dirs, so we set the file attribute back to normal */ - if (0 == SetFileAttributes (path, FILE_ATTRIBUTE_NORMAL)) - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, findError (error)); /* continue */ - } - - if (DeleteFile (path)) - { - return 0; - } - else - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_unlinkdir -/** - * Remove the trailing directory of the path. If the path is a symbolic link to a directory, remove - * the symbolic link. - * - * @param[in] portLibrary The port library - * @param[in] path directory name being removed. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure.. - */ -I_32 VMCALL -hyfile_unlinkdir (struct HyPortLibrary * portLibrary, const char *path) -{ - /* should be able to delete read-only dirs, so we set the file attribute back to normal */ - if (0 == SetFileAttributes (path, FILE_ATTRIBUTE_NORMAL)) - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, findError (error)); /* continue */ - } - - if (RemoveDirectory (path)) - { - return 0; - } - else - { - I_32 error = GetLastError (); - portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - return -1; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_vprintf -/** - * Write to a file. - * - * Writes formatted output to the file referenced by the file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd File descriptor to write. - * @param[in] format The format String. - * @param[in] args Variable argument list. - */ -void VMCALL -hyfile_vprintf (struct HyPortLibrary *portLibrary, IDATA fd, - const char *format, va_list args) -{ - char outputBuffer[256]; - - char *allocatedBuffer; - U_32 numberWritten; - va_list copyOfArgs; - - /* Attempt to write output to stack buffer */ - COPY_VA_LIST (copyOfArgs, args); - numberWritten = - portLibrary->str_vprintf (portLibrary, outputBuffer, - sizeof (outputBuffer), format, copyOfArgs); - - /* str_vprintf always null terminates, returns number characters written excluding the null terminator */ - if (sizeof (outputBuffer) > (numberWritten + 1)) - { - /* write out the buffer */ - portLibrary->file_write_text (portLibrary, fd, outputBuffer, - numberWritten); - return; - } - - /* Either the buffer was too small, or it was the exact size. Unfortunately can't tell the difference, - * need to determine the size of the buffer (another call to str_vprintf) then print to the buffer, - * a third call to str_vprintf - */ - COPY_VA_LIST (copyOfArgs, args); - - /* What is size of buffer required ? Does not include the \0 */ - numberWritten = - portLibrary->str_vprintf (portLibrary, NULL, (U_32) (-1), format, - copyOfArgs); - numberWritten += 1; - - allocatedBuffer = - portLibrary->mem_allocate_memory (portLibrary, numberWritten); - if (NULL == allocatedBuffer) - { - portLibrary->nls_printf (portLibrary, HYNLS_ERROR, - HYNLS_PORT_FILE_MEMORY_ALLOCATE_FAILURE); - return; - } - - numberWritten = - portLibrary->str_vprintf (portLibrary, allocatedBuffer, numberWritten, - format, args); - portLibrary->file_write_text (portLibrary, fd, allocatedBuffer, - numberWritten); - portLibrary->mem_free_memory (portLibrary, allocatedBuffer); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_write -/** - * Write to a file. - * - * Writes up to nbytes from the provided buffer to the file referenced by the file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd File descriptor to write. - * @param[in] buf Buffer to be written. - * @param[in] nbytes Size of buffer. - * - * @return Number of bytes written on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -IDATA VMCALL -hyfile_write (struct HyPortLibrary * portLibrary, IDATA fd, void *buf, - IDATA nbytes) -{ - DWORD nCharsWritten; - IDATA toWrite, offset = 0; - I_32 errorCode; - HANDLE handle; - - if (fd == HYPORT_TTY_OUT) - { - handle = PPG_tty_consoleOutputHd; - } - else if (fd == HYPORT_TTY_ERR) - { - handle = PPG_tty_consoleErrorHd; - } - else - { - handle = (HANDLE) fd; - } - - toWrite = nbytes; - while (nbytes > 0) - { - if (toWrite > nbytes) - { - toWrite = nbytes; - } - if (!WriteFile - (handle, (char *) buf + offset, toWrite, &nCharsWritten, NULL)) - { - errorCode = GetLastError (); - if (errorCode == ERROR_NOT_ENOUGH_MEMORY) - { - /* Use 48K chunks to get around out of memory problem */ - if (toWrite > (48 * 1024)) - { - toWrite = 48 * 1024; - } - else - { - toWrite /= 2; - } - /* If we can't write 128 bytes, just return */ - if (toWrite >= 128) - { - continue; - } - } - return portLibrary->error_set_last_error (portLibrary, errorCode, - findError (errorCode)); - } - offset += nCharsWritten; - nbytes -= nCharsWritten; - } - - return (IDATA) offset; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_printf -/** - * Write to a file. - * - * Writes formatted output to the file referenced by the file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd File descriptor to write to - * @param[in] format The format string to be output. - * @param[in] ... arguments for format. - */ -void VMCALL -hyfile_printf (struct HyPortLibrary *portLibrary, IDATA fd, - const char *format, ...) -{ - va_list args; - - va_start (args, format); - portLibrary->file_vprintf (portLibrary, fd, format, args); - va_end (args); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_set_length -/** - * Set the length of a file to a specified value. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * @param[in] newLength Length to be set - * - * @return 0 on success, negative portable error code on failure - */ -I_32 VMCALL -hyfile_set_length (struct HyPortLibrary *portLibrary, IDATA fd, - I_64 newLength) -{ - I_32 result, error; - I_32 lowValue, highValue; - - lowValue = (I_32) (newLength & 0xFFFFFFFF); - highValue = (I_32) ((newLength >> 32) & 0x7FFFFFFF); - - result = SetFilePointer ((HANDLE) fd, lowValue, &highValue, FILE_BEGIN); - error = GetLastError (); - if ((result == -1) && (error != NO_ERROR)) - { - return portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - } - else - { - if (0 == SetEndOfFile ((HANDLE) fd)) - { - error = GetLastError (); - return portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - } - /* Put pointer back to where it started */ - result = SetFilePointer ((HANDLE) fd, result, &highValue, FILE_BEGIN); - error = GetLastError (); - if ((result == -1) && (error != NO_ERROR)) - { - return portLibrary->error_set_last_error (portLibrary, error, - findError (error)); - } - } - return 0; -} - -#undef CDEV_CURRENT_FUNCTION Index: native-src/win.IA32/port/makefile =================================================================== --- native-src/win.IA32/port/makefile (revision 418697) +++ native-src/win.IA32/port/makefile (working copy) @@ -23,12 +23,15 @@ DLLNAME=..\$(LIBBASE).dll LIBNAME=$(LIBPATH)$(LIBBASE).lib -HYCFLAGS = $(HYCFLAGS) -DHYPORT_LIBRARY_DEFINE /I$(SHAREDSUB) /I..\..\..\modules\luni\src\main\native\include\shared /I..\..\..\modules\luni\src\main\native\include\windows +HYCFLAGS = $(HYCFLAGS) -DHYPORT_LIBRARY_DEFINE -DAPR_DECLARE_EXPORT /I$(SHAREDSUB) /I..\..\..\modules\luni\src\main\native\include\shared /I..\..\..\modules\luni\src\main\native\include\windows \ + /I..\apr\apr-1.2.7\include /I..\apr\apr-1.2.7\include\arch\win32 \ + /I..\apr\apr-1.2.7\include\arch\unix /I..\apr\apr-1.2.7\include\arch + HYLDFLAGS = $(HYLDFLAGS) -def:$(LIBBASE).def BUILDFILES = \ $(SHAREDSUB)port_copyright.obj hycpu.obj $(SHAREDSUB)hyerror.obj \ - hyerrorhelpers.obj $(SHAREDSUB)hyexit.obj hyfile.obj hyfiletext.obj \ + hyerrorhelpers.obj $(SHAREDSUB)hyexit.obj $(SHAREDSUB)hyfile.obj hyfiletext.obj \ $(SHAREDSUB)hygp.obj hyipcmutex.obj hymem.obj hymmap.obj \ $(SHAREDSUB)hynls.obj hynlshelpers.obj hyosdump.obj $(SHAREDSUB)hyport.obj \ $(SHAREDSUB)hyportcontrol.obj hyportptb.obj hyshmem.obj hyshsem.obj \ @@ -42,7 +45,7 @@ Delayimp.lib -delayload:shell32.dll -delayload:Iphlpapi.dll \ ws2_32.lib Iphlpapi.lib shell32.lib -MDLLIBFILES = $(LIBPATH)hythr.lib $(LIBPATH)hycommon.lib $(LIBPATH)hysig.lib +MDLLIBFILES = $(LIBPATH)hythr.lib $(LIBPATH)hycommon.lib $(LIBPATH)hysig.lib $(LIBPATH)hyapr.lib DLLBASE=0x11100000 COMMENT=/comment:"Platform port library. (c) Copyright 1993, 2005 The Apache Software Foundation or its licensors, as applicable." Index: native-src/win.IA32/luni/helpers.c =================================================================== --- native-src/win.IA32/luni/helpers.c (revision 418697) +++ native-src/win.IA32/luni/helpers.c (working copy) @@ -114,7 +114,7 @@ PORT_ACCESS_FROM_ENV (env); FILETIME fileTime; I_32 result, dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; - IDATA hFile; + HANDLE hFile; I_64 tempLongLong; char *unicodePath; @@ -132,12 +132,12 @@ if (result & FILE_ATTRIBUTE_DIRECTORY) dwFlagsAndAttributes = FILE_FLAG_BACKUP_SEMANTICS; - hFile = (IDATA) CreateFile (unicodePath, + hFile = CreateFile (unicodePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL); - if ((IDATA) hFile == (IDATA) INVALID_HANDLE_VALUE) + if (hFile == INVALID_HANDLE_VALUE) { return 0; } @@ -147,10 +147,9 @@ fileTime.dwHighDateTime = (I_32) (tempLongLong >> 32); fileTime.dwLowDateTime = (I_32) tempLongLong; result = - SetFileTime ((HANDLE) hFile, (LPFILETIME) NULL, (LPFILETIME) NULL, + SetFileTime (hFile, (LPFILETIME) NULL, (LPFILETIME) NULL, &fileTime); - hyfile_close (hFile); - + CloseHandle (hFile); return result; } @@ -183,15 +182,11 @@ I_32 setPlatformFileLength (JNIEnv * env, IDATA descriptor, jlong newLength) { - I_32 result; - I_32 lowValue, highValue; - lowValue = (I_32) newLength; - highValue = (I_32) (newLength >> 32); - result = - SetFilePointer ((HANDLE) descriptor, lowValue, &highValue, FILE_BEGIN); - if (result == INVALID_FILE_SIZE && (GetLastError ()) != NO_ERROR) - return 0; - return SetEndOfFile ((HANDLE) descriptor); + PORT_ACCESS_FROM_ENV (env); + if (hyfile_set_length (descriptor, newLength) != 0) { + return 0; + } + return 1; } jbyteArray Index: native-src/win.IA32/luni/procimpl.c =================================================================== --- native-src/win.IA32/luni/procimpl.c (revision 418697) +++ native-src/win.IA32/luni/procimpl.c (working copy) @@ -18,7 +18,156 @@ #include "vmi.h" #include "procimpl.h" #include "jclglob.h" +#include "iohelp.h" +static I_32 +findError (I_32 errorCode) +{ + switch (errorCode) + { + case ERROR_FILENAME_EXCED_RANGE: + return HYPORT_ERROR_FILE_NAMETOOLONG; + case ERROR_ACCESS_DENIED: + return HYPORT_ERROR_FILE_NOPERMISSION; + case ERROR_FILE_NOT_FOUND: + return HYPORT_ERROR_FILE_NOTFOUND; + case ERROR_DISK_FULL: + return HYPORT_ERROR_FILE_DISKFULL; + case ERROR_FILE_EXISTS: + case ERROR_ALREADY_EXISTS: + return HYPORT_ERROR_FILE_EXIST; + case ERROR_NOT_ENOUGH_MEMORY: + return HYPORT_ERROR_FILE_SYSTEMFULL; + default: + return HYPORT_ERROR_FILE_OPFAILED; + } +} + +static IDATA +file_write (JNIEnv * env, IDATA fd, void *buf, + IDATA nbytes) +{ + DWORD nCharsWritten; + IDATA toWrite, offset = 0; + I_32 errorCode; + HANDLE handle; + PORT_ACCESS_FROM_ENV (env); + + handle = (HANDLE) fd; + + toWrite = nbytes; + while (nbytes > 0) + { + if (toWrite > nbytes) + { + toWrite = nbytes; + } + if (!WriteFile + (handle, (char *) buf + offset, toWrite, &nCharsWritten, NULL)) + { + errorCode = GetLastError (); + if (errorCode == ERROR_NOT_ENOUGH_MEMORY) + { + /* Use 48K chunks to get around out of memory problem */ + if (toWrite > (48 * 1024)) + { + toWrite = 48 * 1024; + } + else + { + toWrite /= 2; + } + /* If we can't write 128 bytes, just return */ + if (toWrite >= 128) + { + continue; + } + } + return hyerror_set_last_error (errorCode, + findError (errorCode)); + } + offset += nCharsWritten; + nbytes -= nCharsWritten; + } + + return (IDATA) offset; +} + +static IDATA +file_read (JNIEnv * env, IDATA fd, void *buf, + IDATA nbytes) +{ + DWORD bytesRead; + PORT_ACCESS_FROM_ENV (env); + + if (nbytes == 0) + { + return 0; + } + + if (!ReadFile ((HANDLE) fd, buf, nbytes, &bytesRead, NULL)) + { + I_32 error = GetLastError (); + hyerror_set_last_error (error, + findError (error)); + return -1; + } + if (bytesRead == 0) + { + return -1; + } + + return bytesRead; +} + +static I_64 +file_seek (JNIEnv * env, IDATA fd, I_64 offset, + I_32 whence) +{ + DWORD moveMethod, moveResult; + DWORD lowerOffset, upperOffset; + I_64 result; + I_32 error; + PORT_ACCESS_FROM_ENV (env); + + lowerOffset = (DWORD) (offset & 0xFFFFFFFF); + upperOffset = (DWORD) ((offset >> 32) & 0x7FFFFFFF); + + if ((whence < HySeekSet) || (whence > HySeekEnd)) + { + return -1; + } + if (whence == HySeekSet) + { + moveMethod = FILE_BEGIN; + } + if (whence == HySeekEnd) + { + moveMethod = FILE_END; + } + if (whence == HySeekCur) + { + moveMethod = FILE_CURRENT; + } + moveResult = + SetFilePointer ((HANDLE) fd, lowerOffset, &upperOffset, moveMethod); + if (-1 == moveResult) + { + error = GetLastError (); + if (error != NO_ERROR) + { + hyerror_set_last_error (error, + findError (error)); + return -1; + } + } + + result = (I_64) upperOffset << 32; + result |= moveResult; + + return result; +} + /*Kill the Process with ID procHandle */ int @@ -336,3 +485,217 @@ { return CloseHandle ((HANDLE) procHandle); } + +void +writebytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, + jint offset, jint count, IDATA descriptor) +{ + I_32 result = 0; + jbyte *buf; + PORT_ACCESS_FROM_ENV (env); + jsize len; + char *errorMessage = NULL; + +/* TODO: ARRAY PINNING */ +#define INTERNAL_MAX 512 + jbyte internalBuffer[INTERNAL_MAX]; + + if (buffer == NULL) + { + throwNPException (env, "buffer is null"); + return; + } + + len = (*env)->GetArrayLength (env, buffer); + + /** + * If offset is negative, or count is negative, or offset+count is greater + * than the length of the array b, then an IndexOutOfBoundsException is thrown. + * Must test offset > len, or len - offset < count to avoid int overflow caused + * by offset + count + */ + if (offset < 0 || count < 0 || offset > len || (len - offset) < count) + { + throwIndexOutOfBoundsException (env); + return; + } + + /* If len or count is zero, just return 0 */ + if (len == 0 || count == 0) + return; + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return; + } + if (count > INTERNAL_MAX) + { + buf = hymem_allocate_memory (count); + } + else + { + buf = internalBuffer; + } + + if (buf == NULL) + { + throwNewOutOfMemoryError (env, ""); + return; + } + ((*env)->GetByteArrayRegion (env, buffer, offset, count, buf)); + + result = file_write (env, descriptor, buf, count); + + /** + * if there is an error, find the error message before calling free in case + * hymem_free_memory changes the error code + */ + if (result < 0) + errorMessage = ioLookupErrorString (env, result); + + if (buf != internalBuffer) + { + hymem_free_memory (buf); + } +#undef INTERNAL_MAX + + if (result < 0) + throwJavaIoIOException (env, errorMessage); +} + +/** + * This will write one byte + */ +void +writecharProc (JNIEnv * env, jobject recv, jint c, IDATA descriptor) +{ + I_32 result = 0; + char buf[1]; + PORT_ACCESS_FROM_ENV (env); + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return; + } + + buf[0] = (char) c; + + result = file_write (env, descriptor, buf, 1); + + if (result < 0) + throwJavaIoIOException (env, ioLookupErrorString (env, result)); +} + +/** + * This will read a single character from the descriptor + */ +jint +readcharProc (JNIEnv * env, jobject recv, IDATA descriptor) +{ + I_32 result; + char buf[1]; + PORT_ACCESS_FROM_ENV (env); + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return 0; + } + + if (descriptor == 0) + { + result = hytty_get_chars (buf, 1); + } + else + { + result = file_read (env, descriptor, buf, 1); + } + + if (result <= 0) + return -1; + + return (jint) buf[0] & 0xFF; +} + +/** + * This will read a up to count bytes into buffer starting at offset + */ +jint +readbytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, jint offset, + jint count, IDATA descriptor) +{ + I_32 result; + jsize len; + jbyte *buf; + +/* TODO: ARRAY PINNING */ +#define INTERNAL_MAX 2048 + jbyte internalBuffer[INTERNAL_MAX]; + + PORT_ACCESS_FROM_ENV (env); + + if (buffer == NULL) + { + throwNPException (env, "buffer is null"); + return 0; + } + + len = (*env)->GetArrayLength (env, buffer); + /** + * Throw IndexOutOfBoundsException according to spec. + * Must test offset > len, or len - offset < count to avoid + * int overflow caused by offset + count + */ + if (offset < 0 || count < 0 || offset > len || (len - offset) < count) + { + throwIndexOutOfBoundsException (env); + return 0; + } + /* If len is 0, simply return 0 (even if it is closed) */ + if (len == 0 || count == 0) + return 0; + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return 0; + } + if (len >= INTERNAL_MAX) + { + buf = jclmem_allocate_memory (env, len); + } + else + { + buf = internalBuffer; + } + + if (buf == NULL) + { + throwNewOutOfMemoryError (env, ""); + return 0; + } + /* Must FREE buffer before returning */ + + if (descriptor == 0) + { + /* hytty_get_chars() returns zero on EOF */ + if ((result = hytty_get_chars (buf, count)) == 0) + result = -1; + } + else + { + result = file_read (env, descriptor, buf, count); + } + if (result > 0) + (*env)->SetByteArrayRegion (env, buffer, offset, result, buf); + + if (buf != internalBuffer) + { + jclmem_free_memory (env, buf); + } +#undef INTERNAL_MAX + + return result; +} Index: native-src/win.IA32/luni/OSFileSystemWin32.c =================================================================== --- native-src/win.IA32/luni/OSFileSystemWin32.c (revision 418697) +++ native-src/win.IA32/luni/OSFileSystemWin32.c (working copy) @@ -26,6 +26,7 @@ #include #include "IFileSystem.h" #include "OSFileSystem.h" +#include "apr_arch_file_io.h" typedef SOCKET OSSOCKET; typedef struct hysocket_struct @@ -46,6 +47,7 @@ DWORD dwFlags = 0; OVERLAPPED overlapped; BOOL rc; + apr_file_t* thefile = (apr_file_t*) handle; const DWORD offsetLow = (DWORD) (start & 0xFFFFFFFF); const DWORD offsetHigh = (DWORD) ((start >> 0x20) & 0x7FFFFFFF); const DWORD nNumberOfBytesToLockLow = (DWORD) (length & 0xFFFFFFFF); @@ -66,7 +68,7 @@ overlapped.Offset = offsetLow; overlapped.OffsetHigh = offsetHigh; - rc = LockFileEx ((HANDLE) handle, /* [in] file handle to lock */ + rc = LockFileEx (thefile->filehand, /* [in] file handle to lock */ dwFlags, /* [in] flags describing lock type */ (DWORD) 0, /* [in] reserved */ nNumberOfBytesToLockLow, /* [in] number of bytes to lock (low) */ @@ -89,6 +91,7 @@ { OVERLAPPED overlapped; BOOL rc; + apr_file_t* thefile = (apr_file_t*) handle; const DWORD offsetLow = (DWORD) (start & 0xFFFFFFFF); const DWORD offsetHigh = (DWORD) ((start >> 0x20) & 0x7FFFFFFF); const DWORD nNumberOfBytesToUnlockLow = (DWORD) (length & 0xFFFFFFFF); @@ -99,7 +102,7 @@ overlapped.Offset = offsetLow; overlapped.OffsetHigh = offsetHigh; - rc = UnlockFileEx ((HANDLE) handle, /* [in] file handle to lock */ + rc = UnlockFileEx (thefile->filehand, /* [in] file handle to lock */ (DWORD) 0, /* [in] reserved */ nNumberOfBytesToUnlockLow, /* [in] number of bytes to lock (low) */ nNumberOfBytesToUnlockHigh, /* [in] number of bytes to lock (high) */ Index: native-src/win.IA32/luni/procimpl.h =================================================================== --- native-src/win.IA32/luni/procimpl.h (revision 418697) +++ native-src/win.IA32/luni/procimpl.h (working copy) @@ -26,3 +26,16 @@ int waitForProc (IDATA procHandle); int getAvailable (IDATA sHandle); int termProc (IDATA procHandle); +void +writebytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, + jint offset, jint count, IDATA descriptor); +jint +readbytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, jint offset, + jint count, IDATA descriptor); +jint +readcharProc (JNIEnv * env, jobject recv, IDATA descriptor); +void +writecharProc (JNIEnv * env, jobject recv, jint c, IDATA descriptor); +jint +available (JNIEnv * env, jobject recv, jfieldID fdFID); + Index: native-src/win.IA32/luni/makefile =================================================================== --- native-src/win.IA32/luni/makefile (revision 418697) +++ native-src/win.IA32/luni/makefile (working copy) @@ -22,8 +22,12 @@ LIBBASE=hyluni DLLNAME=..\$(LIBBASE).dll LIBNAME=$(LIBPATH)$(LIBBASE).lib -HYCFLAGS = $(HYCFLAGS) /I$(SHAREDSUB) /I$(SHARED)common /I$(SHARED)fdlibm \ - /I..\..\..\modules\luni\src\main\native\include\shared /I..\..\..\modules\luni\src\main\native\include\windows +HYCFLAGS = $(HYCFLAGS) -DAPR_DECLARE_EXPORT /I$(SHAREDSUB) /I$(SHARED)common /I$(SHARED)fdlibm \ + /I..\..\..\modules\luni\src\main\native\include\shared /I..\..\..\modules\luni\src\main\native\include\windows \ + /I..\..\..\modules\luni\src\main\native\include\shared /I..\..\..\modules\luni\src\main\native\include\windows \ + /I..\apr\apr-1.2.7\include /I..\apr\apr-1.2.7\include\arch\win32 \ + /I..\apr\apr-1.2.7\include\arch\unix /I..\apr\apr-1.2.7\include\arch + HYLDFLAGS = $(HYLDFLAGS) -def:$(LIBBASE).def BUILDFILES = \ @@ -46,7 +50,8 @@ MDLLIBFILES = \ $(LIBPATH)hycommon.lib $(LIBPATH)hysig.lib $(LIBPATH)hyzip.lib $(LIBPATH)hyzlib.lib \ - $(LIBPATH)hypool.lib $(LIBPATH)hyfdlibm.lib $(LIBPATH)hythr.lib $(LIBPATH)vmi.lib + $(LIBPATH)hypool.lib $(LIBPATH)hyfdlibm.lib $(LIBPATH)hythr.lib $(LIBPATH)vmi.lib \ + $(LIBPATH)hyapr.lib $(LIBPATH)hyprt.lib DLLBASE=0x13200000 COMMENT=/comment:"LUNI component native code. (c) Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable." Index: native-src/win.IA32/makefile =================================================================== --- native-src/win.IA32/makefile (revision 418697) +++ native-src/win.IA32/makefile (working copy) @@ -19,7 +19,7 @@ !include all: \ - _sig _pool _thread _port _fdlibm _zip \ + _apr _sig _pool _thread _port _fdlibm _zip \ _zlib _common _luni _vmi _pool: @@ -37,7 +37,7 @@ $(MAKE) /NOLOGO cd .. -_port: _thread _sig _common +_port: _apr _thread _sig _common cd port $(MAKE) /NOLOGO cd .. @@ -71,6 +71,11 @@ cd luni $(MAKE) /NOLOGO cd .. + +_apr: + cd apr + $(MAKE) /NOLOGO + cd .. clean: cd common Index: native-src/linux.IA32/apr/makefile =================================================================== --- native-src/linux.IA32/apr/makefile (revision 0) +++ native-src/linux.IA32/apr/makefile (revision 0) @@ -0,0 +1,57 @@ +# Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# +# Makefile for module 'port' +# + +include $(HY_HDK)/build/make/makefile.include + +LIBNAME=$(LIBPATH)libhyapr.a + +CFLAGS += -fpic -DLINUX -D_REENTRANT -DPLATFORM_POSIX -D_LARGEFILE64_SOURCE -DAPR_DECLARE_EXPORT \ + -D_HAVE_CONFIG_H -D_GNU_SOURCE -I./apr-1.2.7/include \ + -I./apr-1.2.7/include/arch/unix -I./apr-1.2.7/include/arch + +BUILDFILES = \ + apr-1.2.7/file_io/unix/copy.o apr-1.2.7/file_io/unix/fileacc.o \ + apr-1.2.7/file_io/unix/filepath_util.o apr-1.2.7/file_io/unix/fullrw.o \ + apr-1.2.7/file_io/unix/mktemp.o apr-1.2.7/file_io/unix/tempdir.o \ + apr-1.2.7/memory/unix/apr_pools.o \ + apr-1.2.7/misc/unix/charset.c \ + apr-1.2.7/misc/unix/env.o apr-1.2.7/misc/unix/errorcodes.o \ + apr-1.2.7/misc/unix/getopt.o apr-1.2.7/misc/unix/otherchild.o \ + apr-1.2.7/misc/unix/rand.o \ + apr-1.2.7/misc/unix/start.o apr-1.2.7/misc/unix/version.o \ + apr-1.2.7/mmap/unix/common.o \ + apr-1.2.7/network_io/unix/inet_ntop.o apr-1.2.7/network_io/unix/inet_pton.o \ + apr-1.2.7/network_io/unix/sockaddr.o apr-1.2.7/poll/unix/select.o \ + apr-1.2.7/random/unix/apr_random.o apr-1.2.7/random/unix/sha2.o \ + apr-1.2.7/random/unix/sha2_glue.o \ + apr-1.2.7/strings/apr_cpystrn.o apr-1.2.7/strings/apr_fnmatch.o \ + apr-1.2.7/strings/apr_snprintf.o apr-1.2.7/strings/apr_strings.o \ + apr-1.2.7/strings/apr_strnatcmp.o apr-1.2.7/strings/apr_strtok.o \ + apr-1.2.7/tables/apr_hash.o apr-1.2.7/tables/apr_tables.o \ + apr-1.2.7/support/unix/waitio.o apr-1.2.7/mmap/unix/mmap.o apr-1.2.7/file_io/unix/seek.o \ + apr-1.2.7/file_io/unix/open.o apr-1.2.7/file_io/unix/filestat.o \ + apr-1.2.7/file_io/unix/dir.o apr-1.2.7/file_io/unix/filepath.o \ + apr-1.2.7/file_io/unix/pipe.o apr-1.2.7/file_io/unix/tempdir.o \ + apr-1.2.7/file_io/unix/readwrite.o apr-1.2.7/misc/unix/start.o apr-1.2.7/time/unix/time.o \ + apr-1.2.7/atomic/unix/apr_atomic.o apr-1.2.7/locks/unix/thread_mutex.o apr-1.2.7/locks/unix/global_mutex.o \ + apr-1.2.7/threadproc/unix/signals.o apr-1.2.7/threadproc/unix/proc.o \ + apr-1.2.7/threadproc/unix/procsup.o apr-1.2.7/file_io/unix/filedup.o \ + apr-1.2.7/user/unix/groupinfo.o apr-1.2.7/user/unix/userinfo.o \ + apr-1.2.7/locks/unix/proc_mutex.o apr-1.2.7/file_io/unix/flock.o + +include $(HY_HDK)/build/make/rules.mk Index: native-src/linux.IA32/port/hyfile.c =================================================================== --- native-src/linux.IA32/port/hyfile.c (revision 418697) +++ native-src/linux.IA32/port/hyfile.c (working copy) @@ -1,829 +0,0 @@ -/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define CDEV_CURRENT_FUNCTION _comment_ -/** - * @file - * @ingroup Port - * @brief file - */ - -#undef CDEV_CURRENT_FUNCTION - -#include -#include -#include -#include -#include -#include -#include - -#include "hyport.h" -#include "hysock.h" -#include "hystdarg.h" -#include "portnls.h" -#include "ut_hyprt.h" - -#define CDEV_CURRENT_FUNCTION _prototypes_private -static I_32 EsTranslateOpenFlags (I_32 flags); -static I_32 findError (I_32 errorCode); - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION EsTranslateOpenFlags -static I_32 -EsTranslateOpenFlags (I_32 flags) -{ - I_32 realFlags = 0; - - if (flags & HyOpenAppend) - { - realFlags |= O_APPEND; - } - if (flags & HyOpenTruncate) - { - realFlags |= O_TRUNC; - } - if (flags & HyOpenCreate) - { - realFlags |= O_CREAT; - } - if (flags & HyOpenCreateNew) - { - realFlags |= O_EXCL | O_CREAT; - } -#ifdef O_SYNC - if (flags & HyOpenSync) { - realFlags |= O_SYNC; - } -#endif - if (flags & HyOpenRead) - { - if (flags & HyOpenWrite) - { - return (O_RDWR | realFlags); - } - return (O_RDONLY | realFlags); - } - if (flags & HyOpenWrite) - { - return (O_WRONLY | realFlags); - } - return -1; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION findError -/** - * @internal - * Determines the proper portable error code to return given a native error code - * - * @param[in] errorCode The error code reported by the OS - * - * @return the (negative) portable error code - */ -static I_32 -findError (I_32 errorCode) -{ - switch (errorCode) - { - case EACCES: - case EPERM: - return HYPORT_ERROR_FILE_NOPERMISSION; - case ENAMETOOLONG: - return HYPORT_ERROR_FILE_NAMETOOLONG; - case ENOENT: - return HYPORT_ERROR_FILE_NOENT; - case ENOTDIR: - return HYPORT_ERROR_FILE_NOTDIR; - case ELOOP: - return HYPORT_ERROR_FILE_LOOP; - - case EBADF: - return HYPORT_ERROR_FILE_BADF; - case EEXIST: - return HYPORT_ERROR_FILE_EXIST; - case ENOSPC: - case EFBIG: - return HYPORT_ERROR_FILE_DISKFULL; - default: - return HYPORT_ERROR_FILE_OPFAILED; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_close -/** - * Closes a file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_close (struct HyPortLibrary * portLibrary, IDATA fd) -{ - return close ((int) fd); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_open -/** - * Convert a pathname into a file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] path Name of the file to be opened. - * @param[in] flags Portable file read/write attributes. - * @param[in] mode Platform file permissions. - * - * @return The file descriptor of the newly opened file, -1 on failure. - */ -IDATA VMCALL -hyfile_open (struct HyPortLibrary *portLibrary, const char *path, I_32 flags, - I_32 mode) -{ - struct stat buffer; - I_32 fd; - I_32 realFlags = EsTranslateOpenFlags (flags); - I_32 fdflags; - - Trc_PRT_file_open_Entry (path, flags, mode); - - if (realFlags == -1) - { - Trc_PRT_file_open_Exit1 (flags); - portLibrary->error_set_last_error (portLibrary, EINVAL, - findError (EINVAL)); - return -1; - } - - if (!stat (path, &buffer)) - { - if (S_ISDIR (buffer.st_mode)) - { - Trc_PRT_file_open_Exit4 (); - return -1; - } - } - - fd = open (path, realFlags, mode); - - if (-1 == fd) - { - Trc_PRT_file_open_Exit2 (errno, findError (errno)); - portLibrary->error_set_last_error (portLibrary, errno, - findError (errno)); - return -1; - } - - /* Tag this descriptor as being non-inheritable */ - fdflags = fcntl (fd, F_GETFD, 0); - fcntl (fd, F_SETFD, fdflags | FD_CLOEXEC); - - Trc_PRT_file_open_Exit (fd); - return (IDATA) fd; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_unlink -/** - * Remove a file from the file system. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name to remove. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_unlink (struct HyPortLibrary * portLibrary, const char *path) -{ - return unlink (path); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_write -/** - * Write to a file. - * - * Writes up to nbytes from the provided buffer to the file referenced by the file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd File descriptor to write. - * @param[in] buf Buffer to be written. - * @param[in] nbytes Size of buffer. - * - * @return Number of bytes written on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -IDATA VMCALL -hyfile_write (struct HyPortLibrary * portLibrary, IDATA fd, const void *buf, - IDATA nbytes) -{ - IDATA rc = 0; - - /* write will just do the right thing for HYPORT_TTY_OUT and HYPORT_TTY_ERR */ - rc = write ((int) fd, buf, nbytes); - - if (rc == -1) - { - return portLibrary->error_set_last_error (portLibrary, errno, - findError (errno)); - } - return rc; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_read -/** - * Read bytes from a file descriptor into a user provided buffer. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * @param[in,out] buf Buffer to read into. - * @param[in] nbytes Size of buffer. - * - * @return The number of bytes read, or -1 on failure. - */ -IDATA VMCALL -hyfile_read (struct HyPortLibrary * portLibrary, IDATA fd, void *buf, - IDATA nbytes) -{ - IDATA result; - if (nbytes == 0) - { - return 0; - } - - result = read ((int) fd, buf, nbytes); - if (result == 0) - { - return -1; - } - else - { - return result; - } -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_seek -/** - * Repositions the offset of the file descriptor to a given offset as per directive whence. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * @param[in] offset The offset in the file to position to. - * @param[in] whence Portable constant describing how to apply the offset. - * - * @return The resulting offset on success, -1 on failure. - * @note whence is one of HySeekSet (seek from beginning of file), - * HySeekCur (seek from current file pointer) or HySeekEnd (seek backwards from - * end of file). - * @internal @note seek operations return -1 on failure. Negative offsets - * can be returned when seeking beyond end of file. - */ -I_64 VMCALL -hyfile_seek (struct HyPortLibrary * portLibrary, IDATA fd, I_64 offset, - I_32 whence) -{ - off_t localOffset = (off_t) offset; - - if ((whence < HySeekSet) || (whence > HySeekEnd)) - { - return -1; - } - - /* If file offsets are 32 bit, truncate the seek to that range */ - if (sizeof (off_t) < sizeof (I_64)) - { - if (offset > 0x7FFFFFFF) - { - localOffset = 0x7FFFFFFF; - } - else if (offset < -0x7FFFFFFF) - { - localOffset = -0x7FFFFFFF; - } - } - - return (I_64) lseek ((int) fd, localOffset, whence); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_attr -/** - * Determine whether path is a file or directory. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * - * @return EslsFile if a file, EslsDir if a directory, negative portable error code on failure. - */ -I_32 VMCALL -hyfile_attr (struct HyPortLibrary *portLibrary, const char *path) -{ - struct stat buffer; - - /* Neutrino does not handle NULL for stat */ - - if (stat (path, &buffer)) - { - return portLibrary->error_set_last_error (portLibrary, errno, - findError (errno)); - } - if (S_ISDIR (buffer.st_mode)) - { - return HyIsDir; - } - return HyIsFile; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_findclose -/** - * Close the handle returned from @ref hyfile_findfirst. - * - * @param[in] portLibrary The port library - * @param[in] findhandle Handle returned from @ref hyfile_findfirst. - */ -void VMCALL -hyfile_findclose (struct HyPortLibrary *portLibrary, UDATA findhandle) -{ -#if defined(_AIX) - closedir64 ((DIR64 *) findhandle); -#else - closedir ((DIR *) findhandle); -#endif - - -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_findfirst -/** - * Find the first occurrence of a file identified by path. Answers a handle - * to be used in subsequent calls to @ref hyfile_findnext and @ref hyfile_findclose. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * @param[out] resultbuf filename and path matching path. - * - * @return valid handle on success, -1 on failure. - */ -UDATA VMCALL -hyfile_findfirst (struct HyPortLibrary *portLibrary, const char *path, - char *resultbuf) -{ -#if defined(_AIX) - DIR64 *dirp = NULL; - struct dirent64 *entry; -#else - DIR *dirp = NULL; - struct dirent *entry; -#endif - - -#if defined(_AIX) - dirp = opendir64 (path); -#else - dirp = opendir (path); -#endif - - - if (dirp == NULL) - { - return (UDATA) - 1; - } -#if defined(_AIX) - entry = readdir64 (dirp); -#else - entry = readdir (dirp); -#endif - - - if (entry == NULL) - { -#if defined(_AIX) - closedir64 (dirp); -#else - closedir (dirp); -#endif - - - return (UDATA) - 1; - } - strcpy (resultbuf, entry->d_name); - return (UDATA) dirp; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_findnext -/** - * Find the next filename and path matching a given handle. - * - * @param[in] portLibrary The port library - * @param[in] findhandle handle returned from @ref hyfile_findfirst. - * @param[out] resultbuf next filename and path matching findhandle. - * - * @return 0 on success, -1 on failure or if no matching entries. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_findnext (struct HyPortLibrary * portLibrary, UDATA findhandle, - char *resultbuf) -{ -#if defined(_AIX) - struct dirent64 *entry; -#else - struct dirent *entry; -#endif - - -#if defined(_AIX) - entry = readdir64 ((DIR64 *) findhandle); -#else - entry = readdir ((DIR *) findhandle); -#endif - - - if (entry == NULL) - { - return -1; - } - strcpy (resultbuf, entry->d_name); - return 0; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_lastmod -/** - * Return the last modification time of the file path in seconds. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * - * @return last modification time on success, -1 on failure. - */ -I_64 VMCALL -hyfile_lastmod (struct HyPortLibrary * portLibrary, const char *path) -{ - struct stat st; - tzset (); - - /* Neutrino does not handle NULL for stat */ - - if (stat (path, &st)) - { - return -1; - } - return st.st_mtime; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_length -/** - * Answer the length in bytes of the file. - * - * @param[in] portLibrary The port library - * @param[in] path file/path name being queried. - * - * @return Length in bytes of the file on success, negative portable error code on failure - */ -I_64 VMCALL -hyfile_length (struct HyPortLibrary * portLibrary, const char *path) -{ - struct stat st; - - /* Neutrino does not handle NULL for stat */ - - if (stat (path, &st)) - { - return portLibrary->error_set_last_error (portLibrary, errno, - findError (errno)); - } - return (I_64) st.st_size; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_mkdir -/** - * Create a directory. - * - * @param[in] portLibrary The port library - * @param[in] path Directory to be created. - * - * @return 0 on success, -1 on failure. - * @note Assumes all components of path up to the last directory already exist. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_mkdir (struct HyPortLibrary * portLibrary, const char *path) -{ - if (-1 == mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO)) - { - portLibrary->error_set_last_error (portLibrary, errno, - findError (errno)); - return -1; - } - - return 0; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_move -/** - * Move the file pathExist to a new name pathNew. - * - * @param[in] portLibrary The port library - * @param[in] pathExist The existing file name. - * @param[in] pathNew The new file name. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_move (struct HyPortLibrary * portLibrary, const char *pathExist, - const char *pathNew) -{ - return rename (pathExist, pathNew); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_unlinkdir -/** - * Remove the trailing directory of the path. If the path is a symbolic link to a directory, remove - * the symbolic link. - * - * @param[in] portLibrary The port library - * @param[in] path directory name being removed. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure.. - */ -I_32 VMCALL -hyfile_unlinkdir (struct HyPortLibrary * portLibrary, const char *path) -{ - /* Call remove() so symbolic links to directories are removed */ - - /* QNX has modified the API for remove and rmdir. */ - /* Remove does not call rmdir automagically like every other Unix. */ - - return remove (path); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_sync -/** - * Synchronize a file's state with the state on disk. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * - * @return 0 on success, -1 on failure. - * @internal @todo return negative portable return code on failure. - */ -I_32 VMCALL -hyfile_sync (struct HyPortLibrary * portLibrary, IDATA fd) -{ - return fsync ((int) fd); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_error_message -/** - * Return an error message describing the last OS error that occurred. The last - * error returned is not thread safe, it may not be related to the operation that - * failed for this thread. - * - * @param[in] portLibrary The port library - * - * @return error message describing the last OS error, may return NULL. - * - * @internal - * @note This function gets the last error code from the OS and then returns - * the corresponding string. It is here as a helper function for JCL. Once hyerror - * is integrated into the port library this function should probably disappear. - */ -const char *VMCALL -hyfile_error_message (struct HyPortLibrary *portLibrary) -{ - I_32 errorCode; - - errorCode = - portLibrary->error_set_last_error (portLibrary, errno, findError (errno)); - return portLibrary->error_last_error_message (portLibrary); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_shutdown -/** - * PortLibrary shutdown. - * - * This function is called during shutdown of the portLibrary. Any resources that were created by @ref hyfile_startup - * should be destroyed here. - * - * @param[in] portLibrary The port library - * - * @note Most implementations will be empty. - */ -void VMCALL -hyfile_shutdown (struct HyPortLibrary *portLibrary) -{ -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_startup -/** - * PortLibrary startup. - * - * This function is called during startup of the portLibrary. Any resources that are required for - * the file operations may be created here. All resources created here should be destroyed - * in @ref hyfile_shutdown. - * - * @param[in] portLibrary The port library - * - * @return 0 on success, negative error code on failure. Error code values returned are - * \arg HYPORT_ERROR_STARTUP_FILE - * - * @note Most implementations will simply return success. - */ -I_32 VMCALL -hyfile_startup (struct HyPortLibrary *portLibrary) -{ - return 0; -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_vprintf -/** - * Write to a file. - * - * Writes formatted output to the file referenced by the file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd File descriptor to write. - * @param[in] format The format String. - * @param[in] args Variable argument list. - * - */ -void VMCALL -hyfile_vprintf (struct HyPortLibrary *portLibrary, IDATA fd, - const char *format, va_list args) -{ - - char outputBuffer[256]; - - char *allocatedBuffer; - U_32 numberWritten; - va_list copyOfArgs; - - /* Attempt to write output to stack buffer */ - COPY_VA_LIST (copyOfArgs, args); - numberWritten = - portLibrary->str_vprintf (portLibrary, outputBuffer, - sizeof (outputBuffer), format, copyOfArgs); - - /* str_vprintf always null terminates, returns number characters written excluding the null terminator */ - if (sizeof (outputBuffer) > (numberWritten + 1)) - { - /* write out the buffer */ - portLibrary->file_write_text (portLibrary, fd, outputBuffer, - numberWritten); - return; - } - - /* Either the buffer was too small, or it was the exact size. Unfortunately can't tell the difference, - * need to determine the size of the buffer (another call to str_vprintf) then print to the buffer, - * a third call to str_vprintf - */ - COPY_VA_LIST (copyOfArgs, args); - - /* What is size of buffer required ? Does not include the \0 */ - numberWritten = - portLibrary->str_vprintf (portLibrary, NULL, (U_32) (-1), format, - copyOfArgs); - numberWritten += 1; - - allocatedBuffer = - portLibrary->mem_allocate_memory (portLibrary, numberWritten); - if (NULL == allocatedBuffer) - { - portLibrary->nls_printf (portLibrary, HYNLS_ERROR, - HYNLS_PORT_FILE_MEMORY_ALLOCATE_FAILURE); - return; - } - - numberWritten = - portLibrary->str_vprintf (portLibrary, allocatedBuffer, numberWritten, - format, args); - portLibrary->file_write_text (portLibrary, fd, allocatedBuffer, - numberWritten); - portLibrary->mem_free_memory (portLibrary, allocatedBuffer); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_printf -/** - * Write to a file. - * - * Writes formatted output to the file referenced by the file descriptor. - * - * @param[in] portLibrary The port library - * @param[in] fd File descriptor to write to - * @param[in] format The format string to be output. - * @param[in] ... arguments for format. - * - */ -void VMCALL -hyfile_printf (struct HyPortLibrary *portLibrary, IDATA fd, - const char *format, ...) -{ - va_list args; - - va_start (args, format); - portLibrary->file_vprintf (portLibrary, fd, format, args); - va_end (args); -} - -#undef CDEV_CURRENT_FUNCTION - -#define CDEV_CURRENT_FUNCTION hyfile_set_length -/** - * Set the length of a file to a specified value. - * - * @param[in] portLibrary The port library - * @param[in] fd The file descriptor. - * @param[in] newLength Length to be set - * - * @return 0 on success, negative portable error code on failure - */ -I_32 VMCALL -hyfile_set_length (struct HyPortLibrary *portLibrary, IDATA fd, - I_64 newLength) -{ - - I_32 rc; - off_t length = (off_t) newLength; - - /* If file offsets are 32 bit, truncate the newLength to that range */ - if (sizeof (off_t) < sizeof (I_64)) - { - if (newLength > 0x7FFFFFFF) - { - length = 0x7FFFFFFF; - } - else if (newLength < -0x7FFFFFFF) - { - length = -0x7FFFFFFF; - } - } - - rc = ftruncate (fd, length); - if (0 != rc) - { - rc = - portLibrary->error_set_last_error (portLibrary, errno, - findError (errno)); - } - return rc; - -} - -#undef CDEV_CURRENT_FUNCTION Index: native-src/linux.IA32/port/makefile =================================================================== --- native-src/linux.IA32/port/makefile (revision 418697) +++ native-src/linux.IA32/port/makefile (working copy) @@ -18,12 +18,14 @@ include $(HY_HDK)/build/make/makefile.include -CFLAGS += -fpic -DHYPORT_LIBRARY_DEFINE \ +CFLAGS += -fpic -DHYPORT_LIBRARY_DEFINE -DHYPORT_LIBRARY_DEFINE -DLINUX -D_REENTRANT -DPLATFORM_POSIX -D_LARGEFILE64_SOURCE -DAPR_DECLARE_EXPORT \ + -I../apr/apr-1.2.7/include \ + -I../apr/apr-1.2.7/include/arch/unix -I../apr/apr-1.2.7/include/arch \ -I../../../modules/luni/src/main/native/include/shared -I../../../modules/luni/src/main/native/include/linux BUILDFILES = \ $(SHAREDSUB)port_copyright.o hycpu.o $(SHAREDSUB)hyerror.o \ - hyerrorhelpers.o $(SHAREDSUB)hyexit.o hyfile.o hyfiletext.o \ + hyerrorhelpers.o $(SHAREDSUB)hyexit.o $(SHAREDSUB)hyfile.o hyfiletext.o \ $(SHAREDSUB)hygp.o hyipcmutex.o hymem.o \ hymmap.o $(SHAREDSUB)hynls.o hynlshelpers.o hyosdump.o \ $(SHAREDSUB)hyport.o $(SHAREDSUB)hyportcontrol.o hyportptb.o \ @@ -31,7 +33,7 @@ $(SHAREDSUB)hystr.o $(SHAREDSUB)hystrftime.o $(SHAREDSUB)hystsl.o \ hysysinfo.o hytime.o $(SHAREDSUB)hytlshelpers.o hytty.o hyvmem.o -MDLLIBFILES = ../libhythr.so $(LIBPATH)libhycommon.a ../libhysig.so +MDLLIBFILES = ../libhythr.so $(LIBPATH)libhycommon.a ../libhysig.so $(LIBPATH)libhyapr.a DLLNAME = ../libhyprt.so Index: native-src/linux.IA32/luni/helpers.c =================================================================== --- native-src/linux.IA32/luni/helpers.c (revision 418697) +++ native-src/linux.IA32/luni/helpers.c (working copy) @@ -115,9 +115,11 @@ I_32 setPlatformFileLength (JNIEnv * env, IDATA descriptor, jlong newLength) { - - return (ftruncate ((int) descriptor, newLength) == 0); - + PORT_ACCESS_FROM_ENV (env); + if (hyfile_set_length (descriptor, newLength) != 0) { + return 0; + } + return 1; } jbyteArray Index: native-src/linux.IA32/luni/procimpl.c =================================================================== --- native-src/linux.IA32/luni/procimpl.c (revision 418697) +++ native-src/linux.IA32/luni/procimpl.c (working copy) @@ -17,6 +17,7 @@ #include #include #include +#include "iohelp.h" #include @@ -29,6 +30,101 @@ #include "procimpl.h" +static I_32 +findError (I_32 errorCode) +{ + switch (errorCode) + { + case EACCES: + case EPERM: + return HYPORT_ERROR_FILE_NOPERMISSION; + case ENAMETOOLONG: + return HYPORT_ERROR_FILE_NAMETOOLONG; + case ENOENT: + return HYPORT_ERROR_FILE_NOENT; + case ENOTDIR: + return HYPORT_ERROR_FILE_NOTDIR; + case ELOOP: + return HYPORT_ERROR_FILE_LOOP; + + case EBADF: + return HYPORT_ERROR_FILE_BADF; + case EEXIST: + return HYPORT_ERROR_FILE_EXIST; + case ENOSPC: + case EFBIG: + return HYPORT_ERROR_FILE_DISKFULL; + default: + return HYPORT_ERROR_FILE_OPFAILED; + } +} + +static IDATA +file_read (JNIEnv * env, IDATA fd, void *buf, + IDATA nbytes) +{ + IDATA result; + if (nbytes == 0) + { + return 0; + } + + result = read ((int) fd, buf, nbytes); + if (result == 0) + { + return -1; + } + else + { + return result; + } +} + +static IDATA +file_write (JNIEnv * env, IDATA fd, const void *buf, + IDATA nbytes) +{ + PORT_ACCESS_FROM_ENV (env); + IDATA rc = 0; + + /* write will just do the right thing for HYPORT_TTY_OUT and HYPORT_TTY_ERR */ + rc = write ((int) fd, buf, nbytes); + + if (rc == -1) + { + return hyerror_set_last_error (errno, + findError (errno)); + } + return rc; +} + +static I_64 +file_seek (JNIEnv * env, IDATA fd, I_64 offset, + I_32 whence) +{ + off_t localOffset = (off_t) offset; + + if ((whence < HySeekSet) || (whence > HySeekEnd)) + { + return -1; + } + + /* If file offsets are 32 bit, truncate the seek to that range */ + if (sizeof (off_t) < sizeof (I_64)) + { + if (offset > 0x7FFFFFFF) + { + localOffset = 0x7FFFFFFF; + } + else if (offset < -0x7FFFFFFF) + { + localOffset = -0x7FFFFFFF; + } + } + + return (I_64) lseek ((int) fd, localOffset, whence); +} + void sleepFor (unsigned int nanoseconds) { @@ -298,3 +394,217 @@ /* The procHandle (Process ID) should not be closed, as it isn't a file descriptor. */ return 0; } + +void +writebytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, + jint offset, jint count, IDATA descriptor) +{ + I_32 result = 0; + jbyte *buf; + PORT_ACCESS_FROM_ENV (env); + jsize len; + char *errorMessage = NULL; + +/* TODO: ARRAY PINNING */ +#define INTERNAL_MAX 512 + jbyte internalBuffer[INTERNAL_MAX]; + + if (buffer == NULL) + { + throwNPException (env, "buffer is null"); + return; + } + + len = (*env)->GetArrayLength (env, buffer); + + /** + * If offset is negative, or count is negative, or offset+count is greater + * than the length of the array b, then an IndexOutOfBoundsException is thrown. + * Must test offset > len, or len - offset < count to avoid int overflow caused + * by offset + count + */ + if (offset < 0 || count < 0 || offset > len || (len - offset) < count) + { + throwIndexOutOfBoundsException (env); + return; + } + + /* If len or count is zero, just return 0 */ + if (len == 0 || count == 0) + return; + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return; + } + if (count > INTERNAL_MAX) + { + buf = hymem_allocate_memory (count); + } + else + { + buf = internalBuffer; + } + + if (buf == NULL) + { + throwNewOutOfMemoryError (env, ""); + return; + } + ((*env)->GetByteArrayRegion (env, buffer, offset, count, buf)); + + result = file_write (env, descriptor, buf, count); + + /** + * if there is an error, find the error message before calling free in case + * hymem_free_memory changes the error code + */ + if (result < 0) + errorMessage = ioLookupErrorString (env, result); + + if (buf != internalBuffer) + { + hymem_free_memory (buf); + } +#undef INTERNAL_MAX + + if (result < 0) + throwJavaIoIOException (env, errorMessage); +} + +/** + * This will write one byte + */ +void +writecharProc (JNIEnv * env, jobject recv, jint c, IDATA descriptor) +{ + I_32 result = 0; + char buf[1]; + PORT_ACCESS_FROM_ENV (env); + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return; + } + + buf[0] = (char) c; + + result = file_write (env, descriptor, buf, 1); + + if (result < 0) + throwJavaIoIOException (env, ioLookupErrorString (env, result)); +} + +/** + * This will read a single character from the descriptor + */ +jint +readcharProc (JNIEnv * env, jobject recv, IDATA descriptor) +{ + I_32 result; + char buf[1]; + PORT_ACCESS_FROM_ENV (env); + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return 0; + } + + if (descriptor == 0) + { + result = hytty_get_chars (buf, 1); + } + else + { + result = file_read (env, descriptor, buf, 1); + } + + if (result <= 0) + return -1; + + return (jint) buf[0] & 0xFF; +} + +/** + * This will read a up to count bytes into buffer starting at offset + */ +jint +readbytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, jint offset, + jint count, IDATA descriptor) +{ + I_32 result; + jsize len; + jbyte *buf; + +/* TODO: ARRAY PINNING */ +#define INTERNAL_MAX 2048 + jbyte internalBuffer[INTERNAL_MAX]; + + PORT_ACCESS_FROM_ENV (env); + + if (buffer == NULL) + { + throwNPException (env, "buffer is null"); + return 0; + } + + len = (*env)->GetArrayLength (env, buffer); + /** + * Throw IndexOutOfBoundsException according to spec. + * Must test offset > len, or len - offset < count to avoid + * int overflow caused by offset + count + */ + if (offset < 0 || count < 0 || offset > len || (len - offset) < count) + { + throwIndexOutOfBoundsException (env); + return 0; + } + /* If len is 0, simply return 0 (even if it is closed) */ + if (len == 0 || count == 0) + return 0; + + if (descriptor == -1) + { + throwJavaIoIOExceptionClosed (env); + return 0; + } + if (len >= INTERNAL_MAX) + { + buf = jclmem_allocate_memory (env, len); + } + else + { + buf = internalBuffer; + } + + if (buf == NULL) + { + throwNewOutOfMemoryError (env, ""); + return 0; + } + /* Must FREE buffer before returning */ + + if (descriptor == 0) + { + /* hytty_get_chars() returns zero on EOF */ + if ((result = hytty_get_chars (buf, count)) == 0) + result = -1; + } + else + { + result = file_read (env, descriptor, buf, count); + } + if (result > 0) + (*env)->SetByteArrayRegion (env, buffer, offset, result, buf); + + if (buf != internalBuffer) + { + jclmem_free_memory (env, buf); + } +#undef INTERNAL_MAX + + return result; +} Index: native-src/linux.IA32/luni/procimpl.h =================================================================== --- native-src/linux.IA32/luni/procimpl.h (revision 418697) +++ native-src/linux.IA32/luni/procimpl.h (working copy) @@ -20,6 +20,8 @@ #define WAIT_ERROR PROC_DEAD #include #include +#include "jclglob.h" + int getAvailable (IDATA sHandle); int execProgram (JNIEnv * vmthread, jobject recv, char *command[], int commandLineLength, char *env[], int envSize, char *dir, @@ -29,4 +31,16 @@ int closeProc (IDATA procHandle); int termProc (IDATA procHandle); void sleepFor (unsigned int nanoseconds); +void +writebytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, + jint offset, jint count, IDATA descriptor); +jint +readbytesProc (JNIEnv * env, jobject recv, jbyteArray buffer, jint offset, + jint count, IDATA descriptor); +jint +readcharProc (JNIEnv * env, jobject recv, IDATA descriptor); +void +writecharProc (JNIEnv * env, jobject recv, jint c, IDATA descriptor); +jint +available (JNIEnv * env, jobject recv, jfieldID fdFID); #endif /* procimpl_h */ Index: native-src/linux.IA32/luni/OSFileSystemLinux32.c =================================================================== --- native-src/linux.IA32/luni/OSFileSystemLinux32.c (revision 418697) +++ native-src/linux.IA32/luni/OSFileSystemLinux32.c (working copy) @@ -26,6 +26,7 @@ #include "IFileSystem.h" #include "OSFileSystem.h" +#include "apr_arch_file_io.h" typedef int OSSOCKET; typedef struct hysocket_struct @@ -46,6 +47,7 @@ int rc; int waitMode = (waitFlag) ? F_SETLKW : F_SETLK; struct flock lock = { 0 }; + apr_file_t* thefile = (apr_file_t *) handle; // If start or length overflow the max values we can represent, then max them out. #if __WORDSIZE==32 @@ -76,7 +78,7 @@ do { - rc = fcntl (handle, waitMode, &lock); + rc = fcntl (thefile->filedes, waitMode, &lock); } while ((rc < 0) && (errno == EINTR)); @@ -91,6 +93,7 @@ { int rc; struct flock lock = { 0 }; + apr_file_t* thefile = (apr_file_t *) handle; // If start or length overflow the max values we can represent, then max them out. #if __WORDSIZE==32 @@ -112,7 +115,7 @@ do { - rc = fcntl (handle, F_SETLKW, &lock); + rc = fcntl (thefile->filedes, F_SETLKW, &lock); } while ((rc < 0) && (errno == EINTR)); Index: native-src/linux.IA32/luni/makefile =================================================================== --- native-src/linux.IA32/luni/makefile (revision 418697) +++ native-src/linux.IA32/luni/makefile (working copy) @@ -18,8 +18,10 @@ include $(HY_HDK)/build/make/makefile.include -CFLAGS += -fpic -I$(SHARED)common -I$(SHARED)fdlibm \ - -I../../../modules/luni/src/main/native/include/shared -I../../../modules/luni/src/main/native/include/linux +CFLAGS += -fpic -I$(SHARED)common -DAPR_DECLARE_EXPORT -DLINUX -D_REENTRANT -DPLATFORM_POSIX -D_LARGEFILE64_SOURCE -I$(SHARED)fdlibm \ + -I../../../modules/luni/src/main/native/include/shared -I../../../modules/luni/src/main/native/include/linux \ + -I../apr/apr-1.2.7/include -I../apr/apr-1.2.7/include/arch/unix -I../apr/apr-1.2.7/include/arch \ + BUILDFILES = \ $(SHAREDSUB)luni_copyright.o $(SHAREDSUB)file.o procimpl.o \ @@ -37,7 +39,7 @@ MDLLIBFILES = \ $(LIBPATH)libhycommon.a ../libhysig.so $(LIBPATH)libhyzip.a ../libhyzlib.so \ - $(LIBPATH)libhypool.a $(LIBPATH)libhyfdlibm.a ../libhythr.so ../libvmi.so + $(LIBPATH)libhypool.a $(LIBPATH)libhyfdlibm.a ../libhythr.so ../libvmi.so $(LIBPATH)libhyapr.a ../libhyprt.so DLLNAME = ../libhyluni.so Index: native-src/linux.IA32/makefile =================================================================== --- native-src/linux.IA32/makefile (revision 418697) +++ native-src/linux.IA32/makefile (working copy) @@ -17,7 +17,7 @@ # all: \ - _sig _common _pool _thread _port _fdlibm _zip \ + _apr _sig _common _pool _thread _port _fdlibm _zip \ _zlib _vmi _luni _pool: @@ -29,7 +29,7 @@ _thread: _pool _common (cd thread && $(MAKE) ) -_port: _thread _sig +_port: _apr _thread _sig (cd port && $(MAKE) ) _sig: @@ -49,6 +49,9 @@ _luni: _common _sig _zip _zlib _pool _fdlibm _thread _vmi (cd luni && $(MAKE) ) + +_apr: + (cd apr && $(MAKE) ) clean: (cd sig && $(MAKE) clean ) @@ -60,5 +63,6 @@ (cd zlib && $(MAKE) clean ) (cd vmi && $(MAKE) clean ) (cd common && $(MAKE) clean ) + (cd apr && $(MAKE) clean ) (cd luni && $(MAKE) clean ) Index: native-src/build.xml =================================================================== --- native-src/build.xml (revision 418697) +++ native-src/build.xml (working copy) @@ -26,24 +26,20 @@ - + - + - + - - + + - + - + + + @@ -73,7 +71,8 @@ - + + @@ -85,10 +84,34 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -105,21 +128,19 @@ - + - - - - - - + + + + + + - - + + @@ -128,23 +149,23 @@ - - - - - - + + + + + - - - - - - - + + + + + + + @@ -152,36 +173,32 @@ target: make-clean ================================= --> - - - - + + + + - - - - - - - - - - - + + + + + + + + + + + - + - + @@ -207,7 +224,7 @@ - + @@ -216,73 +233,67 @@ - + - - - + + + - - - - - - - - - - - - + + + - - - - + + - - - - - - - + + + + - - - + + + Index: make/depends.xml =================================================================== --- make/depends.xml (revision 418697) +++ make/depends.xml (working copy) @@ -30,99 +30,98 @@ - + - + - + - + - + - + - + - + - + - + - - + + + - - + + + + + + + + - - + + - - + + - + - + - - + + - - + + - - + + - - + + - + - - + + - + - - + + + + + + + + Fetching @{dest} - + @@ -130,7 +129,7 @@ - + ... File @{dest} has incorrect md5 checksum. Expected: @@ -152,14 +151,13 @@ Checking for @{dest} - - - - - - - + + + + + + + ... Missing dependency. The jar from: @@ -179,14 +177,13 @@ Removing signature from bcprov.jar - - - - - - - - + + + + + + + + Index: make/depends.properties =================================================================== --- make/depends.properties (revision 418697) +++ make/depends.properties (working copy) @@ -73,3 +73,14 @@ mx4j.remote.jar=${mx4j.dir}/mx4j-remote.jar mx4j.remote.url=${ibiblio.base}/maven/mx4j/jars/mx4j-remote-3.0.1.jar mx4j.remote.md5=bacb72e0fcad4259825c7cac1a0efe43 + +apr_win.dir=${depends.dir}/oss +apr_win.url=http://apache.rinet.ru/dist/apr/apr-1.2.7-win32-src.zip +apr_win.lib=${apr_win.dir}/apr-1.2.7-win32-src.zip +apr_win.md5=9e63d55178835b57781c561a73b68cd6 + +apr_lnx.dir=${depends.dir}/oss +apr_lnx.url=http://apache.rinet.ru/dist/apr/apr-1.2.7.tar.gz +apr_lnx.lib=${apr_lnx.dir}/apr-1.2.7.tar.gz +apr_lnx.md5=aea926cbe588f844ad9d317157d60175 +