Debian Codegraph @

src/uk/me/parabola/imgfmt/sys/BlockManager.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: 25-Oct-2007
16 */
17 package uk.me.parabola.imgfmt.sys;
18
19 import uk.me.parabola.imgfmt.MapFailedException;
20 import uk.me.parabola.log.Logger;
21
22 /**
23 * This is used to allocate blocks for files in the filesystem/archive.
24 *
25 * @author Steve Ratcliffe
26 */
27 class BlockManager {
28 private static final Logger log = Logger.getLogger(BlockManager.class);
29
30 private int blockSize;
31
32 private int currentBlock;
33 private int maxBlock = 0xfffe;
34 private int maxBlockAllocated;
35 private final int initialBlock;
36
37 BlockManager(int blockSize, int initialBlock) {
38 this.blockSize = blockSize;
39 this.currentBlock = initialBlock;
40 this.initialBlock = initialBlock;
41 this.maxBlockAllocated = initialBlock;
42 }
43
44 /**
45 * Well the algorithm is pretty simple - you just get the next unused block
46 * number.
47 *
48 * @return A block number that is free to be used.
49 */
50 public int allocate() {
51 int n = currentBlock++;
52 if (maxBlock > 0 && n > maxBlock) {
53 log.error("overflowed directory with max block " + maxBlock + ", current=" + n);
54
55 // This problem is fixable so give some useful advice on what
56 // to do about it
57 String message = String.format("Too many blocks." +
58 " Use a larger block size with an option such as" +
59 " --block-size=%d or --block-size=%d",
60 blockSize * 2, blockSize * 4);
61 throw new MapFailedException(message);
62 }
63 maxBlockAllocated++;
64 return n;
65 }
66
67 public int getBlockSize() {
68 return blockSize;
69 }
70
71 public int getMaxBlock() {
72 return maxBlock;
73 }
74
75 public void setMaxBlock(int maxBlock) {
76 this.maxBlock = maxBlock;
77 }
78
79 public void setCurrentBlock(int n) {
80 if (maxBlockAllocated != initialBlock)
81 throw new IllegalStateException("Blocks already allocated");
82 currentBlock = n;
83 maxBlockAllocated = n;
84 }
85
86 public int getMaxBlockAllocated() {
87 return maxBlockAllocated;
88 }
89
90 public void setBlockSize(int blockSize) {
91 this.blockSize = blockSize;
92 }
93 }