1 18 package net.sf.drftpd.event.listeners; 19 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Properties ; 28 29 import net.sf.drftpd.Bytes; 30 import net.sf.drftpd.event.Event; 31 import net.sf.drftpd.event.FtpListener; 32 import net.sf.drftpd.event.TransferEvent; 33 import net.sf.drftpd.master.ConnectionManager; 34 import net.sf.drftpd.master.SlaveManagerImpl; 35 import net.sf.drftpd.master.config.ExcludePath; 36 import net.sf.drftpd.master.config.FtpConfig; 37 import net.sf.drftpd.remotefile.LinkedRemoteFile; 38 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 39 import net.sf.drftpd.remotefile.RemoteFileLastModifiedComparator; 40 41 import org.apache.log4j.BasicConfigurator; 42 import org.apache.log4j.Logger; 43 import org.apache.oro.text.regex.MalformedPatternException; 44 45 49 public class AutoFreeSpace implements FtpListener { 50 private static final Logger logger = Logger.getLogger(AutoFreeSpace.class); 51 52 public static void main(String args[]) throws IOException { 53 BasicConfigurator.configure(); 54 List rslaves = SlaveManagerImpl.loadRSlaves(); 55 LinkedRemoteFileInterface root = 56 ConnectionManager.loadMLSTFileDatabase(rslaves, null); 57 58 AutoFreeSpace space = new AutoFreeSpace(); 59 space.deleteOldReleases(root.getDirectories(), 0); 60 } 61 private long _archiveAfter; 62 63 private ConnectionManager _cm; 64 private long _cycleTime; 65 private ArrayList _exemptList; 66 private long _keepFree; 67 private long _lastchecked = System.currentTimeMillis(); 68 public AutoFreeSpace() { 69 reload(); 70 } 71 72 public void actionPerformed(Event event) { 73 if (event.getCommand().equals("RELOAD")) 74 reload(); 75 if (!(event instanceof TransferEvent) || !event.getCommand().equals("STOR")) 76 return; 77 TransferEvent transevent = (TransferEvent) event; 78 if(!transevent.isComplete()) return; 79 if (System.currentTimeMillis() - _lastchecked <= _cycleTime) { 80 return; 81 } 82 _lastchecked = System.currentTimeMillis(); 83 84 } 106 107 111 112 public boolean checkExclude(LinkedRemoteFileInterface lrf) { 113 for (Iterator iter = _exemptList.iterator(); iter.hasNext();) { 114 ExcludePath ep = (ExcludePath) iter.next(); 115 if (ep.checkPath(lrf)) 116 return true; 117 } 118 return false; 119 } 120 121 125 private void deleteOldReleases(Collection rootDirs, long spaceAvailable) { 126 ArrayList dirs = new ArrayList (); 128 for (Iterator iter = rootDirs.iterator(); iter.hasNext();) { 129 LinkedRemoteFile lrf = (LinkedRemoteFile) iter.next(); 130 if (checkExclude(lrf)) { 131 logger.debug(lrf.getPath() + " is excluded"); 132 continue; 133 } 134 dirs.addAll(lrf.getDirectories()); 135 } 136 Collections.sort(dirs, new RemoteFileLastModifiedComparator(true)); 137 138 if (spaceAvailable < _keepFree) { 144 for (Iterator iter = dirs.iterator(); iter.hasNext();) { 145 logger.debug( 146 Bytes.formatBytes(spaceAvailable) 147 + " < " 148 + Bytes.formatBytes(_keepFree)); 149 if (spaceAvailable >= _keepFree) 150 break; 151 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next(); 152 spaceAvailable += file.length(); 153 logger.info("delete " + file.getPath()); 154 file.delete(); 155 } 156 } 157 } 158 159 public void init(ConnectionManager connectionManager) { 160 _cm = connectionManager; 161 } 162 private void reload() { 163 Properties props = new Properties (); 164 try { 165 props.load(new FileInputStream ("conf/autofreespace.conf")); 166 } catch (IOException e) { 167 throw new RuntimeException (e); 168 } 169 reload(props); 170 } 171 private void reload(Properties props) { 172 _cycleTime = 173 60000 * Long.parseLong(FtpConfig.getProperty(props, "cycleTime")); 174 _keepFree = Bytes.parseBytes(FtpConfig.getProperty(props, "keepFree")); 175 _archiveAfter = 176 60000 177 * Long.parseLong(FtpConfig.getProperty(props, "archiveAfter")); 178 _lastchecked = System.currentTimeMillis(); 179 _exemptList = new ArrayList (); 180 for (int i = 1;; i++) { 181 String path = props.getProperty("exclude." + i); 182 if (path == null) 183 break; 184 try { 185 ExcludePath.makePermission(_exemptList, path); 186 } catch (MalformedPatternException e1) { 187 throw new RuntimeException (e1); 188 } 189 } 190 191 } 192 193 public void unload() { 194 195 } 196 197 } 198 | Popular Tags |