| 1 | |
package org.webslinger.commons.vfs.cow; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Collection; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.HashSet; |
| 8 | |
import java.util.Iterator; |
| 9 | |
import java.util.LinkedHashSet; |
| 10 | |
|
| 11 | |
import org.apache.commons.vfs.FileName; |
| 12 | |
import org.apache.commons.vfs.FileObject; |
| 13 | |
import org.apache.commons.vfs.FileSystem; |
| 14 | |
import org.apache.commons.vfs.FileSystemException; |
| 15 | |
import org.apache.commons.vfs.FileSystemManager; |
| 16 | |
|
| 17 | |
import org.webslinger.collections.CollectionUtil; |
| 18 | |
|
| 19 | |
public class COWResolver { |
| 20 | |
private final COWFileSystem cfs; |
| 21 | 205 | private LinkedHashSet<COWFileObject> results = new LinkedHashSet<COWFileObject>(); |
| 22 | |
private HashMap<String, LinkedHashSet<COWFileObject>> linkedParents; |
| 23 | |
|
| 24 | |
public COWResolver(COWFileSystem cfs) { |
| 25 | 197 | this(cfs, new HashMap<String, LinkedHashSet<COWFileObject>>()); |
| 26 | |
|
| 27 | 197 | } |
| 28 | |
|
| 29 | 205 | private COWResolver(COWFileSystem cfs, HashMap<String, LinkedHashSet<COWFileObject>> linkedParents) { |
| 30 | 205 | this.cfs = cfs; |
| 31 | 205 | this.linkedParents = linkedParents; |
| 32 | 205 | } |
| 33 | |
|
| 34 | |
private void addFile(COWFileObject fo) throws FileSystemException { |
| 35 | |
|
| 36 | 346 | results.remove(fo); |
| 37 | 346 | results.add(fo); |
| 38 | 346 | } |
| 39 | |
|
| 40 | |
private LinkedHashSet<COWFileObject> getPreviousParents(String name) { |
| 41 | 8 | return linkedParents.get(name); |
| 42 | |
} |
| 43 | |
|
| 44 | |
private void addParentLink(COWFileObject parentFile, String suffixName) { |
| 45 | |
|
| 46 | 1207 | LinkedHashSet<COWFileObject> parents = linkedParents.get(suffixName); |
| 47 | 1207 | if (parents == null) { |
| 48 | 641 | parents = new LinkedHashSet<COWFileObject>(); |
| 49 | 641 | linkedParents.put(suffixName, parents); |
| 50 | |
} |
| 51 | 1207 | parents.add(parentFile); |
| 52 | 1207 | } |
| 53 | |
|
| 54 | |
private COWResolver getSubResolver() { |
| 55 | 8 | return new COWResolver(cfs, linkedParents); |
| 56 | |
} |
| 57 | |
|
| 58 | |
private void mergeIn(COWResolver resolver) { |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 8 | if (resolver.results.isEmpty()) return; |
| 63 | 8 | if (results.isEmpty()) { |
| 64 | 8 | results = resolver.results; |
| 65 | 8 | return; |
| 66 | |
} |
| 67 | 0 | resolver.results.addAll(results); |
| 68 | 0 | results = resolver.results; |
| 69 | |
|
| 70 | 0 | } |
| 71 | |
|
| 72 | |
public COWFileObject[] getFiles() { |
| 73 | 197 | return results.toArray(new COWFileObject[results.size()]); |
| 74 | |
} |
| 75 | |
|
| 76 | |
public COWEntry[] getCOWEntries() { |
| 77 | 197 | return entries.keySet().toArray(new COWEntry[entries.size()]); |
| 78 | |
} |
| 79 | |
|
| 80 | |
public int[] getCOWEntrySerials() { |
| 81 | 197 | int[] cowEntrySerials = new int[entries.size()]; |
| 82 | 197 | Iterator it = entries.values().iterator(); |
| 83 | 197 | int i = 0; |
| 84 | 886 | while (it.hasNext()) { |
| 85 | 689 | Integer serial = (Integer) it.next(); |
| 86 | 689 | cowEntrySerials[i++] = serial.intValue(); |
| 87 | 689 | } |
| 88 | 197 | return cowEntrySerials; |
| 89 | |
} |
| 90 | |
|
| 91 | 205 | private final HashMap<COWEntry, Integer> entries = new HashMap<COWEntry, Integer>(); |
| 92 | |
private COWEntry getCOWEntry(COWFileObject file) throws FileSystemException { |
| 93 | 895 | COWEntry entry = cfs.getCOWEntry(file, false); |
| 94 | 895 | if (entry == null) return null; |
| 95 | 882 | entries.put(entry, entry.getSerial()); |
| 96 | 882 | return entry; |
| 97 | |
} |
| 98 | |
|
| 99 | |
protected void rerun(String relative, String suffixName) throws FileSystemException { |
| 100 | |
|
| 101 | 8 | LinkedHashSet<COWFileObject> previousParents = getPreviousParents(suffixName); |
| 102 | |
|
| 103 | |
try { |
| 104 | 8 | COWResolver subResolver = getSubResolver(); |
| 105 | 8 | COWFileObject[] parents = new COWFileObject[previousParents.size()]; |
| 106 | 8 | previousParents.toArray(parents); |
| 107 | 8 | previousParents.clear(); |
| 108 | 8 | FileSystemManager fsm = cfs.getFileSystemManager(); |
| 109 | 16 | for (COWFileObject previousParent: parents) { |
| 110 | |
|
| 111 | |
|
| 112 | 8 | subResolver.resolve(previousParent.getParent().resolveFile(relative).resolveFile(suffixName)); |
| 113 | |
} |
| 114 | 8 | mergeIn(subResolver); |
| 115 | 0 | } finally { |
| 116 | |
|
| 117 | 8 | } |
| 118 | 8 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
protected void resolve(COWFileObject fo) throws FileSystemException { |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
try { |
| 126 | 346 | ArrayList<String> names = new ArrayList<String>(); |
| 127 | 346 | FileSystemManager fsm = cfs.getFileSystemManager(); |
| 128 | 346 | FileObject root = cfs.getParentLayer(); |
| 129 | |
|
| 130 | 346 | addFile(fo); |
| 131 | 346 | addParentLink(fo, ""); |
| 132 | 346 | COWEntry entry = getCOWEntry(fo); |
| 133 | 346 | if (entry == null) return; |
| 134 | 333 | if (entry.isDeleted()) return; |
| 135 | 312 | String suffixName = CollectionUtil.join(names, "/"); |
| 136 | |
OUTER: |
| 137 | |
while (true) { |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
try { |
| 143 | |
do { |
| 144 | 861 | String linkTarget = entry.getSymlink(); |
| 145 | 861 | if (linkTarget != null) { |
| 146 | |
|
| 147 | |
|
| 148 | 8 | results.clear(); |
| 149 | 8 | rerun(linkTarget, suffixName); |
| 150 | |
|
| 151 | |
|
| 152 | |
} |
| 153 | 861 | Collection<COWFileObject> bases = entry.getBaseFiles(); |
| 154 | |
|
| 155 | 861 | if (bases != null) { |
| 156 | 861 | for (COWFileObject base: bases) { |
| 157 | 141 | COWFileObject newFile = base.resolveFile(suffixName); |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | 141 | resolve(newFile); |
| 163 | 141 | } |
| 164 | |
} |
| 165 | |
} while (false); |
| 166 | 0 | } catch (IOException e) { |
| 167 | 0 | e.printStackTrace(); |
| 168 | 861 | } |
| 169 | 861 | addParentLink(fo, suffixName); |
| 170 | 861 | if (fo.getName().getPath().equals("/")) break; |
| 171 | 549 | names.add(0, fo.getName().getBaseName()); |
| 172 | |
|
| 173 | 549 | fo = fo.getParent(); |
| 174 | 549 | entry = getCOWEntry(fo); |
| 175 | 549 | if (entry == null) break; |
| 176 | 549 | suffixName = CollectionUtil.join(names, "/"); |
| 177 | |
} |
| 178 | 312 | return; |
| 179 | 0 | } finally { |
| 180 | |
|
| 181 | |
} |
| 182 | |
} |
| 183 | |
} |
| 184 | |
|