Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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: 03-Feb-2007
16 */
17 package uk.me.parabola.imgfmt.sys;
18
19 import uk.me.parabola.log.Logger;
20
21 import java.nio.ByteBuffer;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 /**
27 * Holds block numbers for a file. It is part of the directory. For a file
28 * that needs more than one block several directory entries exist. Each of
29 * these has the header with the file name etc. in it, but the first one has
30 * extra flags and info.
31 *
32 * <p>What is important here is that only part of a full block is used to
33 * hold block numbers.
34 *
35 * <p>The entries are 512 bytes regardless of the block size.
36 *
37 * @author Steve Ratcliffe
38 */
39 class BlockTable {
40 private static final Logger log = Logger.getLogger(BlockTable.class);
41
42 // Offset of the block table in the directory entry block.
43 private static final int BLOCKS_TABLE_START = 0x20;
44 private static final int ENTRY_SIZE = 512;
45
46 private static final int TABLE_SIZE = (ENTRY_SIZE - BLOCKS_TABLE_START)/2;
47 //private final int tableSize;
48
49 private int curroff;
50 private final List<char[]> blocks;
51 private char[] currTable;
52
53 BlockTable() {
54 blocks = new ArrayList<>(200);
55 }
56
57 /**
58 * Write out the specified table to the given buffer.
59 *
60 * @param buf The buffer to write to.
61 * @param n The number of the block table to write out.
62 */
63 public void writeTable(ByteBuffer buf, int n) {
64 char[] cbuf = blocks.get(n);
65 log.debug("block with length", cbuf.length);
66 for (char c : cbuf) {
67 buf.putChar(c);
68 }
69 }
70
71 /**
72 * Read a block table from the given buffer. The table is added to the
73 * list.
74 * @param buf The buffer to read from.
75 */
76 public void readTable(ByteBuffer buf) {
77 buf.position(BLOCKS_TABLE_START);
78 buf.limit(ENTRY_SIZE);
79
80 char[] cbuf = newTable();
81 for (int i = 0; i < cbuf.length; i++) {
82 char c = buf.getChar();
83 cbuf[i] = c;
84 }
85 }
86
87 /**
88 * Add the given block number to this directory.
89 *
90 * @param n The block number to add.
91 */
92 public void addBlock(int n) {
93 char[] thisTable = currTable;
94 if (curroff >= TABLE_SIZE || currTable == null)
95 thisTable = newTable();
96
97 thisTable[curroff++] = (char) n;
98 }
99
100 /**
101 * Given a logical block number, return the physical block number.
102 *
103 * @param lblock The logical block number, ie with respect to the file.
104 * @return The physical block number in the file system.
105 */
106 public int physFromLogical(int lblock) {
107 int blockNum = lblock / TABLE_SIZE;
108 int offset = lblock - blockNum * TABLE_SIZE;
109 if (blockNum >= blocks.size())
110 return 0xffff;
111
112 char[] cbuf = blocks.get(blockNum);
113 return cbuf[offset];
114 }
115
116 /**
117 * Get the number of block tables. This is the number of blocks that
118 * will be used in the on disk directory structure.
119 *
120 * @return The number of blocks tables.
121 */
122 public int getNBlockTables() {
123 return blocks.size();
124 }
125
126 /**
127 * Allocate a new block to hold more directory block numbers.
128 *
129 * @return Array for more numbers.
130 */
131 private char[] newTable() {
132 char[] table = new char[TABLE_SIZE];
133 Arrays.fill(table, (char) 0xffff);
134
135 curroff = 0;
136 blocks.add(table);
137 currTable = table;
138
139 return table;
140 }
141 }