// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl $0x3, %edi
jne 0x1466
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10e0
movl %eax, %r15d
movq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10d0
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
movq %r14, %rdi
callq 0x1060
movl %eax, %eax
movb $0x0, (%rbx,%rax)
pushq $0x3
popq %rdx
pushq $0x1
popq %r14
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %r14d, %ecx
movl %r15d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2e00(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x1484
leaq 0x400(%rax), %rcx
movq %rcx, 0x2df0(%rip)      # 0x40b8
leaq 0x800(%rax), %rcx
movq %rcx, 0x2dea(%rip)      # 0x40c0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2de5(%rip)      # 0x40c8
movq %rbx, %rdi
callq 0x14e4
pushq $0x8
popq %rdi
callq 0x10d0
movq 0x2db6(%rip), %rsi      # 0x40b0
movq %rbx, %rdi
movq %rax, -0x30(%rbp)
movq %rax, %rdx
callq 0x1615
movl %eax, %r12d
movl %eax, %edi
callq 0x1559
testl %eax, %eax
je 0x1498
movq 0x2da6(%rip), %r8       # 0x40c8
movslq %r12d, %rax
decq %rax
movq %rax, %r13
shlq $0x7, %r13
leaq (%r8,%r13), %rsi
pushq $0x20
popq %rcx
leaq -0xb8(%rbp), %rdx
movq %rdx, %rdi
rep  movsl (%rsi), %es:(%rdi)
movl (%rdx), %ecx
btl $0xe, %ecx
jb 0x14a6
testw %cx, %cx
jns 0x14b6
movq 0x2d52(%rip), %rsi      # 0x40b0
movq -0x30(%rbp), %rcx
movl (%rcx), %ecx
shll $0xa, %ecx
addq %rsi, %rcx
xorl %r9d, %r9d
xorl %edx, %edx
movq %rcx, %r10
xorl %edi, %edi
cmpl $0x3ff, %edi            # imm = 0x3FF
ja 0x13bb
movl (%r10), %r15d
testl %edi, %edi
jne 0x1388
cmpl %r12d, %r15d
je 0x13a5
movzwl 0x4(%r10), %r11d
cmpl %r12d, %r15d
je 0x13ab
movzwl %r11w, %r9d
addl %r9d, %edx
addl %r9d, %edi
leaq (%rcx,%rdi), %r10
movl %r11d, %r9d
jmp 0x1374
andl $0x0, (%r10)
jmp 0x13bb
movzwl %dx, %edx
movzwl %r9w, %edi
subq %rdi, %rdx
addw %r11w, 0x4(%rcx,%rdx)
cmpw $0x1, -0x9e(%rbp)
jne 0x143b
pushq $-0x2
popq %rdx
movl %eax, %ecx
roll %cl, %edx
andl %edx, 0x1000(%rsi)
leaq -0x8c(%rbp), %rdi
movl -0x4(%rdi), %ecx
pushq $0x20
popq %r8
testl %ecx, %ecx
je 0x1402
decl %ecx
movl %ecx, %eax
cltd
idivl %r8d
movl %r14d, %edx
shll %cl, %edx
cltq
xorl %edx, 0xc00(%rsi,%rax,4)
movl (%rdi), %ecx
addq $0x4, %rdi
jmp 0x13e0
leaq -0x38(%rbp), %rdi
callq 0x10c0
movq 0x2cb6(%rip), %r8       # 0x40c8
movl %eax, 0x14(%r8,%r13)
movq 0x2c9a(%rip), %rax      # 0x40b8
incl 0x10(%rax)
movl -0x9c(%rbp), %ecx
shrl %ecx
addl %ecx, 0xc(%rax)
movq 0x2c8d(%rip), %rax      # 0x40c0
incw 0xe(%rax)
addw %cx, 0xc(%rax)
decw 0x1a(%r8,%r13)
movq %rbx, %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2b8b(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb8d(%rip), %rdi       # 0x2004
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
jmp 0x1490
leaq 0xbac(%rip), %rdi       # 0x2037
callq 0x10f0
pushq $0x1
popq %rdi
callq 0x1100
movq 0x2b59(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x2
jmp 0x14b2
movq 0x2b4b(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x15
popq %r14
jmp 0x14c0
movq 0x2b3b(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
movl %r14d, %edi
callq 0x1120
leaq 0xb6d(%rip), %rsi       # 0x203c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1090
movl %r14d, %edi
callq 0x1100