Author: mturk Date: Tue Jun 30 14:08:24 2009 New Revision: 789743 URL: http://svn.apache.org/viewvc?rev=789743&view=rev Log: Add getFileAttributes method
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/test/org/apache/commons/runtime/TestFile.java 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=789743&r1=789742&r2=789743&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 Tue Jun 30 14:08:24 2009 @@ -55,6 +55,8 @@ throws IOException, SecurityException; private static native boolean attrs0(String pathname, int attr, int mask) throws IOException, SecurityException; + private static native int attrg0(String pathname) + throws IOException, SecurityException; // Cached FileType Enum integer value. private int fileType = -1; @@ -252,6 +254,28 @@ } /** + * Get attributes of the file denoted by this abstract pathname. + * <p> + * For getting the set of complete file attributes use the + * {...@code getFileProtection } method. + * </p> + * <pre> + * READONLY - file is readonly + * EXECUTABLE - file is executable + * HIDDEN - file is hidden + * </pre> + * @return Set of {...@code FileAttributes}. + * @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 EnumSet<FileAttributes> getFileAttributes() + throws IOException, SecurityException + { + return FileAttributes.valueOf(attrg0(getPath())); + } + + /** * 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=789743&r1=789742&r2=789743&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 Tue Jun 30 14:08:24 2009 @@ -461,3 +461,31 @@ return JNI_TRUE; } +ACR_IO_EXPORT_DECLARE(int, File, attrg0)(ACR_JNISTDARGS, jstring pathname) +{ + int attr = 0; + + UNREFERENCED_O; + + WITH_CSTR(pathname) { + char *sep; + int protection = ACR_FileProtectionGet(_E, J2S(pathname)); + if (protection >= 0) { + if ((protection & ACR_FPROT_UWRITE) != ACR_FPROT_UWRITE) + attr |= ACR_FILE_ATTR_READONLY; + if ((protection & (ACR_FPROT_UEXECUTE | ACR_FPROT_GEXECUTE | ACR_FPROT_WEXECUTE))) + attr |= ACR_FILE_ATTR_EXECUTABLE; + } + if ((sep = strrchr(J2S(pathname), '/')) != NULL) { + /* Presume dot files are hidden + */ + if (*(sep + 1) == '.') + attr |= ACR_FILE_ATTR_HIDDEN; + } + else if (*J2S(pathname) == '.') + attr |= ACR_FILE_ATTR_HIDDEN; + } END_WITH_CSTR(pathname); + + return attr; +} + Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java?rev=789743&r1=789742&r2=789743&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java Tue Jun 30 14:08:24 2009 @@ -272,6 +272,42 @@ f.delete(); } + public void testGetAttributes() + throws Exception + { + File f = new File("ffoo"); + f.createNewFile(); + EnumSet <FileProtection> fp = f.getFileProtection(); + System.out.println(); + System.out.println("Org Rdonly Protection " + fp); + // Set the rdonly attributes + f.setFileAttributes(EnumSet.of(FileAttributes.READONLY), EnumSet.allOf(FileAttributes.class)); + EnumSet<FileAttributes> fa = f.getFileAttributes(); + System.out.println("Get FileAttributes " + fa); + assertTrue("READONLY", fa.contains(FileAttributes.READONLY)); + // Clear the rdonly attributes + f.setFileAttributes(EnumSet.noneOf(FileAttributes.class), EnumSet.of(FileAttributes.READONLY)); + f.delete(); + } + + public void testGetHiddenAttributes() + throws Exception + { + File f = new File(".ffoo"); + f.createNewFile(); + EnumSet <FileProtection> fp = f.getFileProtection(); + System.out.println(); + System.out.println("Org Rdonly Protection " + fp); + // Set the rdonly attributes + f.setFileAttributes(EnumSet.of(FileAttributes.HIDDEN), EnumSet.allOf(FileAttributes.class)); + EnumSet<FileAttributes> fa = f.getFileAttributes(); + System.out.println("Get Hidden FileAttributes " + fa); + assertTrue("HIDDEN", fa.contains(FileAttributes.HIDDEN)); + // Clear the rdonly attributes + f.setFileAttributes(EnumSet.noneOf(FileAttributes.class), EnumSet.of(FileAttributes.READONLY)); + f.delete(); + } + public void testFileId() throws Exception {