private boolean areFtpFilesIdentical(FTPFile a, FTPFile b) { if (haveDifferentTimestamps(a, b)) { return false; } if (haveDifferentGroups(a, b)) { return false; } if (haveDifferentUsers(a, b)) { return false; } if (a.getSize() != b.getSize()) { return false; } for (int userType : userTypes) { for (int permissionType : permissionTypes) { if (a.hasPermission(userType, permissionType) != b.hasPermission(userType, permissionType)) { return false; } } } return true; }
/** * 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 long getSize() { if (getFileSize() > 0) { return getFileSize(); } else if (isFolder()) { return 0; } setFileSize(ftpFile.getSize()); return getFileSize(); }
public FTPProgressPanel(FTPFile file, String sourcePath, DefaultMutableTreeNode parent) { this.file = file; this.sourcePath = sourcePath; if (file != null) { label = new JLabel(file.getName() + " " + file.getSize()); } add(label); }
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; }
/** * download file from remote host and display the downloaded percentage * * @param folder remote directory * @param fileName the file you want to download * @param destfolder the destination folder you will store the file */ public void downloadFileInProgress(String folder, String fileName, String destfolder) { try { ftp.enterLocalPassiveMode(); ftp.changeWorkingDirectory(folder); LogUtils.log("Changing to directory:[" + folder + "]"); String realFile = destfolder + File.separator + fileName; File localFile = new File(realFile); FileOutputStream fos = new FileOutputStream(localFile); LogUtils.log("Start downloading.."); FTPFile[] fs = ftp.listFiles(); DecimalFormat df = new DecimalFormat("#.00%"); for (FTPFile f : fs) { if (f.getName().equals(fileName)) { InputStream is = ftp.retrieveFileStream(f.getName()); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); long transfered = 0, total = f.getSize(); double ratio = 0.0; while (line != null) { byte[] buff = line.getBytes(); transfered += buff.length; if (transfered * 1.0 / total - ratio >= 0.01) { ratio = transfered * 1.0 / total; LogUtils.log("Download size : " + df.format(ratio)); } fos.write(buff); line = br.readLine(); } is.close(); ftp.logout(); ftp.disconnect(); LogUtils.log("Download done!"); } } fos.close(); } catch (IOException e) { e.printStackTrace(); } }
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 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 size() { return attributes.getSize(); }