1 18 package net.sf.drftpd.util; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import net.sf.drftpd.NoAvailableSlaveException; 28 import net.sf.drftpd.SFVFile; 29 import net.sf.drftpd.SFVFile.SFVStatus; 30 import net.sf.drftpd.master.BaseFtpConnection; 31 import net.sf.drftpd.master.FtpReply; 32 import net.sf.drftpd.remotefile.LinkedRemoteFile; 33 import net.sf.drftpd.remotefile.StaticRemoteFile; 34 35 import org.apache.log4j.Logger; 36 37 41 public class ListUtils { 42 43 private static final Logger logger = Logger.getLogger(ListUtils.class); 44 45 public static final String PADDING = " "; 46 47 public static boolean isLegalFileName(String fileName) { 48 if (fileName == null) 49 throw new RuntimeException (); 50 return fileName.indexOf("/") == -1 51 && fileName.indexOf("?") == -1 52 && fileName.indexOf('*') == -1 53 && !fileName.equals(".") 54 && !fileName.equals(".."); 55 } 56 57 public static List list( 58 LinkedRemoteFile directoryFile, 59 BaseFtpConnection conn) { 60 return list(directoryFile, conn, null); 61 } 62 63 public static List list( 64 LinkedRemoteFile dir, 65 BaseFtpConnection conn, 66 FtpReply response) { 67 ArrayList tempFileList = new ArrayList (dir.getFiles()); 68 ArrayList listFiles = new ArrayList (); 69 int numOnline = 0; 70 int numTotal = 0; 71 for (Iterator iter = tempFileList.iterator(); iter.hasNext();) { 72 LinkedRemoteFile element = (LinkedRemoteFile) iter.next(); 73 if (conn.getConfig() != null 74 && !conn.getConfig().checkPrivPath(conn.getUserNull(), element)) { 75 continue; 77 } 78 if (!element.isAvailable()) { listFiles.add( 104 new StaticRemoteFile( 105 Collections.EMPTY_LIST, 106 element.getName() + "-OFFLINE", 107 element.getUsername(), 108 element.getGroupname(), 109 element.length(), 110 element.lastModified())); 111 numTotal++; 112 } 116 numOnline++; 118 numTotal++; 119 listFiles.add(element); 120 } 121 String statusDirName = null; 122 try { 123 SFVFile sfvfile = dir.lookupSFVFile(); 124 SFVStatus sfvstatus = sfvfile.getStatus(); 125 if (sfvfile.size() != 0) { 126 statusDirName = "[ "; 127 if (sfvstatus.getMissing() != 0) { 128 statusDirName += sfvstatus.getMissing() 129 + " files missing = "; 130 } 131 132 statusDirName 133 += (sfvstatus.getPresent() == 0 134 ? "0" 135 : "" + (sfvstatus.getPresent() * 100) / sfvfile.size()) 136 + "% complete"; 137 138 if (sfvstatus.getOffline() != 0) { 139 statusDirName += " | " 140 + sfvstatus.getOffline() 141 + " files offline = " 142 + ((sfvstatus.getAvailable() * 100) 143 / sfvstatus.getPresent()) 144 + "% online"; 145 } 146 statusDirName += " ]"; 147 148 if (statusDirName == null) 149 throw new RuntimeException (); 150 listFiles.add( 151 new StaticRemoteFile( 152 null, 153 statusDirName, 154 "drftpd", 155 "drftpd", 156 0L, 157 dir.lastModified())); 158 159 for (Iterator iter = sfvfile.getNames().iterator(); 160 iter.hasNext(); 161 ) { 162 String filename = (String ) iter.next(); 163 if (!dir.hasFile(filename)) { 164 listFiles.add( 166 new StaticRemoteFile( 167 Collections.EMPTY_LIST, 168 filename + "-MISSING", 169 "drftpd", 170 "drftpd", 171 0L, 172 dir.lastModified())); 173 } 174 } 175 } 176 } catch (NoAvailableSlaveException e) { 177 logger.warn("No available slaves for SFV file", e); 178 } catch (FileNotFoundException e) { 179 } catch (IOException e) { 181 logger.warn("IO error loading SFV file", e); 182 } catch (Throwable e) { 183 logger.warn("zipscript error", e); 184 } 185 return listFiles; 186 } 187 188 public static String padToLength(String value, int length) { 189 if (value.length() >= length) 190 return value; 191 if (PADDING.length() < length) 192 throw new RuntimeException ("padding must be longer than length"); 193 return PADDING.substring(0, length - value.length()) + value; 194 } 195 196 private ListUtils() { 197 } 198 } 199 | Popular Tags |