Skip to content

Commit

Permalink
HDDS-3093. Allow forced overwrite of local file
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai committed Apr 9, 2020
1 parent 1f2255b commit 2ca1fe2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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())) {
try (InputStream stream = new FileInputStream(dataFile)) {
String hash = DigestUtils.md5Hex(stream);
out().printf("Downloaded file hash : %s%n", hash);
Expand Down

0 comments on commit 2ca1fe2

Please sign in to comment.