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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2009-08-10 14:51:52 +02:00
|
|
|
* $Id: CoffeeFS.java,v 1.4 2009/08/10 12:51:52 nvt-se Exp $
|
2009-08-04 12:36:53 +02:00
|
|
|
*
|
|
|
|
* @author Nicolas Tsiftes
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package se.sics.coffee;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
class CoffeeException extends RuntimeException {
|
|
|
|
public CoffeeException(String message) {
|
|
|
|
super("Coffee error: " + message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CoffeeFileException extends RuntimeException {
|
|
|
|
public CoffeeFileException(String message) {
|
|
|
|
super("Coffee file error: " + message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
int headerSize = conf.NAME_LENGTH + conf.pageTypeSize * 2 + 6;
|
|
|
|
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 {
|
2009-08-04 12:36:53 +02:00
|
|
|
byte[] bytes = new byte[conf.NAME_LENGTH + conf.pageTypeSize * 2 + 6];
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CoffeeFile insertFile(String filename) throws IOException {
|
|
|
|
CoffeeFile coffeeFile;
|
|
|
|
try {
|
|
|
|
File file = new File(filename);
|
|
|
|
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);
|
|
|
|
header.setName(filename);
|
|
|
|
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 {
|
|
|
|
CoffeeFile file = files.get(filename);
|
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-04 17:19:07 +02:00
|
|
|
file.saveContents(filename);
|
2009-08-04 12:36:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|