001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.ftp.parser;
019import java.text.ParseException;
020
021import org.apache.commons.net.ftp.FTPClientConfig;
022import org.apache.commons.net.ftp.FTPFile;
023
024/**
025 * Implementation FTPFileEntryParser and FTPFileListParser for pre MacOS-X Systems.
026 *
027 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
028 * @since 3.1
029 */
030public class MacOsPeterFTPEntryParser extends ConfigurableFTPFileEntryParserImpl
031{
032
033    static final String DEFAULT_DATE_FORMAT
034        = "MMM d yyyy"; //Nov 9 2001
035
036    static final String DEFAULT_RECENT_DATE_FORMAT
037        = "MMM d HH:mm"; //Nov 9 20:06
038
039    /**
040     * this is the regular expression used by this parser.
041     *
042     * Permissions:
043     *    r   the file is readable
044     *    w   the file is writable
045     *    x   the file is executable
046     *    -   the indicated permission is not granted
047     *    L   mandatory locking occurs during access (the set-group-ID bit is
048     *        on and the group execution bit is off)
049     *    s   the set-user-ID or set-group-ID bit is on, and the corresponding
050     *        user or group execution bit is also on
051     *    S   undefined bit-state (the set-user-ID bit is on and the user
052     *        execution bit is off)
053     *    t   the 1000 (octal) bit, or sticky bit, is on [see chmod(1)], and
054     *        execution is on
055     *    T   the 1000 bit is turned on, and execution is off (undefined bit-
056     *        state)
057     *    e   z/OS external link bit
058     */
059    private static final String REGEX =
060        "([bcdelfmpSs-])" // type (1)
061        + "(((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\\+?\\s+" // permission
062        + "("
063        +   "(folder\\s+)"
064        + "|"
065        +   "((\\d+)\\s+(\\d+)\\s+)" // resource size & data size
066        + ")"
067        + "(\\d+)\\s+"              // size
068        /*
069         * numeric or standard format date:
070         *   yyyy-mm-dd (expecting hh:mm to follow)
071         *   MMM [d]d
072         *   [d]d MMM
073         *   N.B. use non-space for MMM to allow for languages such as German which use
074         *   diacritics (e.g. umlaut) in some abbreviations.
075        */
076        + "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S{3}\\s+\\d{1,2})|(?:\\d{1,2}\\s+\\S{3}))\\s+"
077        /*
078           year (for non-recent standard format) - yyyy
079           or time (for numeric or recent standard format) [h]h:mm
080        */
081        + "(\\d+(?::\\d+)?)\\s+"
082
083        + "(\\S*)(\\s*.*)"; // the rest
084
085
086    /**
087     * The default constructor for a UnixFTPEntryParser object.
088     *
089     * @throws IllegalArgumentException
090     * Thrown if the regular expression is unparseable.  Should not be seen
091     * under normal conditions.  It it is seen, this is a sign that
092     * <code>REGEX</code> is  not a valid regular expression.
093     */
094    public MacOsPeterFTPEntryParser()
095    {
096        this(null);
097    }
098
099    /**
100     * This constructor allows the creation of a UnixFTPEntryParser object with
101     * something other than the default configuration.
102     *
103     * @param config The {@link FTPClientConfig configuration} object used to
104     * configure this parser.
105     * @throws IllegalArgumentException
106     * Thrown if the regular expression is unparseable.  Should not be seen
107     * under normal conditions.  It it is seen, this is a sign that
108     * <code>REGEX</code> is  not a valid regular expression.
109     * @since 1.4
110     */
111    public MacOsPeterFTPEntryParser(final FTPClientConfig config)
112    {
113        super(REGEX);
114        configure(config);
115    }
116
117    /**
118     * Parses a line of a unix (standard) FTP server file listing and converts
119     * it into a usable format in the form of an <code> FTPFile </code>
120     * instance.  If the file listing line doesn't describe a file,
121     * <code> null </code> is returned, otherwise a <code> FTPFile </code>
122     * instance representing the files in the directory is returned.
123     *
124     * @param entry A line of text from the file listing
125     * @return An FTPFile instance corresponding to the supplied entry
126     */
127    @Override
128    public FTPFile parseFTPEntry(final String entry) {
129        final FTPFile file = new FTPFile();
130        file.setRawListing(entry);
131        final int type;
132        boolean isDevice = false;
133
134        if (matches(entry))
135        {
136            final String typeStr = group(1);
137            final String hardLinkCount = "0";
138            final String usr = null;
139            final String grp = null;
140            final String filesize = group(20);
141            final String datestr = group(21) + " " + group(22);
142            String name = group(23);
143            final String endtoken = group(24);
144
145            try
146            {
147                file.setTimestamp(super.parseTimestamp(datestr));
148            }
149            catch (final ParseException e)
150            {
151                 // intentionally do nothing
152            }
153
154            // A 'whiteout' file is an ARTIFICIAL entry in any of several types of
155            // 'translucent' filesystems, of which a 'union' filesystem is one.
156
157            // bcdelfmpSs-
158            switch (typeStr.charAt(0))
159            {
160            case 'd':
161                type = FTPFile.DIRECTORY_TYPE;
162                break;
163            case 'e': // NET-39 => z/OS external link
164                type = FTPFile.SYMBOLIC_LINK_TYPE;
165                break;
166            case 'l':
167                type = FTPFile.SYMBOLIC_LINK_TYPE;
168                break;
169            case 'b':
170            case 'c':
171                isDevice = true;
172                type = FTPFile.FILE_TYPE; // TODO change this if DEVICE_TYPE implemented
173                break;
174            case 'f':
175            case '-':
176                type = FTPFile.FILE_TYPE;
177                break;
178            default: // e.g. ? and w = whiteout
179                type = FTPFile.UNKNOWN_TYPE;
180            }
181
182            file.setType(type);
183
184            int g = 4;
185            for (int access = 0; access < 3; access++, g += 4)
186            {
187                // Use != '-' to avoid having to check for suid and sticky bits
188                file.setPermission(access, FTPFile.READ_PERMISSION,
189                                   (!group(g).equals("-")));
190                file.setPermission(access, FTPFile.WRITE_PERMISSION,
191                                   (!group(g + 1).equals("-")));
192
193                final String execPerm = group(g + 2);
194                if (!execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0)))
195                {
196                    file.setPermission(access, FTPFile.EXECUTE_PERMISSION, true);
197                }
198                else
199                {
200                    file.setPermission(access, FTPFile.EXECUTE_PERMISSION, false);
201                }
202            }
203
204            if (!isDevice)
205            {
206                try
207                {
208                    file.setHardLinkCount(Integer.parseInt(hardLinkCount));
209                }
210                catch (final NumberFormatException e)
211                {
212                    // intentionally do nothing
213                }
214            }
215
216            file.setUser(usr);
217            file.setGroup(grp);
218
219            try
220            {
221                file.setSize(Long.parseLong(filesize));
222            }
223            catch (final NumberFormatException e)
224            {
225                // intentionally do nothing
226            }
227
228            if (null == endtoken)
229            {
230                file.setName(name);
231            }
232            else
233            {
234                // oddball cases like symbolic links, file names
235                // with spaces in them.
236                name += endtoken;
237                if (type == FTPFile.SYMBOLIC_LINK_TYPE)
238                {
239
240                    final int end = name.indexOf(" -> ");
241                    // Give up if no link indicator is present
242                    if (end == -1)
243                    {
244                        file.setName(name);
245                    }
246                    else
247                    {
248                        file.setName(name.substring(0, end));
249                        file.setLink(name.substring(end + 4));
250                    }
251
252                }
253                else
254                {
255                    file.setName(name);
256                }
257            }
258            return file;
259        }
260        return null;
261    }
262
263    /**
264     * Defines a default configuration to be used when this class is
265     * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
266     * parameter being specified.
267     * @return the default configuration for this parser.
268     */
269    @Override
270    protected FTPClientConfig getDefaultConfiguration() {
271        return new FTPClientConfig(
272                FTPClientConfig.SYST_UNIX,
273                DEFAULT_DATE_FORMAT,
274                DEFAULT_RECENT_DATE_FORMAT);
275    }
276
277}