1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-13 20:15:22 +02:00

Migrate to jakarta commons net FTP client

This commit is contained in:
Javier Montalvo Orus 2006-10-06 14:45:47 +00:00
parent fd1f09c895
commit 80f04f8b97
6 changed files with 243 additions and 432 deletions

View file

@ -7,6 +7,7 @@ Bundle-Activator: org.eclipse.rse.services.files.ftp.Activator
Bundle-Vendor: %providerName Bundle-Vendor: %providerName
Bundle-Localization: plugin Bundle-Localization: plugin
Require-Bundle: org.eclipse.core.runtime, Require-Bundle: org.eclipse.core.runtime,
org.eclipse.rse.services org.eclipse.rse.services,
org.apache.commons_net
Eclipse-LazyStart: true Eclipse-LazyStart: true
Export-Package: org.eclipse.rse.services.files.ftp Export-Package: org.eclipse.rse.services.files.ftp

View file

@ -1,37 +0,0 @@
/********************************************************************************
* Copyright (c) 2006 IBM Corporation. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight, Kushal Munir,
* Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson,
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
*
* Contributors:
* {Name} (company) - description of contribution.
********************************************************************************/
package org.eclipse.rse.services.files.ftp;
import java.io.IOException;
import sun.net.ftp.FtpClient;
public class FTPClientService extends FtpClient
{
public int sendCommand(String command)
{
try
{
return issueCommand(command);
}
catch (IOException e)
{
e.printStackTrace();
return -1;
}
}
}

View file

@ -12,53 +12,63 @@
* *
* Contributors: * Contributors:
* Michael Berger (IBM) - Fixing 140408 - FTP upload does not work * Michael Berger (IBM) - Fixing 140408 - FTP upload does not work
* Javier Montalvo Orús (Symbian) - Migrate to jakarta commons net FTP client
********************************************************************************/ ********************************************************************************/
package org.eclipse.rse.services.files.ftp; package org.eclipse.rse.services.files.ftp;
import java.io.File; import java.io.File;
import org.apache.commons.net.ftp.FTPFile;
import org.eclipse.rse.services.clientserver.archiveutils.ArchiveHandlerManager; import org.eclipse.rse.services.clientserver.archiveutils.ArchiveHandlerManager;
import org.eclipse.rse.services.files.IHostFile; import org.eclipse.rse.services.files.IHostFile;
public class FTPHostFile implements IHostFile public class FTPHostFile implements IHostFile
{ {
private String _name; private String _name;
private String _parentPath; private String _parentPath;
private boolean _isDirectory = false; private boolean _isDirectory;
private boolean _isRoot = false; private boolean _isArchive;
private boolean _isArchive = false; private long _lastModified;
private long _lastModified = 0; private long _size;
private long _size = 0; private boolean _canRead;
private boolean _exists = false; private boolean _canWrite;
private boolean _isRoot;
private boolean _exists;
public FTPHostFile(String parentPath, String name, boolean isDirectory, boolean isRoot, long lastModified, long size, boolean exists) public FTPHostFile(String parentPath, String name, boolean isDirectory, boolean isRoot, long lastModified, long size, boolean exists)
{ {
_parentPath = parentPath; _parentPath = parentPath;
_name = name; _name = name;
_isDirectory = isDirectory; _isDirectory = isDirectory;
_isRoot = isRoot;
_lastModified = lastModified; _lastModified = lastModified;
_size = size; _size = size;
_isArchive = internalIsArchive(); _isArchive = internalIsArchive();
_canRead = true;
_canWrite = false;
_isRoot = isRoot;
_exists = exists; _exists = exists;
} }
public String getName() public FTPHostFile(String parentPath, FTPFile ftpFile)
{ {
return _name; _parentPath = parentPath;
_name = ftpFile.getName();
_isDirectory = ftpFile.isDirectory();
_lastModified = ftpFile.getTimestamp().getTimeInMillis();
_size = ftpFile.getSize();
_isArchive = internalIsArchive();
_canRead = ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION);
_canWrite = ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION);
_isRoot = false;
_exists = true;
} }
public boolean isHidden() public long getSize()
{ {
String name = getName(); return _size;
return name.charAt(0) == '.';
}
public String getParentPath()
{
return _parentPath;
} }
public boolean isDirectory() public boolean isDirectory()
@ -71,13 +81,20 @@ public class FTPHostFile implements IHostFile
return !(_isDirectory || _isRoot); return !(_isDirectory || _isRoot);
} }
public boolean isRoot() public String getName()
{ {
return _isRoot; return _name;
} }
public boolean exists() public boolean canRead() {
{ return _canRead;
}
public boolean canWrite() {
return _canWrite;
}
public boolean exists() {
return _exists; return _exists;
} }
@ -96,16 +113,32 @@ public class FTPHostFile implements IHostFile
} }
} }
public long getSize()
{
return _size;
}
public long getModifiedDate() public long getModifiedDate()
{ {
return _lastModified; return _lastModified;
} }
public String geParentPath() {
return _parentPath;
}
public boolean isArchive() {
return _isArchive;
}
public boolean isHidden() {
return false;
}
public boolean isRoot() {
return _parentPath==null;
}
public String getParentPath() {
return _parentPath;
}
public void renameTo(String newAbsolutePath) public void renameTo(String newAbsolutePath)
{ {
int i = newAbsolutePath.lastIndexOf("/"); int i = newAbsolutePath.lastIndexOf("/");
@ -120,8 +153,6 @@ public class FTPHostFile implements IHostFile
} }
_isArchive = internalIsArchive(); _isArchive = internalIsArchive();
} }
protected boolean internalIsArchive() protected boolean internalIsArchive()
@ -130,19 +161,4 @@ public class FTPHostFile implements IHostFile
&& !ArchiveHandlerManager.isVirtual(getAbsolutePath()); && !ArchiveHandlerManager.isVirtual(getAbsolutePath());
} }
public boolean isArchive()
{
return _isArchive;
}
//TODO implement this
public boolean canRead() {
return true;
}
//TODO implement this
public boolean canWrite() {
return true;
}
} }

View file

@ -1,80 +0,0 @@
/********************************************************************************
* Copyright (c) 2006 IBM Corporation. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight, Kushal Munir,
* Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson,
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
*
* Contributors:
* Michael Berger (IBM) - Fixing 140408 - FTP upload does not work
********************************************************************************/
package org.eclipse.rse.services.files.ftp;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
public class FTPLinuxDirectoryListingParser implements IFTPDirectoryListingParser
{
public FTPHostFile getFTPHostFile(String line, String parentPath)
{
// Note this assumes that text is always formatted the same way
if (line == null) return null;
String[] tokens = line.split("\\s+", 9);
if (tokens.length < 9) return null;
String name = tokens[8];
boolean isDirectory = line.charAt(0) == 'd';
long length = 0;
long lastMod = 0;
if (tokens.length > 4)
{
try
{
length = Long.parseLong(tokens[tokens.length - 5]);
}
catch (NumberFormatException e) {}
try
{
int i = tokens.length - 4;
int j = tokens.length - 3;
int k = tokens.length - 2;
String time = "";
String year = "";
if (tokens[k].indexOf(":") == -1)
{
time = "11:59 PM";
year = tokens[k];
}
else
{
String[] parts = tokens[k].split(":");
int hours = Integer.parseInt(parts[0]);
boolean morning = hours < 12; // assumes midnight is 00:00
if (morning)
{
if (hours == 0)
{
hours = 12;
}
}
time = hours + ":" + parts[1] + (morning? " AM" : " PM");
year = "" + (Calendar.getInstance().get(Calendar.YEAR));
}
String date = tokens[i] + " " + tokens[j] + ", " + year + " " + time;
lastMod = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, new Locale("EN", "US")).parse(date).getTime();
}
catch (Exception e) {}
}
return new FTPHostFile(parentPath, name, isDirectory, false, lastMod, length, true);
}
}

View file

@ -16,62 +16,57 @@
* delete, move and rename. * delete, move and rename.
* Javier Montalvo Orus (Symbian) - Bug 140348 - FTP did not use port number * Javier Montalvo Orus (Symbian) - Bug 140348 - FTP did not use port number
* Michael Berger (IBM) - Fixing 140404 - FTP new file creation does not work * Michael Berger (IBM) - Fixing 140404 - FTP new file creation does not work
* Javier Montalvo Orús (Symbian) - Migrate to jakarta commons net FTP client
********************************************************************************/ ********************************************************************************/
package org.eclipse.rse.services.files.ftp; package org.eclipse.rse.services.files.ftp;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.rse.services.Mutex;
import org.eclipse.rse.services.clientserver.NamePatternMatcher; import org.eclipse.rse.services.clientserver.NamePatternMatcher;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException; import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.services.files.AbstractFileService; import org.eclipse.rse.services.files.AbstractFileService;
import org.eclipse.rse.services.files.IFileService; import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.files.IHostFile; import org.eclipse.rse.services.files.IHostFile;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
public class FTPService extends AbstractFileService implements IFileService, IFTPService public class FTPService extends AbstractFileService implements IFileService, IFTPService
{ {
private FTPClientService _ftpClient; private FTPClient _ftpClient;
private Mutex _mutex = new Mutex();
private long _mutexTimeout = 5000; //max.5 seconds to obtain dir channel
private String _userHome; private String _userHome;
private IFTPDirectoryListingParser _ftpPropertiesUtil;
private transient String _hostname; private transient String _hostname;
private transient String _userId; private transient String _userId;
private transient String _password; private transient String _password;
private transient int _portNumber; private transient int _portNumber;
private URLConnection _urlConnection;
public FTPService()
{
}
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.IService#getName()
*/
public String getName() public String getName()
{ {
return FTPServiceResources.FTP_File_Service_Name; return FTPServiceResources.FTP_File_Service_Name;
} }
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.IService#getDescription()
*/
public String getDescription() public String getDescription()
{ {
return FTPServiceResources.FTP_File_Service_Description; return FTPServiceResources.FTP_File_Service_Description;
@ -96,18 +91,17 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
_password = password; _password = password;
} }
public void connect() throws Exception public void connect() throws Exception
{ {
FtpClient ftp = getFTPClient(); FTPClient ftp = getFTPClient();
if (_portNumber == 0) { if (_portNumber == 0) {
ftp.openServer(_hostname); ftp.connect(_hostname);
} else { } else {
ftp.openServer(_hostname, _portNumber); ftp.connect(_hostname, _portNumber);
} }
ftp.login(_userId, _password); ftp.login(_userId, _password);
_userHome = ftp.pwd(); _userHome = ftp.printWorkingDirectory();
} }
protected void reconnect() protected void reconnect()
@ -125,114 +119,107 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
{ {
try try
{ {
getFTPClient().closeServer(); getFTPClient().logout();
_ftpClient = null; _ftpClient = null;
} }
catch (Exception e) catch (Exception e)
{ {
// e.printStackTrace();
_ftpClient = null; _ftpClient = null;
} }
} }
public IFTPDirectoryListingParser getDirListingParser() public FTPClient getFTPClient()
{
if (_ftpPropertiesUtil == null)
{
_ftpPropertiesUtil = new FTPLinuxDirectoryListingParser();
}
return _ftpPropertiesUtil;
}
public FTPClientService getFTPClient()
{ {
if (_ftpClient == null) if (_ftpClient == null)
{ {
_ftpClient = new FTPClientService(); _ftpClient = new FTPClient();
} }
return _ftpClient; return _ftpClient;
} }
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.files.IFileService#getFile(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String)
*/
public IHostFile getFile(IProgressMonitor monitor, String remoteParent, String fileName) public IHostFile getFile(IProgressMonitor monitor, String remoteParent, String fileName)
{ {
//No Mutex lock needed here because internalFetch() does the lock
IHostFile[] matches = internalFetch(monitor, remoteParent, fileName, FILE_TYPE_FILES_AND_FOLDERS); IHostFile[] matches = internalFetch(monitor, remoteParent, fileName, FILE_TYPE_FILES_AND_FOLDERS);
if (matches != null && matches.length > 0) if (matches != null && matches.length > 0)
{ {
return matches[0]; return matches[0];
} }
else else
{ {
return new FTPHostFile(remoteParent, fileName, false, false, 0, 0, false); return null;
} }
} }
public boolean isConnected() public boolean isConnected()
{ {
return getFTPClient().serverIsOpen(); return getFTPClient().isConnected();
} }
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.files.AbstractFileService#internalFetch(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, int)
*/
protected IHostFile[] internalFetch(IProgressMonitor monitor, String parentPath, String fileFilter, int fileType) protected IHostFile[] internalFetch(IProgressMonitor monitor, String parentPath, String fileFilter, int fileType)
{ {
if (fileFilter == null) if (fileFilter == null)
{ {
fileFilter = "*"; fileFilter = "*";
} }
NamePatternMatcher filematcher = new NamePatternMatcher(fileFilter, true, true); NamePatternMatcher filematcher = new NamePatternMatcher(fileFilter, true, true);
List results = new ArrayList(); List results = new ArrayList();
if (_mutex.waitForLock(monitor, _mutexTimeout))
try
{ {
FTPClient ftp = getFTPClient();
try try
{ {
FtpClient ftp = getFTPClient(); ftp.noop();
try
{
ftp.noop();
}
catch (Exception e)
{
//e.printStackTrace();
disconnect();
// probably timed out
reconnect();
ftp = getFTPClient();
}
ftp.cd(parentPath);
TelnetInputStream stream = ftp.list();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
while (line != null)
{
FTPHostFile node = getDirListingParser().getFTPHostFile(line, parentPath);
if (node != null && filematcher.matches(node.getName()))
{
if (isRightType(fileType, node))
{
results.add(node);
}
}
line = reader.readLine();
}
} }
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); disconnect();
reconnect();
ftp = getFTPClient();
} }
finally
ftp.changeWorkingDirectory(parentPath);
FTPFile[] ftpFiles = ftp.listFiles();
for(int i=0; i<ftpFiles.length; i++)
{ {
_mutex.release(); if(filematcher.matches(ftpFiles[i].getName()))
{
results.add(new FTPHostFile(parentPath,ftpFiles[i]));
}
} }
} }
catch (Exception e)
{
e.printStackTrace();
}
return (IHostFile[])results.toArray(new IHostFile[results.size()]); return (IHostFile[])results.toArray(new IHostFile[results.size()]);
} }
public String getSeparator() public String getSeparator()
{ {
return "/"; return "/";
} }
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.files.IFileService#upload(org.eclipse.core.runtime.IProgressMonitor, java.io.File, java.lang.String, java.lang.String, boolean, java.lang.String, java.lang.String)
*/
public boolean upload(IProgressMonitor monitor, File localFile, String remoteParent, String remoteFile, boolean isBinary, String srcEncoding, String hostEncoding) public boolean upload(IProgressMonitor monitor, File localFile, String remoteParent, String remoteFile, boolean isBinary, String srcEncoding, String hostEncoding)
{ {
try try
@ -247,95 +234,84 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
} }
} }
private OutputStream getUploadStream(String remotePath, boolean isBinary) throws Exception /*
{ * (non-Javadoc)
String typecode = isBinary ? "i" : "a"; * @see org.eclipse.rse.services.files.IFileService#upload(org.eclipse.core.runtime.IProgressMonitor, java.io.InputStream, java.lang.String, java.lang.String, boolean, java.lang.String)
remotePath = "%2F" + remotePath; */
remotePath = remotePath.replaceAll(" ", "%20");
URL url = new URL("ftp://" + _userId + ":" + _password + "@" + _hostname + "/" + remotePath + ";type=" + typecode);
_urlConnection = url.openConnection();
return _urlConnection.getOutputStream();
}
public boolean upload(IProgressMonitor monitor, InputStream stream, String remoteParent, String remoteFile, boolean isBinary, String hostEncoding) public boolean upload(IProgressMonitor monitor, InputStream stream, String remoteParent, String remoteFile, boolean isBinary, String hostEncoding)
{ {
boolean retValue = false;
try try
{ {
BufferedInputStream bis = new BufferedInputStream(stream); FTPClient ftp = getFTPClient();
String remotePath = remoteParent + getSeparator() + remoteFile; ftp.changeWorkingDirectory(remoteParent);
OutputStream os = getUploadStream(remotePath, isBinary);
byte[] buffer = new byte[1024]; if (isBinary)
int readCount; ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
while( (readCount = bis.read(buffer)) > 0) else
{ ftp.setFileTransferMode(FTP.ASCII_FILE_TYPE);
os.write(buffer, 0, readCount);
} retValue = ftp.storeFile(remoteFile, stream);
os.close();
bis.close(); stream.close();
_urlConnection = null;
} }
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
return true;
return retValue;
} }
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.files.IFileService#download(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, java.io.File, boolean, java.lang.String)
*/
public boolean download(IProgressMonitor monitor, String remoteParent, String remoteFile, File localFile, boolean isBinary, String hostEncoding) public boolean download(IProgressMonitor monitor, String remoteParent, String remoteFile, File localFile, boolean isBinary, String hostEncoding)
{ {
if (_mutex.waitForLock(monitor, _mutexTimeout)) boolean retValue = false;
try
{ {
try FTPClient ftp = getFTPClient();
ftp.changeWorkingDirectory(remoteParent);
if (isBinary)
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
else
ftp.setFileTransferMode(FTP.ASCII_FILE_TYPE);
if (!localFile.exists())
{ {
FtpClient ftp = getFTPClient(); File localParentFile = localFile.getParentFile();
ftp.cd(remoteParent); if (!localParentFile.exists())
/*
if (isBinary)
ftp.binary();
else
ftp.ascii();
*/
// for now only binary seems to work
ftp.binary();
InputStream is = ftp.get(remoteFile);
BufferedInputStream bis = new BufferedInputStream(is);
if (!localFile.exists())
{ {
File localParentFile = localFile.getParentFile(); localParentFile.mkdirs();
if (!localParentFile.exists())
{
localParentFile.mkdirs();
}
localFile.createNewFile();
} }
OutputStream os = new FileOutputStream(localFile); localFile.createNewFile();
BufferedOutputStream bos = new BufferedOutputStream(os); }
byte[] buffer = new byte[1024]; OutputStream output = new FileOutputStream(localFile);
int totalWrote = 0;
int readCount; retValue = ftp.retrieveFile(remoteFile, output);
while( (readCount = bis.read(buffer)) > 0)
{ output.flush();
bos.write(buffer, 0, readCount); output.close();
totalWrote += readCount;
}
bos.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
_mutex.release();
}
} }
return false; catch (Exception e)
{
e.printStackTrace();
}
return retValue;
} }
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.files.IFileService#getUserHome()
*/
public IHostFile getUserHome() public IHostFile getUserHome()
{ {
int lastSlash = _userHome.lastIndexOf('/'); int lastSlash = _userHome.lastIndexOf('/');
@ -344,10 +320,13 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
return getFile(null, parent, name); return getFile(null, parent, name);
} }
/*
* (non-Javadoc)
* @see org.eclipse.rse.services.files.IFileService#getRoots(org.eclipse.core.runtime.IProgressMonitor)
*/
public IHostFile[] getRoots(IProgressMonitor monitor) public IHostFile[] getRoots(IProgressMonitor monitor)
{ {
IHostFile root = new FTPHostFile("/", "/", true, true, 0, 0, true); return new IHostFile[]{new FTPHostFile(null, "/", true, true, 0, 0, true)};
return new IHostFile[] { root };
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -355,20 +334,17 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/ */
public boolean delete(IProgressMonitor monitor, String remoteParent, String fileName) { public boolean delete(IProgressMonitor monitor, String remoteParent, String fileName) {
boolean hasSucceeded = false; boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try { try {
getFTPClient().cd(remoteParent); getFTPClient().cwd(remoteParent);
int returnedValue = getFTPClient().sendCommand("DELE " + fileName); int returnedValue = getFTPClient().dele(fileName);
hasSucceeded = (returnedValue > 0); hasSucceeded = (returnedValue > 0);
}
catch (IOException e) {
// Changing folder raised an exception
hasSucceeded = false;
}
finally {
_mutex.release();
}
} }
catch (IOException e) {
// Changing folder raised an exception
hasSucceeded = false;
}
return hasSucceeded; return hasSucceeded;
} }
@ -376,20 +352,16 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
* @see org.eclipse.rse.services.files.IFileService#rename(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, java.lang.String) * @see org.eclipse.rse.services.files.IFileService#rename(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, java.lang.String)
*/ */
public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName) { public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName) {
boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) { try {
try {
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName); getFTPClient().rename(remoteParent + getSeparator() + oldName, remoteParent + getSeparator() + newName);
if (returnedValue > 0) {
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName); } catch (IOException e) {
hasSucceeded = (returnedValue > 0); return false;
}
}
finally {
_mutex.release();
}
} }
return hasSucceeded;
return true;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -397,18 +369,9 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/ */
public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName, IHostFile oldFile) { public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName, IHostFile oldFile) {
boolean hasSucceeded = false; boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try { oldFile.renameTo(newName);
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName);
if (returnedValue > 0) {
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName);
hasSucceeded = (returnedValue > 0);
}
}
finally {
_mutex.release();
}
}
return hasSucceeded; return hasSucceeded;
} }
@ -417,18 +380,19 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/ */
public boolean move(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName) { public boolean move(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName) {
boolean hasSucceeded = false; boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try { try {
int returnedValue = getFTPClient().sendCommand("RNFR " + srcParent + getSeparator() + srcName);
if (returnedValue > 0) { int returnedValue;
returnedValue = getFTPClient().sendCommand("RNTO " + tgtParent + getSeparator() + tgtName); returnedValue = getFTPClient().sendCommand("RNFR " + srcParent + getSeparator() + srcName);
hasSucceeded = (returnedValue > 0);
} if (returnedValue > 0) {
returnedValue = getFTPClient().sendCommand("RNTO " + tgtParent + getSeparator() + tgtName);
hasSucceeded = (returnedValue > 0);
} }
finally {
_mutex.release(); }catch (IOException e) {}
}
}
return hasSucceeded; return hasSucceeded;
} }
@ -437,27 +401,13 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/ */
public IHostFile createFolder(IProgressMonitor monitor, String remoteParent, String folderName) public IHostFile createFolder(IProgressMonitor monitor, String remoteParent, String folderName)
{ {
if (_mutex.waitForLock(monitor, _mutexTimeout)) try
{ {
try FTPClient ftp = getFTPClient();
{ ftp.makeDirectory(folderName);
FTPClientService ftp = getFTPClient();
ftp.cd(remoteParent);
ftp.sendCommand("MKD " + folderName);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
_mutex.release();
}
} }
//TODO getFile() will acquire the lock again after having released it catch (Exception e) {}
//For optimization, Mutex should notice when the _same_ thread tries
//to acquire a lock that it already holds again. To do so, the current
//locking thread would need to be remembered.
return getFile(monitor, remoteParent, folderName); return getFile(monitor, remoteParent, folderName);
} }
@ -465,19 +415,16 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
* @see org.eclipse.rse.services.files.IFileService#createFile(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String) * @see org.eclipse.rse.services.files.IFileService#createFile(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String)
*/ */
public IHostFile createFile(IProgressMonitor monitor, String remoteParent, String fileName) { public IHostFile createFile(IProgressMonitor monitor, String remoteParent, String fileName) {
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try { try {
File tempFile = File.createTempFile("ftp", "temp"); File tempFile = File.createTempFile("ftp", "temp");
tempFile.deleteOnExit(); tempFile.deleteOnExit();
upload(monitor, tempFile, remoteParent, fileName, true, null, null); upload(monitor, tempFile, remoteParent, fileName, true, null, null);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
_mutex.release();
}
} }
catch (Exception e) {
e.printStackTrace();
}
return getFile(monitor, remoteParent, fileName); return getFile(monitor, remoteParent, fileName);
} }
@ -490,7 +437,7 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
public boolean copy(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName) public boolean copy(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName)
{ {
return false; return move(monitor, srcParent, srcName, tgtParent, tgtName);
} }
public boolean copyBatch(IProgressMonitor monitor, String[] srcParents, String[] srcNames, String tgtParent) throws SystemMessageException public boolean copyBatch(IProgressMonitor monitor, String[] srcParents, String[] srcNames, String tgtParent) throws SystemMessageException

View file

@ -1,36 +0,0 @@
/********************************************************************************
* Copyright (c) 2006 IBM Corporation. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight, Kushal Munir,
* Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson,
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
*
* Contributors:
* {Name} (company) - description of contribution.
********************************************************************************/
package org.eclipse.rse.services.files.ftp;
/**
* Implementers of this interface provide a way to get
* information about file properties from an FTP directory
* listing in a way
* that might be specific to a certain system type.
* @author mjberger
*
*/
public interface IFTPDirectoryListingParser
{
/**
* Return an FTPHostFile representing a line from an FTP directory listing
* @param line The line of text from the directory listing
* @param parentPath The directory that this is a listing of
* @return null if the line is not well formed
*/
public FTPHostFile getFTPHostFile(String line, String parentPath);
}