-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ChecksumCommand.java
99 lines (84 loc) · 2.95 KB
/
ChecksumCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.cli.fs.command;
import alluxio.AlluxioURI;
import alluxio.cli.CommandUtils;
import alluxio.client.ReadType;
import alluxio.client.file.FileInStream;
import alluxio.client.file.FileSystem;
import alluxio.client.file.URIStatus;
import alluxio.client.file.options.OpenFileOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.ExceptionMessage;
import alluxio.exception.FileDoesNotExistException;
import alluxio.exception.status.InvalidArgumentException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.IOException;
import javax.annotation.concurrent.ThreadSafe;
/**
* Calculates the MD5 checksum for a file.
*/
@ThreadSafe
public final class ChecksumCommand extends AbstractFileSystemCommand {
/**
* @param fs the filesystem of Alluxio
*/
public ChecksumCommand(FileSystem fs) {
super(fs);
}
@Override
protected void runPlainPath(AlluxioURI plainPath, CommandLine cl)
throws AlluxioException, IOException {
URIStatus status = mFileSystem.getStatus(plainPath);
if (status.isFolder()) {
throw new FileDoesNotExistException(
ExceptionMessage.PATH_MUST_BE_FILE.getMessage(plainPath.getPath()));
}
String str = calculateChecksum(plainPath);
System.out.println("md5sum: " + str + "\n");
}
@Override
public String getCommandName() {
return "checksum";
}
@Override
public void validateArgs(CommandLine cl) throws InvalidArgumentException {
CommandUtils.checkNumOfArgsEquals(this, cl, 1);
}
@Override
public int run(CommandLine cl) throws AlluxioException, IOException {
String[] args = cl.getArgs();
runWildCardCmd(new AlluxioURI(args[0]), cl);
return 0;
}
/**
* Calculates the md5 checksum for a file.
*
* @param filePath The {@link AlluxioURI} path of the file calculate MD5 checksum on
* @return A string representation of the file's MD5 checksum
*/
private String calculateChecksum(AlluxioURI filePath)
throws AlluxioException, IOException {
OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
try (FileInStream fis = mFileSystem.openFile(filePath, options)) {
return DigestUtils.md5Hex(fis);
}
}
@Override
public String getUsage() {
return "checksum <Alluxio path>";
}
@Override
public String getDescription() {
return "Calculates the md5 checksum of a file in the Alluxio filesystem.";
}
}