83 DEF_STATIC_JNI_OnLoad 84 #endif 85 86 /* 87 * The ZFILE_* functions exist to provide some platform-independence with 88 * respect to file access needs. 89 */ 90 91 /* 92 * Opens the named file for reading, returning a ZFILE. 93 * 94 * Compare this with winFileHandleOpen in windows/native/java/io/io_util_md.c. 95 * This function does not take JNIEnv* and uses CreateFile (instead of 96 * CreateFileW). The expectation is that this function will be called only 97 * from ZIP_Open_Generic, which in turn is used by the JVM, where we do not 98 * need to concern ourselves with wide chars. 99 */ 100 static ZFILE 101 ZFILE_Open(const char *fname, int flags) { 102 #ifdef WIN32 103 const DWORD access = 104 (flags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) : 105 (flags & O_WRONLY) ? GENERIC_WRITE : 106 GENERIC_READ; 107 const DWORD sharing = 108 FILE_SHARE_READ | FILE_SHARE_WRITE; 109 const DWORD disposition = 110 /* Note: O_TRUNC overrides O_CREAT */ 111 (flags & O_TRUNC) ? CREATE_ALWAYS : 112 (flags & O_CREAT) ? OPEN_ALWAYS : 113 OPEN_EXISTING; 114 const DWORD maybeWriteThrough = 115 (flags & (O_SYNC | O_DSYNC)) ? 116 FILE_FLAG_WRITE_THROUGH : 117 FILE_ATTRIBUTE_NORMAL; 118 const DWORD maybeDeleteOnClose = 119 (flags & O_TEMPORARY) ? 120 FILE_FLAG_DELETE_ON_CLOSE : 121 FILE_ATTRIBUTE_NORMAL; 122 const DWORD flagsAndAttributes = maybeWriteThrough | maybeDeleteOnClose; 123 124 return (jlong) CreateFile( 125 fname, /* Wide char path name */ 126 access, /* Read and/or write permission */ 127 sharing, /* File sharing flags */ 128 NULL, /* Security attributes */ 129 disposition, /* creation disposition */ 130 flagsAndAttributes, /* flags and attributes */ 131 NULL); 132 #else 133 return open(fname, flags, 0); 134 #endif 135 } 136 137 /* 138 * The io_util_md.h files do not provide IO_CLOSE, hence we use platform 139 * specifics. 140 */ 141 static void 142 ZFILE_Close(ZFILE zfd) { 143 #ifdef WIN32 144 CloseHandle((HANDLE) zfd); 145 #else 146 close(zfd); 147 #endif 148 } 149 150 static int 151 ZFILE_read(ZFILE zfd, char *buf, jint nbytes) { | 83 DEF_STATIC_JNI_OnLoad 84 #endif 85 86 /* 87 * The ZFILE_* functions exist to provide some platform-independence with 88 * respect to file access needs. 89 */ 90 91 /* 92 * Opens the named file for reading, returning a ZFILE. 93 * 94 * Compare this with winFileHandleOpen in windows/native/java/io/io_util_md.c. 95 * This function does not take JNIEnv* and uses CreateFile (instead of 96 * CreateFileW). The expectation is that this function will be called only 97 * from ZIP_Open_Generic, which in turn is used by the JVM, where we do not 98 * need to concern ourselves with wide chars. 99 */ 100 static ZFILE 101 ZFILE_Open(const char *fname, int flags) { 102 #ifdef WIN32 103 WCHAR *wfname, *wprefixed_fname; 104 size_t converted_chars, fname_length; 105 jlong fhandle; 106 const DWORD access = 107 (flags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) : 108 (flags & O_WRONLY) ? GENERIC_WRITE : 109 GENERIC_READ; 110 const DWORD sharing = 111 FILE_SHARE_READ | FILE_SHARE_WRITE; 112 const DWORD disposition = 113 /* Note: O_TRUNC overrides O_CREAT */ 114 (flags & O_TRUNC) ? CREATE_ALWAYS : 115 (flags & O_CREAT) ? OPEN_ALWAYS : 116 OPEN_EXISTING; 117 const DWORD maybeWriteThrough = 118 (flags & (O_SYNC | O_DSYNC)) ? 119 FILE_FLAG_WRITE_THROUGH : 120 FILE_ATTRIBUTE_NORMAL; 121 const DWORD maybeDeleteOnClose = 122 (flags & O_TEMPORARY) ? 123 FILE_FLAG_DELETE_ON_CLOSE : 124 FILE_ATTRIBUTE_NORMAL; 125 const DWORD flagsAndAttributes = maybeWriteThrough | maybeDeleteOnClose; 126 127 fname_length = strlen(fname); 128 if (fname_length < MAX_PATH) { 129 return (jlong)CreateFile( 130 fname, /* Ascii char path name */ 131 access, /* Read and/or write permission */ 132 sharing, /* File sharing flags */ 133 NULL, /* Security attributes */ 134 disposition, /* creation disposition */ 135 flagsAndAttributes, /* flags and attributes */ 136 NULL); 137 } else { 138 if ((wfname = (WCHAR*)malloc((fname_length + 1) * sizeof(WCHAR))) == NULL) 139 return (jlong)INVALID_HANDLE_VALUE; 140 141 if (mbstowcs_s(&converted_chars, wfname, fname_length + 1, fname, fname_length) != 0) { 142 free(wfname); 143 return (jlong)INVALID_HANDLE_VALUE; 144 } 145 wprefixed_fname = getPrefixed(wfname, (int)fname_length); 146 fhandle = (jlong)CreateFileW( 147 wprefixed_fname, /* Wide char path name */ 148 access, /* Read and/or write permission */ 149 sharing, /* File sharing flags */ 150 NULL, /* Security attributes */ 151 disposition, /* creation disposition */ 152 flagsAndAttributes, /* flags and attributes */ 153 NULL); 154 free(wfname); 155 free(wprefixed_fname); 156 return fhandle; 157 } 158 #else 159 return open(fname, flags, 0); 160 #endif 161 } 162 163 /* 164 * The io_util_md.h files do not provide IO_CLOSE, hence we use platform 165 * specifics. 166 */ 167 static void 168 ZFILE_Close(ZFILE zfd) { 169 #ifdef WIN32 170 CloseHandle((HANDLE) zfd); 171 #else 172 close(zfd); 173 #endif 174 } 175 176 static int 177 ZFILE_read(ZFILE zfd, char *buf, jint nbytes) { |