// File: /workspace/github_c_makefile_star2_O3/JavieW[P]CSCC69_Ext2_File-System/ext2_ln.c
// Function: main
int main(int argc, char **argv) {

    char pathTo[EXT2_NAME_LEN];
    char pathFrom[EXT2_NAME_LEN];
    char pathToCopy[EXT2_NAME_LEN];
    char pathFromCopy[EXT2_NAME_LEN];
    char parentOfPathFrom[EXT2_NAME_LEN];
    char linkName[EXT2_NAME_LEN];

    int flagged = FALSE;
    int inodeNum, parentInodeNum, childInodeNum;

    struct ext2_inode *targetInode, *parentInode, *childInode;

    if(argc!=4 && argc!=5) {
        fprintf(stderr, "Usage: ext2_ln <image file name> <optional flag -s> <absolute path> <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();

    // read other arguments
    if(argc == 4) {
        strcpy(pathTo, argv[2]);
        strcpy(pathFrom, argv[3]);
    } else {
        if (strcmp(argv[2], "-s") != 0) {
            fprintf(stderr, "Invalid Flag\n");
            exit(1);
        }
        flagged = TRUE;
        strcpy(pathTo, argv[3]);
        strcpy(pathFrom, argv[4]);
    }

    // get the inode from pathTo
    strcpy(pathToCopy, pathTo);
    if (pathToCopy[0]!='/') {
        perror("Invalid path argument");
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    } else {
        inodeNum = getInodeFromPath(pathToCopy);
    }

    if (inodeNum == 0) {
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    targetInode = &inodeTable[inodeNum-1];
    if (targetInode->i_mode & EXT2_S_IFDIR){
        fprintf(stderr, "No link to a directory\n");
        return EISDIR;
    }

    // get the upper level inode from pathFrom
    strcpy(pathFromCopy, pathFrom);
    strcpy(parentOfPathFrom, pathFrom);

    if (parentOfPathFrom[0]!='/') {
        perror("Invalid parentDirPath");
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    } else if (parentOfPathFrom[1]=='\0'){
        fprintf(stderr, "No link from a directory\n");
        return EISDIR;
    } else {
        getParentDirPath(parentOfPathFrom);
    }

    parentInodeNum = getInodeFromPath(parentOfPathFrom);
    if (parentInodeNum == 0) {
        perror("parentDirPath not exist");
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    parentInode = &inodeTable[parentInodeNum-1];

    // check whether the link has already existed
    getFileNameFromPath(linkName, pathFromCopy);
    childInodeNum = searchFileInDir(parentInode, linkName);
    if (childInodeNum != 0) {
        fprintf(stderr, "File or directory already exist\n");
        return EEXIST;
    }

    if (!flagged){

        // implementation for the hard link
        initNewDirent(parentInode, inodeNum, EXT2_FT_REG_FILE, linkName);
        // increment the link count of the target inode
        targetInode->i_links_count++;

    }else{

        // implementation for the symbolic link
        childInodeNum = initInode(EXT2_S_IFLNK);
        childInode = &inodeTable[childInodeNum-1];
        childInode->i_size = strlen(pathFromCopy);
        childInode->i_blocks = 0;

        initNewDirent(parentInode, childInodeNum, EXT2_FT_SYMLINK, linkName);

        if (childInode->i_size <= 60){
            strncpy((char *)childInode->i_block, pathTo, 60);
        }else{    
            // append path to the inode block
            int block_num = allocateNewBlock();
            strcpy((char *)getBlock(block_num), pathTo);
            childInode->i_block[0] = block_num;
        }
        
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x608, %rsp            # imm = 0x608
movl %edi, %r12d
leal -0x6(%r12), %eax
cmpl $-0x3, %eax
jbe 0x1656
movq %rsi, %r14
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10c0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1060
movq %rax, 0x3cb9(%rip)      # 0x50a0
cmpq $-0x1, %rax
je 0x165b
callq 0x1789
movq %rax, 0x3cab(%rip)      # 0x50a8
movq 0x10(%r14), %rsi
movl $0x18, %ebx
cmpl $0x4, %r12d
je 0x1432
cmpb $0x2d, (%rsi)
jne 0x1651
cmpb $0x73, 0x1(%rsi)
jne 0x1651
cmpb $0x0, 0x2(%rsi)
jne 0x1651
movq 0x18(%r14), %rsi
movl $0x20, %ebx
leaq -0x430(%rbp), %r15
movq %r15, %rdi
callq 0x1040
movq (%r14,%rbx), %rsi
leaq -0x630(%rbp), %rdi
callq 0x1040
leaq -0x530(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x1040
cmpb $0x2f, (%r14)
jne 0x1601
leaq -0x530(%rbp), %rdi
callq 0x1da5
testl %eax, %eax
je 0x1608
movl %eax, %r14d
movq 0x3c1d(%rip), %r13      # 0x50a8
movslq %eax, %rbx
shlq $0x7, %rbx
testb $0x40, -0x7f(%r13,%rbx)
jne 0x160f
movq %r12, -0x30(%rbp)
leaq -0x330(%rbp), %rdi
leaq -0x630(%rbp), %r15
movq %r15, %rsi
callq 0x1040
leaq -0x230(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x1040
cmpb $0x2f, (%r12)
jne 0x1616
cmpb $0x0, -0x22f(%rbp)
je 0x161d
leaq -0x230(%rbp), %r15
movq %r15, %rdi
callq 0x1ec0
movq %r15, %rdi
callq 0x1da5
testl %eax, %eax
je 0x1629
movq 0x3ba0(%rip), %rcx      # 0x50a8
cltq
shlq $0x7, %rax
leaq (%rcx,%rax), %r15
addq $-0x80, %r15
leaq -0x130(%rbp), %r12
leaq -0x330(%rbp), %rsi
movq %r12, %rdi
callq 0x1e79
movq %r15, %rdi
movq %r12, %rsi
callq 0x1ac6
testl %eax, %eax
jne 0x1645
cmpl $0x4, -0x30(%rbp)
jne 0x1569
addq %rbx, %r13
leaq -0x130(%rbp), %rcx
movq %r15, %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0x1d19
incw -0x66(%r13)
jmp 0x15fd
movl $0xa000, %edi           # imm = 0xA000
callq 0x17cf
movq 0x3b2e(%rip), %r12      # 0x50a8
movslq %eax, %r14
movq %r14, %r13
shlq $0x7, %r13
leaq (%r12,%r13), %rbx
leaq -0x330(%rbp), %rdi
callq 0x1050
movl %eax, -0x7c(%r12,%r13)
movl $0x0, -0x64(%r12,%r13)
leaq -0x130(%rbp), %rcx
movq %r15, %rdi
movl %r14d, %esi
movl $0x7, %edx
callq 0x1d19
cmpl $0x3c, -0x7c(%r12,%r13)
ja 0x15db
addq $-0x58, %rbx
leaq -0x430(%rbp), %rsi
movl $0x3c, %edx
movq %rbx, %rdi
callq 0x1030
jmp 0x15fd
callq 0x1a4c
movl %eax, %r14d
movl %eax, %edi
callq 0x1a36
leaq -0x430(%rbp), %rsi
movq %rax, %rdi
callq 0x1040
movl %r14d, -0x58(%rbx)
xorl %eax, %eax
jmp 0x1633
callq 0x1148
jmp 0x162e
callq 0x1238
jmp 0x162e
callq 0x1176
jmp 0x1622
callq 0x1198
jmp 0x162e
callq 0x1216
movl $0x15, %eax
jmp 0x1633
callq 0x11e8
movl $0x2, %eax
addq $0x608, %rsp            # imm = 0x608
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11c6
movl $0x11, %eax
jmp 0x1633
callq 0x1120
callq 0x1272
callq 0x125a