@Override public int hashCode() { return toShort(); }
/** * Used to get short value of a proto {@link PMode}. * * @param pMode proto mode * @return short value */ public static short protoToShort(PMode pMode) { return Mode.fromProto(pMode).toShort(); }
@Override public OutputStream createDirect(String path, CreateOptions options) throws IOException { path = stripPath(path); if (options.getCreateParent()) { File parent = new File(path).getParentFile(); if (parent != null && !parent.mkdirs() && !parent.isDirectory()) { throw new IOException(ExceptionMessage.PARENT_CREATION_FAILED.getMessage(path)); } } OutputStream stream = new FileOutputStream(path); try { setMode(path, options.getMode().toShort()); } catch (IOException e) { stream.close(); throw e; } return stream; }
/** * Creates a new instance of {@link alluxio.util.webui.UIFileInfo}. * * @param fileInfo underlying {@link alluxio.util.webui.UIFileInfo.LocalFileInfo} */ public UIFileInfo(UIFileInfo.LocalFileInfo fileInfo) { mId = -1; mName = fileInfo.mName; mAbsolutePath = fileInfo.mAbsolutePath; mBlockSizeBytes = 0; mSize = fileInfo.mSize; mCreationTimeMs = fileInfo.mCreationTimeMs; mLastModificationTimeMs = fileInfo.mLastModificationTimeMs; mInAlluxio = false; mInAlluxioPercentage = 0; mIsDirectory = fileInfo.mIsDirectory; mPinned = false; mOwner = ""; mGroup = ""; mMode = FormatUtils.formatMode(Mode.createNoAccess().toShort(), true, false); mPersistenceState = PersistenceState.NOT_PERSISTED.name(); mFileLocations = new ArrayList<>(); }
@Override public OutputStream createDirect(String path, CreateOptions options) throws IOException { IOException te = null; FileSystem hdfs = getFs(); RetryPolicy retryPolicy = new CountingRetry(MAX_TRY); while (retryPolicy.attempt()) { try { // TODO(chaomin): support creating HDFS files with specified block size and replication. OutputStream outputStream = new HdfsUnderFileOutputStream( FileSystem.create(hdfs, new Path(path), new FsPermission(options.getMode().toShort()))); if (options.getAcl() != null) { setAclEntries(path, options.getAcl().getEntries()); } return outputStream; } catch (IOException e) { LOG.warn("Attempt count {} : {} ", retryPolicy.getAttemptCount(), e.getMessage()); te = e; } } throw te; }
/** * Tests the {@link Mode#toShort()} method. */ @Test public void toShort() { Mode mode = new Mode(Mode.Bits.ALL, Mode.Bits.READ_EXECUTE, Mode.Bits.READ_EXECUTE); assertEquals(0755, mode.toShort()); mode = Mode.defaults(); assertEquals(0777, mode.toShort()); mode = new Mode(Mode.Bits.READ_WRITE, Mode.Bits.READ, Mode.Bits.READ); assertEquals(0644, mode.toShort()); }
/** * Changes the permissions of directory or file with the path specified in args. * * @param path The {@link AlluxioURI} path as the input of the command * @param modeStr The new permission to be updated to the file or directory * @param recursive Whether change the permission recursively */ private void chmod(AlluxioURI path, String modeStr, boolean recursive) throws AlluxioException, IOException { Mode mode = ModeParser.parse(modeStr); SetAttributePOptions options = SetAttributePOptions.newBuilder().setMode(mode.toProto()).setRecursive(recursive).build(); mFileSystem.setAttribute(path, options); System.out .println("Changed permission of " + path + " to " + Integer.toOctalString(mode.toShort())); }
@Test public void defaults() { Mode mode = Mode.defaults(); assertEquals(Constants.DEFAULT_FILE_SYSTEM_MODE, mode.toShort()); }
/** * Tests {@link AccessControlList#setMode(short)}. */ @Test public void setMode() { AccessControlList acl = new AccessControlList(); short mode = new Mode(Mode.Bits.EXECUTE, Mode.Bits.WRITE, Mode.Bits.READ).toShort(); acl.setMode(mode); Assert.assertEquals(mode, acl.getMode()); }
acl.mExtendedEntries = null; acl.mMode = Mode.and(new Mode(mMode), defaultMode).toShort(); } else { updateMode.setOwnerBits(updateMode.getOwnerBits().and(defaultMode.getOwnerBits())); updateMode.setOtherBits(updateMode.getOtherBits().and(defaultMode.getOtherBits())); acl.mMode = updateMode.toShort();
/** * Helper function to create a path and set the permission to what specified in option. * * @param path path to construct the {@link AlluxioURI} from * @param context method context for creating a file */ private static void createAndSetPermission(String path, CreateFileContext context) throws Exception { try (LockedInodePath inodePath = sTree.lockInodePath(new AlluxioURI(path), LockPattern.WRITE_EDGE)) { List<Inode> result = sTree.createPath(RpcContext.NOOP, inodePath, context); MutableInode<?> inode = sInodeStore.getMutable(result.get(result.size() - 1).getId()).get(); inode.setOwner(context.getOwner()) .setGroup(context.getGroup()) .setMode(context.getMode().toShort()); sInodeStore.writeInode(inode); } }
/** * Tests the {@link Mode#createNoAccess()} method. */ @Test public void createNoAccess() { Mode mode = Mode.createNoAccess(); assertEquals(Mode.Bits.NONE, mode.getOwnerBits()); assertEquals(Mode.Bits.NONE, mode.getGroupBits()); assertEquals(Mode.Bits.NONE, mode.getOtherBits()); assertEquals(0000, mode.toShort()); }
/** * Tests the {@link Mode#Mode(Mode)} constructor. */ @Test public void copyConstructor() { Mode mode = new Mode(Mode.defaults()); assertEquals(Mode.Bits.ALL, mode.getOwnerBits()); assertEquals(Mode.Bits.ALL, mode.getGroupBits()); assertEquals(Mode.Bits.ALL, mode.getOtherBits()); assertEquals(0777, mode.toShort()); }
/** * Tests the {@link Mode#getUMask()} and * {@link Mode#applyUMask(Mode)} methods. */ @Test public void umask() { String umask = "0022"; Mode mode = ModeUtils.applyDirectoryUMask(Mode.defaults(), umask); assertEquals(Mode.Bits.ALL, mode.getOwnerBits()); assertEquals(Mode.Bits.READ_EXECUTE, mode.getGroupBits()); assertEquals(Mode.Bits.READ_EXECUTE, mode.getOtherBits()); assertEquals(0755, mode.toShort()); }
/** * Tests the {@link MutableInodeDirectory#getMode()} method. */ @Test public void permissionStatus() { MutableInodeDirectory inode2 = createInodeDirectory(); Assert.assertEquals(TEST_OWNER, inode2.getOwner()); Assert.assertEquals(TEST_GROUP, inode2.getGroup()); Assert.assertEquals(ModeUtils.applyDirectoryUMask(Mode.defaults(), ServerConfiguration.get(PropertyKey.SECURITY_AUTHORIZATION_PERMISSION_UMASK)).toShort(), inode2.getMode()); }
/** * Tests the {@link MutableInodeFile#getMode()} method. */ @Test public void permissionStatus() { MutableInodeFile inode1 = createInodeFile(1); assertEquals(TEST_OWNER, inode1.getOwner()); assertEquals(TEST_GROUP, inode1.getGroup()); assertEquals(ModeUtils.applyFileUMask(Mode.defaults(), ServerConfiguration.get(PropertyKey.SECURITY_AUTHORIZATION_PERMISSION_UMASK)).toShort(), inode1.getMode()); } }
/** * Creates an {@link MutableInodeDirectory}. * * @param id id of this inode * @param parentId id of the parent of this inode * @param name name of this inode * @param context context to create this directory * @return the {@link MutableInodeDirectory} representation */ public static MutableInodeDirectory create(long id, long parentId, String name, CreateDirectoryContext context) { return new MutableInodeDirectory(id) .setParentId(parentId) .setName(name) .setTtl(context.getTtl()) .setTtlAction(context.getTtlAction()) .setOwner(context.getOwner()) .setGroup(context.getGroup()) .setMode(context.getMode().toShort()) .setAcl(context.getAcl()) // SetAcl call is also setting default AclEntries .setAcl(context.getDefaultAcl()) .setMountPoint(context.isMountPoint()); }
/** * Tests the {@link InodeTree#createPath(RpcContext, LockedInodePath, CreatePathContext)} * method for creating a file. */ @Test public void createFile() throws Exception { // created nested file createPath(mTree, NESTED_FILE_URI, sNestedFileContext); MutableInode<?> nestedFile = getInodeByPath(NESTED_FILE_URI); assertEquals("file", nestedFile.getName()); assertEquals(2, nestedFile.getParentId()); assertTrue(nestedFile.isFile()); assertEquals("user1", nestedFile.getOwner()); assertEquals("group1", nestedFile.getGroup()); assertEquals(TEST_FILE_MODE.toShort(), nestedFile.getMode()); }
/** * Tests the {@link InodeTree#createPath(RpcContext, LockedInodePath, CreatePathContext)} * method for creating directories. */ @Test public void createDirectory() throws Exception { // create directory createPath(mTree, TEST_URI, sDirectoryContext); assertTrue(mTree.inodePathExists(TEST_URI)); MutableInode<?> test = getInodeByPath(TEST_URI); assertEquals(TEST_PATH, test.getName()); assertTrue(test.isDirectory()); assertEquals("user1", test.getOwner()); assertEquals("group1", test.getGroup()); assertEquals(TEST_DIR_MODE.toShort(), test.getMode()); // create nested directory createPath(mTree, NESTED_URI, sNestedDirectoryContext); assertTrue(mTree.inodePathExists(NESTED_URI)); MutableInode<?> nested = getInodeByPath(NESTED_URI); assertEquals(TEST_PATH, nested.getName()); assertEquals(2, nested.getParentId()); assertTrue(test.isDirectory()); assertEquals("user1", test.getOwner()); assertEquals("group1", test.getGroup()); assertEquals(TEST_DIR_MODE.toShort(), test.getMode()); }
/** * Tests {@link AccessControlList#getMode()}. */ @Test public void getMode() { AccessControlList acl = new AccessControlList(); Assert.assertEquals(0, acl.getMode()); acl.setEntry(new AclEntry.Builder().setType(AclEntryType.OWNING_USER).setSubject(OWNING_USER) .addAction(AclAction.READ).build()); acl.setEntry(new AclEntry.Builder().setType(AclEntryType.OWNING_GROUP).setSubject(OWNING_GROUP) .addAction(AclAction.WRITE).build()); acl.setEntry(new AclEntry.Builder().setType(AclEntryType.OTHER) .addAction(AclAction.EXECUTE).build()); Assert.assertEquals(new Mode(Mode.Bits.READ, Mode.Bits.WRITE, Mode.Bits.EXECUTE).toShort(), acl.getMode()); }