use of org.apache.commons.net.ftp.FTPFile in project ant by apache.
the class FTP method findFileName.
/**
* find a suitable name for local and remote temporary file
*/
private File findFileName(FTPClient ftp) {
FTPFile[] files = null;
final int maxIterations = 1000;
for (int counter = 1; counter < maxIterations; counter++) {
File localFile = FILE_UTILS.createTempFile("ant" + Integer.toString(counter), ".tmp", null, false, false);
String fileName = localFile.getName();
boolean found = false;
try {
if (files == null) {
files = ftp.listFiles();
}
for (FTPFile file : files) {
if (file != null && file.getName().equals(fileName)) {
found = true;
break;
}
}
} catch (IOException ioe) {
throw new BuildException(ioe, getLocation());
}
if (!found) {
localFile.deleteOnExit();
return localFile;
}
}
return null;
}
use of org.apache.commons.net.ftp.FTPFile in project ant by apache.
the class FTP method getTimeDiff.
/**
* auto find the time difference between local and remote
* @param ftp handle to ftp client
* @return number of millis to add to remote time to make it comparable to local time
* @since ant 1.6
*/
private long getTimeDiff(FTPClient ftp) {
long returnValue = 0;
File tempFile = findFileName(ftp);
try {
// create a local temporary file
FILE_UTILS.createNewFile(tempFile);
long localTimeStamp = tempFile.lastModified();
BufferedInputStream instream = new BufferedInputStream(Files.newInputStream(tempFile.toPath()));
ftp.storeFile(tempFile.getName(), instream);
instream.close();
boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
if (success) {
FTPFile[] ftpFiles = ftp.listFiles(tempFile.getName());
if (ftpFiles.length == 1) {
long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime();
returnValue = localTimeStamp - remoteTimeStamp;
}
ftp.deleteFile(ftpFiles[0].getName());
}
// delegate the deletion of the local temp file to the delete task
// because of race conditions occurring on Windows
Delete mydelete = new Delete();
mydelete.bindToOwner(this);
mydelete.setFile(tempFile.getCanonicalFile());
mydelete.execute();
} catch (Exception e) {
throw new BuildException(e, getLocation());
}
return returnValue;
}
use of org.apache.commons.net.ftp.FTPFile in project ant by apache.
the class FTP method getFile.
/**
* Retrieve a single file from the remote host. <code>filename</code> may
* contain a relative path specification. <p>
*
* The file will then be retrieved using the entire relative path spec -
* no attempt is made to change directories. It is anticipated that this
* may eventually cause problems with some FTP servers, but it simplifies
* the coding.</p>
* @param ftp the ftp client
* @param dir local base directory to which the file should go back
* @param filename relative path of the file based upon the ftp remote directory
* and/or the local base directory (dir)
* @throws IOException in unknown circumstances
* @throws BuildException if skipFailedTransfers is false
* and the file cannot be retrieved.
*/
protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
File file = getProject().resolveFile(new File(dir, filename).getPath());
OutputStream outstream = null;
try {
if (newerOnly && isUpToDate(ftp, file, resolveFile(filename))) {
return;
}
if (verbose) {
log("transferring " + filename + " to " + file.getAbsolutePath());
}
File pdir = file.getParentFile();
if (!pdir.exists()) {
pdir.mkdirs();
}
outstream = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
ftp.retrieveFile(resolveFile(filename), outstream);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
String s = "could not get file: " + ftp.getReplyString();
if (skipFailedTransfers) {
log(s, Project.MSG_WARN);
skipped++;
} else {
throw new BuildException(s);
}
} else {
log("File " + file.getAbsolutePath() + " copied from " + server, Project.MSG_VERBOSE);
transferred++;
if (preserveLastModified) {
outstream.close();
outstream = null;
FTPFile[] remote = ftp.listFiles(resolveFile(filename));
if (remote.length > 0) {
FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime());
}
}
}
} finally {
FileUtils.close(outstream);
}
}
use of org.apache.commons.net.ftp.FTPFile in project ant by apache.
the class FTPTaskMirrorImpl method findFileName.
/**
* find a suitable name for local and remote temporary file
*/
private File findFileName(FTPClient ftp) {
FTPFile[] files = null;
final int maxIterations = 1000;
for (int counter = 1; counter < maxIterations; counter++) {
File localFile = FILE_UTILS.createTempFile("ant" + Integer.toString(counter), ".tmp", null, false, false);
String fileName = localFile.getName();
boolean found = false;
try {
if (files == null) {
files = ftp.listFiles();
}
for (FTPFile file : files) {
if (file != null && file.getName().equals(fileName)) {
found = true;
break;
}
}
} catch (IOException ioe) {
throw new BuildException(ioe, task.getLocation());
}
if (!found) {
localFile.deleteOnExit();
return localFile;
}
}
return null;
}
use of org.apache.commons.net.ftp.FTPFile in project ant by apache.
the class FTPTaskMirrorImpl method getFile.
/**
* Retrieve a single file from the remote host. <code>filename</code> may
* contain a relative path specification. <p>
*
* The file will then be retrieved using the entire relative path spec -
* no attempt is made to change directories. It is anticipated that this
* may eventually cause problems with some FTP servers, but it simplifies
* the coding.</p>
* @param ftp the ftp client
* @param dir local base directory to which the file should go back
* @param filename relative path of the file based upon the ftp remote directory
* and/or the local base directory (dir)
* @throws IOException in unknown circumstances
* @throws BuildException if skipFailedTransfers is false
* and the file cannot be retrieved.
*/
protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
OutputStream outstream = null;
try {
File file = task.getProject().resolveFile(new File(dir, filename).getPath());
if (task.isNewer() && isUpToDate(ftp, file, resolveFile(filename))) {
return;
}
if (task.isVerbose()) {
task.log("transferring " + filename + " to " + file.getAbsolutePath());
}
File pdir = file.getParentFile();
if (!pdir.exists()) {
pdir.mkdirs();
}
outstream = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
ftp.retrieveFile(resolveFile(filename), outstream);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
String s = "could not get file: " + ftp.getReplyString();
if (task.isSkipFailedTransfers()) {
task.log(s, Project.MSG_WARN);
skipped++;
} else {
throw new BuildException(s);
}
} else {
task.log("File " + file.getAbsolutePath() + " copied from " + task.getServer(), Project.MSG_VERBOSE);
transferred++;
if (task.isPreserveLastModified()) {
outstream.close();
outstream = null;
FTPFile[] remote = ftp.listFiles(resolveFile(filename));
if (remote.length > 0) {
FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime());
}
}
}
} finally {
FileUtils.close(outstream);
}
}
Aggregations