📄 filesystemconnection.java
字号:
/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package net.sf.jftp.net;import net.sf.jftp.config.Settings;import net.sf.jftp.util.Log;import net.sf.jftp.util.StringUtils;import java.io.*;import java.net.*;import java.util.*;public class FilesystemConnection implements BasicConnection{ public static int filesystemBuffer = 128000; private String path = ""; private String pwd = ""; private Vector listeners = new Vector(); private String[] files; private String[] size = new String[0]; private int[] perms = null; private String baseFile; private int fileCount; private boolean shortProgress = false; public FilesystemConnection() { } public FilesystemConnection(String path, ConnectionListener l) { listeners.add(l); chdir(path); } public int removeFileOrDir(String file) { try { if((file == null) || file.equals("")) { return -1; } String tmp = file; if(StringUtils.isRelative(file)) { tmp = getPWD() + file; } File f = new File(tmp); if(!f.getAbsolutePath().equals(f.getCanonicalPath())) { //Log.debug("WARNING: Symlink removed");//Skipping symlink, remove failed."); //Log.debug("This is necessary to prevent possible data loss when removing those symlinks."); //return -1; if(!f.delete()) { return -1; } } if(f.exists() && f.isDirectory()) { cleanLocalDir(tmp); } //System.out.println(tmp); if(!f.delete()) { Log.debug("Removal failed."); return -1; } } catch(IOException ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); } return -1; } private void cleanLocalDir(String dir) { try { dir = dir.replace('\\', '/'); if(!dir.endsWith("/")) { dir = dir + "/"; } //String remoteDir = StringUtils.removeStart(dir,path); //System.out.println(">>> " + dir); File f2 = new File(dir); String[] tmp = f2.list(); if(tmp == null) { return; } for(int i = 0; i < tmp.length; i++) { File f3 = new File(dir + tmp[i]); if(!f3.getAbsolutePath().equals(f3.getCanonicalPath())) { //Log.debug("WARNING: Symlink remove");//Skipping symlink, remove may fail."); f3.delete(); //Log.debug("This is necessary to prevent possible data loss when removing those symlinks."); //continue; } if(f3.isDirectory()) { //System.out.println(dir); cleanLocalDir(dir + tmp[i]); f3.delete(); } else { //System.out.println(dir+tmp[i]); f3.delete(); } } } catch(IOException ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); } } public void sendRawCommand(String cmd) { } public void disconnect() { } public boolean isConnected() { return true; } public String getPWD() { return pwd; } public boolean cdup() { return chdir(pwd.substring(0, pwd.lastIndexOf("/") + 1)); } public boolean mkdir(String dirName) { if(StringUtils.isRelative(dirName)) { dirName = getPWD() + dirName; } File f = new File(dirName); boolean x = f.mkdir(); fireDirectoryUpdate(); return x; } public void list(String outfile) throws IOException { } public boolean chdir(String p) { String p2 = processPath(p); if(p2 == null) { return false; } File f = new File(p2); if(!f.exists() || !f.isDirectory() || !f.canRead()) { Log.debug("Access denied."); return false; } pwd = p2; fireDirectoryUpdate(); return true; } public boolean chdirNoRefresh(String p) { String p2 = processPath(p); if(p2 == null) { return false; } //System.out.println(p2); pwd = p2; return true; } public String getLocalPath() { //System.out.println("local: " + path); return path; } public String processPath(String p) { p = p.replace('\\', '/'); //System.out.print("processPath 1: "+p); if(StringUtils.isRelative(p)) { p = pwd + p; } p = p.replace('\\', '/'); //System.out.println(", processPath 2: "+p); File f = new File(p); String p2; if(f.exists()) { try { p2 = f.getCanonicalPath(); p2 = p2.replace('\\', '/'); if(!p2.endsWith("/")) { p2 = p2 + "/"; } return p2; } catch(IOException ex) { Log.debug("Error: can not get pathname (processPath)!"); return null; } } else { Log.debug("(processpPath) No such path: \"" + p + "\""); return null; } } public boolean setLocalPath(String p) { p = p.replace('\\', '/'); //System.out.print("local 1:" + p); if(StringUtils.isRelative(p)) { p = path + p; } p = p.replace('\\', '/'); //System.out.println(", local 2:" + p); File f = new File(p); if(f.exists()) { try { path = f.getCanonicalPath(); path = path.replace('\\', '/'); if(!path.endsWith("/")) { path = path + "/"; } //System.out.println("localPath: "+path); } catch(IOException ex) { Log.debug("Error: can not get pathname (local)!"); return false; } } else { Log.debug("(local) No such path: \"" + p + "\""); return false; } return true; } public String[] sortLs(String file) { File f = new File(pwd); files = f.list(); if(files == null) { return new String[0]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -