/** * 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++; } } }
/** * Pads the given permissions with the specified ones: the permission bits that are not supported * (as reported by the supplied permissions mask} are replaced by those of the default permissions. * That means:<br/> * - if the mask indicates that all permission bits are supported (mask = 777 octal), the supplied permissions will * simply be returned, without using any of the default permissions<br/> * - if the mask indicates that none of the permission bits are supported (mask = 0), the default permissions will * be returned, without using any of the supplied permissions<br/> * * @param permissions the permissions to pad with default permissions for the bits that are not supported * @param defaultPermissions permissions to use for the bits that are not supported * @return the permissions padded with the default permissions */ public static FilePermissions padPermissions(FilePermissions permissions, FilePermissions defaultPermissions) { int permissionMask = permissions.getMask().getIntValue(); return new SimpleFilePermissions(( permissions.getIntValue() & permissionMask) | (~permissionMask & defaultPermissions.getIntValue()), defaultPermissions.getMask()); }
/** * 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++; } } }
PermissionBits setPermMask = tempFile.getChangeablePermissions(); int getPermMaskInt = getPermMask.getIntValue(); int setPermMaskInt = tempFile.getChangeablePermissions().getIntValue(); 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+")";
@Override public void run() { try { sleep(800); } catch (InterruptedException e) {} // Do not execute this block (cancel editing) if: // - a double click was made in the last second // - current row changed // - isEditing() is true which could happen if multiple clicks were made if ((System.currentTimeMillis() - lastDoubleClickTimestamp) > 1000 && row == currentRow) { if (column == Column.NAME) { if(!isEditing()) editCurrentFilename(); } else if(column == Column.DATE) { ActionManager.performAction(com.mucommander.ui.action.impl.ChangeDateAction.Descriptor.ACTION_ID, mainFrame); } else if(column == Column.PERMISSIONS) { if(getSelectedFile().getChangeablePermissions().getIntValue()!=0) ActionManager.performAction(com.mucommander.ui.action.impl.ChangePermissionsAction.Descriptor.ACTION_ID, mainFrame); } } } }.start();
@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; }
int permSetMask = firstFile.getChangeablePermissions().getIntValue(); boolean canSetPermission = permSetMask!=0; int defaultPerms = firstFile.getPermissions().getIntValue();
try { if(currentDestFile.getChangeablePermissions().getBitValue(PermissionAccess.USER, PermissionType.WRITE)) currentDestFile.changePermission(PermissionAccess.USER, PermissionType.WRITE, false);
/** * 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(); }
int supportedPerms = permissions.getMask().getIntValue();