Example usage for org.apache.commons.net.ftp FTPFile DIRECTORY_TYPE

List of usage examples for org.apache.commons.net.ftp FTPFile DIRECTORY_TYPE

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPFile DIRECTORY_TYPE.

Prototype

int DIRECTORY_TYPE

To view the source code for org.apache.commons.net.ftp FTPFile DIRECTORY_TYPE.

Click Source Link

Document

A constant indicating an FTPFile is a directory.

Usage

From source file:de.ep3.ftpc.model.CrawlerDirectories.java

/**
 * Provides the next file or directory in the current stack.
 *
 * Since this is a state machine, it will return a different results on
 * consecutive calls.//from w  ww  .  jav  a 2  s  .  c o m
 *
 * @return File, directory or null, if finished.
 * @throws IOException
 */
public CrawlerFile getNextFile() throws IOException {
    if (directoryStack.isEmpty()) {
        if (includePaths.isEmpty()) {
            return null;
        }

        String nextIncludePath = includePaths.firstElement();
        includePaths.remove(nextIncludePath);

        directoryStack.push(new CrawlerDirectory(ftpClient, nextIncludePath));
    }

    CrawlerDirectory currentDirectory = directoryStack.peek();

    FTPFile file = currentDirectory.getNextFile();

    if (file == null) {
        directoryStack.pop();

        return getNextFile();
    }

    if (file.getType() == FTPFile.DIRECTORY_TYPE) {
        String nextPath = currentDirectory.getPath() + file.getName();

        if (!excludePaths.contains(nextPath)) {
            directoryStack.push(new CrawlerDirectory(ftpClient, nextPath));
        }
    }

    return new CrawlerFile(file, currentDirectory.getPath());
}

From source file:ch.cyberduck.core.ftp.parser.UnixFTPEntryParserTest.java

@Test
public void testParseFTPEntryExpected() {
    FTPFileEntryParser parser = new FTPParserSelector().getParser("UNIX");

    FTPFile parsed;/*from ww w .  j a v  a 2s.  c o m*/

    parsed = parser
            .parseFTPEntry("drw-rw-rw-   1 user      ftp             0  Mar 11 20:56 ADMIN_Documentation");
    assertNotNull(parsed);
    assertEquals(parsed.getType(), FTPFile.DIRECTORY_TYPE);
    assertEquals("user", parsed.getUser());
    assertEquals("ftp", parsed.getGroup());
    assertEquals("ADMIN_Documentation", parsed.getName());

    parsed = parser.parseFTPEntry("drwxr--r--   1 user     group          0 Feb 14 18:14 Downloads");
    assertNotNull(parsed);
    assertEquals("Downloads", parsed.getName());
}

From source file:ch.cyberduck.core.ftp.FTPListResponseReader.java

@Override
public AttributedList<Path> read(final Path directory, final List<String> replies,
        final ListProgressListener listener)
        throws IOException, FTPInvalidListException, ConnectionCanceledException {
    final AttributedList<Path> children = new AttributedList<Path>();
    // At least one entry successfully parsed
    boolean success = false;
    // Call hook for those implementors which need to perform some action upon the list after it has been created
    // from the server stream, but before any clients see the list
    parser.preParse(replies);//from   w  w  w .  ja v a  2  s  .c  om
    for (String line : replies) {
        final FTPFile f = parser.parseFTPEntry(line);
        if (null == f) {
            continue;
        }
        final String name = f.getName();
        if (!success) {
            if (lenient) {
                // Workaround for #2410. STAT only returns ls of directory itself
                // Workaround for #2434. STAT of symbolic link directory only lists the directory itself.
                if (directory.getName().equals(name)) {
                    log.warn(String.format("Skip %s matching parent directory name", f.getName()));
                    continue;
                }
                if (name.contains(String.valueOf(Path.DELIMITER))) {
                    if (!name.startsWith(directory.getAbsolute() + Path.DELIMITER)) {
                        // Workaround for #2434.
                        log.warn(String.format("Skip %s with delimiter in name", name));
                        continue;
                    }
                }
            }
        }
        success = true;
        if (name.equals(".") || name.equals("..")) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Skip %s", f.getName()));
            }
            continue;
        }
        final Path parsed = new Path(directory, PathNormalizer.name(name),
                f.getType() == FTPFile.DIRECTORY_TYPE ? EnumSet.of(Path.Type.directory)
                        : EnumSet.of(Path.Type.file));
        switch (f.getType()) {
        case FTPFile.SYMBOLIC_LINK_TYPE:
            parsed.setType(EnumSet.of(Path.Type.file, Path.Type.symboliclink));
            // Symbolic link target may be an absolute or relative path
            final String target = f.getLink();
            if (StringUtils.isBlank(target)) {
                log.warn(String.format("Missing symbolic link target for %s", parsed));
                final EnumSet<AbstractPath.Type> type = parsed.getType();
                type.remove(AbstractPath.Type.symboliclink);
            } else if (StringUtils.startsWith(target, String.valueOf(Path.DELIMITER))) {
                parsed.setSymlinkTarget(new Path(target, EnumSet.of(Path.Type.file)));
            } else if (StringUtils.equals("..", target)) {
                parsed.setSymlinkTarget(directory);
            } else if (StringUtils.equals(".", target)) {
                parsed.setSymlinkTarget(parsed);
            } else {
                parsed.setSymlinkTarget(new Path(directory, target, EnumSet.of(Path.Type.file)));
            }
            break;
        }
        if (parsed.isFile()) {
            parsed.attributes().setSize(f.getSize());
        }
        parsed.attributes().setOwner(f.getUser());
        parsed.attributes().setGroup(f.getGroup());
        Permission.Action u = Permission.Action.none;
        if (f.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION)) {
            u = u.or(Permission.Action.read);
        }
        if (f.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION)) {
            u = u.or(Permission.Action.write);
        }
        if (f.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION)) {
            u = u.or(Permission.Action.execute);
        }
        Permission.Action g = Permission.Action.none;
        if (f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION)) {
            g = g.or(Permission.Action.read);
        }
        if (f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION)) {
            g = g.or(Permission.Action.write);
        }
        if (f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION)) {
            g = g.or(Permission.Action.execute);
        }
        Permission.Action o = Permission.Action.none;
        if (f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION)) {
            o = o.or(Permission.Action.read);
        }
        if (f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION)) {
            o = o.or(Permission.Action.write);
        }
        if (f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION)) {
            o = o.or(Permission.Action.execute);
        }
        final Permission permission = new Permission(u, g, o);
        if (f instanceof FTPExtendedFile) {
            permission.setSetuid(((FTPExtendedFile) f).isSetuid());
            permission.setSetgid(((FTPExtendedFile) f).isSetgid());
            permission.setSticky(((FTPExtendedFile) f).isSticky());
        }
        parsed.attributes().setPermission(permission);
        final Calendar timestamp = f.getTimestamp();
        if (timestamp != null) {
            parsed.attributes().setModificationDate(timestamp.getTimeInMillis());
        }
        children.add(parsed);
    }
    if (!success) {
        throw new FTPInvalidListException(children);
    }
    return children;
}

From source file:ch.cyberduck.core.ftp.parser.RumpusFTPEntryParserTest.java

@Test
public void testUnknownSystIdentifier() throws Exception {
    this.parser = new FTPParserSelector().getParser("Digital Domain FTP");

    FTPFile parsed;//w w  w . j  av a2s.  co  m
    parsed = parser.parseFTPEntry("drwxrwxrwx               folder        0 Jan 19 20:36 Mastered 1644");
    assertNotNull(parsed);
    assertEquals("Mastered 1644", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());

    parsed = parser.parseFTPEntry("-rwxrwxrwx        0   208143684 208143684 Jan 14 02:13 Dhannya dhannya.rar");
    assertNotNull(parsed);
    assertEquals("Dhannya dhannya.rar", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());

    parsed = parser.parseFTPEntry("drwxr-xr-x               folder        0 Jan 14 16:04 Probeordner");
    assertNotNull(parsed);
    assertEquals("Probeordner", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
}

From source file:com.datos.vfs.provider.ftp.FtpFileObject.java

/**
 * Fetches the info for this file.// ww w .  j  a  v  a 2s  . co  m
 */
private void getInfo(final boolean flush) throws IOException {
    final FtpFileObject parent = (FtpFileObject) FileObjectUtils.getAbstractFileObject(getParent());
    FTPFile newFileInfo;
    if (parent != null) {
        newFileInfo = parent.getChildFile(UriParser.decode(getName().getBaseName()), flush);
    } else {
        // Assume the root is a directory and exists
        newFileInfo = new FTPFile();
        newFileInfo.setType(FTPFile.DIRECTORY_TYPE);
    }

    if (newFileInfo == null) {
        this.fileInfo = UNKNOWN;
    } else {
        this.fileInfo = newFileInfo;
    }
}

From source file:au.org.intersect.dms.wn.transports.impl.FtpConnection.java

private FileInfo makeFileInfo(String parentPath, FTPFile info) {
    Date date = info.getTimestamp().getTime();
    int ftpType = info.getType();
    if (ftpType == FTPFile.SYMBOLIC_LINK_TYPE) {
        try {/* w w  w  .ja va  2 s. co m*/
            changeWorkingDirectory(PathUtils.joinPath(parentPath, info.getName()));
            ftpType = FTPFile.DIRECTORY_TYPE;
        } catch (IOException e) {
            throw new TransportException("Cannot get list directory (" + info.getName() + ")");
        } catch (PathNotFoundException e) {
            ftpType = FTPFile.FILE_TYPE;
        }
    }
    FileType type = ftpType == FTPFile.DIRECTORY_TYPE ? FileType.DIRECTORY : FileType.FILE;
    FileInfo item = new FileInfo(type, PathUtils.joinPath(parentPath, info.getName()), info.getName(),
            info.getSize(), date);
    return item;
}

From source file:com.ai.api.util.UnixFTPEntryParser.java

/**
 * 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 file listing line doesn't describe a file,
 * <code> null </code> is returned, otherwise a <code> FTPFile </code>
 * instance representing the files in the directory is returned.
 * <p>/*from w  ww. j a v a  2s.co m*/
 * @param entry A line of text from the file listing
 * @return An FTPFile instance corresponding to the supplied entry
 */
public FTPFile parseFTPEntry(String entry) {
    FTPFile file = new FTPFile();
    file.setRawListing(entry);
    int type;
    boolean isDevice = false;

    if (matches(entry)) {
        String typeStr = group(1);
        String hardLinkCount = group(15);
        String usr = group(16);
        String grp = group(17);
        String filesize = group(18);
        String datestr = group(19) + " " + group(20);
        String name = group(21);
        String endtoken = group(22);

        try {
            //file.setTimestamp(super.parseTimestamp(datestr));
            FTPTimestampParserImplExZH Zh2En = new FTPTimestampParserImplExZH();
            file.setTimestamp(Zh2En.parseTimestamp(datestr));
        } catch (ParseException e) {
            //logger.error(e, e);
            //return null;  // this is a parsing failure too.
            //logger.info(entry+":??");
            file.setTimestamp(Calendar.getInstance());
        }

        // bcdlfmpSs-
        switch (typeStr.charAt(0)) {
        case 'd':
            type = FTPFile.DIRECTORY_TYPE;
            break;
        case 'l':
            type = FTPFile.SYMBOLIC_LINK_TYPE;
            break;
        case 'b':
        case 'c':
            isDevice = true;
            // break; - fall through
        case 'f':
        case '-':
            type = FTPFile.FILE_TYPE;
            break;
        default:
            type = FTPFile.UNKNOWN_TYPE;
        }

        file.setType(type);

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

            String execPerm = group(g + 2);
            if (!execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0))) {
                file.setPermission(access, FTPFile.EXECUTE_PERMISSION, true);
            } else {
                file.setPermission(access, FTPFile.EXECUTE_PERMISSION, false);
            }
        }

        if (!isDevice) {
            try {
                file.setHardLinkCount(Integer.parseInt(hardLinkCount));
            } catch (NumberFormatException e) {
                // intentionally do nothing
            }
        }

        file.setUser(usr);
        file.setGroup(grp);

        try {
            file.setSize(Long.parseLong(filesize));
        } catch (NumberFormatException e) {
            // intentionally do nothing
        }

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

                int end = name.indexOf(" -> ");
                // Give up if no link indicator is present
                if (end == -1) {
                    file.setName(name);
                } else {
                    file.setName(name.substring(0, end));
                    file.setLink(name.substring(end + 4));
                }

            } else {
                file.setName(name);
            }
        }
        return file;
    } else {
        logger.info("matches(entry) failure:" + entry);
    }
    return null;
}

From source file:ch.cyberduck.core.ftp.parser.UnixFTPEntryParserTest.java

@Test
public void testLowerCaseMonths() {
    FTPFileEntryParser parser = new FTPParserSelector().getParser("UNIX");

    FTPFile parsed;// ww w .j  a va2s  .  c  o  m

    parsed = parser.parseFTPEntry("drwxrwxrwx    41 spinkb  spinkb      1394 jan 21 20:57 Desktop");
    assertNotNull(parsed);
    assertEquals("Desktop", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals("spinkb", parsed.getUser());
    assertEquals("spinkb", parsed.getGroup());
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.JANUARY, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(21, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
}

From source file:ch.cyberduck.core.ftp.parser.UnixFTPEntryParserTest.java

@Test
public void testUpperCaseMonths() {
    FTPFileEntryParser parser = new FTPParserSelector().getParser("UNIX");

    FTPFile parsed;//from  w ww.j  a  va  2 s.co  m

    parsed = parser.parseFTPEntry("drwxrwxrwx    41 spinkb  spinkb      1394 Feb 21 20:57 Desktop");
    assertNotNull(parsed);
    assertEquals("Desktop", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals("spinkb", parsed.getUser());
    assertEquals("spinkb", parsed.getGroup());
    assertEquals(Calendar.FEBRUARY, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(21, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
}

From source file:ch.cyberduck.core.ftp.parser.UnixFTPEntryParserTest.java

@Test
public void testSolarisAcl() {
    FTPFileEntryParser parser = new FTPParserSelector().getParser("UNIX");

    FTPFile parsed;/*w w  w . j a  v a 2s .com*/

    //#215
    parsed = parser.parseFTPEntry("drwxrwsr-x+ 34 cristol  molvis      3072 Jul 12 20:16 molvis");
    assertNotNull(parsed);
    assertEquals(parsed.getName(), "molvis");
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals("cristol", parsed.getUser());
    assertEquals("molvis", parsed.getGroup());
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.JULY, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(12, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
}