// File: lrq3000[P]rfrb/./src/raidSystem.cpp
// Function: RaidSystem::raidCheck(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool RaidSystem::raidCheck(std::string path)
{
	bool found = false;
	//No RAID level is chosen from the user
	handle->setBlockSize(CHECKSIZE);
	if (raidSystem == Raid_unknown)
	{
		std::cout << "Starting with Raid check." << std::endl;
		//Calling easyCheck() to estimate RAID level
		found = easyCheck();
		if (found == false)
		{
			//Something went wrong
			std::cout << "No valid Raid system found!" << std::endl;
			return false;
		}
		else
		{
			while (true)
			{
				//RAID level found
				std::string answer;
				std::cout << "Do you want to accept this? [y/n] ";
				std::getline(std::cin, answer);
				if (answer == "n" || answer == "No" || answer == "N")
				{
					std::cout << "Raid system can not be estimated automatically. Must be handed with the program start." << std::endl;
					return false;
				}
				else if (answer == "y" || answer == "Yes" || answer == "Y")
				{
					break;
				}
			}
		}
	}
	else if (raidSystem == Raid5_user)
	{
		//RAID5 was chosen

		//It was not set, if an image is lost
		if (lostImages == -1)
		{
			std::cout << "User input: Raid 5! Checking for lost discs or if it's a Raid 1 system." << std::endl;
			//Check if user was right
			found = easyCheck();
			if (found == false)
			{
				if (raidSystem == Raid1)
				{
					//User was wrong, we think it's RAID1.
					std::cerr << "User input wrong. Estimated Raid 1 system!" << std::endl;
					return false;
				}
				else
				{
					//User was right with RAID5, but an image is lost.
					std::cout << "Discs missing. Will recover disc later!" << std::endl;
					raidSystem = Raid5_incomplete;
				}
				found = true;
			}
			else
			{
				//Complete RAID5 was found. No image must be recovered
				std::cout << "Raid 5 system is complete. No missing discs indicated!" << std::endl;
			}
		}
		else if (lostImages == 0)
		{
			//User says that no image is missing
			raidSystem = Raid5_complete;
			found = true;
		}
		else
		{
			//User says that at least one image is missing
			raidSystem = Raid5_incomplete;
			found = true;
		}
	}
	else if (raidSystem == Raid1)
	{
		//User says it's RAID1
		found = true;
	}
	else if (raidSystem == Raid0)
	{
		//User says it's RAID0

		if (lostImages > 0)
		{
			//User says an image is missing. That can't be recovered. We have to abort.
			std::cerr << "User specifies Raid 0 system with lost discs. Cannot be recovered!" << std::endl;
			return false;
		}
		found = true;
	}

	//Few final checks
	if (found == false)
	{
		//We haven't found one fitting RAID-level.
		std::cerr << "No valid Raid system found! Have to abort!" << std::endl;
		return false;
	}
	if (raidSystem == Raid5_incomplete)
	{
		//RAID5 was found with a missing image. Is recovered here.
		std::cout << "Incomplete Raid 5 system. Will recover disc and mount it internally." << std::endl;
		recoverLostImage();
		handle->addImage(handle->getFileWriter()->getPath());
	}

	//Stripesize estimation
	if (stripeSize == -1)
	{
		//User hasn't specified a stripesize. We has to determine it on our own.
		std::cout << "No Stripesize specified! Trying to calculate Stripesize now." << std::endl;
		found = calculateStripeSize();
	}
	else
	{
		//User specified stripesize
		std::cout << "Using Stripesize specified by user!" << std::endl;
		found = true;
	}

	//Output if stripesize estimation went fine.
	if (found == true)
	{
		std::cout << "Stripesize: " << stripeSize/1024 << "KB" << std::endl;
	}
	else
	{
		std::cerr << "No valid Stripesize found. User should estimate it on its own, or disc could not be recovered." << std::endl;
		return false;
	}
	//For further operations, set blocksize to stripesize.
	handle->setBlockSize(stripeSize);

	//Stripemap estimation
	std::cout << "Trying to estimate the Stripemap." << std::endl;
	if (raidSystem == Raid5_user || raidSystem == Raid5_incomplete || raidSystem == Raid5_complete)
	{
		//RAID5 needs a stripemap and has parity information. That's the call with 'true'
		stripeMap = handle->estimateStripeMap(true);
	}
	else if (raidSystem == Raid0)
	{
		//RAID0 needs a stripemap and has no parity information. That's the call with 'false'
		stripeMap = handle->estimateStripeMap(false);
	}
	else
	{
		//RAID1 needs no stripemap. We're done here.
		std::cout << "No need to estimate Stripemap because of Raid 1 system." << std::endl;
		found = true;
	}

	//Final checks if stripemap estimation went fine
	if (stripeMap.size() == 0 && !(raidSystem == Raid1))
	{
		std::cerr << "No valid Stripemap found. Have to abort Recovery." << std::endl;
		return false;
	}
	else if (raidSystem == Raid1)
	{}
	else
	{
		//If everything went fine, the whole imagefile is created
		std::cout << "Rebuild the imagefile out of the Raid discs." << std::endl;
		found = buildDataImage(path);
	}

	//If nothing bad has happened, the results are printed.
	if (found == true)
	{
		printAllInfos();
	}
	std::vector<FileReader *> inFiles = handle->getInFiles();
	for (unsigned int j = 0; j < inFiles.size(); ++j)
	{
		inFiles.at(j)->closeFile();
	}
	return found;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rsi, 0x48(%rsp)
movq %rdi, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x50(%rsp)
movb $0x0, 0x11f(%rsp)
movq (%rax), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xcc00
movq 0x50(%rsp), %rax
cmpl $0x5, 0x8(%rax)
jne 0x7634
movq 0x4cbbc(%rip), %rdi     # 0x53fb0
leaq 0x32cb2(%rip), %rsi     # 0x3a0ad
callq 0x6420
movq %rax, %rdi
movq 0x4cb66(%rip), %rsi     # 0x53f70
callq 0x6470
movq 0x50(%rsp), %rdi
callq 0x7c90
andb $0x1, %al
movb %al, 0x11f(%rsp)
movb 0x11f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7462
movq 0x4cb76(%rip), %rdi     # 0x53fb0
leaq 0x32c86(%rip), %rsi     # 0x3a0c7
callq 0x6420
movq %rax, %rdi
movq 0x4cb20(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x0, 0x137(%rsp)
jmp 0x7c72
jmp 0x7464
leaq 0xf8(%rsp), %rdi
callq 0x65c0
movq 0x4cb38(%rip), %rdi     # 0x53fb0
leaq 0x32c64(%rip), %rsi     # 0x3a0e3
callq 0x6420
jmp 0x7486
movq 0x4cb4b(%rip), %rdi     # 0x53fd8
leaq 0xf8(%rsp), %rsi
callq 0x60c0
jmp 0x749c
leaq 0x3510a(%rip), %rsi     # 0x3c5ad
leaq 0xf8(%rsp), %rdi
callq 0x9e10
movb %al, 0x47(%rsp)
jmp 0x74b6
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x7508
jmp 0x74c0
leaq 0x32c3f(%rip), %rsi     # 0x3a106
leaq 0xf8(%rsp), %rdi
callq 0x9e10
movb %al, 0x46(%rsp)
jmp 0x74da
movb 0x46(%rsp), %al
testb $0x1, %al
jne 0x7508
jmp 0x74e4
leaq 0x32c1e(%rip), %rsi     # 0x3a109
leaq 0xf8(%rsp), %rdi
callq 0x9e10
movb %al, 0x45(%rsp)
jmp 0x74fe
movb 0x45(%rsp), %al
testb $0x1, %al
jne 0x7508
jmp 0x7573
movq 0x4caa1(%rip), %rdi     # 0x53fb0
leaq 0x32bf5(%rip), %rsi     # 0x3a10b
callq 0x6420
movq %rax, 0x38(%rsp)
jmp 0x7522
movq 0x38(%rsp), %rdi
movq 0x4ca42(%rip), %rsi     # 0x53f70
callq 0x6470
jmp 0x7535
movb $0x0, 0x137(%rsp)
movl $0x1, 0xe8(%rsp)
jmp 0x75f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x6260
jmp 0x7c83
leaq 0x32be8(%rip), %rsi     # 0x3a162
leaq 0xf8(%rsp), %rdi
callq 0x9e10
movb %al, 0x37(%rsp)
jmp 0x758d
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x75df
jmp 0x7597
leaq 0x32bc6(%rip), %rsi     # 0x3a164
leaq 0xf8(%rsp), %rdi
callq 0x9e10
movb %al, 0x36(%rsp)
jmp 0x75b1
movb 0x36(%rsp), %al
testb $0x1, %al
jne 0x75df
jmp 0x75bb
leaq 0x32ba6(%rip), %rsi     # 0x3a168
leaq 0xf8(%rsp), %rdi
callq 0x9e10
movb %al, 0x35(%rsp)
jmp 0x75d5
movb 0x35(%rsp), %al
testb $0x1, %al
jne 0x75df
jmp 0x75ec
movl $0x3, 0xe8(%rsp)
jmp 0x75f9
jmp 0x75ee
movl $0x0, 0xe8(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x6260
movl 0xe8(%rsp), %eax
movl %eax, 0x30(%rsp)
testl %eax, %eax
je 0x7628
jmp 0x7617
movl 0x30(%rsp), %eax
subl $0x1, %eax
je 0x7c72
jmp 0x7626
jmp 0x762d
jmp 0x7464
jmp 0x762f
jmp 0x77cf
movq 0x50(%rsp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x7767
movq 0x50(%rsp), %rax
cmpl $-0x1, 0x28(%rax)
jne 0x772e
movq 0x4c957(%rip), %rdi     # 0x53fb0
leaq 0x32b0a(%rip), %rsi     # 0x3a16a
callq 0x6420
movq %rax, %rdi
movq 0x4c901(%rip), %rsi     # 0x53f70
callq 0x6470
movq 0x50(%rsp), %rdi
callq 0x7c90
andb $0x1, %al
movb %al, 0x11f(%rsp)
movb 0x11f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x770a
movq 0x50(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x76d2
movq 0x4c946(%rip), %rdi     # 0x53ff0
leaq 0x32b01(%rip), %rsi     # 0x3a1b2
callq 0x6420
movq %rax, %rdi
movq 0x4c8b0(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x0, 0x137(%rsp)
jmp 0x7c72
movq 0x4c8d7(%rip), %rdi     # 0x53fb0
leaq 0x32afd(%rip), %rsi     # 0x3a1dd
callq 0x6420
movq %rax, %rdi
movq 0x4c881(%rip), %rsi     # 0x53f70
callq 0x6470
movq 0x50(%rsp), %rax
movl $0x3, 0x8(%rax)
movb $0x1, 0x11f(%rsp)
jmp 0x772c
movq 0x4c89f(%rip), %rdi     # 0x53fb0
leaq 0x32aed(%rip), %rsi     # 0x3a205
callq 0x6420
movq %rax, %rdi
movq 0x4c849(%rip), %rsi     # 0x53f70
callq 0x6470
jmp 0x7765
movq 0x50(%rsp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x774f
movq 0x50(%rsp), %rax
movl $0x4, 0x8(%rax)
movb $0x1, 0x11f(%rsp)
jmp 0x7763
movq 0x50(%rsp), %rax
movl $0x3, 0x8(%rax)
movb $0x1, 0x11f(%rsp)
jmp 0x7765
jmp 0x77cd
movq 0x50(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x777c
movb $0x1, 0x11f(%rsp)
jmp 0x77cb
movq 0x50(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x77c9
movq 0x50(%rsp), %rax
cmpl $0x0, 0x28(%rax)
jle 0x77c1
movq 0x4c857(%rip), %rdi     # 0x53ff0
leaq 0x32a9c(%rip), %rsi     # 0x3a23c
callq 0x6420
movq %rax, %rdi
movq 0x4c7c1(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x0, 0x137(%rsp)
jmp 0x7c72
movb $0x1, 0x11f(%rsp)
jmp 0x77cb
jmp 0x77cd
jmp 0x77cf
movb 0x11f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x780f
movq 0x4c809(%rip), %rdi     # 0x53ff0
leaq 0x32a91(%rip), %rsi     # 0x3a27f
callq 0x6420
movq %rax, %rdi
movq 0x4c773(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x0, 0x137(%rsp)
jmp 0x7c72
movq 0x50(%rsp), %rax
cmpl $0x3, 0x8(%rax)
jne 0x78b7
movq 0x4c78b(%rip), %rdi     # 0x53fb0
leaq 0x32a7e(%rip), %rsi     # 0x3a2aa
callq 0x6420
movq %rax, %rdi
movq 0x4c735(%rip), %rsi     # 0x53f70
callq 0x6470
movq 0x50(%rsp), %rdi
callq 0x6c60
movq 0x50(%rsp), %rax
movq (%rax), %rdi
movq %rdi, 0x20(%rsp)
callq 0xcd30
movq %rax, %rsi
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x34c80
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0xcd40
jmp 0x7882
leaq 0xc8(%rsp), %rdi
callq 0x6260
jmp 0x78b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x6260
jmp 0x7c83
movq 0x50(%rsp), %rax
cmpl $-0x1, 0xc(%rax)
jne 0x78f9
movq 0x4c6e7(%rip), %rdi     # 0x53fb0
leaq 0x32a1f(%rip), %rsi     # 0x3a2ef
callq 0x6420
movq %rax, %rdi
movq 0x4c691(%rip), %rsi     # 0x53f70
callq 0x6470
movq 0x50(%rsp), %rdi
callq 0x89b0
andb $0x1, %al
movb %al, 0x11f(%rsp)
jmp 0x7923
movq 0x4c6b0(%rip), %rdi     # 0x53fb0
leaq 0x32a25(%rip), %rsi     # 0x3a32c
callq 0x6420
movq %rax, %rdi
movq 0x4c65a(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x1, 0x11f(%rsp)
movb 0x11f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x7981
movq 0x4c675(%rip), %rdi     # 0x53fb0
leaq 0x32a0e(%rip), %rsi     # 0x3a350
callq 0x6420
movq %rax, %rdi
movq 0x50(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x400, %ecx            # imm = 0x400
cltd
idivl %ecx
movl %eax, %esi
callq 0x6720
movq %rax, %rdi
leaq 0x329f2(%rip), %rsi     # 0x3a35d
callq 0x6420
movq %rax, %rdi
movq 0x4c5f6(%rip), %rsi     # 0x53f70
callq 0x6470
jmp 0x79b0
movq 0x4c668(%rip), %rdi     # 0x53ff0
leaq 0x329d1(%rip), %rsi     # 0x3a360
callq 0x6420
movq %rax, %rdi
movq 0x4c5d2(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x0, 0x137(%rsp)
jmp 0x7c72
movq 0x50(%rsp), %rax
movq (%rax), %rdi
movslq 0xc(%rax), %rsi
callq 0xcc00
movq 0x4c5e8(%rip), %rdi     # 0x53fb0
leaq 0x329f0(%rip), %rsi     # 0x3a3bf
callq 0x6420
movq %rax, %rdi
movq 0x4c592(%rip), %rsi     # 0x53f70
callq 0x6470
movq 0x50(%rsp), %rax
cmpl $0x2, 0x8(%rax)
je 0x7a04
movq 0x50(%rsp), %rax
cmpl $0x3, 0x8(%rax)
je 0x7a04
movq 0x50(%rsp), %rax
cmpl $0x4, 0x8(%rax)
jne 0x7a43
movq 0x50(%rsp), %rax
movq (%rax), %rsi
leaq 0xb0(%rsp), %rdi
movl $0x1, %edx
callq 0xe0a0
movq 0x50(%rsp), %rdi
addq $0x10, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x9e40
leaq 0xb0(%rsp), %rdi
callq 0x9bf0
jmp 0x7ab6
movq 0x50(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x7a8a
movq 0x50(%rsp), %rax
movq (%rax), %rsi
leaq 0x98(%rsp), %rdi
xorl %edx, %edx
callq 0xe0a0
movq 0x50(%rsp), %rdi
addq $0x10, %rdi
leaq 0x98(%rsp), %rsi
callq 0x9e40
leaq 0x98(%rsp), %rdi
callq 0x9bf0
jmp 0x7ab4
movq 0x4c51f(%rip), %rdi     # 0x53fb0
leaq 0x32949(%rip), %rsi     # 0x3a3e1
callq 0x6420
movq %rax, %rdi
movq 0x4c4c9(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x1, 0x11f(%rsp)
jmp 0x7ab6
movq 0x50(%rsp), %rdi
addq $0x10, %rdi
callq 0x9e80
cmpq $0x0, %rax
jne 0x7b04
movq 0x50(%rsp), %rax
cmpl $0x1, 0x8(%rax)
je 0x7b04
movq 0x4c514(%rip), %rdi     # 0x53ff0
leaq 0x32936(%rip), %rsi     # 0x3a419
callq 0x6420
movq %rax, %rdi
movq 0x4c47e(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x0, 0x137(%rsp)
jmp 0x7c72
movq 0x50(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x7b14
jmp 0x7b9b
movq 0x4c495(%rip), %rdi     # 0x53fb0
leaq 0x32929(%rip), %rsi     # 0x3a44b
callq 0x6420
movq %rax, %rdi
movq 0x4c43f(%rip), %rsi     # 0x53f70
callq 0x6470
movq 0x48(%rsp), %rsi
leaq 0x78(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x6190
movq 0x50(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x8a00
movb %al, 0x1f(%rsp)
jmp 0x7b5f
movb 0x1f(%rsp), %al
andb $0x1, %al
movb %al, 0x11f(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x6260
jmp 0x7b9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x6260
jmp 0x7c83
jmp 0x7b9d
movb 0x11f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x7bb8
movq 0x50(%rsp), %rdi
callq 0x9330
movq 0x50(%rsp), %rax
movq (%rax), %rdi
callq 0xcc90
movq %rax, %rsi
leaq 0x60(%rsp), %rdi
callq 0x9c50
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movq %rax, 0x8(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x7c4d
movl 0x5c(%rsp), %eax
movl %eax, %esi
leaq 0x60(%rsp), %rdi
callq 0x9d70
movq %rax, (%rsp)
jmp 0x7c10
movq (%rsp), %rax
movq (%rax), %rdi
callq 0x304d0
jmp 0x7c1e
jmp 0x7c20
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x7bda
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x9db0
jmp 0x7c83
movb 0x11f(%rsp), %al
andb $0x1, %al
movb %al, 0x137(%rsp)
movl $0x1, 0xe8(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x9db0
movb 0x137(%rsp), %al
andb $0x1, %al
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xf0(%rsp), %rdi
callq 0x6750