Debian Codegraph @

src/uk/me/parabola/imgfmt/sys/Dirent.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 30-Nov-2006
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 * An entry within a directory. This holds its name and a list
31 * of blocks that go to make up this file.
32 *
33 * A directory entry may take more than block in the file system.
34 *
35 * <p>All documentation seems to point to the block numbers having to be
36 * contiguous, but seems strange so I shall experiment.
37 *
38 * <p>Entries are in blocks of 512 bytes, regardless of the block size.
39 *
40 * @author Steve Ratcliffe
41 */
42 class Dirent implements DirectoryEntry {
43 protected static final Logger log = Logger.getLogger(Dirent.class);
44
45 // Constants.
46 static final int MAX_FILE_LEN = 8;
47 static final int MAX_EXT_LEN = 3;
48
49 // Offsets
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 // File names are a base+extension
58 private String name;
59 private String ext;
60
61 // The file size - always use getSize() rather than this field.
62 private int _size;
63
64 // This may be used when writing the file
65 private Sized sizeSource;
66
67 private final BlockManager blockManager;
68
69 // The block table holds all the blocks that belong to this file. The
70 // documentation suggests that block numbers are always contiguous.
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 * Write this entry out to disk. Note that these are 512 bytes, regardless
94 * of the block size.
95 *
96 * @param file The file to write to.
97 * @throws IOException If writing fails for any reason.
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 // Size is only present in the first part
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 // Write out the allocation of blocks for this entry.
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 * Get the file name.
134 *
135 * @return The file name.
136 */
137 public String getName() {
138 return name;
139 }
140
141 /**
142 * Get the file extension.
143 *
144 * @return The file extension.
145 */
146 public String getExt() {
147 return ext;
148 }
149
150 /**
151 * The full name is of the form 8+3 with a dot in between the name and
152 * extension. The full name is used as the index in the directory.
153 *
154 * @return The full name.
155 */
156 public String getFullName() {
157 return name + '.' + ext;
158 }
159
160 /**
161 * Read in the block numbers from the given buffer. If this is the first
162 * directory block for this file, then the size is set too.
163 *
164 * @param buf The data as read from the file.
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 * Get the file size.
182 *
183 * @return The size of the file in bytes.
184 */
185 public int getSize() {
186 return (int) sizeSource.getSize();
187 }
188
189 /**
190 * Calls to getSize() will use this to get the value.
191 */
192 public void setSizeSource(Sized sizeSource) {
193 this.sizeSource = sizeSource;
194 }
195
196 /**
197 * Set the file name. The name should be exactly eight characters long
198 * and it is truncated or left padded with zeros to make this true.
199 *
200 * @param name The file name.
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 * Set the file extension. Can't be longer than three characters.
219 * @param ext The file extension.
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 * The number of blocks that the header covers. The header includes
230 * the directory for the purposes of this routine.
231 *
232 * @return The total number of header basic blocks (blocks of 512 bytes).
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 * Add a block without increasing the size of the file.
246 *
247 * @param n The block number.
248 */
249 void addBlock(int n) {
250 blockTable.addBlock(n);
251 }
252
253 /**
254 * Set for the first directory entry that covers the header and directory
255 * itself.
256 *
257 * @param special Set to true to mark as the special first entry.
258 */
259 public void setSpecial(boolean special) {
260 this.special = special;
261 }
262
263 public boolean isSpecial() {
264 return special;
265 }
266
267 /**
268 * Converts from a logical block to a physical block. If the block does
269 * not exist then 0xffff will be returned.
270 *
271 * @param lblock The logical block in the file.
272 * @return The corresponding physical block in the filesystem.
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 }