-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ChgrpCommand.java
109 lines (91 loc) · 3.12 KB
/
ChgrpCommand.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
100
101
102
103
104
105
106
107
108
109
/*
* 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.file.FileSystem;
import alluxio.client.file.options.SetAttributeOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.status.InvalidArgumentException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import java.io.IOException;
import javax.annotation.concurrent.ThreadSafe;
/**
* Changes the group of a file or directory specified by args.
*/
@ThreadSafe
public final class ChgrpCommand extends AbstractFileSystemCommand {
private static final Option RECURSIVE_OPTION =
Option.builder("R")
.required(false)
.hasArg(false)
.desc("change group recursively")
.build();
private String mGroup;
/**
* Creates a new instance of {@link ChgrpCommand}.
*
* @param fs an Alluxio file system handle
*/
public ChgrpCommand(FileSystem fs) {
super(fs);
}
@Override
public String getCommandName() {
return "chgrp";
}
@Override
public void validateArgs(CommandLine cl) throws InvalidArgumentException {
CommandUtils.checkNumOfArgsEquals(this, cl, 2);
}
@Override
public Options getOptions() {
return new Options().addOption(RECURSIVE_OPTION);
}
/**
* Changes the group for the directory or file with the path specified in args.
*
* @param path The {@link AlluxioURI} path as the input of the command
* @param group The group to be updated to the file or directory
* @param recursive Whether change the group recursively
*/
private void chgrp(AlluxioURI path, String group, boolean recursive)
throws AlluxioException, IOException {
SetAttributeOptions options =
SetAttributeOptions.defaults().setGroup(group).setRecursive(recursive);
mFileSystem.setAttribute(path, options);
System.out.println("Changed group of " + path + " to " + group);
}
@Override
protected void runPlainPath(AlluxioURI path, CommandLine cl)
throws IOException, AlluxioException {
chgrp(path, mGroup, cl.hasOption("R"));
}
@Override
public int run(CommandLine cl) throws AlluxioException, IOException {
String[] args = cl.getArgs();
mGroup = args[0];
AlluxioURI path = new AlluxioURI(args[1]);
runWildCardCmd(path, cl);
return 0;
}
@Override
public String getUsage() {
return "chgrp [-R] <group> <path>";
}
@Override
public String getDescription() {
return "Changes the group of a file or directory specified by args."
+ " Specify -R to change the group recursively.";
}
}