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 package net.sf.hermesftp.cmd.impl;
26
27 import java.io.File;
28 import java.util.Map;
29 import java.util.Set;
30
31 import net.sf.hermesftp.cmd.AbstractFtpCmd;
32 import net.sf.hermesftp.exception.FtpCmdException;
33 import net.sf.hermesftp.utils.IOUtils;
34
35 /***
36 * <b>STATUS (STAT)</b>
37 * <p>
38 * This command shall cause a status response to be sent over the control connection in the form of
39 * a reply. The command may be sent during a file transfer (along with the Telnet IP and Synch
40 * signals--see the Section on FTP Commands) in which case the server will respond with the status
41 * of the operation in progress, or it may be sent between file transfers. In the latter case, the
42 * command may have an argument field. If the argument is a pathname, the command is analogous to
43 * the "list" command except that data shall be transferred over the control connection. If a
44 * partial pathname is given, the server may respond with a list of file names or attributes
45 * associated with that specification. If no argument is given, the server should return general
46 * status information about the server FTP process. This should include current values of all
47 * transfer parameters and the status of connections.
48 * <p>
49 * <i>[Excerpt from RFC-959, Postel and Reynolds]</i>
50 * </p>
51 *
52 * @author Lars Behnke
53 */
54 public class FtpCmdStat extends AbstractFtpCmd {
55
56 /***
57 * {@inheritDoc}
58 */
59 public void execute() throws FtpCmdException {
60 String clientHost = getCtx().getClientSocket().getInetAddress().getHostAddress();
61 String msg = getCtx().getUser() + "(" + clientHost + ")";
62 msgOut(MSG211_STAT, new Object[] {msg});
63 Map<String, Long> map = getCtx().getUserManager().getUserStatistics(getCtx().getUser());
64 String arg = getArguments();
65 if (arg.length() == 0) {
66 printUserStatistics(map);
67 } else {
68 File dir = new File(getPathArg());
69 if (!dir.exists()) {
70 msgOut(MSG550);
71 return;
72 }
73 if (dir.isDirectory()) {
74 File[] files = dir.listFiles();
75 for (int i = 0; i < files.length; i++) {
76 doPrintFileInfo(files[i]);
77 }
78 } else {
79 doPrintFileInfo(dir);
80 }
81 }
82 out("211 ");
83 }
84
85 private void doPrintFileInfo(File file) {
86 int permission = getCtx().getPermission(file.getAbsolutePath());
87 boolean read = (permission & PRIV_READ) > 0;
88 boolean write = (permission & PRIV_WRITE) > 0;
89 out("211-" + IOUtils.formatUnixFtpFileInfo(file, read, write));
90
91 }
92
93 private void printUserStatistics(Map<String, Long> map) {
94 Set<Map.Entry<String, Long>> entrySet = map.entrySet();
95 for (Map.Entry<String, Long> entry : entrySet) {
96 String statName = (String) entry.getKey();
97 Long value = (Long) entry.getValue();
98 printOutStats(statName, value);
99
100 }
101
102 }
103
104 private void printOutStats(String statName, Long value) {
105 long statValue = value == null ? 0 : value.longValue();
106 out("211-" + statName + ": " + statValue);
107 }
108
109 /***
110 * {@inheritDoc}
111 */
112 public String getHelp() {
113 return "Returns short client session based statistic";
114 }
115
116 /***
117 * {@inheritDoc}
118 */
119 public boolean isAuthenticationRequired() {
120 return true;
121 }
122
123 }