/** * Returns the FsPermission in an octal format. * * @return short Unlike {@link #toShort()} which provides a binary * representation, this method returns the standard octal style permission. */ public short toOctal() { int n = this.toShort(); int octal = (n>>>9&1)*1000 + (n>>>6&7)*100 + (n>>>3&7)*10 + (n&7); return (short)octal; }
/** * @param perm Permission. * @return String. */ private static String toString(FsPermission perm) { return String.format("%04o", perm.toShort()); }
/** * @param perm Permission. * @return String. */ private static String toString(FsPermission perm) { return String.format("%04o", perm.toShort()); }
/** * Encodes the object to a short. Unlike {@link #toShort()}, this method may * return values outside the fixed range 00000 - 01777 if extended features * are encoded into this permission, such as the ACL bit. * * @return short extended short representation of this permission */ @Deprecated public short toExtendedShort() { return toShort(); }
private static void checkReturnValue(boolean rv, File p, FsPermission permission ) throws IOException { if (!rv) { throw new IOException("Failed to set permissions of path: " + p + " to " + String.format("%04o", permission.toShort())); } }
@Override @Deprecated public void write(DataOutput out) throws IOException { out.writeShort(toShort()); }
/** Set the user file creation mask (umask) */ public static void setUMask(Configuration conf, FsPermission umask) { conf.set(UMASK_LABEL, String.format("%1$03o", umask.toShort())); }
/** * Helper method that serializes a {@link FsPermission} object. */ private static void serializeFsPermissions(State state, String key, FsPermission fsPermissions) { state.setProp(key, String.format("%04o", fsPermissions.toShort())); }
private static void execSetPermission(File f, FsPermission permission ) throws IOException { if (NativeIO.isAvailable()) { NativeIO.POSIX.chmod(f.getCanonicalPath(), permission.toShort()); } else { execCommand(f, Shell.getSetPermissionCommand( String.format("%04o", permission.toShort()), false)); } }
@Override public UfsFileStatus getFileStatus(String path) throws IOException { Path tPath = new Path(path); FileSystem hdfs = getFs(); FileStatus fs = hdfs.getFileStatus(tPath); String contentHash = UnderFileSystemUtils.approximateContentHash(fs.getLen(), fs.getModificationTime()); return new UfsFileStatus(path, contentHash, fs.getLen(), fs.getModificationTime(), fs.getOwner(), fs.getGroup(), fs.getPermission().toShort()); }
/** * Apply permission against specified file and determine what the * new mode would be * @param file File against which to apply mode * @return File's new mode if applied. */ public short applyNewPermission(FileStatus file) { FsPermission perms = file.getPermission(); int existing = perms.toShort(); boolean exeOk = file.isDirectory() || (existing & 0111) != 0; return (short)combineModes(existing, exeOk); } }
@Override public UfsDirectoryStatus getDirectoryStatus(String path) throws IOException { Path tPath = new Path(path); FileSystem hdfs = getFs(); FileStatus fs = hdfs.getFileStatus(tPath); return new UfsDirectoryStatus(path, fs.getOwner(), fs.getGroup(), fs.getPermission().toShort(), fs.getModificationTime()); }
/** * Deserializes a {@link FsPermission}s object that should be used when a {@link DataWriter} is creating directories. */ public static FsPermission deserializeWriterDirPermissions(State state, int numBranches, int branchId) { return new FsPermission(state.getPropAsShortWithRadix( ForkOperatorUtils.getPropertyNameForBranch(ConfigurationKeys.WRITER_DIR_PERMISSIONS, numBranches, branchId), FsPermission.getDefault().toShort(), ConfigurationKeys.PERMISSION_PARSING_RADIX)); }
/** * Deserializes a {@link FsPermission}s object that should be used when a {@link DataWriter} is writing a file. */ public static FsPermission deserializeWriterFilePermissions(State state, int numBranches, int branchId) { return new FsPermission(state.getPropAsShortWithRadix( ForkOperatorUtils.getPropertyNameForBranch(ConfigurationKeys.WRITER_FILE_PERMISSIONS, numBranches, branchId), FsPermission.getDefault().toShort(), ConfigurationKeys.PERMISSION_PARSING_RADIX)); }
@Override public UfsStatus getStatus(String path) throws IOException { Path tPath = new Path(path); FileSystem hdfs = getFs(); FileStatus fs = hdfs.getFileStatus(tPath); if (!fs.isDir()) { // Return file status. String contentHash = UnderFileSystemUtils.approximateContentHash(fs.getLen(), fs.getModificationTime()); return new UfsFileStatus(path, contentHash, fs.getLen(), fs.getModificationTime(), fs.getOwner(), fs.getGroup(), fs.getPermission().toShort()); } // Return directory status. return new UfsDirectoryStatus(path, fs.getOwner(), fs.getGroup(), fs.getPermission().toShort(), fs.getModificationTime()); }
@Test public void fsShortSerializationTest() { State state = new State(); short mode = 420; FsPermission perms = new FsPermission(mode); HadoopUtils.serializeWriterFilePermissions(state, 0, 0, perms); FsPermission deserializedPerms = HadoopUtils.deserializeWriterFilePermissions(state, 0, 0); Assert.assertEquals(mode, deserializedPerms.toShort()); }
@Test public void fsOctalSerializationTest() { State state = new State(); String mode = "0755"; HadoopUtils.setWriterFileOctalPermissions(state, 0, 0, mode); FsPermission deserializedPerms = HadoopUtils.deserializeWriterFilePermissions(state, 0, 0); Assert.assertEquals(Integer.parseInt(mode, 8), deserializedPerms.toShort()); }
@Override public boolean mkdirs(Path path, FsPermission permission) throws IOException { ChannelSftp channel = null; try { channel = this.fsHelper.getSftpChannel(); channel.mkdir(HadoopUtils.toUriPath(path)); channel.chmod(permission.toShort(), HadoopUtils.toUriPath(path)); } catch (SftpException e) { throw new IOException(e); } finally { safeDisconnect(channel); } return true; }
/** * Changes permission of a path. * * @param path path to set permission * @param permission permission set to path */ @Override public void setPermission(Path path, FsPermission permission) throws IOException { LOG.debug("setMode({},{})", path, permission.toString()); AlluxioURI uri = new AlluxioURI(HadoopUtils.getPathWithoutScheme(path)); SetAttributePOptions options = SetAttributePOptions.newBuilder() .setMode(new Mode(permission.toShort()).toProto()).setRecursive(false).build(); try { mFileSystem.setAttribute(uri, options); } catch (AlluxioException e) { throw new IOException(e); } }
private void assertFileInfoEqualsFileStatus(FileInfo info, FileStatus status) { assertEquals(info.getOwner(), status.getOwner()); assertEquals(info.getGroup(), status.getGroup()); assertEquals(info.getMode(), status.getPermission().toShort()); assertEquals(info.getLastModificationTimeMs(), status.getModificationTime()); assertEquals(info.isFolder(), status.isDir()); } }