| 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 |
|
| 23 |
import uk.me.parabola.imgfmt.Sized; |
| 24 |
import uk.me.parabola.imgfmt.Utils; |
| 25 |
import uk.me.parabola.imgfmt.fs.DirectoryEntry; |
| 26 |
import uk.me.parabola.imgfmt.fs.ImgChannel; |
| 27 |
import uk.me.parabola.log.Logger; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class Dirent implements DirectoryEntry { |
| 43 |
protected static final Logger log = Logger.getLogger(Dirent.class); |
| 44 |
|
| 45 |
|
| 46 |
static final int MAX_FILE_LEN = 8; |
| 47 |
static final int MAX_EXT_LEN = 3; |
| 48 |
|
| 49 |
|
| 50 |
static final int OFF_FILE_USED = 0x00; |
| 51 |
static final int OFF_NAME = 0x01; |
| 52 |
static final int OFF_EXT = 0x09; |
| 53 |
static final int OFF_FLAG = 0x10; |
| 54 |
static final int OFF_FILE_PART = 0x11; |
| 55 |
private static final int OFF_SIZE = 0x0c; |
| 56 |
|
| 57 |
|
| 58 |
private String name; |
| 59 |
private String ext; |
| 60 |
|
| 61 |
|
| 62 |
private int _size; |
| 63 |
|
| 64 |
|
| 65 |
private Sized sizeSource; |
| 66 |
|
| 67 |
private final BlockManager blockManager; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private final BlockTable blockTable; |
| 72 |
|
| 73 |
private boolean special; |
| 74 |
private static final int OFF_USED_FLAG = 0; |
| 75 |
private boolean initialized; |
| 76 |
|
| 77 |
Dirent(String name, BlockManager blockManager) { |
| 78 |
this.blockManager = blockManager; |
| 79 |
|
| 80 |
int dot = name.lastIndexOf('.'); |
| 81 |
if (dot >= 0) { |
| 82 |
setName(name.substring(0, dot)); |
| 83 |
setExt(name.substring(dot+1)); |
| 84 |
} else |
| 85 |
throw new IllegalArgumentException("Filename did not have dot"); |
| 86 |
|
| 87 |
blockTable = new BlockTable(); |
| 88 |
|
| 89 |
sizeSource = () -> _size; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void sync(ImgChannel file) throws IOException { |
| 100 |
int ntables = blockTable.getNBlockTables(); |
| 101 |
ByteBuffer buf = ByteBuffer.allocate(ENTRY_SIZE * ntables); |
| 102 |
buf.order(ByteOrder.LITTLE_ENDIAN); |
| 103 |
|
| 104 |
for (int part = 0; part < ntables; part++) { |
| 105 |
log.debug("position at part", part, "is", buf.position()); |
| 106 |
|
| 107 |
buf.put((byte) 1); |
| 108 |
|
| 109 |
buf.put(Utils.toBytes(name, MAX_FILE_LEN, (byte) ' ')); |
| 110 |
buf.put(Utils.toBytes(ext, MAX_EXT_LEN, (byte) ' ')); |
| 111 |
|
| 112 |
|
| 113 |
if (part == 0) { |
| 114 |
log.debug("dirent", name, '.', ext, "size is going to", getSize()); |
| 115 |
buf.putInt(getSize()); |
| 116 |
} else { |
| 117 |
buf.putInt(0); |
| 118 |
} |
| 119 |
|
| 120 |
buf.put((byte) (special? 0x3: 0)); |
| 121 |
buf.putChar((char) part); |
| 122 |
|
| 123 |
|
| 124 |
buf.position(ENTRY_SIZE * part + 0x20); |
| 125 |
blockTable.writeTable(buf, part); |
| 126 |
} |
| 127 |
|
| 128 |
buf.flip(); |
| 129 |
file.write(buf); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public String getName() { |
| 138 |
return name; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public String getExt() { |
| 147 |
return ext; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public String getFullName() { |
| 157 |
return name + '.' + ext; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
void initBlocks(ByteBuffer buf) { |
| 167 |
|
| 168 |
byte used = buf.get(OFF_USED_FLAG); |
| 169 |
if (used != 1) |
| 170 |
return; |
| 171 |
|
| 172 |
int part = buf.get(OFF_FILE_PART) & 0xff; |
| 173 |
if (part == 0 || (isSpecial() && part == 3)) |
| 174 |
_size = buf.getInt(OFF_SIZE); |
| 175 |
|
| 176 |
blockTable.readTable(buf); |
| 177 |
initialized = true; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public int getSize() { |
| 186 |
return (int) sizeSource.getSize(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public void setSizeSource(Sized sizeSource) { |
| 193 |
this.sizeSource = sizeSource; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
private void setName(String name) { |
| 203 |
int len = name.length(); |
| 204 |
if (len > MAX_FILE_LEN) { |
| 205 |
this.name = name.substring(0, 8); |
| 206 |
} else if (len < MAX_FILE_LEN) { |
| 207 |
StringBuilder sb = new StringBuilder(); |
| 208 |
for (int i = 0; i < MAX_FILE_LEN - len; i++) { |
| 209 |
sb.append('0'); |
| 210 |
} |
| 211 |
sb.append(name); |
| 212 |
this.name = sb.toString(); |
| 213 |
} else |
| 214 |
this.name = name; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private void setExt(String ext) { |
| 222 |
log.debug("ext len", ext.length()); |
| 223 |
if (ext.length() != MAX_EXT_LEN) |
| 224 |
throw new IllegalArgumentException("File extension is wrong size"); |
| 225 |
this.ext = ext; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
int numberHeaderBlocks() { |
| 235 |
return blockTable.getNBlockTables(); |
| 236 |
} |
| 237 |
|
| 238 |
void setSize(int size) { |
| 239 |
if (log.isDebugEnabled()) |
| 240 |
log.debug("setting size", getName(), getExt(), "to", size); |
| 241 |
this._size = size; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void addBlock(int n) { |
| 250 |
blockTable.addBlock(n); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public void setSpecial(boolean special) { |
| 260 |
this.special = special; |
| 261 |
} |
| 262 |
|
| 263 |
public boolean isSpecial() { |
| 264 |
return special; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public int getPhysicalBlock(int lblock) { |
| 275 |
return blockTable.physFromLogical(lblock); |
| 276 |
} |
| 277 |
|
| 278 |
public BlockManager getBlockManager() { |
| 279 |
return blockManager; |
| 280 |
} |
| 281 |
|
| 282 |
protected void setInitialized(boolean initialized) { |
| 283 |
this.initialized = initialized; |
| 284 |
} |
| 285 |
|
| 286 |
protected boolean isInitialized() { |
| 287 |
return initialized; |
| 288 |
} |
| 289 |
} |