@Override public FilePermissions getPermissions() { if(isSymlink()) return getCanonicalFile().getAncestor(FTPFile.class).permissions; return permissions; }
@Override public InputStream getInputStream() throws IOException { return getInputStream(0); }
protected FTPFile(FileURL fileURL, org.apache.commons.net.ftp.FTPFile file) throws IOException { super(fileURL); this.absPath = fileURL.getPath(); if(file==null) { this.file = getFTPFile(fileURL); // If file doesn't exist (could not be resolved), create it if(this.file==null) { String name = fileURL.getFilename(); // Filename could potentially be null this.file = createFTPFile(name==null?"":name, false); this.fileExists = false; } else { this.fileExists = true; } } else { this.file = file; this.fileExists = true; } this.permissions = new FTPFilePermissions(this.file); }
return createFTPFile("/", true); files = listFiles(connHandler, parentURL.getPath());
public long getLength() throws IOException { return FTPFile.this.getSize(); }
file = getFTPFile(getURL()); isClosed = true;
@Override public AbstractFile getParent() { if(!parentValSet) { FileURL parentFileURL = this.fileURL.getParent(); if(parentFileURL!=null) { try { parent = FileFactory.getFile(parentFileURL, null, createFTPFile(parentFileURL.getFilename(), true)); } catch(IOException e) { // No parent, that's all } } parentValSet = true; } return parent; }
@Override public void changePermission(PermissionAccess access, PermissionType permission, boolean enabled) throws IOException, UnsupportedFileOperationException { changePermissions(ByteUtils.setBit(permissions.getIntValue(), (permission.toInt() << (access.toInt()*3)), enabled)); }
/** * Implementation notes: server-to-server renaming will work if the destination file also uses the 'FTP' scheme * and is located on the same host. */ @Override public void renameTo(AbstractFile destFile) throws IOException { // Throw an exception if the file cannot be renamed to the specified destination checkRenamePrerequisites(destFile, false, false); FTPConnectionHandler connHandler = null; try { // Retrieve a ConnectionHandler and lock it connHandler = (FTPConnectionHandler)ConnectionPool.getConnectionHandler(this, fileURL, true); // Makes sure the connection is started, if not starts it connHandler.checkConnection(); if(!connHandler.ftpClient.rename(absPath, destFile.getURL().getPath())) throw new IOException(); } catch(IOException e) { // Checks if the IOException corresponds to a socket error and in that case, closes the connection if(connHandler!=null) connHandler.checkSocketException(e); throw e; } finally { // Release the lock on the ConnectionHandler if(connHandler!=null) connHandler.releaseLock(); } }
@Override public void delete() throws IOException { // Retrieve a ConnectionHandler and lock it FTPConnectionHandler connHandler = (FTPConnectionHandler)ConnectionPool.getConnectionHandler(this, fileURL, true); try { // Makes sure the connection is started, if not starts it connHandler.checkConnection(); if(isDirectory()) connHandler.ftpClient.removeDirectory(absPath); else connHandler.ftpClient.deleteFile(absPath); fileExists = false; // need to set to false since the file is cached // Throw an IOException if server replied with an error connHandler.checkServerReply(); } catch(IOException e) { // Checks if the IOException corresponds to a socket error and in that case, closes the connection connHandler.checkSocketException(e); // Re-throw IOException throw e; } finally { // Release the lock on the ConnectionHandler connHandler.releaseLock(); } }
@Override public long getDate() { if(isSymlink()) return ((org.apache.commons.net.ftp.FTPFile)getCanonicalFile().getUnderlyingFileObject()).getTimestamp().getTimeInMillis(); return file.getTimestamp().getTimeInMillis(); }
@Override public long getSize() { if(isSymlink()) return ((org.apache.commons.net.ftp.FTPFile)getCanonicalFile().getUnderlyingFileObject()).getSize(); return file.getSize(); }
@Override public boolean isDirectory() { // org.apache.commons.net.ftp.FTPFile#isDirectory() returns false if the file is a symlink pointing to a // directory, this is a limitation of the Commons-net library. // Todo: fix this by either: // a) find a combination of 'LIST' switches which allows the output to contain both the 'is symlink' and the // 'is the symlink target a directory' information. At a first glance, there doesn't seem to be one: either // symlinks are followed or there aren't. // b) Patch #ls() to issue an extra 'LIST -ldH *' to retrieve all symlinks' information when the directory has // at least one symlink. // c) if this file is a symlink, retrieve the symlink's target using #getFTPFile(FileURL) with '-ldH' switches // and return the value of isDirectory(). This clearly is the least effective solution at it requires issuing // one 'ls' command per symlink. if(isSymlink()) return ((org.apache.commons.net.ftp.FTPFile)getCanonicalFile().getUnderlyingFileObject()).isDirectory(); return file.isDirectory(); }