MacOsPeterFTPEntryParser.java

  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.  *      http://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. package org.apache.commons.net.ftp.parser;

  18. import java.text.ParseException;

  19. import org.apache.commons.net.ftp.FTPClientConfig;
  20. import org.apache.commons.net.ftp.FTPFile;

  21. /**
  22.  * Implementation FTPFileEntryParser and FTPFileListParser for pre MacOS-X Systems.
  23.  *
  24.  * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
  25.  * @since 3.1
  26.  */
  27. public class MacOsPeterFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {

  28.     static final String DEFAULT_DATE_FORMAT = "MMM d yyyy"; // Nov 9 2001

  29.     static final String DEFAULT_RECENT_DATE_FORMAT = "MMM d HH:mm"; // Nov 9 20:06

  30.     /**
  31.      * this is the regular expression used by this parser.
  32.      *
  33.      * 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
  34.      * 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
  35.      * 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,
  36.      * 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
  37.      */
  38.     private static final String REGEX = "([bcdelfmpSs-])" // type (1)
  39.             + "(((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\\+?\\s+" // permission
  40.             + "(" + "(folder\\s+)" + "|" + "((\\d+)\\s+(\\d+)\\s+)" // resource size & data size
  41.             + ")" + "(\\d+)\\s+" // size
  42.             /*
  43.              * numeric or standard format date: yyyy-mm-dd (expecting hh:mm to follow) MMM [d]d [d]d MMM N.B. use non-space for MMM to allow for languages such
  44.              * as German which use diacritics (e.g. umlaut) in some abbreviations.
  45.              */
  46.             + "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S{3}\\s+\\d{1,2})|(?:\\d{1,2}\\s+\\S{3}))\\s+"
  47.             /*
  48.              * year (for non-recent standard format) - yyyy or time (for numeric or recent standard format) [h]h:mm
  49.              */
  50.             + "(\\d+(?::\\d+)?)\\s+"

  51.             + "(\\S*)(\\s*.*)"; // the rest

  52.     /**
  53.      * The default constructor for a UnixFTPEntryParser object.
  54.      *
  55.      * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
  56.      *                                  sign that <code>REGEX</code> is not a valid regular expression.
  57.      */
  58.     public MacOsPeterFTPEntryParser() {
  59.         this(null);
  60.     }

  61.     /**
  62.      * This constructor allows the creation of a UnixFTPEntryParser object with something other than the default configuration.
  63.      *
  64.      * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
  65.      * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
  66.      *                                  sign that <code>REGEX</code> is not a valid regular expression.
  67.      * @since 1.4
  68.      */
  69.     public MacOsPeterFTPEntryParser(final FTPClientConfig config) {
  70.         super(REGEX);
  71.         configure(config);
  72.     }

  73.     /**
  74.      * Defines a default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
  75.      *
  76.      * @return the default configuration for this parser.
  77.      */
  78.     @Override
  79.     protected FTPClientConfig getDefaultConfiguration() {
  80.         return new FTPClientConfig(FTPClientConfig.SYST_UNIX, DEFAULT_DATE_FORMAT, DEFAULT_RECENT_DATE_FORMAT);
  81.     }

  82.     /**
  83.      * 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</code> instance. If the
  84.      * file listing line doesn't describe a file, <code>null</code> is returned, otherwise a <code>FTPFile</code> instance representing the files in the
  85.      * directory is returned.
  86.      *
  87.      * @param entry A line of text from the file listing
  88.      * @return An FTPFile instance corresponding to the supplied entry
  89.      */
  90.     @Override
  91.     public FTPFile parseFTPEntry(final String entry) {
  92.         final FTPFile file = new FTPFile();
  93.         file.setRawListing(entry);
  94.         final int type;
  95.         boolean isDevice = false;

  96.         if (matches(entry)) {
  97.             final String typeStr = group(1);
  98.             final String hardLinkCount = "0";
  99.             final String filesize = group(20);
  100.             final String datestr = group(21) + " " + group(22);
  101.             String name = group(23);
  102.             final String endtoken = group(24);

  103.             try {
  104.                 file.setTimestamp(super.parseTimestamp(datestr));
  105.             } catch (final ParseException e) {
  106.                 // intentionally do nothing
  107.             }

  108.             // A 'whiteout' file is an ARTIFICIAL entry in any of several types of
  109.             // 'translucent' filesystems, of which a 'union' filesystem is one.

  110.             // bcdelfmpSs-
  111.             switch (typeStr.charAt(0)) {
  112.             case 'd':
  113.                 type = FTPFile.DIRECTORY_TYPE;
  114.                 break;
  115.             case 'e': // NET-39 => z/OS external link
  116.                 type = FTPFile.SYMBOLIC_LINK_TYPE;
  117.                 break;
  118.             case 'l':
  119.                 type = FTPFile.SYMBOLIC_LINK_TYPE;
  120.                 break;
  121.             case 'b':
  122.             case 'c':
  123.                 isDevice = true;
  124.                 type = FTPFile.FILE_TYPE; // TODO change this if DEVICE_TYPE implemented
  125.                 break;
  126.             case 'f':
  127.             case '-':
  128.                 type = FTPFile.FILE_TYPE;
  129.                 break;
  130.             default: // e.g. ? and w = whiteout
  131.                 type = FTPFile.UNKNOWN_TYPE;
  132.             }

  133.             file.setType(type);

  134.             int g = 4;
  135.             for (int access = 0; access < 3; access++, g += 4) {
  136.                 // Use != '-' to avoid having to check for suid and sticky bits
  137.                 file.setPermission(access, FTPFile.READ_PERMISSION, !group(g).equals("-"));
  138.                 file.setPermission(access, FTPFile.WRITE_PERMISSION, !group(g + 1).equals("-"));

  139.                 final String execPerm = group(g + 2);
  140.                 file.setPermission(access, FTPFile.EXECUTE_PERMISSION, !execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0)));
  141.             }

  142.             if (!isDevice) {
  143.                 try {
  144.                     file.setHardLinkCount(Integer.parseInt(hardLinkCount));
  145.                 } catch (final NumberFormatException e) {
  146.                     // intentionally do nothing
  147.                 }
  148.             }

  149.             file.setUser(null);
  150.             file.setGroup(null);

  151.             try {
  152.                 file.setSize(Long.parseLong(filesize));
  153.             } catch (final NumberFormatException e) {
  154.                 // intentionally do nothing
  155.             }

  156.             if (null == endtoken) {
  157.                 file.setName(name);
  158.             } else {
  159.                 // oddball cases like symbolic links, file names
  160.                 // with spaces in them.
  161.                 name += endtoken;
  162.                 if (type == FTPFile.SYMBOLIC_LINK_TYPE) {

  163.                     final int end = name.indexOf(" -> ");
  164.                     // Give up if no link indicator is present
  165.                     if (end == -1) {
  166.                         file.setName(name);
  167.                     } else {
  168.                         file.setName(name.substring(0, end));
  169.                         file.setLink(name.substring(end + 4));
  170.                     }

  171.                 } else {
  172.                     file.setName(name);
  173.                 }
  174.             }
  175.             return file;
  176.         }
  177.         return null;
  178.     }

  179. }