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

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

Introduction

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

Prototype

int READ_PERMISSION

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

Click Source Link

Document

A constant indicating file/directory read permission.

Usage

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

@Test
public void testParse() {
    FTPFile parsed;//from  ww w .java  2  s  . c o  m

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1  freebox  freebox 2064965868 Apr 15 21:17 M6 - Capital 15-04-2007 21h37 1h40m.ts");
    assertNotNull(parsed);
    assertEquals(parsed.getName(), "M6 - Capital 15-04-2007 21h37 1h40m.ts");
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals("freebox", parsed.getUser());
    assertEquals("freebox", parsed.getGroup());
    assertEquals(2064965868, parsed.getSize());
    assertEquals(Calendar.APRIL, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(15, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1  freebox  freebox 75906880 Sep 08 06:33 Direct 8 - Gym direct - 08-09-2007 08h30 1h08m.ts");
    assertNotNull(parsed);
    assertEquals("Direct 8 - Gym direct - 08-09-2007 08h30 1h08m.ts", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals("freebox", parsed.getUser());
    assertEquals("freebox", parsed.getGroup());
    assertEquals(75906880, parsed.getSize());
    assertEquals(Calendar.SEPTEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(8, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1  freebox  freebox 1171138668 May 19 17:20 France 3 national - 19-05-2007 18h15 1h05m.ts");
    assertNotNull(parsed);
    assertEquals("France 3 national - 19-05-2007 18h15 1h05m.ts", parsed.getName());
}

From source file:lucee.commons.io.res.type.ftp.FTPResource.java

@Override
public boolean isReadable() {
    Boolean rtn = hasPermission(FTPFile.READ_PERMISSION);
    if (rtn == null)
        return false;
    return rtn.booleanValue();
}

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

/**
 * http://trac.cyberduck.ch/ticket/1198//  w w w .  j a va  2s .  c  om
 */
@Test
public void testFolder() {
    FTPFile parsed;

    parsed = parser.parseFTPEntry("dr--r--r--                folder          0 Aug  1 10:18 TestCyberduck");
    assertNotNull(parsed);
    assertEquals("TestCyberduck", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals(Calendar.AUGUST, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(1, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertEquals(10, parsed.getTimestamp().get(Calendar.HOUR_OF_DAY));
    assertEquals(18, parsed.getTimestamp().get(Calendar.MINUTE));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
}

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

protected FTPFile parseFTPEntry(String typeStr, String usr, String grp, long filesize, String datestr,
        String name, String endtoken) {
    final FTPExtendedFile file = new FTPExtendedFile();
    int type;//from  w w w.j a va  2  s.  co  m
    try {
        file.setTimestamp(this.parseTimestamp(datestr));
    } catch (ParseException e) {
        log.warn(e.getMessage());
    }

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

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

    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("-")) {
            file.setPermission(access, FTPFile.EXECUTE_PERMISSION, false);
        } else {
            file.setPermission(access, FTPFile.EXECUTE_PERMISSION, Character.isLowerCase(execPerm.charAt(0)));
            if (0 == access) {
                file.setSetuid(execPerm.charAt(0) == 's' || execPerm.charAt(0) == 'S');
            }
            if (1 == access) {
                file.setSetgid(execPerm.charAt(0) == 's' || execPerm.charAt(0) == 'S');
            }
            if (2 == access) {
                file.setSticky(execPerm.charAt(0) == 't' || execPerm.charAt(0) == 'T');
            }
        }
    }

    file.setSize(filesize);

    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;
}

From source file:com.naryx.tagfusion.cfm.tag.net.ftp.FtpList.java

private String getMode(FTPFile ftpfile, int access) {
    int octet = 0;

    if (ftpfile.hasPermission(access, FTPFile.READ_PERMISSION))
        octet += 4;//from  w  w  w .jav  a  2s  .  com
    if (ftpfile.hasPermission(access, FTPFile.WRITE_PERMISSION))
        octet += 2;
    if (ftpfile.hasPermission(access, FTPFile.SYMBOLIC_LINK_TYPE)
            || ftpfile.hasPermission(access, FTPFile.EXECUTE_PERMISSION))
        octet += 1;

    return octet + "";
}

From source file:com.github.fge.ftpfs.io.commonsnetimpl.CommonsNetFtpAgent.java

private static EnumSet<AccessMode> calculateAccess(final FTPFile file) {
    final EnumSet<AccessMode> ret = EnumSet.noneOf(AccessMode.class);
    if (file.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION))
        ret.add(AccessMode.READ);
    if (file.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION))
        ret.add(AccessMode.WRITE);
    if (file.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION))
        ret.add(AccessMode.EXECUTE);
    return ret;/*from www  .  jav a  2 s .c  o  m*/
}

From source file:hd3gtv.storage.AbstractFileBridgeFtp.java

public boolean canRead() {
    return file.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION);
}

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 ww. j av a 2  s. c o m*/
    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:com.jaeksoft.searchlib.crawler.file.process.fileInstances.FtpFileInstance.java

public static List<SecurityAccess> getSecurity(FTPFile ftpFile) {
    List<SecurityAccess> accesses = new ArrayList<SecurityAccess>();
    if (ftpFile == null)
        return accesses;
    if (ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION)) {
        SecurityAccess access = new SecurityAccess();
        access.setGrant(SecurityAccess.Grant.ALLOW);
        access.setType(SecurityAccess.Type.USER);
        access.setId(ftpFile.getUser());
        accesses.add(access);//from ww  w  .j  a  v  a  2  s . c o  m
    }

    if (ftpFile.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION)) {
        SecurityAccess access = new SecurityAccess();
        access.setGrant(SecurityAccess.Grant.ALLOW);
        access.setType(SecurityAccess.Type.GROUP);
        access.setId(ftpFile.getGroup());
        accesses.add(access);
    }

    return accesses;
}

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>//w  ww .  j a  v  a 2s .c  o 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;
}