/** * Create the directory or check permissions if it already exists. * * The semantics of mkdirsWithExistsAndPermissionCheck method is different * from the mkdirs method provided in the Sun's java.io.File class in the * following way: * While creating the non-existent parent directories, this method checks for * the existence of those directories if the mkdir fails at any point (since * that directory might have just been created by some other process). * If both mkdir() and the exists() check fails for any seemingly * non-existent directory, then we signal an error; Sun's mkdir would signal * an error (return false) if a directory it is attempting to create already * exists or the mkdir fails. * * @param localFS local filesystem * @param dir directory to be created or checked * @param expected expected permission * @throws IOException */ static void mkdirsWithExistsAndPermissionCheck( LocalFileSystem localFS, Path dir, FsPermission expected) throws IOException { File directory = localFS.pathToFile(dir); boolean created = false; if (!directory.exists()) created = mkdirsWithExistsCheck(directory); if (created || !localFS.getFileStatus(dir).getPermission().equals(expected)) localFS.setPermission(dir, expected); }
/** * Set sticky bit on path to pin file. * @param localFS local file system * @param path path to be pinned with sticky bit * @throws IOException */ public void setPinning(LocalFileSystem localFS, Path path) throws IOException { FsPermission oldPermission = localFS.getFileStatus(path).getPermission(); FsPermission permission = new FsPermission(oldPermission.getUserAction(), oldPermission.getGroupAction(), oldPermission.getOtherAction(), true); localFS.setPermission(path, permission); }
@Override public void setPinning(ExtendedBlock block) throws IOException { if (!blockPinningEnabled) { return; } File f = getBlockFile(block); Path p = new Path(f.getAbsolutePath()); FsPermission oldPermission = localFS.getFileStatus( new Path(f.getAbsolutePath())).getPermission(); //sticky bit is used for pinning purpose FsPermission permission = new FsPermission(oldPermission.getUserAction(), oldPermission.getGroupAction(), oldPermission.getOtherAction(), true); localFS.setPermission(p, permission); }
@Override public void setPinning(ExtendedBlock block) throws IOException { if (!blockPinningEnabled) { return; } File f = getBlockFile(block); Path p = new Path(f.getAbsolutePath()); FsPermission oldPermission = localFS.getFileStatus( new Path(f.getAbsolutePath())).getPermission(); //sticky bit is used for pinning purpose FsPermission permission = new FsPermission(oldPermission.getUserAction(), oldPermission.getGroupAction(), oldPermission.getOtherAction(), true); localFS.setPermission(p, permission); }
@Test public void testGetDataDirsFromURIs() throws Throwable { File localDir = make(stub(File.class).returning(true).from.exists()); when(localDir.mkdir()).thenReturn(true); FsPermission normalPerm = new FsPermission("700"); FsPermission badPerm = new FsPermission("000"); FileStatus stat = make(stub(FileStatus.class) .returning(normalPerm, normalPerm, badPerm).from.getPermission()); when(stat.isDirectory()).thenReturn(true); LocalFileSystem fs = make(stub(LocalFileSystem.class) .returning(stat).from.getFileStatus(any(Path.class))); when(fs.pathToFile(any(Path.class))).thenReturn(localDir); Collection<URI> uris = Arrays.asList(new URI("file:/p1/"), new URI("file:/p2/"), new URI("file:/p3/")); List<File> dirs = DataNode.getDataDirsFromURIs(uris, fs, normalPerm); verify(fs, times(2)).setPermission(any(Path.class), eq(normalPerm)); verify(fs, times(6)).getFileStatus(any(Path.class)); assertEquals("number of valid data dirs", dirs.size(), 1); } }
FsPermission none = new FsPermission((short)0); localfs.setPermission(f, none); assertEquals(none, getPermission(localfs, f)); localfs.setPermission(f, all); assertEquals(all, getPermission(localfs, f));
private void _mkdirs(boolean exists, FsPermission before, FsPermission after) throws Throwable { File localDir = make(stub(File.class).returning(exists).from.exists()); when(localDir.mkdir()).thenReturn(true); Path dir = mock(Path.class); // use default stubs LocalFileSystem fs = make(stub(LocalFileSystem.class) .returning(localDir).from.pathToFile(dir)); FileStatus stat = make(stub(FileStatus.class) .returning(after).from.getPermission()); when(fs.getFileStatus(dir)).thenReturn(stat); try { DiskChecker.mkdirsWithExistsAndPermissionCheck(fs, dir, before); if (!exists) verify(fs).setPermission(dir, before); else { verify(fs).getFileStatus(dir); verify(stat).getPermission(); } } catch (DiskErrorException e) { if (before != after) assertTrue(e.getMessage().startsWith("Incorrect permission")); } }
/** * Create the directory or check permissions if it already exists. * * The semantics of mkdirsWithExistsAndPermissionCheck method is different * from the mkdirs method provided in the Sun's java.io.File class in the * following way: * While creating the non-existent parent directories, this method checks for * the existence of those directories if the mkdir fails at any point (since * that directory might have just been created by some other process). * If both mkdir() and the exists() check fails for any seemingly * non-existent directory, then we signal an error; Sun's mkdir would signal * an error (return false) if a directory it is attempting to create already * exists or the mkdir fails. * * @param localFS local filesystem * @param dir directory to be created or checked * @param expected expected permission * @throws IOException */ public static void mkdirsWithExistsAndPermissionCheck( LocalFileSystem localFS, Path dir, FsPermission expected) throws IOException { File directory = localFS.pathToFile(dir); boolean created = false; if (!directory.exists()) created = mkdirsWithExistsCheck(directory); if (created || !localFS.getFileStatus(dir).getPermission().equals(expected)) localFS.setPermission(dir, expected); }
/** * Create the directory or check permissions if it already exists. * * The semantics of mkdirsWithExistsAndPermissionCheck method is different * from the mkdirs method provided in the Sun's java.io.File class in the * following way: * While creating the non-existent parent directories, this method checks for * the existence of those directories if the mkdir fails at any point (since * that directory might have just been created by some other process). * If both mkdir() and the exists() check fails for any seemingly * non-existent directory, then we signal an error; Sun's mkdir would signal * an error (return false) if a directory it is attempting to create already * exists or the mkdir fails. * * @param localFS local filesystem * @param dir directory to be created or checked * @param expected expected permission * @throws IOException */ public static void mkdirsWithExistsAndPermissionCheck( LocalFileSystem localFS, Path dir, FsPermission expected) throws IOException { File directory = localFS.pathToFile(dir); boolean created = false; if (!directory.exists()) created = mkdirsWithExistsCheck(directory); if (created || !localFS.getFileStatus(dir).getPermission().equals(expected)) localFS.setPermission(dir, expected); }
/** * Create the directory or check permissions if it already exists. * * The semantics of mkdirsWithExistsAndPermissionCheck method is different * from the mkdirs method provided in the Sun's java.io.File class in the * following way: * While creating the non-existent parent directories, this method checks for * the existence of those directories if the mkdir fails at any point (since * that directory might have just been created by some other process). * If both mkdir() and the exists() check fails for any seemingly * non-existent directory, then we signal an error; Sun's mkdir would signal * an error (return false) if a directory it is attempting to create already * exists or the mkdir fails. * * @param localFS local filesystem * @param dir directory to be created or checked * @param expected expected permission * @throws IOException */ public static void mkdirsWithExistsAndPermissionCheck( LocalFileSystem localFS, Path dir, FsPermission expected) throws IOException { File directory = localFS.pathToFile(dir); boolean created = false; if (!directory.exists()) created = mkdirsWithExistsCheck(directory); if (created || !localFS.getFileStatus(dir).getPermission().equals(expected)) localFS.setPermission(dir, expected); }
localFS.setPermission(dir, expected);
@Test public void testSetPermissionCrc() throws Exception { FileSystem rawFs = localFs.getRawFileSystem(); Path p = new Path(TEST_ROOT_DIR, "testCrcPermissions"); localFs.createNewFile(p); Path crc = localFs.getChecksumFile(p); assert(rawFs.exists(crc)); for (short mode : Arrays.asList((short)0666, (short)0660, (short)0600)) { FsPermission perm = new FsPermission(mode); localFs.setPermission(p, perm); assertEquals(perm, localFs.getFileStatus(p).getPermission()); assertEquals(perm, rawFs.getFileStatus(crc).getPermission()); } } }
@Test public void testSetPermissionCrc() throws Exception { FileSystem rawFs = localFs.getRawFileSystem(); Path p = new Path(TEST_ROOT_DIR, "testCrcPermissions"); localFs.createNewFile(p); Path crc = localFs.getChecksumFile(p); assert(rawFs.exists(crc)); for (short mode : Arrays.asList((short)0666, (short)0660, (short)0600)) { FsPermission perm = new FsPermission(mode); localFs.setPermission(p, perm); assertEquals(perm, localFs.getFileStatus(p).getPermission()); assertEquals(perm, rawFs.getFileStatus(crc).getPermission()); } } }
FsPermission none = new FsPermission((short)0); localfs.setPermission(f, none); assertEquals(none, getPermission(localfs, f)); localfs.setPermission(f, all); assertEquals(all, getPermission(localfs, f));
FsPermission none = new FsPermission((short)0); localfs.setPermission(f, none); assertEquals(none, getPermission(localfs, f)); localfs.setPermission(f, all); assertEquals(all, getPermission(localfs, f));
private void _mkdirs(boolean exists, FsPermission before, FsPermission after) throws Throwable { File localDir = make(stub(File.class).returning(exists).from.exists()); when(localDir.mkdir()).thenReturn(true); Path dir = mock(Path.class); // use default stubs LocalFileSystem fs = make(stub(LocalFileSystem.class) .returning(localDir).from.pathToFile(dir)); FileStatus stat = make(stub(FileStatus.class) .returning(after).from.getPermission()); when(fs.getFileStatus(dir)).thenReturn(stat); try { DiskChecker.mkdirsWithExistsAndPermissionCheck(fs, dir, before); if (!exists) verify(fs).setPermission(dir, before); else { verify(fs).getFileStatus(dir); verify(stat).getPermission(); } } catch (DiskErrorException e) { if (before != after) assertTrue(e.getMessage().startsWith("Incorrect permission")); } }
private void _mkdirs(boolean exists, FsPermission before, FsPermission after) throws Throwable { File localDir = make(stub(File.class).returning(exists).from.exists()); when(localDir.mkdir()).thenReturn(true); Path dir = mock(Path.class); // use default stubs LocalFileSystem fs = make(stub(LocalFileSystem.class) .returning(localDir).from.pathToFile(dir)); FileStatus stat = make(stub(FileStatus.class) .returning(after).from.getPermission()); when(fs.getFileStatus(dir)).thenReturn(stat); try { DiskChecker.mkdirsWithExistsAndPermissionCheck(fs, dir, before); if (!exists) verify(fs).setPermission(dir, before); else { verify(fs).getFileStatus(dir); verify(stat).getPermission(); } } catch (DiskErrorException e) { if (before != after) assertTrue(e.getMessage().startsWith("Incorrect permission")); } }