// File: /workspace/github_c_makefile_star2_O0/Sidd0107[P]EXT2-File-System/ext2_rm.c
// Function: main
int main(int argc, char **argv) {
    //Checks valid no of argument
    if(argc != 3) {
        fprintf(stderr, "Usage: ext2_rm <image file name> <file path on ext2 image>\n");
        exit(1);
    }
    int fd = open(argv[1], O_RDWR);
    char *path = argv[2];
    //maps disk in memory
    disk = mmap(NULL, 128 * BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    struct ext2_super_block *sb = (struct ext2_super_block *)(disk + BLOCK_SIZE);
    struct ext2_group_desc *gd = (struct ext2_group_desc *)  (disk + 2*BLOCK_SIZE + ((sb -> s_block_group_nr) *  sizeof(struct ext2_group_desc)));
    
        
    char *dirc, *basec, *dirName, *pth;
    dirc = strdup(path);
    basec = strdup(path);
    pth = dirname(dirc);
    dirName = basename(basec);

    
    // Checks if file already exists and returns if yes.
    if(file_exists(dirc, get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), dirName)==0){
        fprintf(stderr, "rm: File does not exist\n");
        return ENOENT;
    }
    //Getting info to check that destination is a directory and not reserved
    struct basic_fileinfo file_info = find_file(pth, get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
    if(file_info.inode < 11 && file_info.inode != 2){
        fprintf(stderr, "Invalid destination");
        return ENOENT;
    }
	
	file_info = find_file(argv[2], get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
	if(file_info.type == 'd'){
        fprintf(stderr, "rm: Cant delete a directory");
        return EISDIR;
    }
    //gets inode of file to be deleted.
    int inode_num = file_info.inode;
    struct ext2_inode *current_file_inode = get_inode(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), inode_num);
    // Getting parent directory
    struct basic_fileinfo parent_info = find_file(pth, get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
    inode_num = parent_info.inode;
    char *filename = extract_filename(argv[2]);
    char *inode_base = get_block(disk, gd->bg_inode_table, BLOCK_SIZE);
	
    // Loops through the iblocks of parent directory in search for the directory
    // entry to be deleted.
    struct ext2_inode *current_inode = get_inode(inode_base, inode_num);
    unsigned int block_num = current_inode->i_block[0];
    
    unsigned int cur_index_serviced = 0, cur_inode_index = 0;
    char *current_block = get_block(disk, block_num, BLOCK_SIZE);
    struct ext2_dir_entry_2 *current = (struct ext2_dir_entry_2 *) current_block;
    struct ext2_dir_entry_2 *previous = NULL;
    while(cur_index_serviced < current_inode -> i_size){
        current = (struct ext2_dir_entry_2 *) (current_block + (cur_index_serviced % BLOCK_SIZE));
        // Checks if current entry is the one to be deleted(name match)
        if (strlen(filename) == current -> name_len && !strncmp(filename, current -> name, strlen(filename))){
		    //If there is no previous entry, two cases exist.
            if(previous==NULL){
				//If the current directory entry takes up the entire block, "free" block by unsetting the corresponding bit in bitmap.
				//and "shift" the i_block numbers down an index including the indirect blocks.
                if(current->rec_len==BLOCK_SIZE){
                    //unset block and move blocks after by 1.
                    unsigned int cur_index = cur_inode_index;
                    unsigned int blocks_used = (current_inode -> i_size > 0)? ((current_inode -> i_size - 1)/BLOCK_SIZE) + 1 : 0;
                    //Loops through all blocks coming up and shifts
					if(cur_inode_index < 12){
						unset_bit(get_block(disk, gd->bg_block_bitmap, BLOCK_SIZE), current_inode -> i_block[cur_inode_index] - 1);
						gd -> bg_free_blocks_count++;
					}
					else{
                        //Gets indirect block and unsets bit of corresponding element in that block
						unsigned int *indirect = (unsigned int*) get_block(disk, current_inode -> i_block[12], BLOCK_SIZE);
						unset_bit(get_block(disk, gd->bg_block_bitmap, BLOCK_SIZE), indirect[cur_inode_index - 12] - 1);
					}
                    //Loops through all blocks coming up
                    while(cur_index < blocks_used-1){
                        unsigned int* indirect_block;
                        //We are on the 11th iblock
                        if(cur_index==11){
                            //Implies 12th block is being used does the shift
							indirect_block = (unsigned int*)get_block(disk, current_inode->i_block[cur_index+1], BLOCK_SIZE);
                            current_inode->i_block[cur_index]=indirect_block[0];
							//if indirect block is being used
                            if(cur_index+1 < blocks_used-1){
								unset_bit(get_block(disk, gd->bg_block_bitmap, BLOCK_SIZE), current_inode -> i_block[12] - 1);
								current_inode -> i_blocks -= 2;
								//IN THIS CASE FREE THE BLOCK BY UNSETTING THE I_BLOCK[12]TH BIT IN THE BITMAP
                            }
                        }else{
                            //If entry is in indirect block complete the shift within the indirect block.
                            if(cur_index>=12){
                                indirect_block[cur_index - 12] = indirect_block[cur_index - 11];
                            }
							else{
								current_inode->i_block[cur_index] = current_inode->i_block[cur_index+1];
							}
                        }
						// Increment curr index
						cur_index++;
                    }
                    current_inode -> i_size -= BLOCK_SIZE;
                    current_inode -> i_blocks -= 2;
				}
			    else{
				    //Copy next element in place of current and increment length.
					//Setting previous to next element
					unsigned short current_reclen = current -> rec_len;
					//AND NEXT NOW REPRESENTS NEXT
					struct ext2_dir_entry_2 *next = (struct ext2_dir_entry_2 *) (current_block + ((cur_index_serviced + current -> rec_len) % BLOCK_SIZE));
					memcpy(current, next, next->rec_len); //Note previous is now the next element
					current -> rec_len += current_reclen;
                }
			}else{
                    //Not first
                    previous->rec_len += current->rec_len;
            }
			break;
        }
        //Sets the previous element to current and incremenets cur index
        previous = current;
        cur_index_serviced += current -> rec_len;
        if (cur_index_serviced < current_inode->i_size && cur_index_serviced % BLOCK_SIZE == 0){
            cur_inode_index++;
            current_block = get_block_from_inode(inode_base, inode_num, cur_inode_index);
            previous=NULL;
        }
    }
    
    //unsets all block bits
    char *bitmaprs = get_block(disk, gd->bg_block_bitmap, BLOCK_SIZE);
    char *bitmapind = get_block(disk, gd->bg_inode_bitmap, BLOCK_SIZE);
    // Decrements i_link_count to make sure it is 0 else we cant
    // unset as there is still a hard link pointing to it.
	current_file_inode -> i_links_count --;
    // unset inode(unset_bit) if no hard links and unset all block bits(free_inode helper)
	if(current_file_inode -> i_links_count == 0){
		gd -> bg_free_inodes_count++;
		gd -> bg_free_blocks_count += current_file_inode -> i_blocks / 2;
		free_inode(current_file_inode, disk, bitmaprs);
		unset_bit(bitmapind, file_info.inode - 1);
	}
	printf("File removed.");
    return 0;
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x23c1
movq 0x2c52(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xcd2(%rip), %rsi       # 0x3082
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x1100
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10e0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, 0x2c9d(%rip)      # 0x50a8
movq $-0x1, %rax
cmpq %rax, 0x2c8f(%rip)      # 0x50a8
jne 0x2431
leaq 0xc9c(%rip), %rdi       # 0x30be
callq 0x10f0
movl $0x1, %edi
callq 0x1100
movq 0x2c70(%rip), %rax      # 0x50a8
addq $0x400, %rax            # imm = 0x400
movq %rax, -0x28(%rbp)
movq 0x2c5f(%rip), %rax      # 0x50a8
addq $0x800, %rax            # imm = 0x800
movq -0x28(%rbp), %rcx
movzwl 0x5e(%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1110
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1110
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1090
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
callq 0x10c0
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq 0x2c00(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq -0xf8(%rbp), %rdi
movq %rax, %rsi
movq -0x48(%rbp), %rdx
callq 0x1390
cmpl $0x0, %eax
jne 0x24f5
movq 0x2b20(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xbe1(%rip), %rsi       # 0x30c3
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x2bb3
movq -0x50(%rbp), %rax
movq %rax, -0x100(%rbp)
movq 0x2ba1(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq -0x100(%rbp), %rdi
movq %rax, %rsi
callq 0x13e0
movq %rax, -0x58(%rbp)
cmpl $0xb, -0x54(%rbp)
jae 0x255b
cmpl $0x2, -0x54(%rbp)
je 0x255b
movq 0x2aba(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xb94(%rip), %rsi       # 0x30dc
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x2bb3
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x108(%rbp)
movq 0x2b37(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x13e0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x58(%rbp)
movsbl -0x58(%rbp), %eax
cmpl $0x64, %eax
jne 0x25ca
movq 0x2a4b(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xb39(%rip), %rsi       # 0x30f0
movb $0x0, %al
callq 0x10a0
movl $0x15, -0x4(%rbp)
jmp 0x2bb3
movl -0x54(%rbp), %eax
movl %eax, -0x64(%rbp)
movq 0x2ad1(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, %rdi
movl -0x64(%rbp), %esi
callq 0x17c0
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x110(%rbp)
movq 0x2a9f(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x13e0
movq %rax, -0x78(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x20a0
movq %rax, -0x80(%rbp)
movq 0x2a5d(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x17c0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movq 0x2a04(%rip), %rdi      # 0x50a8
movl -0x94(%rbp), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movl -0x98(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2af4
movq -0xa8(%rbp), %rax
movl -0x98(%rbp), %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1060
movq -0xb0(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
cmpq %rcx, %rax
jne 0x2a72
movq -0x80(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x118(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1060
movq -0x120(%rbp), %rdi
movq -0x118(%rbp), %rsi
movq %rax, %rdx
callq 0x1050
cmpl $0x0, %eax
jne 0x2a72
cmpq $0x0, -0xb8(%rbp)
jne 0x2a51
movq -0xb0(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x400, %eax            # imm = 0x400
jne 0x29d9
movl -0x9c(%rbp), %eax
movl %eax, -0xbc(%rbp)
movq -0x90(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jbe 0x27c3
movq -0x90(%rbp), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
shrl $0xa, %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x27cd
xorl %eax, %eax
movl %eax, -0x124(%rbp)
jmp 0x27cd
movl -0x124(%rbp), %eax
movl %eax, -0xc0(%rbp)
cmpl $0xc, -0x9c(%rbp)
jae 0x2827
movq 0x28bf(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, %rdi
movq -0x90(%rbp), %rax
movl -0x9c(%rbp), %ecx
movl 0x28(%rax,%rcx,4), %esi
subl $0x1, %esi
callq 0x1bf0
movq -0x30(%rbp), %rax
movw 0xc(%rax), %cx
addw $0x1, %cx
movw %cx, 0xc(%rax)
jmp 0x2880
movq 0x287a(%rip), %rdi      # 0x50a8
movq -0x90(%rbp), %rax
movl 0x58(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, -0xc8(%rbp)
movq 0x2858(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, %rdi
movq -0xc8(%rbp), %rax
movl -0x9c(%rbp), %ecx
subl $0xc, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %esi
subl $0x1, %esi
callq 0x1bf0
jmp 0x2882
movl -0xbc(%rbp), %eax
movl -0xc0(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x29b4
cmpl $0xb, -0xbc(%rbp)
jne 0x2942
movq 0x27fb(%rip), %rdi      # 0x50a8
movq -0x90(%rbp), %rax
movl -0xbc(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl 0x28(%rax,%rcx,4), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movl (%rax), %edx
movq -0x90(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %edx, 0x28(%rax,%rcx,4)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl -0xc0(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x2940
movq 0x279d(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, %rdi
movq -0x90(%rbp), %rax
movl 0x58(%rax), %esi
subl $0x1, %esi
callq 0x1bf0
movq -0x90(%rbp), %rax
movl 0x1c(%rax), %ecx
subl $0x2, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x29a0
cmpl $0xc, -0xbc(%rbp)
jb 0x2977
movq -0xd0(%rbp), %rax
movl -0xbc(%rbp), %ecx
subl $0xb, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq -0xd0(%rbp), %rax
movl -0xbc(%rbp), %ecx
subl $0xc, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x299e
movq -0x90(%rbp), %rax
movl -0xbc(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl 0x28(%rax,%rcx,4), %edx
movq -0x90(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %edx, 0x28(%rax,%rcx,4)
jmp 0x29a0
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x2882
movq -0x90(%rbp), %rax
movl 0x4(%rax), %ecx
subl $0x400, %ecx            # imm = 0x400
movl %ecx, 0x4(%rax)
movq -0x90(%rbp), %rax
movl 0x1c(%rax), %ecx
subl $0x2, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x2a4f
movq -0xb0(%rbp), %rax
movw 0x4(%rax), %ax
movw %ax, -0xd2(%rbp)
movq -0xa8(%rbp), %rax
movl -0x98(%rbp), %ecx
movq -0xb0(%rbp), %rdx
movzwl 0x4(%rdx), %edx
addl %edx, %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq -0xe0(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, %edx
callq 0x10b0
movzwl -0xd2(%rbp), %edx
movq -0xb0(%rbp), %rax
movzwl 0x4(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x4(%rax)
jmp 0x2a6d
movq -0xb0(%rbp), %rax
movzwl 0x4(%rax), %edx
movq -0xb8(%rbp), %rax
movzwl 0x4(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x4(%rax)
jmp 0x2af4
movq -0xb0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x98(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2aef
movl -0x98(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl $0x0, %eax
jne 0x2aef
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
movq -0x88(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x9c(%rbp), %edx
callq 0x1880
movq %rax, -0xa8(%rbp)
movq $0x0, -0xb8(%rbp)
jmp 0x26d4
movq 0x25ad(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, -0xe8(%rbp)
movq 0x258f(%rip), %rdi      # 0x50a8
movq -0x30(%rbp), %rax
movl 0x4(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17e0
movq %rax, -0xf0(%rbp)
movq -0x70(%rbp), %rax
movw 0x1a(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x1a(%rax)
movq -0x70(%rbp), %rax
movzwl 0x1a(%rax), %eax
cmpl $0x0, %eax
jne 0x2b9e
movq -0x30(%rbp), %rax
movw 0xe(%rax), %cx
addw $0x1, %cx
movw %cx, 0xe(%rax)
movq -0x70(%rbp), %rax
movl 0x1c(%rax), %edx
shrl %edx
movq -0x30(%rbp), %rax
movzwl 0xc(%rax), %ecx
addl %edx, %ecx
movw %cx, 0xc(%rax)
movq -0x70(%rbp), %rdi
movq 0x2528(%rip), %rsi      # 0x50a8
movq -0xe8(%rbp), %rdx
callq 0x22a0
movq -0xf0(%rbp), %rdi
movl -0x54(%rbp), %esi
subl $0x1, %esi
callq 0x1bf0
leaq 0x567(%rip), %rdi       # 0x310c
movb $0x0, %al
callq 0x1080
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq