2009-08-04 12:36:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @author Nicolas Tsiftes
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package se.sics.coffee;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
public class CoffeeFS {
|
|
|
|
private CoffeeImage image;
|
|
|
|
private CoffeeConfiguration conf;
|
|
|
|
private int currentPage;
|
|
|
|
private Map<String, CoffeeFile> files;
|
|
|
|
private static final int INVALID_PAGE = -1;
|
|
|
|
|
|
|
|
public CoffeeFS(CoffeeImage image) throws IOException {
|
|
|
|
this.image = image;
|
|
|
|
conf = image.getConfiguration();
|
|
|
|
currentPage = 0;
|
|
|
|
files = new TreeMap<String, CoffeeFile>();
|
|
|
|
|
|
|
|
while(currentPage < (conf.fsSize / conf.pageSize)) {
|
|
|
|
CoffeeHeader header = readHeader(currentPage);
|
2009-08-10 14:51:52 +02:00
|
|
|
if (header.isActive() && !header.isLog()) {
|
2009-08-04 12:36:53 +02:00
|
|
|
CoffeeFile file = new CoffeeFile(this, header);
|
|
|
|
files.put(file.getName(), file);
|
|
|
|
}
|
|
|
|
currentPage = nextFile(header, currentPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int pageCount(long size) {
|
2012-01-28 03:34:16 +01:00
|
|
|
int headerSize = conf.nameLength + conf.pageTypeSize * 2 + 6;
|
2009-08-04 12:36:53 +02:00
|
|
|
return (int)(size + headerSize + conf.pageSize - 1) / conf.pageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int findFreeExtent(int pages) throws IOException {
|
|
|
|
CoffeeHeader header;
|
|
|
|
int currentPage = 0;
|
|
|
|
int start = INVALID_PAGE;
|
|
|
|
|
|
|
|
while(currentPage < (conf.fsSize / conf.pageSize)) {
|
|
|
|
header = readHeader(currentPage);
|
2009-08-10 14:51:52 +02:00
|
|
|
if (header.isFree()) {
|
|
|
|
if (start == INVALID_PAGE) {
|
2009-08-04 12:36:53 +02:00
|
|
|
start = currentPage;
|
|
|
|
}
|
|
|
|
currentPage = nextFile(header, currentPage);
|
2009-08-10 14:51:52 +02:00
|
|
|
if (start + pages <= currentPage) {
|
2009-08-04 12:36:53 +02:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start = INVALID_PAGE;
|
|
|
|
currentPage = nextFile(header, currentPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return INVALID_PAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CoffeeImage getImage() {
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CoffeeConfiguration getConfiguration() {
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2009-08-10 14:51:52 +02:00
|
|
|
public CoffeeHeader readHeader(int page) throws IOException {
|
2012-01-28 03:34:16 +01:00
|
|
|
byte[] bytes = new byte[conf.nameLength + conf.pageTypeSize * 2 + 6];
|
2009-08-04 12:36:53 +02:00
|
|
|
|
|
|
|
image.read(bytes, bytes.length, page * conf.pageSize);
|
|
|
|
CoffeeHeader header = new CoffeeHeader(this, page, bytes);
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2009-08-10 14:51:52 +02:00
|
|
|
public void writeHeader(CoffeeHeader header) throws IOException {
|
2009-08-04 12:36:53 +02:00
|
|
|
byte[] bytes = header.toRawHeader();
|
|
|
|
|
|
|
|
image.write(bytes, bytes.length, header.getPage() * conf.pageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int nextFile(CoffeeHeader header, int page) {
|
2009-08-10 14:51:52 +02:00
|
|
|
if (header.isFree()) {
|
2009-08-04 12:36:53 +02:00
|
|
|
return page + conf.pagesPerSector & ~(conf.pagesPerSector - 1);
|
2009-08-10 14:51:52 +02:00
|
|
|
} else if (header.isIsolated()) {
|
2009-08-04 12:36:53 +02:00
|
|
|
return page + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return page + header.maxPages;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final Map<String, CoffeeFile> getFiles() {
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2009-08-11 19:03:59 +02:00
|
|
|
public CoffeeFile insertFile(String filename) throws IOException {
|
|
|
|
return insertFile(new File(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
public CoffeeFile insertFile(File file) throws IOException {
|
2009-08-04 12:36:53 +02:00
|
|
|
CoffeeFile coffeeFile;
|
|
|
|
try {
|
|
|
|
FileInputStream input = new FileInputStream(file);
|
|
|
|
int allocatePages = pageCount(file.length());
|
|
|
|
int start = findFreeExtent(allocatePages);
|
2009-08-04 12:39:13 +02:00
|
|
|
|
2009-08-10 14:51:52 +02:00
|
|
|
if (start == INVALID_PAGE) {
|
2009-08-04 12:36:53 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
CoffeeHeader header = new CoffeeHeader(this, start);
|
2009-08-11 19:03:59 +02:00
|
|
|
header.setName(file.getName());
|
2009-08-04 12:36:53 +02:00
|
|
|
header.setReservedSize(allocatePages);
|
|
|
|
header.allocate();
|
|
|
|
coffeeFile = new CoffeeFile(this, header);
|
|
|
|
writeHeader(header);
|
|
|
|
coffeeFile.insertContents(input);
|
|
|
|
input.close();
|
|
|
|
return coffeeFile;
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeFile(String filename)
|
|
|
|
throws CoffeeFileException, IOException {
|
|
|
|
CoffeeFile file = files.get(filename);
|
2009-08-10 14:51:52 +02:00
|
|
|
if (file == null) {
|
2009-08-04 12:36:53 +02:00
|
|
|
throw new CoffeeFileException("Coffee: attempt to remove inexistent file");
|
|
|
|
}
|
|
|
|
|
|
|
|
file.remove();
|
|
|
|
writeHeader(file.getHeader());
|
|
|
|
files.remove(file.getName());
|
|
|
|
}
|
|
|
|
|
2009-08-04 17:19:07 +02:00
|
|
|
public boolean extractFile(String filename) throws IOException {
|
2009-08-11 19:03:59 +02:00
|
|
|
return extractFile(filename, new File(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean extractFile(String fileInCoffee, File fileOnDisk) throws IOException {
|
|
|
|
CoffeeFile file = files.get(fileInCoffee);
|
2009-08-04 12:36:53 +02:00
|
|
|
|
2009-08-10 14:51:52 +02:00
|
|
|
if (file == null) {
|
2009-08-04 12:36:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-11 19:03:59 +02:00
|
|
|
file.saveContents(fileOnDisk);
|
2009-08-04 12:36:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-08-11 19:03:59 +02:00
|
|
|
|
2012-01-28 03:34:16 +01:00
|
|
|
public static class CoffeeException extends RuntimeException {
|
|
|
|
private static final long serialVersionUID = 1146474084441011154L;
|
|
|
|
|
|
|
|
public CoffeeException(String message) {
|
|
|
|
super("Coffee error: " + message);
|
|
|
|
}
|
2009-08-11 19:03:59 +02:00
|
|
|
}
|
|
|
|
|
2012-01-28 03:34:16 +01:00
|
|
|
public static class CoffeeFileException extends RuntimeException {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -2954553141887245203L;
|
|
|
|
|
|
|
|
public CoffeeFileException(String message) {
|
|
|
|
super("Coffee file error: " + message);
|
|
|
|
}
|
2009-08-11 19:03:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|