HADOOP-12943. Add -w -r options in dfs -test command. Contributed by Weiwei Yang.
This commit is contained in:
parent
51d16e7b38
commit
09e82acaf9
|
@ -18,11 +18,14 @@
|
||||||
|
|
||||||
package org.apache.hadoop.fs.shell;
|
package org.apache.hadoop.fs.shell;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
import org.apache.hadoop.classification.InterfaceAudience;
|
import org.apache.hadoop.classification.InterfaceAudience;
|
||||||
import org.apache.hadoop.classification.InterfaceStability;
|
import org.apache.hadoop.classification.InterfaceStability;
|
||||||
|
import org.apache.hadoop.fs.permission.FsAction;
|
||||||
|
import org.apache.hadoop.security.AccessControlException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform shell-like file tests
|
* Perform shell-like file tests
|
||||||
|
@ -38,18 +41,25 @@ class Test extends FsCommand {
|
||||||
public static final String NAME = "test";
|
public static final String NAME = "test";
|
||||||
public static final String USAGE = "-[defsz] <path>";
|
public static final String USAGE = "-[defsz] <path>";
|
||||||
public static final String DESCRIPTION =
|
public static final String DESCRIPTION =
|
||||||
"Answer various questions about <path>, with result via exit status.\n" +
|
"Answer various questions about <path>, with result via exit status.\n"
|
||||||
" -d return 0 if <path> is a directory.\n" +
|
+ " -d return 0 if <path> is a directory.\n"
|
||||||
" -e return 0 if <path> exists.\n" +
|
+ " -e return 0 if <path> exists.\n"
|
||||||
" -f return 0 if <path> is a file.\n" +
|
+ " -f return 0 if <path> is a file.\n"
|
||||||
" -s return 0 if file <path> is greater than zero bytes in size.\n" +
|
+ " -s return 0 if file <path> is greater "
|
||||||
" -z return 0 if file <path> is zero bytes in size, else return 1.";
|
+ " than zero bytes in size.\n"
|
||||||
|
+ " -w return 0 if file <path> exists "
|
||||||
|
+ " and write permission is granted.\n"
|
||||||
|
+ " -r return 0 if file <path> exists "
|
||||||
|
+ " and read permission is granted.\n"
|
||||||
|
+ " -z return 0 if file <path> is "
|
||||||
|
+ " zero bytes in size, else return 1.";
|
||||||
|
|
||||||
private char flag;
|
private char flag;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void processOptions(LinkedList<String> args) {
|
protected void processOptions(LinkedList<String> args) {
|
||||||
CommandFormat cf = new CommandFormat(1, 1, "e", "d", "f", "s", "z");
|
CommandFormat cf = new CommandFormat(1, 1,
|
||||||
|
"e", "d", "f", "s", "z", "w", "r");
|
||||||
cf.parse(args);
|
cf.parse(args);
|
||||||
|
|
||||||
String[] opts = cf.getOpts().toArray(new String[0]);
|
String[] opts = cf.getOpts().toArray(new String[0]);
|
||||||
|
@ -68,29 +78,47 @@ class Test extends FsCommand {
|
||||||
protected void processPath(PathData item) throws IOException {
|
protected void processPath(PathData item) throws IOException {
|
||||||
boolean test = false;
|
boolean test = false;
|
||||||
switch (flag) {
|
switch (flag) {
|
||||||
case 'e':
|
case 'e':
|
||||||
test = true;
|
test = true;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
test = item.stat.isDirectory();
|
test = item.stat.isDirectory();
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
test = item.stat.isFile();
|
test = item.stat.isFile();
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
test = (item.stat.getLen() > 0);
|
test = (item.stat.getLen() > 0);
|
||||||
break;
|
break;
|
||||||
case 'z':
|
case 'z':
|
||||||
test = (item.stat.getLen() == 0);
|
test = (item.stat.getLen() == 0);
|
||||||
break;
|
break;
|
||||||
default:
|
case 'w':
|
||||||
break;
|
test = testAccess(item, FsAction.WRITE);
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
test = testAccess(item, FsAction.READ);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!test) {
|
||||||
|
exitCode = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean testAccess(PathData item, FsAction action)
|
||||||
|
throws IOException {
|
||||||
|
try {
|
||||||
|
item.fs.access(item.path, action);
|
||||||
|
return true;
|
||||||
|
} catch (AccessControlException | FileNotFoundException e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
if (!test) exitCode = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void processNonexistentPath(PathData item) throws IOException {
|
protected void processNonexistentPath(PathData item) throws IOException {
|
||||||
exitCode = 1;
|
exitCode = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -669,8 +669,11 @@ Options:
|
||||||
* -e: if the path exists, return 0.
|
* -e: if the path exists, return 0.
|
||||||
* -f: if the path is a file, return 0.
|
* -f: if the path is a file, return 0.
|
||||||
* -s: if the path is not empty, return 0.
|
* -s: if the path is not empty, return 0.
|
||||||
|
* -r: if the path exists and read permission is granted, return 0.
|
||||||
|
* -w: if the path exists and write permission is granted, return 0.
|
||||||
* -z: if the file is zero length, return 0.
|
* -z: if the file is zero length, return 0.
|
||||||
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
* `hadoop fs -test -e filename`
|
* `hadoop fs -test -e filename`
|
||||||
|
|
|
@ -1179,8 +1179,8 @@ public class TestDFSShell {
|
||||||
* Tests various options of DFSShell.
|
* Tests various options of DFSShell.
|
||||||
*/
|
*/
|
||||||
@Test (timeout = 120000)
|
@Test (timeout = 120000)
|
||||||
public void testDFSShell() throws IOException {
|
public void testDFSShell() throws Exception {
|
||||||
Configuration conf = new HdfsConfiguration();
|
final Configuration conf = new HdfsConfiguration();
|
||||||
/* This tests some properties of ChecksumFileSystem as well.
|
/* This tests some properties of ChecksumFileSystem as well.
|
||||||
* Make sure that we create ChecksumDFS */
|
* Make sure that we create ChecksumDFS */
|
||||||
MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build();
|
MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build();
|
||||||
|
@ -1532,6 +1532,73 @@ public class TestDFSShell {
|
||||||
assertEquals(0, val);
|
assertEquals(0, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify -test -w/-r
|
||||||
|
{
|
||||||
|
Path permDir = new Path("/test/permDir");
|
||||||
|
Path permFile = new Path("/test/permDir/permFile");
|
||||||
|
mkdir(fs, permDir);
|
||||||
|
writeFile(fs, permFile);
|
||||||
|
|
||||||
|
// Verify -test -w positive case (dir exists and can write)
|
||||||
|
final String[] wargs = new String[3];
|
||||||
|
wargs[0] = "-test";
|
||||||
|
wargs[1] = "-w";
|
||||||
|
wargs[2] = permDir.toString();
|
||||||
|
int val = -1;
|
||||||
|
try {
|
||||||
|
val = shell.run(wargs);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Exception raised from DFSShell.run " +
|
||||||
|
e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
assertEquals(0, val);
|
||||||
|
|
||||||
|
// Verify -test -r positive case (file exists and can read)
|
||||||
|
final String[] rargs = new String[3];
|
||||||
|
rargs[0] = "-test";
|
||||||
|
rargs[1] = "-r";
|
||||||
|
rargs[2] = permFile.toString();
|
||||||
|
try {
|
||||||
|
val = shell.run(rargs);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Exception raised from DFSShell.run " +
|
||||||
|
e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
assertEquals(0, val);
|
||||||
|
|
||||||
|
// Verify -test -r negative case (file exists but cannot read)
|
||||||
|
runCmd(shell, "-chmod", "600", permFile.toString());
|
||||||
|
|
||||||
|
UserGroupInformation smokeUser =
|
||||||
|
UserGroupInformation.createUserForTesting("smokeUser",
|
||||||
|
new String[] {"hadoop"});
|
||||||
|
smokeUser.doAs(new PrivilegedExceptionAction<String>() {
|
||||||
|
@Override
|
||||||
|
public String run() throws Exception {
|
||||||
|
FsShell shell = new FsShell(conf);
|
||||||
|
int exitCode = shell.run(rargs);
|
||||||
|
assertEquals(1, exitCode);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify -test -w negative case (dir exists but cannot write)
|
||||||
|
runCmd(shell, "-chown", "-R", "not_allowed", permDir.toString());
|
||||||
|
runCmd(shell, "-chmod", "-R", "700", permDir.toString());
|
||||||
|
|
||||||
|
smokeUser.doAs(new PrivilegedExceptionAction<String>() {
|
||||||
|
@Override
|
||||||
|
public String run() throws Exception {
|
||||||
|
FsShell shell = new FsShell(conf);
|
||||||
|
int exitCode = shell.run(wargs);
|
||||||
|
assertEquals(1, exitCode);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
fs.delete(permDir, true);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
fileSys.close();
|
fileSys.close();
|
||||||
|
|
Loading…
Reference in New Issue