HADOOP-7237. Refactor the touchz commands to conform to new FsCommand class. Contributed by Daryn Sharp

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1102068 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tsz-wo Sze 2011-05-11 20:20:18 +00:00
parent 4aa51ca6f9
commit cd2079f0e4
5 changed files with 89 additions and 40 deletions

View File

@ -144,6 +144,9 @@ Trunk (unreleased changes)
HADOOP-7275. Refactor the stat commands to conform to new FsCommand HADOOP-7275. Refactor the stat commands to conform to new FsCommand
class. (Daryn Sharp via szetszwo) class. (Daryn Sharp via szetszwo)
HADOOP-7237. Refactor the touchz commands to conform to new FsCommand
class. (Daryn Sharp via szetszwo)
OPTIMIZATIONS OPTIMIZATIONS
BUG FIXES BUG FIXES

View File

@ -432,27 +432,6 @@ public class FsShell extends Configured implements Tool {
} }
} }
/**
* (Re)create zero-length file at the specified path.
* This will be replaced by a more UNIX-like touch when files may be
* modified.
*/
void touchz(String src) throws IOException {
Path f = new Path(src);
FileSystem srcFs = f.getFileSystem(getConf());
FileStatus st;
if (srcFs.exists(f)) {
st = srcFs.getFileStatus(f);
if (st.isDirectory()) {
// TODO: handle this
throw new IOException(src + " is a directory");
} else if (st.getLen() != 0)
throw new IOException(src + " must be a zero-length file");
}
FSDataOutputStream out = srcFs.create(f);
out.close();
}
/** /**
* Check file types. * Check file types.
*/ */
@ -765,7 +744,7 @@ public class FsShell extends Configured implements Tool {
GET_SHORT_USAGE + "\n\t" + GET_SHORT_USAGE + "\n\t" +
"[" + COPYTOLOCAL_SHORT_USAGE + "] [-moveToLocal <src> <localdst>]\n\t" + "[" + COPYTOLOCAL_SHORT_USAGE + "] [-moveToLocal <src> <localdst>]\n\t" +
"[-report]\n\t" + "[-report]\n\t" +
"[-touchz <path>] [-test -[ezd] <path>]"; "[-test -[ezd] <path>]";
String conf ="-conf <configuration file>: Specify an application configuration file."; String conf ="-conf <configuration file>: Specify an application configuration file.";
@ -838,10 +817,6 @@ public class FsShell extends Configured implements Tool {
String moveToLocal = "-moveToLocal <src> <localdst>: Not implemented yet \n"; String moveToLocal = "-moveToLocal <src> <localdst>: Not implemented yet \n";
String touchz = "-touchz <path>: Creates a file of zero length\n"
+ "\t\t at <path> with current time as the timestamp of that <path>.\n"
+ "\t\t An error is returned if the file exists with non-zero length\n";
String test = "-test -[ezd] <path>: If file { exists, has zero length, is a directory\n" + String test = "-test -[ezd] <path>: If file { exists, has zero length, is a directory\n" +
"\t\tthen return 0, else return 1.\n"; "\t\tthen return 0, else return 1.\n";
@ -889,8 +864,6 @@ public class FsShell extends Configured implements Tool {
System.out.println(moveToLocal); System.out.println(moveToLocal);
} else if ("get".equals(cmd)) { } else if ("get".equals(cmd)) {
System.out.println(get); System.out.println(get);
} else if ("touchz".equals(cmd)) {
System.out.println(touchz);
} else if ("test".equals(cmd)) { } else if ("test".equals(cmd)) {
System.out.println(test); System.out.println(test);
} else if ("help".equals(cmd)) { } else if ("help".equals(cmd)) {
@ -917,7 +890,6 @@ public class FsShell extends Configured implements Tool {
System.out.println(get); System.out.println(get);
System.out.println(copyToLocal); System.out.println(copyToLocal);
System.out.println(moveToLocal); System.out.println(moveToLocal);
System.out.println(touchz);
System.out.println(test); System.out.println(test);
for (String thisCmdName : commandFactory.getNames()) { for (String thisCmdName : commandFactory.getNames()) {
@ -974,8 +946,6 @@ public class FsShell extends Configured implements Tool {
delete(argv[i], true, rmSkipTrash); delete(argv[i], true, rmSkipTrash);
} else if ("-df".equals(cmd)) { } else if ("-df".equals(cmd)) {
df(argv[i]); df(argv[i]);
} else if ("-touchz".equals(cmd)) {
touchz(argv[i]);
} }
} catch (IOException e) { } catch (IOException e) {
LOG.debug("Error", e); LOG.debug("Error", e);
@ -1005,8 +975,7 @@ public class FsShell extends Configured implements Tool {
} else if ("-D".equals(cmd)) { } else if ("-D".equals(cmd)) {
System.err.println("Usage: java FsShell" + System.err.println("Usage: java FsShell" +
" [-D <[property=value>]"); " [-D <[property=value>]");
} else if ("-du".equals(cmd) || "-dus".equals(cmd) || } else if ("-du".equals(cmd) || "-dus".equals(cmd)) {
"-touchz".equals(cmd)) {
System.err.println("Usage: java FsShell" + System.err.println("Usage: java FsShell" +
" [" + cmd + " <path>]"); " [" + cmd + " <path>]");
} else if ("-df".equals(cmd) ) { } else if ("-df".equals(cmd) ) {
@ -1048,7 +1017,6 @@ public class FsShell extends Configured implements Tool {
System.err.println(" [" + GET_SHORT_USAGE + "]"); System.err.println(" [" + GET_SHORT_USAGE + "]");
System.err.println(" [" + COPYTOLOCAL_SHORT_USAGE + "]"); System.err.println(" [" + COPYTOLOCAL_SHORT_USAGE + "]");
System.err.println(" [-moveToLocal [-crc] <src> <localdst>]"); System.err.println(" [-moveToLocal [-crc] <src> <localdst>]");
System.err.println(" [-touchz <path>]");
System.err.println(" [-test -[ezd] <path>]"); System.err.println(" [-test -[ezd] <path>]");
for (String name : commandFactory.getNames()) { for (String name : commandFactory.getNames()) {
instance = commandFactory.getInstance(name); instance = commandFactory.getInstance(name);
@ -1098,8 +1066,7 @@ public class FsShell extends Configured implements Tool {
printUsage(cmd); printUsage(cmd);
return exitCode; return exitCode;
} }
} else if ("-rm".equals(cmd) || "-rmr".equals(cmd) || } else if ("-rm".equals(cmd) || "-rmr".equals(cmd)) {
"-touchz".equals(cmd)) {
if (argv.length < 2) { if (argv.length < 2) {
printUsage(cmd); printUsage(cmd);
return exitCode; return exitCode;
@ -1168,8 +1135,6 @@ public class FsShell extends Configured implements Tool {
du(argv, i); du(argv, i);
} else if ("-dus".equals(cmd)) { } else if ("-dus".equals(cmd)) {
dus(argv, i); dus(argv, i);
} else if ("-touchz".equals(cmd)) {
exitCode = doall(cmd, argv, i);
} else if ("-test".equals(cmd)) { } else if ("-test".equals(cmd)) {
exitCode = test(argv, i); exitCode = test(argv, i);
} else if ("-help".equals(cmd)) { } else if ("-help".equals(cmd)) {

View File

@ -52,6 +52,7 @@ abstract public class FsCommand extends Command {
factory.registerCommands(SetReplication.class); factory.registerCommands(SetReplication.class);
factory.registerCommands(Stat.class); factory.registerCommands(Stat.class);
factory.registerCommands(Tail.class); factory.registerCommands(Tail.class);
factory.registerCommands(Touch.class);
} }
protected FsCommand() {} protected FsCommand() {}

View File

@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs.shell;
import java.io.IOException;
import java.util.LinkedList;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.shell.PathExceptions.PathIOException;
import org.apache.hadoop.fs.shell.PathExceptions.PathIsDirectoryException;
/**
* Unix touch like commands
*/
@InterfaceAudience.Private
@InterfaceStability.Unstable
class Touch extends FsCommand {
public static void registerCommands(CommandFactory factory) {
factory.addClass(Touchz.class, "-touchz");
}
/**
* (Re)create zero-length file at the specified path.
* This will be replaced by a more UNIX-like touch when files may be
* modified.
*/
public static class Touchz extends Touch {
public static final String NAME = "touchz";
public static final String USAGE = "<path> ...";
public static final String DESCRIPTION =
"Creates a file of zero length\n" +
"at <path> with current time as the timestamp of that <path>.\n" +
"An error is returned if the file exists with non-zero length\n";
@Override
protected void processOptions(LinkedList<String> args) {
CommandFormat cf = new CommandFormat(null, 1, Integer.MAX_VALUE);
cf.parse(args);
}
@Override
protected void processPath(PathData item) throws IOException {
if (item.stat.isDirectory()) {
// TODO: handle this
throw new PathIsDirectoryException(item.toString());
}
if (item.stat.getLen() != 0) {
throw new PathIOException(item.toString(), "Not a zero-length file");
}
touchz(item);
}
@Override
protected void processNonexistentPath(PathData item) throws IOException {
touchz(item);
}
private void touchz(PathData item) throws IOException {
item.fs.create(item.path).close();
}
}
}

View File

@ -541,7 +541,7 @@
<comparators> <comparators>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
<expected-output>^-touchz &lt;path&gt;: Creates a file of zero length( )*</expected-output> <expected-output>^-touchz &lt;path&gt; \.\.\.:( |\t)*Creates a file of zero length( )*</expected-output>
</comparator> </comparator>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
@ -549,7 +549,7 @@
</comparator> </comparator>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
<expected-output>^( |\t)* An error is returned if the file exists with non-zero length( )*</expected-output> <expected-output>^( |\t)*An error is returned if the file exists with non-zero length( )*</expected-output>
</comparator> </comparator>
</comparators> </comparators>
</test> </test>