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

View File

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

View File

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