1/*
2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "SuperBlock.h"
8
9#include <string.h>
10
11#include "BlockAllocator.h"
12#include "Directory.h"
13#include "Volume.h"
14
15
16bool
17SuperBlock::Check(uint64 totalBlocks) const
18{
19	if (strncmp(signature1, CHECK_SUM_FS_SIGNATURE_1,
20			kCheckSumFSSignatureLength) != 0
21		|| strncmp(signature2, CHECK_SUM_FS_SIGNATURE_2,
22			kCheckSumFSSignatureLength) != 0
23		|| Version() != kCheckSumFSVersion
24		|| TotalBlocks() < kCheckSumFSMinSize / B_PAGE_SIZE
25		|| TotalBlocks() > totalBlocks
26		|| strnlen(name, kCheckSumFSNameLength) >= kCheckSumFSNameLength) {
27		return false;
28	}
29	// TODO: Check free blocks and location of root directory and block bitmap.
30
31	return true;
32}
33
34
35void
36SuperBlock::Initialize(Volume* volume)
37{
38	memcpy(signature1, CHECK_SUM_FS_SIGNATURE_1, kCheckSumFSSignatureLength);
39	memcpy(signature2, CHECK_SUM_FS_SIGNATURE_2, kCheckSumFSSignatureLength);
40
41	version = kCheckSumFSVersion;
42	totalBlocks = volume->TotalBlocks();
43	freeBlocks = volume->GetBlockAllocator()->FreeBlocks();
44	blockBitmap = volume->GetBlockAllocator()->BaseBlock();
45	rootDir = volume->RootDirectory()->BlockIndex();
46	strlcpy(name, volume->Name(), kCheckSumFSNameLength);
47}
48
49
50void
51SuperBlock::SetFreeBlocks(uint64 count)
52{
53	freeBlocks = count;
54}
55
56
57void
58SuperBlock::SetName(const char* name)
59{
60	strlcpy(this->name, name, kCheckSumFSNameLength);
61}
62