/** * Convert the file information in FTPFile to a {@link FileStatus} object. * * * @param ftpFile * @param parentPath * @return FileStatus */ private FileStatus getFileStatus(FTPFile ftpFile, Path parentPath) { long length = ftpFile.getSize(); boolean isDir = ftpFile.isDirectory(); int blockReplication = 1; // Using default block size since there is no way in FTP client to know of // block sizes on server. The assumption could be less than ideal. long blockSize = DEFAULT_BLOCK_SIZE; long modTime = ftpFile.getTimestamp().getTimeInMillis(); long accessTime = 0; FsPermission permission = getPermissions(ftpFile); String user = ftpFile.getUser(); String group = ftpFile.getGroup(); Path filePath = new Path(parentPath, ftpFile.getName()); return new FileStatus( length, isDir, blockReplication, blockSize, modTime, accessTime, permission, user, group, filePath.makeQualified(this)); }
/** @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory() */ @Override public void testParseFieldsOnDirectory() throws Exception { FTPFile dir = getParser().parseFTPEntry(" 0 DIR 11-28-97 09:42 PC"); assertNotNull("Could not parse entry.", dir); assertTrue("Should have been a directory.", dir.isDirectory()); assertEquals(0, dir.getSize()); assertEquals("PC", dir.getName()); assertEquals("Fri Nov 28 09:42:00 1997", df.format(dir.getTimestamp().getTime())); }
/** @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile() */ @Override public void testParseFieldsOnFile() throws Exception { FTPFile file = getParser().parseFTPEntry("5000000000 A 11-17-98 16:07 POPUPLOG.OS2"); assertNotNull("Could not parse entry.", file); assertTrue("Should have been a file.", file.isFile()); assertEquals(5000000000L, file.getSize()); assertEquals("POPUPLOG.OS2", file.getName()); assertEquals("Tue Nov 17 16:07:00 1998", df.format(file.getTimestamp().getTime())); }
public static FTPStorageDirectory createFromFTPFile(FTPFile file, String path) throws FTPStorageNotADirectory { if (file.getType() != FTPStorageSystem.FTP_STORAGE_DIRECTORY) { throw new FTPStorageNotADirectory(); } FTPStorageDirectory fsd = new FTPStorageDirectory(file.getName(), StorageDirectory.buildFromPath(path, "/")); fsd.setGroup(file.getGroup()); fsd.setUser(file.getUser()); fsd.setCreateDate(file.getTimestamp().getTime()); return fsd; }
private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file, String charset) { RemoteFile<FTPFile> answer = new RemoteFile<FTPFile>(); answer.setCharset(charset); answer.setEndpointPath(endpointPath); answer.setFile(file); answer.setFileNameOnly(file.getName()); answer.setFileLength(file.getSize()); answer.setDirectory(file.isDirectory()); if (file.getTimestamp() != null) { answer.setLastModified(file.getTimestamp().getTimeInMillis()); } answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost()); // absolute or relative path boolean absolute = FileUtil.hasLeadingSeparator(absolutePath); answer.setAbsolute(absolute); // create a pseudo absolute name String dir = FileUtil.stripTrailingSeparator(absolutePath); String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getName()); // if absolute start with a leading separator otherwise let it be relative if (absolute) { absoluteFileName = "/" + absoluteFileName; } answer.setAbsoluteFilePath(absoluteFileName); // the relative filename, skip the leading endpoint configured path String relativePath = ObjectHelper.after(absoluteFileName, endpointPath); // skip leading / relativePath = FileUtil.stripLeadingSeparator(relativePath); answer.setRelativeFilePath(relativePath); // the file name should be the relative path answer.setFileName(answer.getRelativeFilePath()); return answer; }
public static lucee.runtime.type.Query toQuery( FTPFile[] files, String prefix, String directory, String hostName) throws PageException { String[] cols = new String[] { "name", "isdirectory", "lastmodified", "length", "mode", "path", "url", "type", "raw", "attributes" }; String[] types = new String[] { "VARCHAR", "BOOLEAN", "DATE", "DOUBLE", "VARCHAR", "VARCHAR", "VARCHAR", "VARCHAR", "VARCHAR", "VARCHAR" }; lucee.runtime.type.Query query = new QueryImpl(cols, types, 0, "query"); // translate directory path for display if (directory.length() == 0) directory = "/"; else if (directory.startsWith("./")) directory = directory.substring(1); else if (directory.charAt(0) != '/') directory = '/' + directory; if (directory.charAt(directory.length() - 1) != '/') directory = directory + '/'; int row; for (int i = 0; i < files.length; i++) { FTPFile file = files[i]; if (file.getName().equals(".") || file.getName().equals("..")) continue; row = query.addRow(); query.setAt("attributes", row, ""); query.setAt("isdirectory", row, Caster.toBoolean(file.isDirectory())); query.setAt("lastmodified", row, new DateTimeImpl(file.getTimestamp())); query.setAt("length", row, Caster.toDouble(file.getSize())); query.setAt("mode", row, FTPConstant.getPermissionASInteger(file)); query.setAt("type", row, FTPConstant.getTypeAsString(file.getType())); // query.setAt("permission",row,FTPConstant.getPermissionASInteger(file)); query.setAt("raw", row, file.getRawListing()); query.setAt("name", row, file.getName()); query.setAt("path", row, directory + file.getName()); query.setAt("url", row, prefix + "://" + hostName + "" + directory + file.getName()); } return query; }
public long getLastModifiedTime() { return ftpFile.getTimestamp().getTimeInMillis(); }
public boolean acquireExclusiveReadLock( GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; LOG.trace("Waiting for exclusive read lock to file: " + file); long lastModified = Long.MIN_VALUE; long length = Long.MIN_VALUE; StopWatch watch = new StopWatch(); long startTime = (new Date()).getTime(); while (!exclusive) { // timeout check if (timeout > 0) { long delta = watch.taken(); if (delta > timeout) { CamelLogger.log( LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file); // we could not get the lock within the timeout period, so return false return false; } } long newLastModified = 0; long newLength = 0; List<FTPFile> files; if (fastExistsCheck) { // use the absolute file path to only pickup the file we want to check, this avoids // expensive // list operations if we have a lot of files in the directory LOG.trace("Using fast exists to update file information for {}", file); files = operations.listFiles(file.getAbsoluteFilePath()); } else { LOG.trace( "Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file); // fast option not enabled, so list the directory and filter the file name files = operations.listFiles(file.getParent()); } LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size()); for (FTPFile f : files) { if (f.getName().equals(file.getFileNameOnly())) { newLength = f.getSize(); if (f.getTimestamp() != null) { newLastModified = f.getTimestamp().getTimeInMillis(); } } } LOG.trace( "Previous last modified: " + lastModified + ", new last modified: " + newLastModified); LOG.trace("Previous length: " + length + ", new length: " + newLength); long newOlderThan = startTime + watch.taken() - minAge; LOG.trace("New older than threshold: {}", newOlderThan); if (newLength >= minLength && ((minAge == 0 && newLastModified == lastModified && newLength == length) || (minAge != 0 && newLastModified < newOlderThan))) { LOG.trace("Read lock acquired."); exclusive = true; } else { // set new base file change information lastModified = newLastModified; length = newLength; boolean interrupted = sleep(); if (interrupted) { // we were interrupted while sleeping, we are likely being shutdown so return false return false; } } } return exclusive; }
@Override public long lastModifiedTime() { return attributes.getTimestamp().getTimeInMillis(); }
private boolean haveDifferentTimestamps(FTPFile a, FTPFile b) { return !a.getTimestamp().equals(b.getTimestamp()); }