// File: /workspace/github_c_makefile_star2_O3/sheheryarbutt[P]Ext2-File-System/ext2_restore.c
// Function: main
int main(int argc, char **argv) {
	if(argc != 3) { 
        fprintf(stderr, "Usage: ext2_restore <image file name> <Absolute path>\n");
         exit(1);
    }
    int fd = open(argv[1], O_RDWR);
 
	char *path = malloc(sizeof(char)*((unsigned int)strlen(argv[2]) + 1));
	strcpy(path, argv[2]);
	path[(unsigned int)strlen(argv[2])] = '\0';

    disk = mmap(NULL, 128 * EXT2_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
		perror("mmap");
		exit(1);
 	}

 	sb = (struct ext2_super_block *)(disk + EXT2_BLOCK_SIZE);
    gd = (struct ext2_group_desc *)(disk + (2*EXT2_BLOCK_SIZE));
		 
 	inode_table = (struct ext2_inode *)(disk + (5*EXT2_BLOCK_SIZE));



	//This part gives us directory_path and file_name
	char *absPath=malloc((unsigned int)strlen(path)); strcpy(absPath,path);
	if((unsigned int)strlen(absPath)==1 && strcmp(absPath,"/")==0){
		fprintf(stderr, "ERROR: %s\n", strerror(EISDIR)); 
		exit(EISDIR);
	}
	remove_trailing_slash(absPath); // remove the trailing slashes
	char * file_name=basename(absPath); unsigned int file_name_length= (unsigned int)strlen(file_name);
	unsigned int absPath_length=(unsigned int) strlen(absPath);
	absPath[absPath_length - (file_name_length + 1)]='\0';
	char directory_path[absPath_length - file_name_length]; strcpy(directory_path,absPath);
	
	if((unsigned int)strlen(directory_path) == 0){ // if only "/file" is given 
		strcpy(directory_path,"/");
	}

	int * file_in_dir_block=malloc(sizeof(int *)); *file_in_dir_block=-1;
	int * file_offset=malloc(sizeof(int *));  *file_offset=-1; 
	int * entry_offset=malloc(sizeof(int *));  *entry_offset=-1;
	int * file_inode=malloc(sizeof(int *)); *file_inode=0;
	int in_gap=file_in_gap(directory_path,file_name,file_in_dir_block, file_offset, file_inode,entry_offset);

	if(in_gap){//maybe we found the file
		if(*file_inode>EXT2_GOOD_OLD_FIRST_INO || *file_inode<=32){ 

				unsigned int * ib = (unsigned int *)(disk+EXT2_BLOCK_SIZE*4);
				// -0 represents what we want the n=10 bit too (its really 11 tho!)
				if ( ib[0]&(1<< ((*file_inode) - 1 ))){// check if its a 1 or 0
					fprintf(stderr, "ERROR: %s\n", strerror(EPERM)); 
					exit(EPERM); 
				}
				//block/data bitmap
				struct ext2_inode inode = inode_table[*file_inode-1];//access the specific inode!
				int cur_index=0;
				int inode_i_block= inode.i_block[cur_index];// accessing the first i_block[0]= some block #
				unsigned int * bb=(unsigned int *)(disk + EXT2_BLOCK_SIZE*3);//accessing the data bitmap

				while(inode_i_block){// going through all the data blocks gotten  from i_block[] of the inode!
					int index_of_bb=(inode_i_block-1)/32;// gonna be 0,1,2 or 3  
					// If data block is replaced -> return error
					if(bb[index_of_bb]& (1<< (inode_i_block -1) % 32)){
						fprintf(stderr, "ERROR: %s\n", strerror(EPERM)); 
						exit(EPERM); 
					}
					cur_index++;
					inode_i_block= inode.i_block[cur_index];
				}// After this while we know all data blocks havent been reused!

				ib[0] ^= (-1 ^ ib[0]) & (1U << (*file_inode-1)); // change inode bitmap val. to 1
	
				cur_index=0; inode_i_block= inode.i_block[cur_index];
				while(inode_i_block){// going through all the data blocks 
					int index_of_bb=(inode_i_block-1)/32;// gonna be 0,1,2 or 3  
					bb[index_of_bb] ^= 1U << (inode_i_block -1) % 32;
					cur_index++;
					inode_i_block= inode.i_block[cur_index];
				}
				//decreasing number of free inodes and blocks
				sb->s_free_inodes_count-=1;  sb->s_free_blocks_count-=inode.i_blocks/2;
				gd->bg_free_inodes_count-=1; gd->bg_free_blocks_count-=inode.i_blocks/2;
				//changing links count and d_time
				 inode_table[*file_inode-1].i_links_count=1;  inode_table[*file_inode-1].i_dtime=0;
				//Updating rec_len of the enteries!
				struct ext2_dir_entry *direntry = (struct ext2_dir_entry *)(disk + (*file_in_dir_block)*EXT2_BLOCK_SIZE + (*entry_offset));
				struct ext2_dir_entry *gapEntry = (struct ext2_dir_entry *)(disk + (*file_in_dir_block)*EXT2_BLOCK_SIZE + (*file_offset));
				int entry_rec_len= direntry->rec_len;
				direntry->rec_len=(*file_offset)-(*entry_offset);
				gapEntry->rec_len=entry_rec_len - (direntry->rec_len);

		}
		else{
			fprintf(stderr, "ERROR: %s\n", strerror(EPERM)); 
			exit(EPERM); 
		}
	}
	else{// no such file found in gaps!
		fprintf(stderr, "ERROR: %s\n", strerror(EPERM)); 
		exit(EPERM); 
	}
	free(file_in_dir_block); free(file_offset);
	free(entry_offset); free(file_inode);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
cmpl $0x3, %edi
jne 0x16af
movq %rsi, %r14
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10e0
movl %eax, %ebx
movq 0x10(%r14), %r15
movq %r15, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10d0
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x1050
movq %r15, %rdi
callq 0x1060
movl %eax, %eax
movb $0x0, (%r14,%rax)
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2c9e(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x16b4
leaq 0x400(%rax), %rcx
movq %rcx, 0x2c8e(%rip)      # 0x40b8
leaq 0x800(%rax), %rcx
movq %rcx, 0x2c88(%rip)      # 0x40c0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2c83(%rip)      # 0x40c8
movq %r14, %rdi
callq 0x1060
movl %eax, %edi
callq 0x10d0
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
movq %rbx, %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x147e
cmpb $0x2f, (%rbx)
jne 0x147e
cmpb $0x0, 0x1(%rbx)
je 0x16c3
movq %rbx, %rdi
callq 0x1a50
movq %rbx, %rdi
callq 0x10b0
movq %rax, -0x30(%rbp)
movq %rax, %rdi
callq 0x1060
movq %rax, %r14
movq %rbx, %rdi
callq 0x1060
movl %r14d, %ecx
notl %ecx
addl %eax, %ecx
movb $0x0, (%rbx,%rcx)
subl %r14d, %eax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1050
movq %r14, %rdi
callq 0x1060
testl %eax, %eax
jne 0x14e1
movw $0x2f, (%r14)
movl $0x8, %edi
callq 0x10d0
movq %rax, %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, (%rax)
movl $0x8, %edi
callq 0x10d0
movq %rax, %rbx
movl %r12d, (%rax)
movl $0x8, %edi
callq 0x10d0
movq %rax, %r15
movl %r12d, (%rax)
movl $0x8, %edi
callq 0x10d0
movq %rax, %r12
movl $0x0, (%rax)
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %rax, %r8
movq %r15, %r9
callq 0x16c8
testl %eax, %eax
je 0x16b9
movslq (%r12), %r8
leal -0x1(%r8), %ecx
movl $0x1, %r10d
shll %cl, %r10d
movq 0x2b4e(%rip), %rdx      # 0x40b0
movl 0x1000(%rdx), %r11d
movzbl %cl, %eax
btl %eax, %r11d
jb 0x16be
movq 0x2b4b(%rip), %r9       # 0x40c8
shlq $0x7, %r8
leaq (%r9,%r8), %rsi
addq $-0x80, %rsi
leaq -0xb0(%rbp), %rax
movl $0x20, %ecx
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
movl 0x28(%rax), %eax
testl %eax, %eax
je 0x1614
leaq -0x84(%rbp), %rcx
movl %eax, %esi
leal -0x1(%rsi), %edi
addl $0x1e, %esi
testl %edi, %edi
cmovnsl %edi, %esi
sarl $0x5, %esi
movslq %esi, %rsi
movl 0xc00(%rdx,%rsi,4), %esi
btl %edi, %esi
jb 0x16aa
movl (%rcx), %esi
addq $0x4, %rcx
testl %esi, %esi
jne 0x15aa
orl %r10d, %r11d
movl %r11d, 0x1000(%rdx)
testl %eax, %eax
je 0x161e
leaq -0x84(%rbp), %rsi
leal -0x1(%rax), %ecx
addl $0x1e, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x5, %eax
movl $0x1, %edi
shll %cl, %edi
cltq
xorl %edi, 0xc00(%rdx,%rax,4)
movl (%rsi), %eax
addq $0x4, %rsi
testl %eax, %eax
jne 0x15ea
jmp 0x161e
orl %r10d, %r11d
movl %r11d, 0x1000(%rdx)
addq %r8, %r9
movq 0x2a90(%rip), %rax      # 0x40b8
decl 0x10(%rax)
movl -0x94(%rbp), %ecx
shrl %ecx
subl %ecx, 0xc(%rax)
movq 0x2a83(%rip), %rax      # 0x40c0
decw 0xe(%rax)
subw %cx, 0xc(%rax)
movw $0x1, -0x66(%r9)
movl $0x0, -0x6c(%r9)
movslq (%r13), %rax
shlq $0xa, %rax
addq %rax, %rdx
movslq (%r15), %rax
movslq (%rbx), %rcx
movl 0x4(%rax,%rdx), %esi
movl %ecx, %edi
subl %eax, %edi
movw %di, 0x4(%rax,%rdx)
subl %edi, %esi
movw %si, 0x4(%rcx,%rdx)
movq %r13, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11a1
callq 0x1140
callq 0x124c
callq 0x11da
callq 0x1168
callq 0x1213