// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
cmpl $0x3, %edi
jne 0x1569
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, 0x2d0e(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x156e
leaq 0x400(%rax), %rcx
movq %rcx, 0x2cfe(%rip)      # 0x40b8
leaq 0x800(%rax), %rcx
movq %rcx, 0x2cf8(%rip)      # 0x40c0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2cf3(%rip)      # 0x40c8
movq %rbx, %rdi
callq 0x1584
movl $0x8, %edi
callq 0x10d0
movq %rax, %r14
movq 0x2cbf(%rip), %rsi      # 0x40b0
movq %rbx, %rdi
movq %rax, %rdx
callq 0x16cc
movl %eax, %r15d
movl %eax, %edi
callq 0x15f9
testl %eax, %eax
je 0x1573
movq 0x2cb3(%rip), %r8       # 0x40c8
movslq %r15d, %rax
decq %rax
movq %rax, %r12
shlq $0x7, %r12
leaq (%r8,%r12), %rsi
leaq -0xb0(%rbp), %rdx
movl $0x20, %ecx
movq %rdx, %rdi
rep  movsl (%rsi), %es:(%rdi)
movl (%rdx), %ecx
btl $0xe, %ecx
jb 0x1578
testw %cx, %cx
jns 0x157d
movq 0x2c5d(%rip), %rdx      # 0x40b0
movl (%r14), %ecx
shll $0xa, %ecx
addq %rdx, %rcx
xorl %esi, %esi
movq %rcx, %r9
xorl %edi, %edi
xorl %r10d, %r10d
movl (%r9), %r13d
testl %esi, %esi
jne 0x1472
cmpl %r15d, %r13d
je 0x1497
movzwl 0x4(%r9), %r11d
cmpl %r15d, %r13d
je 0x14a0
movzwl %r11w, %r9d
addl %r9d, %edi
addl %r9d, %esi
leaq (%rcx,%rsi), %r9
movl %r11d, %r10d
cmpl $0x400, %esi            # imm = 0x400
jb 0x1466
jmp 0x14b0
movl $0x0, (%r9)
jmp 0x14b0
movzwl %di, %esi
movzwl %r10w, %edi
subq %rdi, %rsi
addw %r11w, 0x4(%rcx,%rsi)
cmpw $0x1, -0x96(%rbp)
jne 0x153f
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movl %eax, %ecx
roll %cl, %esi
andl %esi, 0x1000(%rdx)
movl -0x88(%rbp), %eax
testl %eax, %eax
je 0x1506
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 0x14de
leaq -0x30(%rbp), %rdi
callq 0x10c0
movq 0x2bb2(%rip), %r8       # 0x40c8
movl %eax, 0x14(%r8,%r12)
movq 0x2b96(%rip), %rax      # 0x40b8
incl 0x10(%rax)
movl -0x94(%rbp), %ecx
shrl %ecx
addl %ecx, 0xc(%rax)
movq 0x2b89(%rip), %rax      # 0x40c0
incw 0xe(%rax)
addw %cx, 0xc(%rax)
decw 0x1a(%r8,%r12)
movq %rbx, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1140
callq 0x1213
callq 0x11da
callq 0x1168
callq 0x11a1
nop