| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.sys; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.nio.ByteBuffer; |
| 21 |
import java.nio.ByteOrder; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.LinkedHashMap; |
| 24 |
import java.util.List; |
| 25 |
import java.util.Map; |
| 26 |
import java.util.Objects; |
| 27 |
|
| 28 |
import uk.me.parabola.imgfmt.FileExistsException; |
| 29 |
import uk.me.parabola.imgfmt.Utils; |
| 30 |
import uk.me.parabola.imgfmt.fs.DirectoryEntry; |
| 31 |
import uk.me.parabola.imgfmt.fs.ImgChannel; |
| 32 |
import uk.me.parabola.log.Logger; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class Directory { |
| 42 |
private static final Logger log = Logger.getLogger(Directory.class); |
| 43 |
|
| 44 |
|
| 45 |
private ImgChannel chan; |
| 46 |
|
| 47 |
private final BlockManager headerBlockManager; |
| 48 |
private long startPos; |
| 49 |
|
| 50 |
|
| 51 |
private final Map<String, DirectoryEntry> entries = new LinkedHashMap<>(); |
| 52 |
|
| 53 |
Directory(BlockManager headerBlockManager) { |
| 54 |
this.headerBlockManager = headerBlockManager; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
Dirent create(String name, BlockManager blockManager) throws FileExistsException { |
| 67 |
|
| 68 |
|
| 69 |
if (entries.get(name) != null) |
| 70 |
throw new FileExistsException("File " + name + " already exists"); |
| 71 |
|
| 72 |
Dirent ent; |
| 73 |
if (Objects.equals(name, ImgFS.DIRECTORY_FILE_NAME)) { |
| 74 |
ent = new HeaderDirent(name, blockManager); |
| 75 |
} else { |
| 76 |
ent = new Dirent(name, blockManager); |
| 77 |
} |
| 78 |
addEntry(ent); |
| 79 |
|
| 80 |
return ent; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
void readInit(byte xorByte) throws IOException { |
| 90 |
assert chan != null; |
| 91 |
|
| 92 |
ByteBuffer buf = ByteBuffer.allocate(512); |
| 93 |
buf.order(ByteOrder.LITTLE_ENDIAN); |
| 94 |
|
| 95 |
chan.position(startPos); |
| 96 |
Dirent current = null; |
| 97 |
while (chan.read(buf) > 0) { |
| 98 |
buf.flip(); |
| 99 |
if(xorByte != 0) { |
| 100 |
byte[] bufBytes = buf.array(); |
| 101 |
for(int i = 0; i < bufBytes.length; ++i) |
| 102 |
bufBytes[i] ^= xorByte; |
| 103 |
} |
| 104 |
|
| 105 |
int used = buf.get(Dirent.OFF_FILE_USED); |
| 106 |
if (used != 1) |
| 107 |
continue; |
| 108 |
|
| 109 |
String name = Utils.bytesToString(buf, Dirent.OFF_NAME, Dirent.MAX_FILE_LEN); |
| 110 |
String ext = Utils.bytesToString(buf, Dirent.OFF_EXT, Dirent.MAX_EXT_LEN); |
| 111 |
|
| 112 |
log.debug("readinit name", name, ext); |
| 113 |
|
| 114 |
int flag = buf.get(Dirent.OFF_FLAG); |
| 115 |
int part = buf.get(Dirent.OFF_FILE_PART) & 0xff; |
| 116 |
|
| 117 |
if (flag == 3 && current == null) { |
| 118 |
current = (Dirent) entries.get(ImgFS.DIRECTORY_FILE_NAME); |
| 119 |
current.initBlocks(buf); |
| 120 |
} else if (part == 0) { |
| 121 |
current = create(name + '.' + ext, headerBlockManager); |
| 122 |
current.initBlocks(buf); |
| 123 |
} else { |
| 124 |
assert current != null; |
| 125 |
current.initBlocks(buf); |
| 126 |
} |
| 127 |
buf.clear(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public void sync() throws IOException { |
| 140 |
|
| 141 |
|
| 142 |
long dirPosition = chan.position(); |
| 143 |
|
| 144 |
|
| 145 |
chan.position(dirPosition + DirectoryEntry.ENTRY_SIZE); |
| 146 |
|
| 147 |
|
| 148 |
for (DirectoryEntry dir : entries.values()) { |
| 149 |
Dirent ent = (Dirent) dir; |
| 150 |
|
| 151 |
if (!ent.isSpecial()) { |
| 152 |
log.debug("writing", dir.getFullName(), "at", chan.position()); |
| 153 |
log.debug("ent size", ent.getSize()); |
| 154 |
ent.sync(chan); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
chan.position(dirPosition); |
| 160 |
Dirent ent = (Dirent) entries.values().iterator().next(); |
| 161 |
log.debug("ent header size", ent.getSize()); |
| 162 |
ent.sync(chan); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public List<DirectoryEntry> getEntries() { |
| 172 |
return new ArrayList<>(entries.values()); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private void addEntry(DirectoryEntry ent) { |
| 181 |
entries.put(ent.getFullName(), ent); |
| 182 |
} |
| 183 |
|
| 184 |
public void setFile(ImgChannel chan) { |
| 185 |
this.chan = chan; |
| 186 |
} |
| 187 |
|
| 188 |
public void setStartPos(long startPos) { |
| 189 |
this.startPos = startPos; |
| 190 |
} |
| 191 |
|
| 192 |
public DirectoryEntry lookup(String name) { |
| 193 |
return entries.get(name); |
| 194 |
} |
| 195 |
} |