1/*
2 * Copyright 2022, Raghav Sharma, raghavself28@gmail.com
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _XFS_VHEADER_H
6#define _XFS_VHEADER_H
7
8
9#include "BPlusTree.h"
10#include "Checksum.h"
11#include "Extent.h"
12#include "Inode.h"
13#include "LeafDirectory.h"
14#include "Node.h"
15#include "xfs_types.h"
16#include "system_dependencies.h"
17
18
19// Common template function to Verify all forms of header
20template<class T>
21bool VerifyHeader(T* header, char* buffer, Inode* inode,
22	int howManyBlocksFurther, ExtentMapEntry* map, int8 WhichDirectory)
23{
24	if (header->Magic() != T::ExpectedMagic(WhichDirectory, inode)) {
25		ERROR("Bad magic number");
26		return false;
27	}
28
29	if (inode->Version() == 1 || inode->Version() == 2)
30		return true;
31
32	if (!xfs_verify_cksum(buffer, inode->DirBlockSize(), T::CRCOffset())) {
33		ERROR("CRC is invalid");
34		return false;
35	}
36
37	// For Block header we pass NULL
38	if(map != NULL) {
39		uint64 actualBlockToRead = inode->FileSystemBlockToAddr(map->br_startblock
40			+ howManyBlocksFurther) / XFS_MIN_BLOCKSIZE;
41
42		if (actualBlockToRead != header->Blockno()) {
43			ERROR("Wrong Block number");
44			return false;
45		}
46	}
47
48	if (!inode->GetVolume()->UuidEquals(header->Uuid())) {
49		ERROR("UUID is incorrect");
50		return false;
51	}
52
53	if (inode->ID() != header->Owner()) {
54		ERROR("Wrong data owner");
55		return false;
56	}
57
58	return true;
59}
60
61#endif