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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-11-19 12:17:37 +01:00
|
|
|
package org.contikios.coffee;
|
2009-08-04 12:36:53 +02:00
|
|
|
|
|
|
|
class CoffeeHeader {
|
2012-01-28 03:34:16 +01:00
|
|
|
private final CoffeeConfiguration conf;
|
|
|
|
private final int page;
|
2009-08-04 12:36:53 +02:00
|
|
|
|
|
|
|
private static final int HDR_FLAG_VALID = 0x1;
|
|
|
|
private static final int HDR_FLAG_ALLOCATED = 0x2;
|
|
|
|
private static final int HDR_FLAG_OBSOLETE = 0x4;
|
|
|
|
private static final int HDR_FLAG_MODIFIED = 0x8;
|
|
|
|
private static final int HDR_FLAG_LOG = 0x10;
|
|
|
|
private static final int HDR_FLAG_ISOLATED = 0x20;
|
|
|
|
|
|
|
|
int logPage;
|
|
|
|
int logRecords;
|
|
|
|
int logRecordSize;
|
|
|
|
int maxPages;
|
|
|
|
String name;
|
|
|
|
|
2012-01-28 03:34:16 +01:00
|
|
|
private int flags;
|
2009-08-04 12:36:53 +02:00
|
|
|
|
|
|
|
public CoffeeHeader(CoffeeFS coffeeFS, int page) {
|
|
|
|
this.page = page;
|
|
|
|
conf = coffeeFS.getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CoffeeHeader(CoffeeFS coffeeFS, int page, byte[] bytes) {
|
2012-01-28 03:34:16 +01:00
|
|
|
this(coffeeFS, page);
|
2009-08-04 12:36:53 +02:00
|
|
|
|
|
|
|
processRawHeader(bytes);
|
|
|
|
}
|
|
|
|
|
2012-01-28 03:34:16 +01:00
|
|
|
private int getInt(byte[] bytes, int index) {
|
|
|
|
return (bytes[index] & 0xff) + ((bytes[index + 1] & 0xff) << 8);
|
|
|
|
}
|
|
|
|
|
2009-08-04 12:36:53 +02:00
|
|
|
private void processRawHeader(byte[] bytes) {
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
logPage = getPageValue(bytes, 0);
|
|
|
|
index += conf.pageTypeSize;
|
|
|
|
|
2012-01-28 03:34:16 +01:00
|
|
|
logRecords = getInt(bytes, index);
|
2009-08-04 12:36:53 +02:00
|
|
|
index += 2;
|
|
|
|
|
2012-01-28 03:34:16 +01:00
|
|
|
logRecordSize = getInt(bytes, index);
|
2009-08-04 12:36:53 +02:00
|
|
|
index += 2;
|
|
|
|
|
|
|
|
maxPages = getPageValue(bytes, index);
|
|
|
|
index += conf.pageTypeSize;
|
|
|
|
|
|
|
|
index++; // Skip deprecated EOF hint field.
|
|
|
|
|
2012-01-28 03:34:16 +01:00
|
|
|
flags = bytes[index] & 0xff;
|
2009-08-04 12:36:53 +02:00
|
|
|
index++;
|
|
|
|
|
|
|
|
name = new String(bytes).substring(index,
|
2012-01-28 03:34:16 +01:00
|
|
|
index + conf.nameLength);
|
2009-08-04 12:36:53 +02:00
|
|
|
int nullCharOffset = name.indexOf(0);
|
2009-08-10 14:51:52 +02:00
|
|
|
if (nullCharOffset >= 0) {
|
2009-08-04 12:36:53 +02:00
|
|
|
name = name.substring(0, nullCharOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private byte[] setPageValue(int page) {
|
|
|
|
byte[] bytes = new byte[conf.pageTypeSize];
|
|
|
|
|
2009-08-10 14:51:52 +02:00
|
|
|
for (int i = conf.pageTypeSize - 1; i >= 0; i--) {
|
2009-08-04 12:36:53 +02:00
|
|
|
bytes[i] = (byte) (page >> (8 * i));
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getPageValue(byte[] bytes, int offset) {
|
|
|
|
int page = 0;
|
2009-08-10 14:51:52 +02:00
|
|
|
for (int i = 0; i < conf.pageTypeSize; i++) {
|
2012-01-28 03:34:16 +01:00
|
|
|
page |= (bytes[offset + i] & 0xff) << (8 * i);
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] toRawHeader() {
|
|
|
|
byte[] bytes = new byte[2 * conf.pageTypeSize +
|
2012-01-28 03:34:16 +01:00
|
|
|
conf.nameLength + 6];
|
2009-08-04 12:36:53 +02:00
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
System.arraycopy(setPageValue(logPage), 0, bytes, 0,
|
|
|
|
conf.pageTypeSize);
|
|
|
|
index += conf.pageTypeSize;
|
|
|
|
|
|
|
|
bytes[index++] = (byte) (logRecords >> 8);
|
|
|
|
bytes[index++] = (byte) logRecords;
|
|
|
|
|
|
|
|
bytes[index++] = (byte) (logRecordSize >> 8);
|
|
|
|
bytes[index++] = (byte) logRecordSize;
|
|
|
|
|
|
|
|
System.arraycopy(setPageValue(maxPages), 0, bytes, index,
|
|
|
|
conf.pageTypeSize);
|
|
|
|
index += conf.pageTypeSize;
|
|
|
|
|
|
|
|
bytes[index++] = 0; // Deprecated EOF hint field.
|
2012-01-28 03:34:16 +01:00
|
|
|
bytes[index++] = (byte) flags;
|
2009-08-04 12:36:53 +02:00
|
|
|
|
|
|
|
byte[] nameBytes = name.getBytes();
|
2012-01-28 03:34:16 +01:00
|
|
|
int copyLength = nameBytes.length > conf.nameLength ?
|
|
|
|
conf.nameLength : nameBytes.length;
|
2009-08-04 12:36:53 +02:00
|
|
|
System.arraycopy(nameBytes, 0, bytes, index, copyLength);
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int rawLength() {
|
2012-01-28 03:34:16 +01:00
|
|
|
return 2 * conf.pageTypeSize + conf.nameLength + 6;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getPage() {
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isValid() {
|
2012-01-28 03:34:16 +01:00
|
|
|
return (flags & HDR_FLAG_VALID) != 0;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAllocated() {
|
2012-01-28 03:34:16 +01:00
|
|
|
return (flags & HDR_FLAG_ALLOCATED) != 0;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isObsolete() {
|
2012-01-28 03:34:16 +01:00
|
|
|
return (flags & HDR_FLAG_OBSOLETE) != 0;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isModified() {
|
2012-01-28 03:34:16 +01:00
|
|
|
return (flags & HDR_FLAG_MODIFIED) != 0;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isIsolated() {
|
2012-01-28 03:34:16 +01:00
|
|
|
return (flags & HDR_FLAG_ISOLATED) != 0;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isLog() {
|
2012-01-28 03:34:16 +01:00
|
|
|
return (flags & HDR_FLAG_LOG) != 0;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFree() {
|
|
|
|
return !isAllocated();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isActive() {
|
|
|
|
return isAllocated() && !isObsolete() && !isIsolated();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void allocate() {
|
2012-01-28 03:34:16 +01:00
|
|
|
flags |= HDR_FLAG_ALLOCATED;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void makeObsolete() {
|
2012-01-28 03:34:16 +01:00
|
|
|
flags |= HDR_FLAG_OBSOLETE;
|
2009-08-04 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setReservedSize(int pages) {
|
|
|
|
maxPages = pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getReservedSize() {
|
|
|
|
return maxPages;
|
|
|
|
}
|
|
|
|
}
|