/** * 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; }
try { if(currentDestFile.getChangeablePermissions().getBitValue(PermissionAccess.USER, PermissionType.WRITE)) currentDestFile.changePermission(PermissionAccess.USER, PermissionType.WRITE, false);
/** * Tests {@link AbstractFile#getPermissions()}. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ @Test public void testGetPermissions() throws IOException, NoSuchAlgorithmException { assert tempFile.getPermissions() != null; createFile(tempFile, 0); FilePermissions permissions = tempFile.getPermissions(); PermissionBits getPermMask = permissions.getMask(); assert permissions != null; int getPermMaskInt = getPermMask.getIntValue(); int bitShift = 0; int bitMask; boolean canGetPermission; for(PermissionAccess a : PermissionAccess.values()) { for(PermissionType p : PermissionType.values()) { bitMask = 1<<bitShift; canGetPermission = (getPermMaskInt & bitMask)!=0; assert getPermMask.getBitValue(a, p)==canGetPermission: "inconsistent bit and int value for ("+a+", "+p+")"; if(canGetPermission) { assert permissions.getBitValue(a, p)==((permissions.getIntValue() & bitMask)!=0): "inconsistent bit and int value for ("+a+", "+p+")"; } bitShift++; } } }
assert getPermMask.getBitValue(a, p)==canGetPermission:"inconsistent bit and int value for ("+a+", "+p+")"; assert setPermMask.getBitValue(a, p)==canSetPermission: "inconsistent bit and int value for ("+a+", "+p+")";