@Override public void changePermission(PermissionAccess access, PermissionType permission, boolean enabled) throws IOException, UnsupportedFileOperationException { file.changePermission(access, permission, enabled); }
tempFile.changePermission(a, p, enabled); tempFile.changePermissions(enabled?bitMask:(0777&~bitMask));
/** * Changes this file's permissions to the specified permissions int. * The permissions int should be constructed using the permission types and accesses defined in * {@link com.mucommander.commons.file.PermissionType} and {@link com.mucommander.commons.file.PermissionAccess}. * * <p>Implementation note: the default implementation of this method calls sequentially {@link #changePermission(int, int, boolean)}, * for each permission and access (that's a total 9 calls). This may affect performance on filesystems which need * to perform an I/O request to change each permission individually. In that case, and if the fileystem allows * to change all permissions at once, this method should be overridden.</p> * * @param permissions new permissions for this file * @throws IOException if the permissions couldn't be changed, either because of insufficient permissions or because * of an I/O error. * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported * or not implemented by the underlying filesystem. */ public void changePermissions(int permissions) throws IOException, UnsupportedFileOperationException { int bitShift = 0; PermissionBits mask = getChangeablePermissions(); for(PermissionAccess a : PermissionAccess.values()) { for(PermissionType p : PermissionType.values()) { if(mask.getBitValue(a, p)) changePermission(a, p, (permissions & (1<<bitShift))!=0); bitShift++; } } }
@Override protected boolean processFile(AbstractFile file, Object recurseParams) { if(!super.processFile(file, recurseParams)) return false; // TODO: temporary files seem to be left after the JVM quits under Mac OS X, even if the files permissions are unchanged // Add the file to the list of files to open, only if it is one of the top-level files if(filesToOpen.indexOf(file)!=-1) { if(!currentDestFile.isDirectory()) { // Do not change directories' permissions try { // Make the temporary file read only if(currentDestFile.getChangeablePermissions().getBitValue(PermissionAccess.USER, PermissionType.WRITE)) currentDestFile.changePermission(PermissionAccess.USER, PermissionType.WRITE, false); } catch(IOException e) { LOGGER.debug("Caught exeception while changing permissions of "+currentDestFile, e); return false; } } tempFiles.add(currentDestFile); } return true; }
currentDestFile.changePermission(PermissionAccess.USER, PermissionType.WRITE, false);
/** * Validates that FileMonitor properly reports {@link FileMonitor#PERMISSIONS_ATTRIBUTE} changes when a file's * permissions change. * * @throws IOException should not normally happen */ @Test public void testPermissionsAttribute() throws IOException { setUp(PERMISSIONS_ATTRIBUTE); file.changePermission(PermissionAccess.USER, PermissionType.WRITE, !file.getPermissions().getBitValue(PermissionAccess.USER, PermissionType.WRITE)); assert hasAttributeChanged(PERMISSIONS_ATTRIBUTE); }
/** * Tests {@link AbstractFile#changePermissions(int)} when the operation is not supported. * * @throws IOException should not happen */ protected void testChangePermissionsUnsupported() throws IOException { // Assert that #changePermission throws a proper UnsupportedFileOperationException when called UnsupportedFileOperationException e = null; try { tempFile.changePermission(PermissionAccess.USER, PermissionType.WRITE, true); } catch(UnsupportedFileOperationException ex) { e = ex; } assertUnsupportedFileOperationException(e, FileOperation.CHANGE_PERMISSION); // Assert that #getChangeablePermissions() returns empty permission bits assert PermissionBits.EMPTY_PERMISSION_INT == tempFile.getChangeablePermissions().getIntValue(); }