// File: /workspace/github_c_makefile_star2_O1/JavieW[P]CSCC69_Ext2_File-System/ext2_rm.c
// Function: main
int main(int argc, char **argv) {
    char parentDirPath[EXT2_NAME_LEN];
    char fileName[EXT2_NAME_LEN];
    int parentInodeNum, childInodeNum;
    struct ext2_inode *inodeTable;
    struct ext2_inode *parentInode, *childInode;

    if(argc!=3) {
        fprintf(stderr, "Usage: ext2_rm <image file name> <absolute path>\n");
        exit(1);
    }

    // read disk and get inode table
    int fd = open(argv[1], O_RDWR);
    disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    inodeTable = getInodeTable();

    // get the parent directory inode
    strcpy(parentDirPath, argv[2]);
    // if it's not a absolute path
    if (parentDirPath[0]!='/') {
        fprintf(stderr, "Must be an absolute path\n");
        return ENOENT;
    // if it's a root directory
    } else if (parentDirPath[1]=='\0'){
        fprintf(stderr, "Cannot remove root directory\n");
        return ENOENT;
    // general case
    } else {
        getParentDirPath(parentDirPath);
    }
    parentInodeNum = getInodeFromPath(parentDirPath);
    if (parentInodeNum == 0) {
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    parentInode = &inodeTable[parentInodeNum-1];

    // check file exist
    getFileNameFromPath(fileName, argv[2]);
    if (strcmp(fileName, ".") == 0 || strcmp(fileName, "..") == 0){
        fprintf(stderr, "Cannot remove . or ..\n");
        return ENOENT;
    }
    childInodeNum = searchFileInDir(parentInode, fileName);
    if (childInodeNum == 0) {
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    childInode = &inodeTable[childInodeNum-1];

    // if the file is a directory
    if (childInode->i_mode & EXT2_S_IFDIR) {
        fprintf(stderr, "Cannot remove a directory\n");
        return ENOENT;
    }

    rm(parentInode, fileName);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
cmpl $0x3, %edi
jne 0x1460
movq %rsi, %r14
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10b0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1050
movq %rax, 0x2d3d(%rip)      # 0x4098
cmpq $-0x1, %rax
je 0x1465
callq 0x1595
movq %rax, %rbx
movq 0x10(%r14), %rsi
leaq -0x220(%rbp), %r15
movq %r15, %rdi
callq 0x1030
cmpb $0x2f, (%r15)
jne 0x142c
cmpb $0x0, -0x21f(%rbp)
je 0x1433
movq %r15, %rdi
callq 0x1cdd
movq %r15, %rdi
callq 0x1bc2
testl %eax, %eax
je 0x143a
movl %eax, %r15d
movq 0x10(%r14), %rsi
leaq -0x120(%rbp), %r14
movq %r14, %rdi
callq 0x1c96
cmpw $0x2e, (%r14)
je 0x1425
movl $0x2e2e, %eax           # imm = 0x2E2E
xorl -0x120(%rbp), %eax
movzbl -0x11e(%rbp), %ecx
orw %ax, %cx
je 0x1425
movslq %r15d, %rax
shlq $0x7, %rax
leaq (%rbx,%rax), %r14
addq $-0x80, %r14
leaq -0x120(%rbp), %rsi
movq %r14, %rdi
callq 0x18d2
testl %eax, %eax
je 0x1441
cltq
shlq $0x7, %rax
testb $0x40, -0x7f(%rbx,%rax)
jne 0x1448
leaq -0x120(%rbp), %rsi
movq %r14, %rdi
callq 0x1d43
xorl %eax, %eax
jmp 0x1452
callq 0x119e
jmp 0x144d
callq 0x1138
jmp 0x144d
callq 0x11e2
jmp 0x144d
callq 0x11c0
jmp 0x144d
callq 0x117c
jmp 0x144d
callq 0x115a
movl $0x2, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x1110
callq 0x1204
nop