// File: /workspace/github_c_makefile_star2_O2/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 0x155f
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10e0
movl %eax, %r15d
movq 0x10(%rbx), %rbx
movq %rbx, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10d0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1050
movq %rbx, %rdi
callq 0x1060
movl %eax, %eax
movb $0x0, (%r14,%rax)
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %r15d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2e03(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x157d
leaq 0x400(%rax), %rcx
movq %rcx, 0x2df3(%rip)      # 0x40b8
leaq 0x800(%rax), %rcx
movq %rcx, 0x2ded(%rip)      # 0x40c0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2de8(%rip)      # 0x40c8
movq %r14, %rdi
callq 0x1060
movl %eax, %edi
callq 0x10d0
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
movq %r15, %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x131b
cmpb $0x2f, (%r15)
jne 0x131b
cmpb $0x0, 0x1(%r15)
je 0x1591
movq %r15, %rdi
callq 0x193c
movq %r15, %rdi
callq 0x10b0
movq %rax, -0x30(%rbp)
movq %rax, %rdi
callq 0x1060
movq %rax, %r12
movq %r15, %rdi
callq 0x1060
movl %r12d, %ecx
notl %ecx
addl %eax, %ecx
movb $0x0, (%r15,%rcx)
subl %r12d, %eax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %r12, %rdi
movq %r15, %rsi
callq 0x1050
movq %r12, %rdi
callq 0x1060
testl %eax, %eax
jne 0x1380
movw $0x2f, (%r12)
pushq $0x8
popq %rbx
movq %rbx, %rdi
callq 0x10d0
movq %rax, %r14
orl $-0x1, (%rax)
movq %rbx, %rdi
callq 0x10d0
movq %rax, %r15
orl $-0x1, (%rax)
movq %rbx, %rdi
callq 0x10d0
movq %rax, %r13
orl $-0x1, (%rax)
movq %rbx, %rdi
callq 0x10d0
movq %rax, %rbx
andl $0x0, (%rax)
movq %r12, %rdi
movq %r14, %r12
movq -0x30(%rbp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %rax, %r8
movq %r13, -0x30(%rbp)
movq %r13, %r9
callq 0x159f
testl %eax, %eax
je 0x152d
movslq (%rbx), %rax
leal -0x1(%rax), %ecx
pushq $0x1
popq %r10
shll %cl, %r10d
movq 0x2cba(%rip), %r8       # 0x40b0
movl 0x1000(%r8), %r11d
movzbl %cl, %ecx
btl %ecx, %r11d
jb 0x152d
movq %rbx, %r13
movq 0x2cb4(%rip), %rcx      # 0x40c8
shlq $0x7, %rax
leaq (%rcx,%rax), %r9
leaq (%rcx,%rax), %rsi
addq $-0x80, %rsi
pushq $0x20
popq %rcx
leaq -0xb0(%rbp), %rax
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
movl 0x28(%rax), %ecx
leaq -0x84(%rbp), %r14
pushq $0x20
popq %rdi
movl %ecx, %esi
testl %esi, %esi
je 0x1469
decl %esi
movl %esi, %eax
cltd
idivl %edi
cltq
movl 0xc00(%r8,%rax,4), %eax
btl %esi, %eax
jb 0x152d
movl (%r14), %esi
addq $0x4, %r14
jmp 0x1442
orl %r10d, %r11d
movl %r11d, 0x1000(%r8)
leaq -0x84(%rbp), %rsi
pushq $0x1
popq %r10
testl %ecx, %ecx
je 0x14a0
decl %ecx
movl %ecx, %eax
cltd
idivl %edi
movl %r10d, %edx
shll %cl, %edx
cltq
xorl %edx, 0xc00(%r8,%rax,4)
movl (%rsi), %ecx
addq $0x4, %rsi
jmp 0x147e
movq 0x2c11(%rip), %rax      # 0x40b8
decl 0x10(%rax)
movl -0x94(%rbp), %ecx
shrl %ecx
subl %ecx, 0xc(%rax)
movq 0x2c04(%rip), %rax      # 0x40c0
decw 0xe(%rax)
subw %cx, 0xc(%rax)
movw $0x1, -0x66(%r9)
andl $0x0, -0x6c(%r9)
movslq (%r12), %rax
shlq $0xa, %rax
addq %rax, %r8
movq -0x30(%rbp), %rbx
movslq (%rbx), %rax
movslq (%r15), %rcx
movl 0x4(%rax,%r8), %edx
movl %ecx, %esi
subl %eax, %esi
movw %si, 0x4(%rax,%r8)
subl %esi, %edx
movw %dx, 0x4(%rcx,%r8)
movq %r12, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2ac4(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x1
popq %r14
movl %r14d, %edi
callq 0x1120
leaq 0xaf6(%rip), %rsi       # 0x2040
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1090
movl %r14d, %edi
callq 0x1100
movq 0x2a92(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xa94(%rip), %rdi       # 0x2004
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
jmp 0x1589
leaq 0xab7(%rip), %rdi       # 0x203b
callq 0x10f0
pushq $0x1
popq %rdi
callq 0x1100
movq 0x2a60(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x15
jmp 0x1539