/** * Creates and returns a permissions int using the values of the permission checkboxes. */ private int getPermInt() { JCheckBox permCheckBox; int perms = 0; for(PermissionAccess a : PermissionAccess.reverseValues()) { for(PermissionType p : PermissionType.reverseValues()) { permCheckBox = permCheckBoxes[a.toInt()][p.toInt()]; if(permCheckBox.isSelected()) perms |= (p.toInt()<<a.toInt()*3); } } return perms; }
public static List<PermissionAccess> reverseValues() { List<PermissionAccess> values = Arrays.asList(PermissionAccess.values()); Collections.reverse(values); return values; } }
public boolean getBitValue(PermissionAccess access, PermissionType type) { return (permissions & (type.toInt() << (access.toInt()*3))) != 0; } }
@Override public void changePermission(PermissionAccess access, PermissionType permission, boolean enabled) throws IOException, UnsupportedFileOperationException { changePermissions(ByteUtils.setBit(permissions.getIntValue(), (permission.toInt() << (access.toInt()*3)), enabled)); }
/** * Updates the permission checkboxes' values to reflect the octal permissions text field. */ private void updatePermCheckBoxes() { JCheckBox permCheckBox; String octalStr = octalPermTextField.getText(); int perms = octalStr.equals("")?0:Integer.parseInt(octalStr, 8); for(PermissionAccess a : PermissionAccess.reverseValues()) { for(PermissionType p : PermissionType.reverseValues()) { permCheckBox = permCheckBoxes[a.toInt()][p.toInt()]; // if(permCheckBox.isEnabled()) permCheckBox.setSelected((perms & (p.toInt()<<a.toInt()*3))!=0); } } }
@Override public void changePermission(PermissionAccess access, PermissionType permission, boolean enabled) throws IOException, UnsupportedFileOperationException { changePermissions(ByteUtils.setBit(getPermissions().getIntValue(), (permission.toInt() << (access.toInt()*3)), enabled)); }
public int getIntValue() { int bitShift = 0; int perms = 0; for(PermissionAccess a : PermissionAccess.values()) { for(PermissionType p : PermissionType.values()) { if(getBitValue(a, p)) perms |= (1<<bitShift); bitShift++; } } return perms; } }
gridPanel.add(new JLabel(Translator.get("permissions.executable"))); for(PermissionAccess a : PermissionAccess.reverseValues()) { gridPanel.add(new JLabel(Translator.get(a==PermissionAccess.USER ?"permissions.user":a==PermissionAccess.GROUP?"permissions.group":"permissions.other"))); permCheckBox.setSelected((defaultPerms & (p.toInt()<<a.toInt()*3))!=0); if((permSetMask & (p.toInt()<<a.toInt()*3))==0) permCheckBox.setEnabled(false); else permCheckBoxes[a.toInt()][p.toInt()] = permCheckBox;
@Override public void changePermission(PermissionAccess access, PermissionType permission, boolean enabled) throws IOException { changePermissions(ByteUtils.setBit(getPermissions().getIntValue(), (permission.toInt() << (access.toInt()*3)), enabled)); }
/** * 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++; } } }
int bitShift = PermissionAccess.USER.toInt() *3; for(PermissionAccess a : PermissionAccess.reverseValues()) {
@Override public void changePermission(PermissionAccess access, PermissionType permission, boolean enabled) throws IOException { changePermissions(ByteUtils.setBit(getPermissions().getIntValue(), (permission.toInt() << (access.toInt()*3)), enabled)); }
/** * 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++; } } }
boolean canGetPermission, canSetPermission; for(PermissionAccess a : PermissionAccess.values()) { for(PermissionType p : PermissionType.values()) { bitMask = 1<<bitShift;