added support for extracting micro logs

This commit is contained in:
nvt-se 2009-08-10 12:51:52 +00:00
parent 8acea82207
commit 9d030cad2c
7 changed files with 179 additions and 57 deletions

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: CoffeeConfiguration.java,v 1.1 2009/08/04 10:36:53 nvt-se Exp $ * $Id: CoffeeConfiguration.java,v 1.2 2009/08/10 12:51:52 nvt-se Exp $
* *
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: CoffeeFS.java,v 1.3 2009/08/04 15:19:08 nvt-se Exp $ * $Id: CoffeeFS.java,v 1.4 2009/08/10 12:51:52 nvt-se Exp $
* *
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
@ -67,7 +67,7 @@ public class CoffeeFS {
while(currentPage < (conf.fsSize / conf.pageSize)) { while(currentPage < (conf.fsSize / conf.pageSize)) {
CoffeeHeader header = readHeader(currentPage); CoffeeHeader header = readHeader(currentPage);
if(header.isActive()) { if (header.isActive() && !header.isLog()) {
CoffeeFile file = new CoffeeFile(this, header); CoffeeFile file = new CoffeeFile(this, header);
files.put(file.getName(), file); files.put(file.getName(), file);
} }
@ -111,7 +111,7 @@ public class CoffeeFS {
return conf; return conf;
} }
private CoffeeHeader readHeader(int page) throws IOException { public CoffeeHeader readHeader(int page) throws IOException {
byte[] bytes = new byte[conf.NAME_LENGTH + conf.pageTypeSize * 2 + 6]; byte[] bytes = new byte[conf.NAME_LENGTH + conf.pageTypeSize * 2 + 6];
int index = 0; int index = 0;
@ -121,7 +121,7 @@ public class CoffeeFS {
return header; return header;
} }
private void writeHeader(CoffeeHeader header) throws IOException { public void writeHeader(CoffeeHeader header) throws IOException {
byte[] bytes = header.toRawHeader(); byte[] bytes = header.toRawHeader();
image.write(bytes, bytes.length, header.getPage() * conf.pageSize); image.write(bytes, bytes.length, header.getPage() * conf.pageSize);

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: CoffeeFile.java,v 1.2 2009/08/04 10:39:13 nvt-se Exp $ * $Id: CoffeeFile.java,v 1.3 2009/08/10 12:51:52 nvt-se Exp $
* *
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
@ -39,21 +39,27 @@ package se.sics.coffee;
import java.io.*; import java.io.*;
public class CoffeeFile { public class CoffeeFile {
private CoffeeFS coffeeFS; protected CoffeeFS coffeeFS;
private CoffeeHeader header; protected CoffeeHeader header;
private String name; private String name;
private int length; private int length;
private int startPage; private int startPage;
private int reservedSize; private int reservedSize;
private CoffeeFile logFile; private CoffeeMicroLog microLog;
private boolean knownLength; private boolean knownLength;
public CoffeeFile(CoffeeFS coffeeFS, CoffeeHeader header) { public CoffeeFile(CoffeeFS coffeeFS, CoffeeHeader header) throws IOException {
this.coffeeFS = coffeeFS; this.coffeeFS = coffeeFS;
this.header = header; this.header = header;
name = header.name; name = header.name;
startPage = header.getPage(); startPage = header.getPage();
reservedSize = header.maxPages * coffeeFS.getConfiguration().pageSize; reservedSize = header.maxPages * coffeeFS.getConfiguration().pageSize;
if (header.isModified() &&
coffeeFS.getConfiguration().useMicroLogs == true) {
microLog = new CoffeeMicroLog(coffeeFS, coffeeFS.readHeader(header.logPage));
} else {
microLog = null;
}
} }
private int calculateLength() throws IOException { private int calculateLength() throws IOException {
@ -89,17 +95,30 @@ public class CoffeeFile {
} }
public void saveContents(String filename) throws IOException { public void saveContents(String filename) throws IOException {
byte[] bytes = new byte[1];
int startOffset = header.getPage() * int startOffset = header.getPage() *
coffeeFS.getConfiguration().pageSize + coffeeFS.getConfiguration().pageSize +
header.rawLength(); header.rawLength();
int i; int i;
byte[] bytes;
FileOutputStream fOut = new FileOutputStream(filename); FileOutputStream fOut = new FileOutputStream(filename);
if(microLog != null) {
for(i = 0; i < microLog.getLogRecords(); i++) {
bytes = microLog.getRegion(i);
if(bytes == null) {
bytes = new byte[1];
coffeeFS.getImage().read(bytes, 1, i * microLog.getLogRecordSize());
}
fOut.write(bytes);
}
} else {
bytes = new byte[1];
for (i = 0; i < getLength(); i++) { for (i = 0; i < getLength(); i++) {
coffeeFS.getImage().read(bytes, 1, startOffset + i); coffeeFS.getImage().read(bytes, 1, startOffset + i);
fOut.write(bytes); fOut.write(bytes);
} }
}
fOut.close(); fOut.close();
} }

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: CoffeeHeader.java,v 1.1 2009/08/04 10:36:53 nvt-se Exp $ * $Id: CoffeeHeader.java,v 1.2 2009/08/10 12:51:52 nvt-se Exp $
* *
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: CoffeeImageFile.java,v 1.1 2009/08/04 10:36:53 nvt-se Exp $ * $Id: CoffeeImageFile.java,v 1.2 2009/08/10 12:51:52 nvt-se Exp $
* *
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: CoffeeManager.java,v 1.2 2009/08/04 15:19:08 nvt-se Exp $ * $Id: CoffeeManager.java,v 1.3 2009/08/10 12:51:52 nvt-se Exp $
* *
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *

View file

@ -0,0 +1,103 @@
/*
* 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.
*
* $Id: CoffeeMicroLog.java,v 1.1 2009/08/10 12:51:52 nvt-se Exp $
*
* @author Nicolas Tsiftes
*
*/
package se.sics.coffee;
import java.io.IOException;
public class CoffeeMicroLog extends CoffeeFile {
private int logRecords;
private int logRecordSize;
private int indexStart;
private int indexSize;
private int recordStart;
private int[] index;
public CoffeeMicroLog(CoffeeFS fs, CoffeeHeader header)
throws IOException {
super(fs, header);
CoffeeConfiguration conf = fs.getConfiguration();
if (header.logRecordSize == 0) {
logRecordSize = conf.pageSize;
}
int logAreaSize;
if (header.logRecords == 0) {
logRecords = conf.defaultLogSize / logRecordSize;
} else {
logRecords = header.logRecords;
}
indexStart = header.getPage() * conf.pageSize +
header.rawLength();
/* An index entry uses two bytes. */
indexSize = logRecords * 2;
recordStart = indexStart + indexSize;
index = new int[logRecords];
byte[] bytes = new byte[2];
for (int i = 0; i < logRecords; i++) {
coffeeFS.getImage().read(bytes, bytes.length,
indexStart + i * 2);
index[i] = bytes[1] << 8 | bytes[0];
}
}
public byte[] getRegion(int region) throws IOException {
int headerSize = header.rawLength();
int indexSize = logRecords * 2;
for(int i = logRecords - 1; i >= 0; i--) {
if(index[i] - 1 == region) {
byte[] bytes = new byte[logRecordSize];
coffeeFS.getImage().read(bytes, bytes.length,
recordStart + i * logRecordSize);
return bytes;
}
}
return null;
}
public int getLogRecords() {
return logRecords;
}
public int getLogRecordSize() {
return logRecordSize;
}
}