// File: /workspace/github_c_makefile_star2_O1/sheheryarbutt[P]Ext2-File-System/ext2_rm.c
// Function: main
int main(int argc, char **argv) {
	if(argc != 3) { 
        fprintf(stderr, "Usage: ext2_rm <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));

 	remove_trailing_slash(path);
	//the block the files in that we want to remove
	unsigned int * parent_block= malloc(sizeof(unsigned int *));
	int cur_inode=pathTraversal(path,disk,parent_block);

	if(valid(cur_inode)){ 
		struct ext2_inode inode = inode_table[cur_inode-1];// represents the current inode

		if((inode.i_mode & EXT2_S_IFDIR) == EXT2_S_IFDIR){//is it a directory? raise error!
			fprintf(stderr, "ERROR: %s\n", strerror(EISDIR)); 
			exit(EISDIR);
		}
		else if((inode.i_mode & EXT2_S_IFREG) == EXT2_S_IFREG) { 
			//1) Update the directory entries
			int size=0;
			struct ext2_dir_entry *direntry = (struct ext2_dir_entry *)(disk + *parent_block*EXT2_BLOCK_SIZE +size);
			unsigned short prev_entry=0; unsigned short prev_rec_len=0;
			while(size<EXT2_BLOCK_SIZE){// going through whole block
				if(size==0 && (direntry->inode == cur_inode)){ // so if its the first block -> dummy node
					direntry->inode=0;
					break;
				}
				else if(direntry->inode==cur_inode){
					unsigned short cur_rec_len=0; cur_rec_len=direntry->rec_len; // get currents rec_len 
					direntry = (struct ext2_dir_entry *)(disk + *parent_block*EXT2_BLOCK_SIZE + (prev_entry - prev_rec_len));
					direntry->rec_len+=cur_rec_len; // add current rec_len to previous ones rec_len 
					break;
				}			
				prev_entry+=direntry->rec_len;// contstanly adds all the rec_lens
				prev_rec_len=direntry->rec_len;
				//add to size so we can increment the pointer to point to next entry!
				size+=direntry->rec_len; 
				direntry = (struct ext2_dir_entry *)(disk + *parent_block*EXT2_BLOCK_SIZE +size);				
			}
		
			//2 and 3 if hard links count==1 then change inode and data bitmap!
			if(inode.i_links_count==1){
				unsigned int * ib = (unsigned int *)(disk+EXT2_BLOCK_SIZE*4);
				//-0 represents what we want the n=10 bit too (its really 11 tho!)
				//inode bitmap
				ib[0] ^= (-0 ^ ib[0]) & (1U << (cur_inode-1));
				//block/data bitmap
				int cur_index=0;
				int inode_i_block= inode.i_block[cur_index];
				unsigned int * bb=(unsigned int *)(disk + EXT2_BLOCK_SIZE*3);

				while(inode_i_block){
					int index_of_bb=(inode_i_block-1)/32;       
					bb[index_of_bb] ^= 1U << (inode_i_block -1) % 32;
					cur_index++;
					inode_i_block= inode.i_block[cur_index];
				}

				time_t rawtime; // setting deletion time
				inode_table[cur_inode-1].i_dtime = time(&rawtime); 
				//increasing free blocks and inodes count!
				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;
			}// end of if 

			//#4 modify links count (hardlinks)
			inode_table[cur_inode-1].i_links_count=inode_table[cur_inode-1].i_links_count -1;

		}// end of else if
		else{ // If we encounter anything other than a file
		fprintf(stderr, "ERROR: %s\n", strerror(EPERM)); 
	  	exit(EPERM);

		}
	}
	else{
		fprintf(stderr, "ERROR: %s\n", strerror(ENOENT)); 
	  	exit(ENOENT);

		}
	free(path); free(parent_block);
	return 0; 

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
cmpl $0x3, %edi
jne 0x155e
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10e0
movl %eax, %r14d
movq 0x10(%rbx), %r15
movq %r15, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10d0
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x1050
movq %r15, %rdi
callq 0x1060
movl %eax, %eax
movb $0x0, (%rbx,%rax)
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2d10(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x1563
leaq 0x400(%rax), %rcx
movq %rcx, 0x2d00(%rip)      # 0x40b8
leaq 0x800(%rax), %rcx
movq %rcx, 0x2cfa(%rip)      # 0x40c0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2cf5(%rip)      # 0x40c8
movq %rbx, %rdi
callq 0x1578
movl $0x8, %edi
callq 0x10d0
movq %rax, %r14
movq 0x2cc1(%rip), %rsi      # 0x40b0
movq %rbx, %rdi
movq %rax, %rdx
callq 0x16d8
movl %eax, %r15d
movl %eax, %edi
callq 0x15ed
testl %eax, %eax
je 0x1568
movslq %r15d, %rax
decq %rax
movq %rax, %r12
shlq $0x7, %r12
movq 0x2ca8(%rip), %rsi      # 0x40c8
addq %r12, %rsi
leaq -0xa8(%rbp), %rdx
movl $0x20, %ecx
movq %rdx, %rdi
rep  movsl (%rsi), %es:(%rdi)
movl (%rdx), %ecx
btl $0xe, %ecx
jb 0x156d
testw %cx, %cx
jns 0x1572
movq 0x2c60(%rip), %rdx      # 0x40b0
movl (%r14), %ecx
shll $0xa, %ecx
addq %rdx, %rcx
xorl %esi, %esi
movq %rcx, %r8
xorl %edi, %edi
xorl %r10d, %r10d
testl %esi, %esi
jne 0x146c
cmpl %r15d, (%r8)
je 0x14a3
movzwl 0x4(%r8), %r9d
cmpl %r15d, (%r8)
je 0x1491
movzwl %r9w, %r8d
addl %r8d, %edi
addl %r8d, %esi
leaq (%rcx,%rsi), %r8
movl %r9d, %r10d
cmpl $0x400, %esi            # imm = 0x400
jb 0x1463
jmp 0x14aa
movzwl %di, %esi
movzwl %r10w, %edi
subq %rdi, %rsi
addw %r9w, 0x4(%rcx,%rsi)
jmp 0x14aa
movl $0x0, (%r8)
cmpw $0x1, -0x8e(%rbp)
jne 0x152f
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movl %eax, %ecx
roll %cl, %esi
andl %esi, 0x1000(%rdx)
movl -0x80(%rbp), %eax
testl %eax, %eax
je 0x14f6
leaq -0x7c(%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 0x14ce
leaq -0x28(%rbp), %rdi
callq 0x10c0
movq 0x2bc2(%rip), %rcx      # 0x40c8
movl %eax, 0x14(%rcx,%r12)
movq 0x2ba6(%rip), %rax      # 0x40b8
incl 0x10(%rax)
movl -0x8c(%rbp), %ecx
shrl %ecx
addl %ecx, 0xc(%rax)
movq 0x2b99(%rip), %rax      # 0x40c0
incw 0xe(%rax)
addw %cx, 0xc(%rax)
movq 0x2b92(%rip), %rax      # 0x40c8
decw 0x1a(%rax,%r12)
movq %rbx, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x1140
callq 0x1213
callq 0x11da
callq 0x1168
callq 0x11a1
nop