Minor improvements of coffee-manager.

This commit is contained in:
Niclas Finne 2012-11-19 14:47:35 +01:00 committed by Nicolas Tsiftes
parent c23bb4cc5c
commit 2f72cb043d
3 changed files with 32 additions and 35 deletions

View File

@ -37,8 +37,8 @@ import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
public class CoffeeFS { public class CoffeeFS {
private CoffeeImage image; private final CoffeeImage image;
private CoffeeConfiguration conf; private final CoffeeConfiguration conf;
private int currentPage; private int currentPage;
private Map<String, CoffeeFile> files; private Map<String, CoffeeFile> files;
private static final int INVALID_PAGE = -1; private static final int INVALID_PAGE = -1;
@ -129,27 +129,24 @@ public class CoffeeFS {
} }
public CoffeeFile insertFile(File file) throws IOException { public CoffeeFile insertFile(File file) throws IOException {
CoffeeFile coffeeFile; CoffeeFile coffeeFile;
try { FileInputStream input = new FileInputStream(file);
FileInputStream input = new FileInputStream(file); int allocatePages = pageCount(file.length());
int allocatePages = pageCount(file.length()); int start = findFreeExtent(allocatePages);
int start = findFreeExtent(allocatePages);
if (start == INVALID_PAGE) { if (start == INVALID_PAGE) {
return null; input.close();
} return null;
CoffeeHeader header = new CoffeeHeader(this, start); }
header.setName(file.getName()); CoffeeHeader header = new CoffeeHeader(this, start);
header.setReservedSize(allocatePages); header.setName(file.getName());
header.allocate(); header.setReservedSize(allocatePages);
coffeeFile = new CoffeeFile(this, header); header.allocate();
writeHeader(header); coffeeFile = new CoffeeFile(this, header);
coffeeFile.insertContents(input); writeHeader(header);
input.close(); coffeeFile.insertContents(input);
return coffeeFile; input.close();
} catch (FileNotFoundException e) { return coffeeFile;
}
return null;
} }
public void removeFile(String filename) public void removeFile(String filename)

View File

@ -32,38 +32,40 @@
package se.sics.coffee; package se.sics.coffee;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
public class CoffeeImageFile implements CoffeeImage { public class CoffeeImageFile implements CoffeeImage {
private RandomAccessFile imageFile; private final RandomAccessFile imageFile;
private CoffeeConfiguration conf; private final CoffeeConfiguration conf;
public CoffeeImageFile(String filename, CoffeeConfiguration conf) throws IOException { public CoffeeImageFile(String filename, CoffeeConfiguration conf) throws IOException {
this.conf = conf; this.conf = conf;
File file = new File(filename); imageFile = new RandomAccessFile(filename, "rw");
imageFile = new RandomAccessFile(file, "rw");
if (imageFile.length() == 0) { if (imageFile.length() == 0) {
// Allocate a full file system image. // Allocate a full file system image.
imageFile.setLength(conf.fsSize); imageFile.setLength(conf.fsSize);
} }
} }
@Override
public CoffeeConfiguration getConfiguration() { public CoffeeConfiguration getConfiguration() {
return conf; return conf;
} }
@Override
public void read(byte[] bytes, int size, int offset) throws IOException { public void read(byte[] bytes, int size, int offset) throws IOException {
imageFile.seek(conf.startOffset + offset); imageFile.seek(conf.startOffset + offset);
imageFile.read(bytes, 0, size); imageFile.read(bytes, 0, size);
} }
@Override
public void write(byte[] bytes, int size, int offset) throws IOException { public void write(byte[] bytes, int size, int offset) throws IOException {
imageFile.seek(conf.startOffset + offset); imageFile.seek(conf.startOffset + offset);
imageFile.write(bytes, 0, size); imageFile.write(bytes, 0, size);
} }
@Override
public void erase(int size, int offset) throws IOException { public void erase(int size, int offset) throws IOException {
byte[] bytes = new byte[256]; byte[] bytes = new byte[256];
int chunkSize; int chunkSize;

View File

@ -40,20 +40,18 @@ import se.sics.coffee.CoffeeFS.CoffeeException;
import se.sics.coffee.CoffeeFS.CoffeeFileException; import se.sics.coffee.CoffeeFS.CoffeeFileException;
public class CoffeeManager { public class CoffeeManager {
private static CoffeeFS coffeeFS;
public enum Command { INSERT, EXTRACT, REMOVE, LIST, STATS }; public enum Command { INSERT, EXTRACT, REMOVE, LIST, STATS };
public static void main(String args[]) { public static void main(String args[]) {
String platform = "sky"; String platform = "sky";
String usage = "Usage: java -jar coffee.jar ";
Command command = Command.STATS; Command command = Command.STATS;
String filename = ""; String filename = "";
String fsImage = ""; String fsImage = "";
String usage = "Usage: java -jar coffee.jar " +
usage += "[-p <hardware platform>] "; "[-p <hardware platform>] " +
usage += "[-i|e|r <file>] "; "[-i|e|r <file>] " +
usage += "[-l|s] "; "[-l|s] " +
usage += "<file system image>"; "<file system image>";
if (args.length < 2) { if (args.length < 2) {
System.err.println(usage); System.err.println(usage);
@ -98,7 +96,7 @@ public class CoffeeManager {
try { try {
CoffeeConfiguration conf = new CoffeeConfiguration(platform + ".properties"); CoffeeConfiguration conf = new CoffeeConfiguration(platform + ".properties");
coffeeFS = new CoffeeFS(new CoffeeImageFile(fsImage, conf)); CoffeeFS coffeeFS = new CoffeeFS(new CoffeeImageFile(fsImage, conf));
switch (command) { switch (command) {
case INSERT: case INSERT:
if (coffeeFS.getFiles().get(filename) != null) { if (coffeeFS.getFiles().get(filename) != null) {