Skip to content

Commit

Permalink
Minor improvements of coffee-manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
nfi authored and Nicolas Tsiftes committed Nov 20, 2012
1 parent c23bb4c commit 2f72cb0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
43 changes: 20 additions & 23 deletions tools/coffee-manager/se/sics/coffee/CoffeeFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
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;
Expand Down Expand Up @@ -129,27 +129,24 @@ public CoffeeFile insertFile(String filename) throws IOException {
}

public CoffeeFile insertFile(File file) throws IOException {
CoffeeFile coffeeFile;
try {
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;
CoffeeFile coffeeFile;
FileInputStream input = new FileInputStream(file);
int allocatePages = pageCount(file.length());
int start = findFreeExtent(allocatePages);

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)
Expand Down
12 changes: 7 additions & 5 deletions tools/coffee-manager/se/sics/coffee/CoffeeImageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 6 additions & 8 deletions tools/coffee-manager/se/sics/coffee/CoffeeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,18 @@
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);
Expand Down Expand Up @@ -98,7 +96,7 @@ public static void main(String args[]) {

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) {
Expand Down

0 comments on commit 2f72cb0

Please sign in to comment.