📄 ftp.java
字号:
if (!result && !isCaseSensitive() && (remoteSystemCaseSensitive || !remoteSensitivityChecked)) { currentPathElement = findPathElementCaseUnsensitive(this.curpwd, currentPathElement); if (currentPathElement == null) { return; } } else if (!result) { return; } this.curpwd = this.curpwd + remoteFileSep + currentPathElement; } catch (IOException ioe) { throw new BuildException("could not change working dir to " + (String) pathElements.elementAt(fcount) + " from " + this.curpwd); } } String lastpathelement = (String) pathElements.elementAt(pathElements.size() - 1); FTPFile [] theFiles = listFiles(this.curpwd); this.ftpFile = getFile(theFiles, lastpathelement); } /** * find a file in a directory in case unsensitive way * @param parentPath where we are * @param soughtPathElement what is being sought * @return the first file found or null if not found */ private String findPathElementCaseUnsensitive(String parentPath, String soughtPathElement) { // we are already in the right path, so the second parameter // is false FTPFile[] theFiles = listFiles(parentPath, false); if (theFiles == null) { return null; } for (int icounter = 0; icounter < theFiles.length; icounter++) { if (theFiles[icounter] != null && theFiles[icounter].getName().equalsIgnoreCase(soughtPathElement)) { return theFiles[icounter].getName(); } } return null; } /** * find out if the file exists * @return true if the file exists */ public boolean exists() { return (ftpFile != null); } /** * if the file is a symbolic link, find out to what it is pointing * @return the target of the symbolic link */ public String getLink() { return ftpFile.getLink(); } /** * get the name of the file * @return the name of the file */ public String getName() { return ftpFile.getName(); } /** * find out the absolute path of the file * @return absolute path as string */ public String getAbsolutePath() { return curpwd + remoteFileSep + ftpFile.getName(); } /** * find out the relative path assuming that the path used to construct * this AntFTPFile was spelled properly with regards to case. * This is OK on a case sensitive system such as UNIX * @return relative path */ public String getFastRelativePath() { String absPath = getAbsolutePath(); if (absPath.indexOf(rootPath + remoteFileSep) == 0) { return absPath.substring(rootPath.length() + remoteFileSep.length()); } return null; } /** * find out the relative path to the rootPath of the enclosing scanner. * this relative path is spelled exactly like on disk, * for instance if the AntFTPFile has been instantiated as ALPHA, * but the file is really called alpha, this method will return alpha. * If a symbolic link is encountered, it is followed, but the name of the link * rather than the name of the target is returned. * (ie does not behave like File.getCanonicalPath()) * @return relative path, separated by remoteFileSep * @throws IOException if a change directory fails, ... * @throws BuildException if one of the components of the relative path cannot * be found. */ public String getRelativePath() throws IOException, BuildException { if (!relativePathCalculated) { if (parent != null) { traversesSymlinks = parent.isTraverseSymlinks(); relativePath = getRelativePath(parent.getAbsolutePath(), parent.getRelativePath()); } else { relativePath = getRelativePath(rootPath, ""); relativePathCalculated = true; } } return relativePath; } /** * get thge relative path of this file * @param currentPath base path * @param currentRelativePath relative path of the base path with regards to remote dir * @return relative path */ private String getRelativePath(String currentPath, String currentRelativePath) { Vector pathElements = SelectorUtils.tokenizePath(getAbsolutePath(), remoteFileSep); Vector pathElements2 = SelectorUtils.tokenizePath(currentPath, remoteFileSep); String relPath = currentRelativePath; for (int pcount = pathElements2.size(); pcount < pathElements.size(); pcount++) { String currentElement = (String) pathElements.elementAt(pcount); FTPFile[] theFiles = listFiles(currentPath); FTPFile theFile = null; if (theFiles != null) { theFile = getFile(theFiles, currentElement); } if (!relPath.equals("")) { relPath = relPath + remoteFileSep; } if (theFile == null) { // hit a hidden file assume not a symlink relPath = relPath + currentElement; currentPath = currentPath + remoteFileSep + currentElement; log("Hidden file " + relPath + " assumed to not be a symlink.", Project.MSG_VERBOSE); } else { traversesSymlinks = traversesSymlinks || theFile.isSymbolicLink(); relPath = relPath + theFile.getName(); currentPath = currentPath + remoteFileSep + theFile.getName(); } } return relPath; } /** * find a file matching a string in an array of FTPFile. * This method will find "alpha" when requested for "ALPHA" * if and only if the caseSensitive attribute is set to false. * When caseSensitive is set to true, only the exact match is returned. * @param theFiles array of files * @param lastpathelement the file name being sought * @return null if the file cannot be found, otherwise return the matching file. */ public FTPFile getFile(FTPFile[] theFiles, String lastpathelement) { if (theFiles == null) { return null; } for (int fcount = 0; fcount < theFiles.length; fcount++) { if (theFiles[fcount] != null) { if (theFiles[fcount].getName().equals(lastpathelement)) { return theFiles[fcount]; } else if (!isCaseSensitive() && theFiles[fcount].getName().equalsIgnoreCase( lastpathelement)) { return theFiles[fcount]; } } } return null; } /** * tell if a file is a directory. * note that it will return false for symbolic links pointing to directories. * @return <code>true</code> for directories */ public boolean isDirectory() { return ftpFile.isDirectory(); } /** * tell if a file is a symbolic link * @return <code>true</code> for symbolic links */ public boolean isSymbolicLink() { return ftpFile.isSymbolicLink(); } /** * return the attached FTP client object. * Warning : this instance is really shared with the enclosing class. * @return FTP client */ protected FTPClient getClient() { return client; } /** * sets the current path of an AntFTPFile * @param curpwd the current path one wants to set */ protected void setCurpwd(String curpwd) { this.curpwd = curpwd; } /** * returns the path of the directory containing the AntFTPFile. * of the full path of the file itself in case of AntFTPRootFile * @return parent directory of the AntFTPFile */ public String getCurpwd() { return curpwd; } /** * find out if a symbolic link is encountered in the relative path of this file * from rootPath. * @return <code>true</code> if a symbolic link is encountered in the relative path. * @throws IOException if one of the change directory or directory listing operations * fails * @throws BuildException if a path component in the relative path cannot be found. */ public boolean isTraverseSymlinks() throws IOException, BuildException { if (!relativePathCalculated) { // getRelativePath also finds about symlinks getRelativePath(); } return traversesSymlinks; } /** * Get a string rep of this object. * @return a string containing the pwd and the file. */ public String toString() { return "AntFtpFile: " + curpwd + "%" + ftpFile; } } /** * special class to represent the remote directory itself * @since Ant 1.6 */ protected class AntFTPRootFile extends AntFTPFile { private String remotedir; /** * constructor * @param aclient FTP client * @param remotedir remote directory */ public AntFTPRootFile(FTPClient aclient, String remotedir) { super(aclient, null, remotedir); this.remotedir = remotedir; try { this.getClient().changeWorkingDirectory(this.remotedir); this.setCurpwd(this.getClient().printWorkingDirectory()); } catch (IOException ioe) { throw new BuildException(ioe, getLocation()); } } /** * find the absolute path * @return absolute path */ public String getAbsolutePath() { return this.getCurpwd(); } /** * find out the relative path to root * @return empty string * @throws BuildException actually never * @throws IOException actually never */ public String getRelativePath() throws BuildException, IOException { return ""; } } } /** * check FTPFiles to check whether they function as directories too * the FTPFile API seem to make directory and symbolic links incompatible * we want to find out if we can cd to a symbolic link * @param dir the parent directory of the file to test * @param file the file to test * @return true if it is possible to cd to this directory * @since ant 1.6 */ private boolean isFunctioningAsDirectory(FTPClient ftp, String dir, FTPFile file) { boolean result = false; String currentWorkingDir = null; if (file.isDirectory()) { return true; } else if (file.isFile()) { return false; } try { currentWorkingDir = ftp.printWorkingDirectory(); } catch (IOException ioe) { getProject().log("could not find current working directory " + dir + " while checking a symlink", Project.MSG_DEBUG); } if (currentWorkingDir != null) { try { result = ftp.changeWorkingDirectory(file.getLink()); } catch (IOException ioe) { getProject().log("could not cd to " + file.getLink() + " while checking a symlink", Project.MSG_DEBUG); } if (result) { boolean comeback = false; try { comeback = ftp.changeWorkingDirectory(currentWorkingDir); } catch (IOException ioe) { getProject().log("could not cd back to " + dir + " while checking a symlink", Project.MSG_ERR); } finally { if (!comeback) { throw new BuildException("could not cd back to " + dir + " while checking a symlink"); } } } } return result; } /** * check FTPFiles to check whether they function as directories too * the FTPFile API seem to make directory and symbolic links incompatible * we want to find out if we can cd to a symbolic link * @param dir the parent directory of the file to test * @param file the file to test * @return true if it is possible to cd to this directory * @since ant 1.6 */ private boolean isFunctioningAsFile(FTPClient ftp, String dir, FTPFile file) { if (file.isDirectory()) { return false; } else if (file.isFile()) { return true; } return !isFunctioningAsDirectory(ftp, dir, file); } /** * Sets the remote directory where files will be placed. This may be a * relative or absolute path, and must be in the path syntax expected by
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -