Debian Codegraph @

src/uk/me/parabola/imgfmt/sys/Directory.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: 26-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 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 * The directory. There is only one directory and it contains the
36 * file names and block information. On disk each entry is a
37 * multiple of the block size.
38 *
39 * @author Steve Ratcliffe
40 */
41 class Directory {
42 private static final Logger log = Logger.getLogger(Directory.class);
43
44 //private final FileChannel file;
45 private ImgChannel chan;
46
47 private final BlockManager headerBlockManager;
48 private long startPos;
49
50 // The list of files themselves.
51 private final Map<String, DirectoryEntry> entries = new LinkedHashMap<>();
52
53 Directory(BlockManager headerBlockManager) {
54 this.headerBlockManager = headerBlockManager;
55 }
56
57 /**
58 * Create a new file in the directory.
59 *
60 * @param name The file name. Must be 8+3 characters.
61 * @param blockManager To allocate blocks for the created file entry.
62 * @return The new directory entity.
63 * @throws FileExistsException If the entry already
64 * exists.
65 */
66 Dirent create(String name, BlockManager blockManager) throws FileExistsException {
67
68 // Check to see if it is already there.
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 * Initialise the directory for reading the file. The whole directory
85 * is read in.
86 *
87 * @throws IOException If it cannot be read.
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 * Write out the directory to the file. The file should be correctly
134 * positioned by the caller.
135 *
136 * @throws IOException If there is a problem writing out any
137 * of the directory entries.
138 */
139 public void sync() throws IOException {
140
141 // Save the current position
142 long dirPosition = chan.position();
143
144 // Skip the header/directory entry for now
145 chan.position(dirPosition + DirectoryEntry.ENTRY_SIZE);
146
147 // Write the blocks that will will contain the header blocks.
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 // Now go back and write in the directory entry for the header.
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 * Get the entries. Used for listing the directory.
167 *
168 * @return A list of the directory entries. They will be in the same
169 * order as in the file.
170 */
171 public List<DirectoryEntry> getEntries() {
172 return new ArrayList<>(entries.values());
173 }
174
175 /**
176 * Add an entry to the directory.
177 *
178 * @param ent The entry to add.
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 }