// File: /workspace/github_c_makefile_star2_O2/kevinp89[P]Operating-System-EXT2-file-system-with-commands/ext2_rm.c
// Function: main
int main(int argc, char **argv) {
	// We need two arguments, so argc must be 3.
	if (argc != 3) {
		fprintf(stderr, "Usage: %s [disk] [path]\n", argv[0]);
		exit(1);
	}
	// Opening disk
	int fd = open(argv[1], O_RDWR);
	if (!fd) {
		fprintf(stderr, "Disk image '%s' not found.", argv[1]);
		exit(ENOENT);
	}
	// mmap the disk
	disk = mmap(NULL, 128*1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (disk == MAP_FAILED) {
		perror("mmap");
		exit(1);
	}
	// Grabbing super block and block descriptor
	sb = (struct ext2_super_block *)(disk + 1024);
	desc = (struct ext2_group_desc *)(disk + 2*1024);
	// Grabbing inode map and data map
	unsigned char *imap = (unsigned char *)(disk + 1024 * desc->bg_inode_bitmap);
	unsigned char *map = (unsigned char *)(disk + 1024 * desc->bg_block_bitmap);
	// Get parent inode index
	int parent_inode_index = check_parent(disk, argv[2]);
	if (parent_inode_index == -1) { // Directory not found.
		fprintf(stderr, "Directory does not exist.");
		exit(ENOENT);
	}
	// Get file name
	int i;
	int filename_len = 0;
	if (argv[2][strlen(argv[2])-1] == '/') {
		fprintf(stderr, "Invalid filename.");
		exit(ENOENT);
	}
	for (i = strlen(argv[2]); argv[2][i] != '/'; i--) {
		filename_len++;
	}
	char filename[filename_len];
	strcpy(filename, argv[2] + i + 1);
	// Check that the file exists
	struct ext2_inode *parent = (struct ext2_inode *)((disk + 1024 * desc->bg_inode_table) +
													(sb->s_inode_size * parent_inode_index));
	int targ_inode = search_directories(disk, parent, filename, 0);
	if (targ_inode == -1) {
		fprintf(stderr, "File does not exist.");
		exit(ENOENT);
	}
	// Find whether the directory is a link or file
	struct ext2_inode *target = (struct ext2_inode *)((disk + 1024 * desc->bg_inode_table) +
													(sb->s_inode_size * targ_inode));
	if (get_inode_type(target) == 'd') {
		fprintf(stderr, "Cannot remove directories.");
		exit(ENOENT);
	} else {
		// Directory stuff straight from readimage.c
		int cur_rec_len;
		char curfilename[256];
		struct ext2_dir_entry *cur_dir;
		struct ext2_dir_entry *prev_dir;
		for (i = 0; parent->i_block[i] != 0; i++) {
			prev_dir = NULL;
			for (cur_rec_len = 0; cur_rec_len < 1024; ) {
				memset(curfilename, 0, sizeof(curfilename));
				cur_dir = (struct ext2_dir_entry *)(disk + (1024 * parent->i_block[i]) + cur_rec_len);
				strncpy(curfilename, cur_dir->name, cur_dir->name_len);
				cur_rec_len += cur_dir->rec_len;
				// Checking if the current file is the file we're looking for
				if (strcmp(curfilename, filename) == 0) {
					if (prev_dir != NULL) {
						prev_dir->rec_len += cur_dir->rec_len;
						break;
					} else {
						// rec-len is 1024, dir_entry has its own data block
						parent->i_block[i] = 0;
						set_node(parent->i_block[i] - 1, map);
						adjust_free_blocks(1, sb, desc);
					}
				} else {
					// Setting the prev_dir to modify later
					prev_dir = cur_dir;
				}
			}
		}
		target->i_links_count--;
		if (target->i_links_count <= 0) {
			// De-allocate everything, set inode's i_dtime, find directory and set prev dir's rec_len over
			target->i_dtime = (unsigned int)time(0);
			set_node(targ_inode, imap);
			adjust_free_inodes(1, sb, desc);
			// De-allocate the data blocks as well
			for (i = 0; target->i_block[i] != 0; i++) {
				set_node(target->i_block[i] - 1, map);
				adjust_free_blocks(1, sb, desc);
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %r12
cmpl $0x3, %edi
jne 0x17bf
movq 0x8(%r12), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
je 0x17dd
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2b72(%rip)      # 0x40a8
cmpq $-0x1, %rax
je 0x17fc
movq %rax, %r15
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x2b60(%rip)      # 0x40b0
leaq 0x800(%r15), %rax
movq %rax, 0x2b5a(%rip)      # 0x40b8
movl 0x800(%r15), %r14d
movl 0x804(%r15), %r13d
movq 0x10(%r12), %rsi
movq %r15, %rdi
callq 0x1389
cmpl $-0x1, %eax
je 0x180c
movl %eax, %ebx
movq 0x10(%r12), %r12
movq %r12, %rdi
callq 0x1060
cmpb $0x2f, -0x1(%r12,%rax)
je 0x1821
shll $0xa, %r13d
addq %r15, %r13
movq %r13, -0x50(%rbp)
shll $0xa, %r14d
addq %r15, %r14
movq %r14, -0x48(%rbp)
cltq
leaq (%rax,%r12), %rsi
incq %rsi
xorl %eax, %eax
cmpb $0x2f, -0x1(%rsi)
je 0x15cc
decq %rsi
incq %rax
jmp 0x15be
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
callq 0x1050
movq 0x2abc(%rip), %rdi      # 0x40a8
movq 0x2ac5(%rip), %rax      # 0x40b8
movl 0x8(%rax), %ecx
shll $0xa, %ecx
addq %rdi, %rcx
movq 0x2aad(%rip), %rax      # 0x40b0
movzwl 0x58(%rax), %r14d
movslq %ebx, %rsi
movq %rcx, %rbx
imulq %r14, %rsi
addq %rcx, %rsi
movq %rsi, -0x30(%rbp)
movq %r15, -0x60(%rbp)
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x12b8
cmpl $-0x1, %eax
je 0x1836
movl %eax, -0x3c(%rbp)
cltq
imulq %r14, %rax
addq %rax, %rbx
movq %rbx, -0x58(%rbp)
movq %rbx, %rdi
callq 0x126e
cmpb $0x64, %al
je 0x184b
xorl %ebx, %ebx
movq -0x30(%rbp), %rax
cmpl $0x0, 0x28(%rax,%rbx,4)
je 0x1724
movq %rbx, -0x38(%rbp)
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpl $0x3ff, %r15d           # imm = 0x3FF
ja 0x1718
movq %r13, %r14
movl $0x100, %edx            # imm = 0x100
leaq -0x160(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1080
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %eax
shll $0xa, %eax
addq 0x2a01(%rip), %rax      # 0x40a8
movl %r15d, %ecx
leaq (%rax,%rcx), %r13
leaq (%rcx,%rax), %r12
addq $0x8, %r12
movzbl -0x2(%r12), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1040
movzwl -0x4(%r12), %r12d
addl %r12d, %r15d
movq %rbx, %rdi
movq -0x60(%rbp), %rsi
callq 0x1090
testl %eax, %eax
jne 0x166b
testq %r14, %r14
jne 0x1713
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
andl $0x0, 0x28(%rax,%rcx,4)
movq -0x48(%rbp), %rax
xorb $-0x80, (%rax)
movq 0x29b0(%rip), %rax      # 0x40b0
movq 0x29b1(%rip), %rcx      # 0x40b8
incl 0xc(%rax)
incw 0xc(%rcx)
jmp 0x1668
addw %r12w, 0x4(%r14)
movq -0x38(%rbp), %rbx
incq %rbx
jmp 0x1652
movq -0x58(%rbp), %r14
decw 0x1a(%r14)
pushq $0x1
popq %rbx
je 0x1743
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x10b0
movl -0x3c(%rbp), %edx
movl %edx, %ecx
andb $0x7, %cl
movl %ebx, %edi
shll %cl, %edi
movl %eax, 0x14(%r14)
pushq $0x8
popq %rsi
movl %edx, %eax
cltd
idivl %esi
cltq
movq -0x50(%rbp), %rcx
xorb %dil, (%rcx,%rax)
movq 0x293d(%rip), %rax      # 0x40b0
movq 0x293e(%rip), %rcx      # 0x40b8
incl 0x10(%rax)
incw 0xe(%rcx)
addq $0x28, %r14
movl (%r14), %eax
testl %eax, %eax
je 0x1732
decl %eax
movl %eax, %ecx
andb $0x7, %cl
movl %ebx, %edi
shll %cl, %edi
cltd
idivl %esi
cltq
movq -0x48(%rbp), %rcx
xorb %dil, (%rcx,%rax)
movq 0x2905(%rip), %rax      # 0x40b0
movq 0x2906(%rip), %rcx      # 0x40b8
incl 0xc(%rax)
incw 0xc(%rcx)
addq $0x4, %r14
jmp 0x1785
movq 0x2832(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x846(%rip), %rsi       # 0x201a
xorl %eax, %eax
callq 0x10a0
jmp 0x1808
movq 0x2814(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x8(%r12), %rdx
leaq 0x840(%rip), %rsi       # 0x2033
xorl %eax, %eax
callq 0x10a0
jmp 0x1867
leaq 0x84b(%rip), %rdi       # 0x204e
callq 0x10e0
pushq $0x1
jmp 0x1869
movq 0x27e5(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x836(%rip), %rdi       # 0x2053
pushq $0x19
jmp 0x185e
movq 0x27d0(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x83b(%rip), %rdi       # 0x206d
pushq $0x11
jmp 0x185e
movq 0x27bb(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x838(%rip), %rdi       # 0x207f
pushq $0x14
jmp 0x185e
movq 0x27a6(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x838(%rip), %rdi       # 0x2094
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $0x2
popq %rdi
callq 0x10f0