1 18 package net.sf.drftpd.remotefile; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Hashtable ; 26 import java.util.Iterator ; 27 28 import net.sf.drftpd.FatalException; 29 import net.sf.drftpd.InvalidDirectoryException; 30 import net.sf.drftpd.slave.Root; 31 import net.sf.drftpd.slave.RootBasket; 32 33 39 public class FileRemoteFile extends AbstractRemoteFile { 40 RootBasket rootBasket; 41 String path; 42 43 public FileRemoteFile(RootBasket rootBasket) throws IOException { 44 this(rootBasket, ""); 45 } 46 private boolean isFile; 47 private boolean isDirectory; 48 private long length; 49 50 public FileRemoteFile(RootBasket rootBasket, String path) 51 throws IOException { 52 this.path = path; 56 this.rootBasket = rootBasket; 57 59 boolean first = true; 61 for (Iterator iter = rootBasket.iterator(); iter.hasNext();) { 62 Root root = (Root) iter.next(); 63 File file = root.getFile(path); 64 65 if (!file.exists()) 66 continue; 67 68 77 if (first) { 78 first = false; 79 isDirectory = file.isDirectory(); 80 isFile = file.isFile(); 81 if ((!isFile() && !isDirectory()) 82 || (isFile() && isDirectory)) { 83 throw new IOException ( 84 "(!isFile() && !isDirectory()) || (isFile() && isDirectory): " 85 + isFile() 86 + isDirectory() 87 + " " 88 + path); 89 } 90 if (isDirectory()) { 91 length = 0; 92 } else { 93 length = file.length(); 94 } 95 } else { 96 if (file.isDirectory() != isDirectory) 97 throw new IOException ( 98 "roots are out of sync, dir&file mix: " + path); 99 if (file.isFile() != isFile) 100 throw new IOException ( 101 "roots are out of sync, file&dir mix: " + path); 102 if (isFile) 103 throw new IOException ("File collision: " + path); 104 } 106 107 if (!file 108 .getCanonicalPath() 109 .equalsIgnoreCase(file.getAbsolutePath())) { 110 throw new InvalidDirectoryException( 111 "Not following symlink: " + file.getAbsolutePath()); 112 } 113 } 114 } 115 116 private File getFile() { 117 try { 118 return rootBasket.getFile(getPath()); 119 } catch (FileNotFoundException ex) { 120 throw new RuntimeException (ex); 121 } 122 } 123 124 public String getName() { 125 return path.substring(path.lastIndexOf(File.separatorChar) + 1).toString(); 126 } 127 128 public String getParent() { 129 throw new UnsupportedOperationException (); 130 } 132 133 public String getPath() { 134 return path; 135 } 138 139 public String getGroupname() { 140 return "drftpd"; 141 } 142 143 public String getUsername() { 144 return "drftpd"; 145 } 146 147 public boolean isDirectory() { 148 return isDirectory; 149 } 150 151 public boolean isFile() { 152 return isFile; 153 } 154 155 public long lastModified() { 156 return this.getFile().lastModified(); 157 } 158 159 public long length() { 160 if (isDirectory()) { 161 return 0; 162 } 163 return getFile().length(); 164 } 165 166 169 public RemoteFileInterface[] listFiles() { 170 return (RemoteFileInterface[]) getFiles().toArray( 171 new FileRemoteFile[0]); 172 } 173 174 Hashtable filefiles; 175 private void buildFileFiles() { 176 if (filefiles != null) 177 return; 178 filefiles = new Hashtable (); 179 180 if (!isDirectory()) { 181 throw new IllegalArgumentException ("listFiles() called on !isDirectory()"); 182 } 183 for (Iterator iter = rootBasket.iterator(); iter.hasNext();) { 184 Root root = (Root) iter.next(); 185 File file = new File (root.getPath() + "/" + path); 186 if (!file.exists()) 187 continue; 188 if (!file.isDirectory()) 189 throw new FatalException( 190 file.getPath() 191 + " is not a directory, attempt to getFiles() on it"); 192 if (!file.canRead()) 193 throw new FatalException("Cannot read: " + file); 194 File tmpFiles[] = file.listFiles(); 195 if (tmpFiles == null) 197 throw new NullPointerException ( 198 "list() on " + file + " returned null"); 199 for (int i = 0; i < tmpFiles.length; i++) { 200 try { 201 if (tmpFiles[i].isDirectory() && isEmpty(tmpFiles[i])) { 202 continue; 203 } 204 FileRemoteFile listfile = 205 new FileRemoteFile( 206 rootBasket, 207 path + File.separatorChar + tmpFiles[i].getName()); 208 filefiles.put(tmpFiles[i].getName(), listfile); 209 } catch (IOException e) { 210 e.printStackTrace(); 211 } 212 } 213 } 214 if(!getName().equals("") && filefiles.isEmpty()) throw new RuntimeException (); 215 } 216 219 public Collection getFiles() { 220 buildFileFiles(); 221 return filefiles.values(); 222 } 223 224 227 private static boolean isEmpty(File dir) { 228 File listfiles[] = dir.listFiles(); 229 if (listfiles == null) 230 throw new FatalException("Not a directory or IO error: " + dir); 231 for (int i = 0; i < listfiles.length; i++) { 232 File file = listfiles[i]; 233 if (file.isFile()) 234 return false; 235 } 236 237 for (int i = 0; i < listfiles.length; i++) { 238 File file = listfiles[i]; 239 if (!isEmpty(file)) 241 return false; 242 } 243 dir.delete(); 244 return true; 245 } 246 247 public Collection getSlaves() { 248 return new ArrayList (); 249 } 250 251 public boolean isDeleted() { 252 return false; 253 } 254 } 255 | Popular Tags |