Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDDS-3093. Allow forced overwrite of local file #800

Merged
merged 10 commits into from
Apr 10, 2020
6 changes: 5 additions & 1 deletion hadoop-ozone/dist/src/main/smoketest/basic/ozone-shell.robot
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ Test key handling
Execute ozone sh key put ${protocol}${server}/${volume}/bb1/key1 /opt/hadoop/NOTICE.txt
Execute rm -f /tmp/NOTICE.txt.1
Execute ozone sh key get ${protocol}${server}/${volume}/bb1/key1 /tmp/NOTICE.txt.1
Execute ls -l /tmp/NOTICE.txt.1
Execute diff -q /opt/hadoop/NOTICE.txt /tmp/NOTICE.txt.1
${result} = Execute And Ignore Error ozone sh key get ${protocol}${server}/${volume}/bb1/key1 /tmp/NOTICE.txt.1
Should Contain ${result} NOTICE.txt.1 exists
${result} = Execute ozone sh key get --force ${protocol}${server}/${volume}/bb1/key1 /tmp/NOTICE.txt.1
Should Not Contain ${result} NOTICE.txt.1 exists
${result} = Execute ozone sh key info ${protocol}${server}/${volume}/bb1/key1 | jq -r '. | select(.name=="key1")'
Should contain ${result} creationTime
${result} = Execute ozone sh key list ${protocol}${server}/${volume}/bb1 | jq -r '. | select(.name=="key1") | .name'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_CHUNK_SIZE_DEFAULT;
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_CHUNK_SIZE_KEY;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;

Expand All @@ -51,6 +52,13 @@ public class GetKeyHandler extends KeyHandler {
description = "File path to download the key to")
private String fileName;

@CommandLine.Option(
names = {"-f", "--force"},
description = "Overwrite local file if it exists",
defaultValue = "false"
)
private boolean force;

@Override
protected void execute(OzoneClient client, OzoneAddress address)
throws IOException, OzoneClientException {
Expand All @@ -65,7 +73,7 @@ protected void execute(OzoneClient client, OzoneAddress address)
dataFile = new File(fileName, keyName);
}

if (dataFile.exists()) {
if (dataFile.exists() && !force) {
throw new OzoneClientException(dataFile.getPath() + " exists."
+ " Download would overwrite an existing file. Aborting.");
}
Expand All @@ -80,7 +88,7 @@ protected void execute(OzoneClient client, OzoneAddress address)
IOUtils.copyBytes(input, output, chunkSize);
}

if (isVerbose()) {
if (isVerbose() && !"/dev/null".equals(dataFile.getAbsolutePath())) {
xiaoyuyao marked this conversation as resolved.
Show resolved Hide resolved
try (InputStream stream = new FileInputStream(dataFile)) {
String hash = DigestUtils.md5Hex(stream);
out().printf("Downloaded file hash : %s%n", hash);
Expand Down