1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.net.ftp.parser;
19
20 import java.text.ParseException;
21
22 import org.apache.commons.net.ftp.FTPClientConfig;
23 import org.apache.commons.net.ftp.FTPFile;
24
25 /**
26 * Implementation FTPFileEntryParser and FTPFileListParser for pre MacOS-X Systems.
27 *
28 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
29 * @since 3.1
30 */
31 public class MacOsPeterFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {
32
33 static final String DEFAULT_DATE_FORMAT = "MMM d yyyy"; // Nov 9 2001
34
35 static final String DEFAULT_RECENT_DATE_FORMAT = "MMM d HH:mm"; // Nov 9 20:06
36
37 /**
38 * this is the regular expression used by this parser.
39 *
40 * Permissions: r the file is readable w the file is writable x the file is executable - the indicated permission is not granted L mandatory locking occurs
41 * during access (the set-group-ID bit is on and the group execution bit is off) s the set-user-ID or set-group-ID bit is on, and the corresponding user or
42 * group execution bit is also on S undefined bit-state (the set-user-ID bit is on and the user execution bit is off) t the 1000 (octal) bit, or sticky bit,
43 * is on [see chmod(1)], and execution is on T the 1000 bit is turned on, and execution is off (undefined bit-state) e z/OS external link bit
44 */
45 private static final String REGEX = "([bcdelfmpSs-])" // type (1)
46 + "(((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\\+?\\s+" // permission
47 + "(" + "(folder\\s+)" + "|" + "((\\d+)\\s+(\\d+)\\s+)" // resource size & data size
48 + ")" + "(\\d+)\\s+" // size
49 /*
50 * numeric or standard format date: yyyy-mm-dd (expecting hh:mm to follow) MMM [d]d [d]d MMM Use non-space for MMM to allow for languages such
51 * as German which use diacritics (e.g. umlaut) in some abbreviations.
52 */
53 + "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S{3}\\s+\\d{1,2})|(?:\\d{1,2}\\s+\\S{3}))\\s+"
54 /*
55 * year (for non-recent standard format) - yyyy or time (for numeric or recent standard format) [h]h:mm
56 */
57 + "(\\d+(?::\\d+)?)\\s+"
58
59 + "(\\S*)(\\s*.*)"; // the rest
60
61 /**
62 * The default constructor for a UnixFTPEntryParser object.
63 *
64 * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
65 * sign that {@code REGEX} is not a valid regular expression.
66 */
67 public MacOsPeterFTPEntryParser() {
68 this(null);
69 }
70
71 /**
72 * This constructor allows the creation of a UnixFTPEntryParser object with something other than the default configuration.
73 *
74 * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
75 * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
76 * sign that {@code REGEX} is not a valid regular expression.
77 * @since 1.4
78 */
79 public MacOsPeterFTPEntryParser(final FTPClientConfig config) {
80 super(REGEX);
81 configure(config);
82 }
83
84 /**
85 * Gets a new default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
86 *
87 * @return the default configuration for this parser.
88 */
89 @Override
90 protected FTPClientConfig getDefaultConfiguration() {
91 return new FTPClientConfig(FTPClientConfig.SYST_UNIX, DEFAULT_DATE_FORMAT, DEFAULT_RECENT_DATE_FORMAT);
92 }
93
94 /**
95 * Parses a line of a Unix (standard) FTP server file listing and converts it into a usable format in the form of an {@code FTPFile} instance. If the
96 * file listing line doesn't describe a file, {@code null} is returned, otherwise a {@code FTPFile} instance representing the files in the
97 * directory is returned.
98 *
99 * @param entry A line of text from the file listing
100 * @return An FTPFile instance corresponding to the supplied entry
101 */
102 @Override
103 public FTPFile parseFTPEntry(final String entry) {
104 final FTPFile file = new FTPFile();
105 file.setRawListing(entry);
106 final int type;
107 boolean isDevice = false;
108
109 if (matches(entry)) {
110 final String typeStr = group(1);
111 final String hardLinkCount = "0";
112 final String fileSize = group(20);
113 final String datestr = group(21) + " " + group(22);
114 String name = group(23);
115 final String endtoken = group(24);
116
117 try {
118 file.setTimestamp(super.parseTimestamp(datestr));
119 } catch (final ParseException e) {
120 // intentionally do nothing
121 }
122
123 // A 'whiteout' file is an ARTIFICIAL entry in any of several types of
124 // 'translucent' filesystems, of which a 'union' filesystem is one.
125
126 // bcdelfmpSs-
127 switch (typeStr.charAt(0)) {
128 case 'd':
129 type = FTPFile.DIRECTORY_TYPE;
130 break;
131 case 'e': // NET-39 => z/OS external link
132 type = FTPFile.SYMBOLIC_LINK_TYPE;
133 break;
134 case 'l':
135 type = FTPFile.SYMBOLIC_LINK_TYPE;
136 break;
137 case 'b':
138 case 'c':
139 isDevice = true;
140 type = FTPFile.FILE_TYPE; // TODO change this if DEVICE_TYPE implemented
141 break;
142 case 'f':
143 case '-':
144 type = FTPFile.FILE_TYPE;
145 break;
146 default: // e.g. ? and w = whiteout
147 type = FTPFile.UNKNOWN_TYPE;
148 }
149
150 file.setType(type);
151
152 int g = 4;
153 for (int access = 0; access < 3; access++, g += 4) {
154 // Use != '-' to avoid having to check for suid and sticky bits
155 file.setPermission(access, FTPFile.READ_PERMISSION, !group(g).equals("-"));
156 file.setPermission(access, FTPFile.WRITE_PERMISSION, !group(g + 1).equals("-"));
157
158 final String execPerm = group(g + 2);
159 file.setPermission(access, FTPFile.EXECUTE_PERMISSION, !execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0)));
160 }
161
162 if (!isDevice) {
163 try {
164 file.setHardLinkCount(Integer.parseInt(hardLinkCount));
165 } catch (final NumberFormatException e) {
166 // intentionally do nothing
167 }
168 }
169
170 file.setUser(null);
171 file.setGroup(null);
172
173 try {
174 file.setSize(Long.parseLong(fileSize));
175 } catch (final NumberFormatException e) {
176 // intentionally do nothing
177 }
178
179 if (null == endtoken) {
180 file.setName(name);
181 } else {
182 // oddball cases like symbolic links, file names
183 // with spaces in them.
184 name += endtoken;
185 if (type == FTPFile.SYMBOLIC_LINK_TYPE) {
186
187 final int end = name.indexOf(" -> ");
188 // Give up if no link indicator is present
189 if (end == -1) {
190 file.setName(name);
191 } else {
192 file.setName(name.substring(0, end));
193 file.setLink(name.substring(end + 4));
194 }
195
196 } else {
197 file.setName(name);
198 }
199 }
200 return file;
201 }
202 return null;
203 }
204
205 }