Author: mturk Date: Sat Jun 13 07:37:10 2009 New Revision: 784345 URL: http://svn.apache.org/viewvc?rev=784345&view=rev Log: Implement setFileAttributes method from apr_file_attrs_set()
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java?rev=784345&r1=784344&r2=784345&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java Sat Jun 13 07:37:10 2009 @@ -51,6 +51,8 @@ throws IOException, SecurityException; private static native String target0(String link) throws IOException, SecurityException; + private static native boolean attrs0(String pathname, int attr, int mask) + throws IOException, SecurityException; // Catched FileType Enum integer value. private int fileType = -1; @@ -218,6 +220,36 @@ } /** + * Set attributes of the file denoted by this abstract pathname. + * <p> + * This function should be used in preference to explict manipulation + * of the file permissions, because the operations to provide these + * attributes are platform specific and may involve more than simply + * setting permission bits. + * </p> + * + * @param attributes Set of {...@code FileAttributes}. + * <pre> + * READONLY - make the file readonly + * EXECUTABLE - make the file executable + * HIDDEN - make the file hidden + * </pre> + * @param mask Mask of valid bits in attributes. + * @return {...@code true} if the file attributes were set. + * @throws IOException If an I/O error occured. + * @throws SecurityException If Search permission is denied for one of + * the directories in the path prefix this {...@code File} path. + */ + public boolean setFileAttributes(EnumSet<FileAttributes> attributes, + EnumSet<FileAttributes> mask) + throws IOException, SecurityException + { + return attrs0(getPath(), + FileAttributes.bitmapOf(attributes), + FileAttributes.bitmapOf(mask)); + } + + /** * Returns {...@code true} if the file denoted by this abstract * pathname is symbolic link. * Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c?rev=784345&r1=784344&r2=784345&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c Sat Jun 13 07:37:10 2009 @@ -362,3 +362,49 @@ else return JNI_TRUE; } + +ACR_IO_EXPORT_DECLARE(jboolean, File, attrs0)(ACR_JNISTDARGS, jstring pathname, + jint attr, jint mask) +{ + int rc = -1; + + UNREFERENCED_O; + + WITH_CSTR(pathname) { + int protection = ACR_FileProtectionGet(_E, J2S(pathname)); + if (protection >= 0) { + if (mask & ACR_FILE_ATTR_READONLY) { + if (attr & ACR_FILE_ATTR_READONLY) { + protection &= ~ACR_FPROT_UWRITE; + protection &= ~ACR_FPROT_GWRITE; + protection &= ~ACR_FPROT_WWRITE; + } + else { + /* ### umask this! */ + protection |= ACR_FPROT_UWRITE; + protection |= ACR_FPROT_GWRITE; + protection |= ACR_FPROT_WWRITE; + } + } + if (mask & ACR_FILE_ATTR_EXECUTABLE) { + if (attr & ACR_FILE_ATTR_EXECUTABLE) { + /* ### umask this! */ + protection |= ACR_FPROT_UEXECUTE; + protection |= ACR_FPROT_GEXECUTE; + protection |= ACR_FPROT_WEXECUTE; + } + else { + protection &= ~ACR_FPROT_UEXECUTE; + protection &= ~ACR_FPROT_GEXECUTE; + protection &= ~ACR_FPROT_WEXECUTE; + } + } + rc = ACR_FileProtectionSet(_E, J2S(pathname), protection); + } + } END_WITH_CSTR(pathname); + + if (rc) + return JNI_FALSE; + else + return JNI_TRUE; +} Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c?rev=784345&r1=784344&r2=784345&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c Sat Jun 13 07:37:10 2009 @@ -660,14 +660,60 @@ NULL); } END_WITH_WSTR(pathname); ACR_SetTokenPrivilege(L"SeTakeOwnershipPrivilege", FALSE); - if (rc) { + if (rc == ERROR_SUCCESS) + return JNI_TRUE; + else { rc = ACR_FROM_OS_ERROR(rc); if (ACR_STATUS_IS_EACCES(rc)) ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); else ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); return JNI_FALSE; - } - else + } +} + +ACR_IO_EXPORT_DECLARE(jboolean, File, attrs0)(ACR_JNISTDARGS, jstring pathname, + jint attr, jint mask) +{ + int rc = EINVAL; + DWORD flags; + + UNREFERENCED_O; + + /* Don't do anything if we can't handle the requested attributes */ + if (!(mask & (ACR_FILE_ATTR_READONLY | ACR_FILE_ATTR_HIDDEN))) return JNI_TRUE; + + WITH_WSTR(pathname) { + flags = GetFileAttributesW(J2W(pathname)); + if (flags != 0xFFFFFFFF) + rc = GetLastError(); + else { + if (mask & ACR_FILE_ATTR_READONLY) { + if (attr & ACR_FILE_ATTR_READONLY) + flags |= FILE_ATTRIBUTE_READONLY; + else + flags &= ~FILE_ATTRIBUTE_READONLY; + } + if (mask & ACR_FILE_ATTR_HIDDEN) { + if (attr & ACR_FILE_ATTR_HIDDEN) + flags |= FILE_ATTRIBUTE_HIDDEN; + else + flags &= ~FILE_ATTRIBUTE_HIDDEN; + } + rc = SetFileAttributesW(J2W(pathname), flags); + } + } END_WITH_WSTR(pathname); + + if (rc == ERROR_SUCCESS) + return JNI_TRUE; + else { + rc = ACR_FROM_OS_ERROR(rc); + if (ACR_STATUS_IS_EACCES(rc)) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); + return JNI_FALSE; + } } + Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c?rev=784345&r1=784344&r2=784345&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c Sat Jun 13 07:37:10 2009 @@ -64,8 +64,6 @@ { if (acr_ioh_tab) return EEXIST; - InitializeCriticalSection(&ios_lock); - if (size > 0) { int i, s; for (i = 10; i < 27; i++) { @@ -97,6 +95,7 @@ acr_ioh_tab = NULL; return rv; } + InitializeCriticalSection(&ios_lock); return 0; } @@ -193,7 +192,9 @@ void acr_ioh_cleanup() { int i; - + + if (!__bitmap) + return; EnterCriticalSection(&ios_lock); for (i = 0; i < acr_ioh_mask; i++) { if (acr_ioh_tab[i].h) { Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c?rev=784345&r1=784344&r2=784345&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c Sat Jun 13 07:37:10 2009 @@ -129,12 +129,14 @@ "ws2_32.dll", "shell32.dll", "advapi32.dll", - "jvm.dll" + "jvm.dll", + "ktmw32.dll" }; -static HMODULE late_dll_handles[SYSDLL_defined] = { NULL, NULL, NULL, - NULL, NULL, NULL, - NULL, NULL, NULL }; +static HMODULE late_dll_handles[SYSDLL_defined] = { + NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL +}; FARPROC acr_load_dll_func(acr_dlltoken_e fnLib, const char* fnName, int ordinal) { @@ -179,8 +181,10 @@ rc = GetLastError(); SetErrorMode(em); } - if (!late_dll_handles[fnLib]) + if (!late_dll_handles[fnLib] && (fnLib < SYSDLL_KTMW32)) { + /* Unable to load required library */ return rc; + } } } return ERROR_SUCCESS; @@ -261,7 +265,7 @@ * Return the error to the application instead */ em = SetErrorMode(0); - SetErrorMode( em | SEM_NOOPENFILEERRORBOX); + SetErrorMode(em | SEM_NOOPENFILEERRORBOX); return JNI_VERSION_1_4; } Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c?rev=784345&r1=784344&r2=784345&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c Sat Jun 13 07:37:10 2009 @@ -49,12 +49,9 @@ ACR_JNI_EXPORT_DECLARE(jstring, OS, getVersion)(ACR_JNISTDARGS) { - char buf[ACR_SBUFF_SIZ]; + char buf[ACR_SBUFF_SIZ] = { '\0' }; switch (acr_osver->dwMajorVersion) { - case 4: - strcpy(buf, "NT4"); - break; case 5: switch (acr_osver->dwMinorVersion) { case 0: @@ -98,11 +95,12 @@ break; } break; - default: - sprintf(buf, "%d.%d", acr_osver->dwMajorVersion, - acr_osver->dwMinorVersion); - break; } + if (!buf[0]) { + /* Use the genric number format */ + sprintf(buf, "%d.%d", acr_osver->dwMajorVersion, + acr_osver->dwMinorVersion); + } if (acr_osver->szCSDVersion[0]) { strcat(buf, " ("); strncat(buf, acr_osver->szCSDVersion, ACR_SBUFF_LEN - 32); @@ -153,7 +151,8 @@ strcpy(buf, "ia64"); case PROCESSOR_ARCHITECTURE_INTEL: #if !defined(_WIN64) - if (acr_osinf->wProcessorLevel > 2 && acr_osinf->wProcessorLevel < 7) + if (acr_osinf->wProcessorLevel > 2 && + acr_osinf->wProcessorLevel < 7) sprintf(buf, "i%d86", acr_osinf->wProcessorLevel); else #endif