| 1 | |
package org.webslinger.commons.vfs.virtual; |
| 2 | |
|
| 3 | |
import java.io.FileNotFoundException; |
| 4 | |
import java.net.URL; |
| 5 | |
import java.util.ArrayList; |
| 6 | |
import java.util.Arrays; |
| 7 | |
import java.util.Collection; |
| 8 | |
import java.util.Collections; |
| 9 | |
import java.util.HashMap; |
| 10 | |
import java.util.HashSet; |
| 11 | |
import java.util.LinkedHashSet; |
| 12 | |
import java.util.Map; |
| 13 | |
import java.util.Set; |
| 14 | |
import java.util.SortedMap; |
| 15 | |
import java.util.TreeMap; |
| 16 | |
import java.util.concurrent.ConcurrentMap; |
| 17 | |
import java.util.concurrent.atomic.AtomicReference; |
| 18 | |
|
| 19 | |
import org.apache.commons.vfs.Capability; |
| 20 | |
import org.apache.commons.vfs.FileChangeEvent; |
| 21 | |
import org.apache.commons.vfs.FileListener; |
| 22 | |
import org.apache.commons.vfs.FileName; |
| 23 | |
import org.apache.commons.vfs.FileObject; |
| 24 | |
import org.apache.commons.vfs.FileSystem; |
| 25 | |
import org.apache.commons.vfs.FileSystemException; |
| 26 | |
import org.apache.commons.vfs.FileSystemOptions; |
| 27 | |
import org.apache.commons.vfs.FileType; |
| 28 | |
import org.apache.commons.vfs.NameScope; |
| 29 | |
import org.apache.commons.vfs.events.AbstractFileChangeEvent; |
| 30 | |
import org.apache.commons.vfs.events.ChangedEvent; |
| 31 | |
import org.apache.commons.vfs.events.CreateEvent; |
| 32 | |
import org.apache.commons.vfs.events.DeleteEvent; |
| 33 | |
|
| 34 | |
import org.webslinger.commons.vfs.FileSet; |
| 35 | |
import org.webslinger.commons.vfs.FileSystemAndNameKey; |
| 36 | |
import org.webslinger.commons.vfs.GenerationalFileObject; |
| 37 | |
import org.webslinger.commons.vfs.GenerationalFileSystem; |
| 38 | |
import org.webslinger.commons.vfs.handlers.HandlerFactory; |
| 39 | |
import org.webslinger.commons.vfs.handlers.cow.COWStorageHandler; |
| 40 | |
import org.webslinger.lang.ConcurrentUtil; |
| 41 | |
|
| 42 | 193156 | public class VirtualFileSystem extends GenerationalFileSystem<FileName, VirtualFileObject, VirtualFileSystem.VirtualResolution, VirtualFileSystem> { |
| 43 | 1 | private final static Collection<Capability> capabilities = Collections.unmodifiableCollection( |
| 44 | |
Arrays.asList( |
| 45 | |
new Capability[] { |
| 46 | |
Capability.READ_CONTENT, |
| 47 | |
Capability.WRITE_CONTENT, |
| 48 | |
Capability.RANDOM_ACCESS_READ, |
| 49 | |
Capability.RANDOM_ACCESS_WRITE, |
| 50 | |
Capability.APPEND_CONTENT, |
| 51 | |
Capability.LAST_MODIFIED, |
| 52 | |
Capability.GET_LAST_MODIFIED, |
| 53 | |
Capability.SET_LAST_MODIFIED_FILE, |
| 54 | |
Capability.SET_LAST_MODIFIED_FOLDER, |
| 55 | |
|
| 56 | |
Capability.CREATE, |
| 57 | |
Capability.DELETE, |
| 58 | |
Capability.RENAME, |
| 59 | |
Capability.LIST_CHILDREN, |
| 60 | |
Capability.DISPATCHER, |
| 61 | |
Capability.JUNCTIONS, |
| 62 | |
} |
| 63 | |
) |
| 64 | |
); |
| 65 | |
|
| 66 | 53 | protected final SortedMap<FileName, MountPoint> mounts = ConcurrentUtil.createSortedMap(); |
| 67 | |
protected COWStorageHandler storageHandler; |
| 68 | |
|
| 69 | |
public VirtualFileSystem(FileName name, FileSystemOptions options) throws FileSystemException { |
| 70 | 53 | super(name, null, options); |
| 71 | 53 | newGeneration(); |
| 72 | 53 | } |
| 73 | |
|
| 74 | |
protected int newGeneration() { |
| 75 | 417 | return super.newGeneration(); |
| 76 | |
} |
| 77 | |
|
| 78 | |
protected COWStorageHandler getStorageHandler() throws FileSystemException { |
| 79 | 150871 | if (storageHandler != null) return storageHandler; |
| 80 | 53 | HandlerFactory<COWStorageHandler, VirtualFileSystem> factory = VirtualConfigBuilder.getInstance().getStorageHandlerFactory(getFileSystemOptions()); |
| 81 | 53 | if (factory != null) storageHandler = factory.getHandler(this, COWStorageHandler.class); |
| 82 | 53 | if (storageHandler == null) storageHandler = COWStorageHandler.EmptyStorageHandler; |
| 83 | 53 | return storageHandler; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public void addCapabilities(Collection caps) { |
| 87 | 53 | caps.addAll(capabilities); |
| 88 | 53 | } |
| 89 | |
|
| 90 | |
public FileObject createFile(FileName name) throws FileSystemException { |
| 91 | 26556 | return new VirtualFileObject(name, this); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public boolean clear(FileName name) throws FileSystemException { |
| 95 | 50 | getStorageHandler().submitRequests(name, new COWStorageHandler.ClearRequest()); |
| 96 | |
MountPoint oldPoint, newPoint; |
| 97 | |
OUTER: |
| 98 | |
do { |
| 99 | 50 | oldPoint = mounts.get(name); |
| 100 | 50 | if (oldPoint == null) return false; |
| 101 | 45 | newPoint = oldPoint.clear(); |
| 102 | 45 | if (newPoint == null) { |
| 103 | 23 | if (((ConcurrentMap) mounts).remove(name, oldPoint)) break; |
| 104 | |
continue OUTER; |
| 105 | 22 | } else if (newPoint == oldPoint) { |
| 106 | 1 | return false; |
| 107 | |
} |
| 108 | 21 | } while (!((ConcurrentMap) mounts).replace(name, oldPoint, newPoint)); |
| 109 | 44 | newGeneration(); |
| 110 | 44 | updateListeners(name); |
| 111 | 44 | return true; |
| 112 | |
} |
| 113 | |
|
| 114 | |
public void addJunction(String path, FileObject file) throws FileSystemException { |
| 115 | 195 | FileName name = resolveFile(path).getName(); |
| 116 | |
MountPoint oldPoint, newPoint; |
| 117 | |
OUTER: |
| 118 | |
do { |
| 119 | 195 | oldPoint = mounts.get(name); |
| 120 | 195 | if (oldPoint == null) { |
| 121 | 174 | newPoint = newMountPoint(name, file, null); |
| 122 | 174 | if (((ConcurrentMap) mounts).putIfAbsent(name, newPoint) == null) break; |
| 123 | |
continue OUTER; |
| 124 | |
} else { |
| 125 | 21 | newPoint = oldPoint.setMount(file); |
| 126 | |
} |
| 127 | 21 | } while (!((ConcurrentMap) mounts).replace(name, oldPoint, newPoint)); |
| 128 | 195 | newGeneration(); |
| 129 | 195 | updateListeners(name); |
| 130 | 195 | } |
| 131 | |
|
| 132 | |
public boolean addBase(FileName name, String relative) throws FileSystemException { |
| 133 | 118 | VirtualFileObject file = resolveFile(getFileSystemManager().resolveName(name.getParent(), relative)); |
| 134 | 118 | relative = name.getParent().getRelativeName(file.getName()); |
| 135 | 118 | getStorageHandler().submitRequests(name, new COWStorageHandler.AddBaseRequest(relative)); |
| 136 | |
MountPoint oldPoint, newPoint; |
| 137 | |
OUTER: |
| 138 | |
do { |
| 139 | 118 | oldPoint = mounts.get(name); |
| 140 | 118 | if (oldPoint == null) { |
| 141 | 25 | newPoint = newMountPoint(name, null, file); |
| 142 | 25 | if (((ConcurrentMap) mounts).putIfAbsent(name, newPoint) == null) break; |
| 143 | |
continue OUTER; |
| 144 | |
} else { |
| 145 | 93 | newPoint = oldPoint.addBase(file); |
| 146 | |
} |
| 147 | 93 | } while (!((ConcurrentMap) mounts).replace(name, oldPoint, newPoint)); |
| 148 | 118 | newGeneration(); |
| 149 | 118 | updateListeners(name); |
| 150 | 118 | return true; |
| 151 | |
} |
| 152 | |
|
| 153 | |
public void removeJunction(String path) throws FileSystemException { |
| 154 | 1 | FileName name = resolveFile(path).getName(); |
| 155 | |
TreeMap<String, FileObject> oldMounts, newMounts; |
| 156 | |
MountPoint oldPoint, newPoint; |
| 157 | |
do { |
| 158 | 1 | oldPoint = mounts.get(name); |
| 159 | 1 | if (oldPoint == null || oldPoint.mount == null) return; |
| 160 | 1 | newPoint = oldPoint.removeMount(); |
| 161 | 1 | } while (!((ConcurrentMap) mounts).replace(name, oldPoint, newPoint)); |
| 162 | 1 | newGeneration(); |
| 163 | 1 | updateListeners(name); |
| 164 | 1 | } |
| 165 | |
|
| 166 | |
public boolean removeBase(FileName name, String relative) throws FileSystemException { |
| 167 | 6 | VirtualFileObject file = resolveFile(getFileSystemManager().resolveName(name.getParent(), relative)); |
| 168 | 6 | relative = name.getParent().getRelativeName(file.getName()); |
| 169 | 6 | getStorageHandler().submitRequests(name, new COWStorageHandler.RemoveBaseRequest(relative)); |
| 170 | |
MountPoint oldPoint, newPoint; |
| 171 | |
do { |
| 172 | 6 | oldPoint = mounts.get(name); |
| 173 | 6 | if (oldPoint == null) return false; |
| 174 | 6 | newPoint = oldPoint.removeBase(file); |
| 175 | 6 | if (newPoint == null) return false; |
| 176 | 6 | } while (!((ConcurrentMap) mounts).replace(name, oldPoint, newPoint)); |
| 177 | 6 | newGeneration(); |
| 178 | 6 | updateListeners(name); |
| 179 | 6 | return true; |
| 180 | |
} |
| 181 | |
|
| 182 | |
protected MountPoint setDeleted(FileName name, boolean isDeleted, boolean newGeneration) throws FileSystemException { |
| 183 | 20 | getStorageHandler().submitRequests(name, new COWStorageHandler.SetDeletedRequest(isDeleted)); |
| 184 | |
MountPoint oldPoint, newPoint; |
| 185 | |
OUTER: |
| 186 | |
do { |
| 187 | 20 | oldPoint = mounts.get(name); |
| 188 | 20 | if (oldPoint == null) { |
| 189 | 7 | if (!isDeleted) return null; |
| 190 | 7 | newPoint = newMountPoint(name, isDeleted, null); |
| 191 | 7 | if (((ConcurrentMap) mounts).putIfAbsent(name, newPoint) == null) break; |
| 192 | |
continue OUTER; |
| 193 | |
} else { |
| 194 | 13 | newPoint = oldPoint.setDeleted(isDeleted); |
| 195 | |
} |
| 196 | 13 | } while (!((ConcurrentMap) mounts).replace(name, oldPoint, newPoint)); |
| 197 | 20 | if (oldPoint != null) oldPoint.valid = false; |
| 198 | 20 | if (newGeneration) newGeneration(); |
| 199 | 20 | updateListeners(name); |
| 200 | 20 | return newPoint; |
| 201 | |
} |
| 202 | |
|
| 203 | |
protected VirtualResolution newResolution(int generation, VirtualFileObject genFile) throws FileSystemException { |
| 204 | 16913 | VirtualFileObject ptr = genFile; |
| 205 | 16913 | ArrayList<FileLookup> resolvedFiles = new ArrayList<FileLookup>(); |
| 206 | 16913 | FileObject foundJunction = null; |
| 207 | 16913 | MountPoint foundFiles = getMounts(ptr.getName()); |
| 208 | 16913 | boolean isDeleted = foundFiles != null && foundFiles.isDeleted; |
| 209 | |
do { |
| 210 | 16913 | if (foundFiles != null) { |
| 211 | 714 | resolvedFiles.addAll(Arrays.asList(foundFiles.bases)); |
| 212 | 714 | if (foundFiles.mount != null) foundJunction = foundFiles.mount; |
| 213 | |
} |
| 214 | 16913 | String namePart = ptr.getName().getBaseName(); |
| 215 | 16913 | if (namePart.equals("/")) break; |
| 216 | 16913 | ptr = ptr.getParent(); |
| 217 | 16913 | if (ptr == null) break; |
| 218 | 16552 | VirtualResolution resolution = ptr.getResolution(); |
| 219 | 16552 | if (foundJunction == null && resolution.mount != null) foundJunction = resolution.mount.resolveFile(namePart); |
| 220 | 37365 | for (FileLookup base: resolution.bases) { |
| 221 | 20813 | resolvedFiles.add(new ChildFileLookup(base, namePart)); |
| 222 | |
} |
| 223 | |
} while (false); |
| 224 | 16913 | Collections.reverse(resolvedFiles); |
| 225 | 16913 | return new VirtualResolution(generation, resolvedFiles.toArray(new FileLookup[resolvedFiles.size()]), genFile, foundJunction, foundFiles); |
| 226 | |
} |
| 227 | |
|
| 228 | |
protected FileObject findMounted(FileName dir, String name) throws FileSystemException { |
| 229 | 82839 | FileName tmpName = getFileSystemManager().resolveName(dir, name, NameScope.CHILD); |
| 230 | 82839 | MountPoint tmpPoint = mounts.get(tmpName); |
| 231 | 82839 | if (tmpPoint != null) return tmpPoint.mount; |
| 232 | 82839 | return resolveFile(dir).getResolution().mount.resolveFile(name); |
| 233 | |
} |
| 234 | |
|
| 235 | |
protected VirtualFileObject[] newArray(int length) { |
| 236 | 67125 | return new VirtualFileObject[length]; |
| 237 | |
} |
| 238 | |
|
| 239 | |
protected MountPoint getMounts(FileName name) throws FileSystemException { |
| 240 | |
while (true) { |
| 241 | 16937 | MountPoint point = mounts.get(name); |
| 242 | 16937 | if (point != null) return point; |
| 243 | 16245 | boolean[] isDeleted = new boolean[1]; |
| 244 | 16245 | StandardFileLookup[] bases = loadData(name, null, isDeleted); |
| 245 | 16245 | if (bases == null) return null; |
| 246 | 40 | MountPoint newPoint = new MountPoint(name, null, isDeleted[0], bases); |
| 247 | 40 | if (((ConcurrentMap) mounts).putIfAbsent(name, newPoint) == null) return newPoint; |
| 248 | 0 | } |
| 249 | |
} |
| 250 | |
|
| 251 | |
protected int addMountedChildren(FileName startName, Collection<String> names) throws FileSystemException { |
| 252 | 67113 | String start = startName.getPath(); |
| 253 | 67113 | int prefixLength = start.length(); |
| 254 | 67244 | while (prefixLength > 0 && start.charAt(prefixLength - 1) == FileName.SEPARATOR_CHAR) prefixLength--; |
| 255 | 67113 | int nameStart = prefixLength + 1; |
| 256 | 67113 | int count = 0; |
| 257 | 67113 | for (Map.Entry<FileName, MountPoint> entry: mounts.tailMap(startName).entrySet()) { |
| 258 | 64799 | FileName name = entry.getKey(); |
| 259 | 64799 | MountPoint point = entry.getValue(); |
| 260 | 64799 | String path = name.getPath(); |
| 261 | |
|
| 262 | 64799 | if (!path.startsWith(start) || path.length() < prefixLength) break; |
| 263 | 3461 | if (path.length() <= nameStart) continue; |
| 264 | 112 | if (path.charAt(prefixLength) != FileName.SEPARATOR_CHAR) continue; |
| 265 | 112 | if (point.isDeleted) continue; |
| 266 | |
|
| 267 | 56 | if (point.mount == null && point.bases.length == 0) continue; |
| 268 | 31 | int slash = path.indexOf(FileName.SEPARATOR_CHAR, nameStart); |
| 269 | 31 | if (slash == -1) { |
| 270 | 25 | names.add(path.substring(nameStart)); |
| 271 | |
} else { |
| 272 | 6 | names.add(path.substring(nameStart, slash)); |
| 273 | |
} |
| 274 | 31 | count++; |
| 275 | 31 | } |
| 276 | 67113 | return count; |
| 277 | |
} |
| 278 | |
|
| 279 | |
protected FileSet getRealFiles(VirtualFileObject file) throws FileSystemException { |
| 280 | 0 | return createFileSet(new FileObject[] {file.getFile()}, new FileObject[0]); |
| 281 | |
} |
| 282 | |
|
| 283 | |
protected interface FileLookup { |
| 284 | |
VirtualFileObject lookupFile() throws FileSystemException; |
| 285 | |
void refresh() throws FileSystemException; |
| 286 | |
} |
| 287 | |
|
| 288 | |
protected static class StandardFileLookup implements FileLookup { |
| 289 | |
protected final VirtualFileObject file; |
| 290 | |
|
| 291 | 158 | protected StandardFileLookup(VirtualFileObject file) { |
| 292 | 158 | this.file = file; |
| 293 | 158 | } |
| 294 | |
|
| 295 | |
public VirtualFileObject lookupFile() throws FileSystemException { |
| 296 | 156173 | return file; |
| 297 | |
} |
| 298 | |
|
| 299 | |
public void refresh() throws FileSystemException { |
| 300 | 1474 | VirtualFileObject file = lookupFile(); |
| 301 | 1474 | if (file != null) file.refresh(); |
| 302 | 1474 | } |
| 303 | |
} |
| 304 | |
|
| 305 | |
protected static class ChildFileLookup implements FileLookup { |
| 306 | |
protected final FileLookup lookup; |
| 307 | |
protected final String childName; |
| 308 | |
|
| 309 | 20813 | protected ChildFileLookup(FileLookup lookup, String childName) { |
| 310 | 20813 | this.lookup = lookup; |
| 311 | 20813 | this.childName = childName; |
| 312 | 20813 | } |
| 313 | |
|
| 314 | |
public VirtualFileObject lookupFile() throws FileSystemException { |
| 315 | 414219 | VirtualFileObject base = lookup.lookupFile(); |
| 316 | 414219 | if (base == null) return null; |
| 317 | 298180 | return base.getChild(childName); |
| 318 | |
} |
| 319 | |
|
| 320 | |
public void refresh() throws FileSystemException { |
| 321 | 79280 | VirtualFileObject base = lookup.lookupFile(); |
| 322 | 79280 | if (base == null) return; |
| 323 | 43259 | VirtualFileObject file = base.getChild(childName); |
| 324 | 43259 | (file != null ? file : base).refresh(); |
| 325 | 43259 | } |
| 326 | |
} |
| 327 | |
|
| 328 | |
protected StandardFileLookup[] loadData(FileName name, VirtualFileObject givenBase, boolean[] isDeleted) throws FileSystemException { |
| 329 | 16451 | ArrayList<StandardFileLookup> lookups = new ArrayList<StandardFileLookup>(); |
| 330 | 16451 | boolean foundGivenBase = false; |
| 331 | 16451 | Collection<String> bases = getStorageHandler().loadData(name, isDeleted); |
| 332 | 16451 | if (bases == null) return null; |
| 333 | 72 | for (String relative: bases) { |
| 334 | 65 | VirtualFileObject base = resolveFile(getFileSystemManager().resolveName(name.getParent(), relative)); |
| 335 | 65 | if (base == givenBase) foundGivenBase = true; |
| 336 | 65 | lookups.add(new StandardFileLookup(base)); |
| 337 | 65 | } |
| 338 | 72 | if (!foundGivenBase && givenBase != null) lookups.add(new StandardFileLookup(givenBase)); |
| 339 | 72 | if (lookups.isEmpty() && !isDeleted[0]) return null; |
| 340 | 72 | return lookups.toArray(new StandardFileLookup[lookups.size()]); |
| 341 | |
} |
| 342 | |
|
| 343 | |
protected MountPoint newMountPoint(FileName name, FileObject mount, VirtualFileObject givenBase) throws FileSystemException { |
| 344 | 199 | boolean[] isDeleted = new boolean[1]; |
| 345 | 199 | StandardFileLookup[] bases = loadData(name, givenBase, isDeleted); |
| 346 | 199 | return new MountPoint(name, mount, isDeleted[0], bases != null ? bases : new StandardFileLookup[0]); |
| 347 | |
} |
| 348 | |
|
| 349 | |
protected MountPoint newMountPoint(FileName name, boolean isDeleted, VirtualFileObject givenBase) throws FileSystemException { |
| 350 | 7 | boolean[] isDeletedLoad = new boolean[1]; |
| 351 | 7 | StandardFileLookup[] bases = loadData(name, givenBase, isDeletedLoad); |
| 352 | 7 | return new MountPoint(name, null, isDeleted, bases != null ? bases : new StandardFileLookup[0]); |
| 353 | |
} |
| 354 | |
|
| 355 | |
private static final void addChildNames(Collection<String> names, FileObject[] children) { |
| 356 | 75672 | if (children == null) return; |
| 357 | 546529 | for (FileObject child: children) { |
| 358 | 470857 | names.add(child.getName().getBaseName()); |
| 359 | |
} |
| 360 | 75672 | } |
| 361 | |
|
| 362 | |
protected final static class MountPoint { |
| 363 | |
protected final FileName name; |
| 364 | |
protected boolean isDeleted; |
| 365 | |
protected final FileObject mount; |
| 366 | |
protected final StandardFileLookup[] bases; |
| 367 | 401 | protected boolean valid = true; |
| 368 | |
|
| 369 | 401 | protected MountPoint(FileName name, FileObject mount, boolean isDeleted, StandardFileLookup[] bases) { |
| 370 | 401 | this.name = name; |
| 371 | 401 | this.mount = mount; |
| 372 | 401 | this.isDeleted = isDeleted; |
| 373 | 401 | this.bases = bases; |
| 374 | 401 | } |
| 375 | |
|
| 376 | |
protected MountPoint setMount(FileObject mount) throws FileSystemException { |
| 377 | 21 | if (this.mount != null) throw new FileSystemException("Already mounted(" + name + ")"); |
| 378 | 21 | return new MountPoint(name, mount, isDeleted, bases); |
| 379 | |
} |
| 380 | |
|
| 381 | |
protected MountPoint removeMount() { |
| 382 | 1 | if (mount == null) return null; |
| 383 | 1 | return new MountPoint(name, null, isDeleted, bases); |
| 384 | |
} |
| 385 | |
|
| 386 | |
protected MountPoint setDeleted(boolean isDeleted) { |
| 387 | 13 | if (this.isDeleted == isDeleted) return this; |
| 388 | 13 | return new MountPoint(name, mount, isDeleted, bases); |
| 389 | |
} |
| 390 | |
|
| 391 | |
protected MountPoint addBase(VirtualFileObject base) { |
| 392 | 93 | if (bases == null) return new MountPoint(name, mount, isDeleted, new StandardFileLookup[] {new StandardFileLookup(base)}); |
| 393 | 93 | StandardFileLookup[] bases = new StandardFileLookup[this.bases.length + 1]; |
| 394 | 93 | System.arraycopy(this.bases, 0, bases, 0, this.bases.length); |
| 395 | 93 | bases[this.bases.length] = new StandardFileLookup(base); |
| 396 | 93 | return new MountPoint(name, mount, isDeleted, bases); |
| 397 | |
} |
| 398 | |
|
| 399 | |
protected MountPoint removeBase(VirtualFileObject base) { |
| 400 | 6 | for (int i = 0; i < bases.length; i++) { |
| 401 | 6 | if (bases[i].file.equals(base)) { |
| 402 | 6 | StandardFileLookup[] bases = new StandardFileLookup[this.bases.length - 1]; |
| 403 | 6 | System.arraycopy(this.bases, 0, bases, 0, i); |
| 404 | 6 | System.arraycopy(this.bases, i + 1, bases, 0, bases.length - i); |
| 405 | 6 | return new MountPoint(name, mount, isDeleted, bases); |
| 406 | |
} |
| 407 | |
} |
| 408 | 0 | return null; |
| 409 | |
} |
| 410 | |
|
| 411 | |
protected MountPoint clear() { |
| 412 | 45 | if (mount == null) return null; |
| 413 | 22 | if (!isDeleted && bases.length == 0) return this; |
| 414 | 21 | return new MountPoint(name, mount, false, new StandardFileLookup[0]); |
| 415 | |
} |
| 416 | |
} |
| 417 | |
|
| 418 | |
public class VirtualResolution extends GenerationalFileSystem.Resolution<FileName, VirtualFileObject, VirtualResolution, VirtualFileSystem> { |
| 419 | |
public final FileLookup[] bases; |
| 420 | |
public final FileObject mount; |
| 421 | |
protected MountPoint point; |
| 422 | |
|
| 423 | 16913 | protected VirtualResolution(int generation, FileLookup[] bases, VirtualFileObject owner, FileObject mount, MountPoint point) throws FileSystemException { |
| 424 | 16913 | super(generation, owner); |
| 425 | 16913 | this.bases = bases; |
| 426 | 16913 | this.mount = mount; |
| 427 | 16913 | for (FileLookup base: bases) base.refresh(); |
| 428 | 16913 | if (mount != null) mount.refresh(); |
| 429 | 16913 | this.point = point; |
| 430 | 16913 | } |
| 431 | |
|
| 432 | |
protected boolean isDeleted() throws FileSystemException { |
| 433 | 181474 | if (point == null) return false; |
| 434 | 8115 | if (!point.valid) point = getMounts(file.getName()); |
| 435 | 8115 | return point.isDeleted; |
| 436 | |
} |
| 437 | |
|
| 438 | |
protected boolean isWriteable() { |
| 439 | 18 | return mount != null; |
| 440 | |
} |
| 441 | |
|
| 442 | |
protected boolean refresh() throws FileSystemException { |
| 443 | 85574 | for (FileLookup base: bases) base.refresh(); |
| 444 | 85574 | if (mount != null) mount.refresh(); |
| 445 | 85574 | return super.refresh(); |
| 446 | |
} |
| 447 | |
|
| 448 | |
protected boolean isOutOfDate(boolean create) throws FileSystemException { |
| 449 | 974900 | return false; |
| 450 | |
} |
| 451 | |
|
| 452 | |
protected FileObject doGetFile(boolean create) throws FileSystemException { |
| 453 | 79835 | if (create) { |
| 454 | 1068 | if (mount == null) throw new IllegalArgumentException("No mount found for " + file.getName()); |
| 455 | 1068 | if (isDeleted()) point = setDeleted(file.getName(), false, false); |
| 456 | 1068 | return mount; |
| 457 | |
} |
| 458 | 78767 | if (isDeleted()) return null; |
| 459 | 78751 | if (mount != null && mount.exists()) return mount; |
| 460 | 48018 | for (FileLookup lookup: bases) { |
| 461 | 38273 | VirtualFileObject base = lookup.lookupFile(); |
| 462 | 38273 | if (base == null) continue; |
| 463 | 13119 | if (getResolution(base, false).isDeleted()) return null; |
| 464 | 13119 | if (base.exists()) return base; |
| 465 | |
} |
| 466 | 9745 | return mount != null ? mount : null; |
| 467 | |
} |
| 468 | |
|
| 469 | |
protected Collection<URL> getAllURLs() throws FileSystemException { |
| 470 | 0 | Collection<URL> urls = new ArrayList<URL>(bases.length + 1); |
| 471 | 0 | if (mount != null && mount.exists()) urls.add(mount.getURL()); |
| 472 | 0 | for (FileLookup lookup: bases) { |
| 473 | 0 | VirtualFileObject base = lookup.lookupFile(); |
| 474 | 0 | if (base == null || !base.exists()) continue; |
| 475 | 0 | urls.add(base.getURL()); |
| 476 | |
} |
| 477 | 0 | return urls; |
| 478 | |
} |
| 479 | |
|
| 480 | |
protected void doDelete() throws FileSystemException { |
| 481 | 12 | point = setDeleted(file.getName(), true, false); |
| 482 | 12 | if (mount != null) mount.delete(); |
| 483 | 12 | } |
| 484 | |
|
| 485 | |
protected void injectType(FileType newType) throws FileSystemException { |
| 486 | 1080 | if (newType == null) { |
| 487 | |
|
| 488 | |
} else { |
| 489 | 1080 | if (isDeleted()) { |
| 490 | 12 | if (!FileType.IMAGINARY.equals(newType)) point = setDeleted(file.getName(), false, false); |
| 491 | |
} else { |
| 492 | 1068 | if (FileType.IMAGINARY.equals(newType)) { |
| 493 | 0 | throw new InternalError(); |
| 494 | |
} |
| 495 | |
} |
| 496 | |
} |
| 497 | 1080 | super.injectType(newType); |
| 498 | 1080 | } |
| 499 | |
|
| 500 | |
protected String[] getChildNames() throws FileSystemException { |
| 501 | 67113 | LinkedHashSet<String> names = new LinkedHashSet<String>(); |
| 502 | |
OUTER: |
| 503 | |
do { |
| 504 | 67113 | if (mount != null) { |
| 505 | 67113 | if (isDeleted()) break OUTER; |
| 506 | 67113 | if (mount.exists()) { |
| 507 | 55345 | if (mount.getType().hasChildren()) { |
| 508 | 55345 | addChildNames(names, mount.getChildren()); |
| 509 | |
} else { |
| 510 | 0 | throw new FileSystemException("Found file/directory overlap"); |
| 511 | |
} |
| 512 | |
} |
| 513 | |
} |
| 514 | 104253 | for (FileLookup lookup: bases) { |
| 515 | 37140 | VirtualFileObject base = lookup.lookupFile(); |
| 516 | 37140 | if (base == null) continue; |
| 517 | 20327 | if (getResolution(base, false).isDeleted()) break OUTER; |
| 518 | 20327 | if (!base.getType().hasChildren()) continue; |
| 519 | 20327 | addChildNames(names, base.getChildren()); |
| 520 | |
} |
| 521 | |
} while (false); |
| 522 | 67113 | names.remove(".svn"); |
| 523 | 67113 | addMountedChildren(file.getName(), names); |
| 524 | 67113 | getStorageHandler().removeDeletedChildren(file.getName(), names); |
| 525 | 67113 | getStorageHandler().excludeNames(names); |
| 526 | 67113 | return names.toArray(new String[names.size()]); |
| 527 | |
} |
| 528 | |
} |
| 529 | |
} |
| 530 | |
|