|
@@ -27,12 +27,10 @@ import com.google.common.base.Enums;
|
|
import com.google.common.base.Function;
|
|
import com.google.common.base.Function;
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
|
-import org.apache.commons.codec.DecoderException;
|
|
|
|
-import org.apache.commons.codec.binary.Hex;
|
|
|
|
-import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.hadoop.HadoopIllegalArgumentException;
|
|
import org.apache.hadoop.HadoopIllegalArgumentException;
|
|
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.XAttrCodec;
|
|
import org.apache.hadoop.util.StringUtils;
|
|
import org.apache.hadoop.util.StringUtils;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -49,60 +47,6 @@ class XAttrCommands extends FsCommand {
|
|
factory.addClass(SetfattrCommand.class, "-" + SET_FATTR);
|
|
factory.addClass(SetfattrCommand.class, "-" + SET_FATTR);
|
|
}
|
|
}
|
|
|
|
|
|
- private static enum ENCODE {
|
|
|
|
- TEXT,
|
|
|
|
- HEX,
|
|
|
|
- BASE64;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static final String ENCODE_HEX = "0x";
|
|
|
|
- private static final String ENCODE_BASE64 = "0s";
|
|
|
|
-
|
|
|
|
- private static String convert(String name, byte[] value, ENCODE encode)
|
|
|
|
- throws IOException {
|
|
|
|
- final StringBuilder buffer = new StringBuilder();
|
|
|
|
- buffer.append(name);
|
|
|
|
- if (value != null && value.length != 0) {
|
|
|
|
- buffer.append("=");
|
|
|
|
- if (encode == ENCODE.TEXT) {
|
|
|
|
- buffer.append("\"").append(new String(value, "utf-8")).append("\"");
|
|
|
|
- } else if (encode == ENCODE.HEX) {
|
|
|
|
- buffer.append(ENCODE_HEX).append(Hex.encodeHexString(value));
|
|
|
|
- } else if (encode == ENCODE.BASE64) {
|
|
|
|
- buffer.append(ENCODE_BASE64).append(Base64.encodeBase64String(value));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return buffer.toString();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static byte[] convert(String valueArg) throws IOException {
|
|
|
|
- String value = valueArg;
|
|
|
|
- byte[] result = null;
|
|
|
|
- if (value != null) {
|
|
|
|
- if (value.length() >= 2) {
|
|
|
|
- final String en = value.substring(0, 2);
|
|
|
|
- if (value.startsWith("\"") && value.endsWith("\"")) {
|
|
|
|
- value = value.substring(1, value.length()-1);
|
|
|
|
- result = value.getBytes("utf-8");
|
|
|
|
- } else if (en.equalsIgnoreCase(ENCODE_HEX)) {
|
|
|
|
- value = value.substring(2, value.length());
|
|
|
|
- try {
|
|
|
|
- result = Hex.decodeHex(value.toCharArray());
|
|
|
|
- } catch (DecoderException e) {
|
|
|
|
- throw new IOException(e);
|
|
|
|
- }
|
|
|
|
- } else if (en.equalsIgnoreCase(ENCODE_BASE64)) {
|
|
|
|
- value = value.substring(2, value.length());
|
|
|
|
- result = Base64.decodeBase64(value);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (result == null) {
|
|
|
|
- result = value.getBytes("utf-8");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Implements the '-getfattr' command for the FsShell.
|
|
* Implements the '-getfattr' command for the FsShell.
|
|
*/
|
|
*/
|
|
@@ -121,30 +65,26 @@ class XAttrCommands extends FsCommand {
|
|
"strings are enclosed in double quotes (\"), and values encoded" +
|
|
"strings are enclosed in double quotes (\"), and values encoded" +
|
|
" as hexadecimal and base64 are prefixed with 0x and 0s, respectively.\n" +
|
|
" as hexadecimal and base64 are prefixed with 0x and 0s, respectively.\n" +
|
|
"<path>: The file or directory.\n";
|
|
"<path>: The file or directory.\n";
|
|
- private static final String NAME_OPT = "-n";
|
|
|
|
- private static final String ENCODE_OPT = "-e";
|
|
|
|
- final CommandFormat cf = new CommandFormat(0, Integer.MAX_VALUE, "d", "R");
|
|
|
|
|
|
+ private final static Function<String, XAttrCodec> enValueOfFunc =
|
|
|
|
+ Enums.valueOfFunction(XAttrCodec.class);
|
|
|
|
|
|
private String name = null;
|
|
private String name = null;
|
|
private boolean dump = false;
|
|
private boolean dump = false;
|
|
- private ENCODE encode = ENCODE.TEXT;
|
|
|
|
-
|
|
|
|
- private final static Function<String, ENCODE> encodeValueOfFunc =
|
|
|
|
- Enums.valueOfFunction(ENCODE.class);
|
|
|
|
|
|
+ private XAttrCodec encoding = XAttrCodec.TEXT;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
protected void processOptions(LinkedList<String> args) throws IOException {
|
|
protected void processOptions(LinkedList<String> args) throws IOException {
|
|
- name = StringUtils.popOptionWithArgument(NAME_OPT, args);
|
|
|
|
- String en = StringUtils.popOptionWithArgument(ENCODE_OPT, args);
|
|
|
|
|
|
+ name = StringUtils.popOptionWithArgument("-n", args);
|
|
|
|
+ String en = StringUtils.popOptionWithArgument("-e", args);
|
|
if (en != null) {
|
|
if (en != null) {
|
|
- encode = encodeValueOfFunc.apply(en.toUpperCase());
|
|
|
|
|
|
+ encoding = enValueOfFunc.apply(en.toUpperCase());
|
|
|
|
+ Preconditions.checkArgument(encoding != null,
|
|
|
|
+ "Invalid/unsupported encoding option specified: " + en);
|
|
}
|
|
}
|
|
- Preconditions.checkArgument(encode != null,
|
|
|
|
- "Invalid/unsupported encoding option specified: " + en);
|
|
|
|
|
|
|
|
- cf.parse(args);
|
|
|
|
- setRecursive(cf.getOpt("R"));
|
|
|
|
- dump = cf.getOpt("d");
|
|
|
|
|
|
+ boolean r = StringUtils.popOption("-R", args);
|
|
|
|
+ setRecursive(r);
|
|
|
|
+ dump = StringUtils.popOption("-d", args);
|
|
|
|
|
|
if (!dump && name == null) {
|
|
if (!dump && name == null) {
|
|
throw new HadoopIllegalArgumentException(
|
|
throw new HadoopIllegalArgumentException(
|
|
@@ -168,14 +108,20 @@ class XAttrCommands extends FsCommand {
|
|
Iterator<Entry<String, byte[]>> iter = xattrs.entrySet().iterator();
|
|
Iterator<Entry<String, byte[]>> iter = xattrs.entrySet().iterator();
|
|
while(iter.hasNext()) {
|
|
while(iter.hasNext()) {
|
|
Entry<String, byte[]> entry = iter.next();
|
|
Entry<String, byte[]> entry = iter.next();
|
|
- out.println(convert(entry.getKey(), entry.getValue(), encode));
|
|
|
|
|
|
+ printXAttr(entry.getKey(), entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
byte[] value = item.fs.getXAttr(item.path, name);
|
|
byte[] value = item.fs.getXAttr(item.path, name);
|
|
- if (value != null) {
|
|
|
|
- out.println(convert(name, value, encode));
|
|
|
|
- }
|
|
|
|
|
|
+ printXAttr(name, value);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void printXAttr(String name, byte[] value) throws IOException{
|
|
|
|
+ if (value != null && value.length != 0) {
|
|
|
|
+ out.println(name + "=" + XAttrCodec.encodeValue(value, encoding));
|
|
|
|
+ } else {
|
|
|
|
+ out.println(name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -197,9 +143,6 @@ class XAttrCommands extends FsCommand {
|
|
"base64 encoding.\n" +
|
|
"base64 encoding.\n" +
|
|
"-x name: Remove the extended attribute.\n" +
|
|
"-x name: Remove the extended attribute.\n" +
|
|
"<path>: The file or directory.\n";
|
|
"<path>: The file or directory.\n";
|
|
- private static final String NAME_OPT = "-n";
|
|
|
|
- private static final String VALUE_OPT = "-v";
|
|
|
|
- private static final String REMOVE_OPT = "-x";
|
|
|
|
|
|
|
|
private String name = null;
|
|
private String name = null;
|
|
private byte[] value = null;
|
|
private byte[] value = null;
|
|
@@ -207,12 +150,12 @@ class XAttrCommands extends FsCommand {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
protected void processOptions(LinkedList<String> args) throws IOException {
|
|
protected void processOptions(LinkedList<String> args) throws IOException {
|
|
- name = StringUtils.popOptionWithArgument(NAME_OPT, args);
|
|
|
|
- String v = StringUtils.popOptionWithArgument(VALUE_OPT, args);
|
|
|
|
|
|
+ name = StringUtils.popOptionWithArgument("-n", args);
|
|
|
|
+ String v = StringUtils.popOptionWithArgument("-v", args);
|
|
if (v != null) {
|
|
if (v != null) {
|
|
- value = convert(v);
|
|
|
|
|
|
+ value = XAttrCodec.decodeValue(v);
|
|
}
|
|
}
|
|
- xname = StringUtils.popOptionWithArgument(REMOVE_OPT, args);
|
|
|
|
|
|
+ xname = StringUtils.popOptionWithArgument("-x", args);
|
|
|
|
|
|
if (name != null && xname != null) {
|
|
if (name != null && xname != null) {
|
|
throw new HadoopIllegalArgumentException(
|
|
throw new HadoopIllegalArgumentException(
|