// File: /workspace/github_c_makefile_star2_O2/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 $0x618, %rsp            # imm = 0x618
movl %edi, %r13d
leal -0x6(%r13), %eax
cmpl $-0x3, %eax
jbe 0x1525
movq %rsi, %r15
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10b0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1060
movq %rax, 0x2e46(%rip)      # 0x4098
cmpq $-0x1, %rax
je 0x1543
callq 0x1673
movq %rax, 0x2e38(%rip)      # 0x40a0
movq 0x10(%r15), %rsi
cmpl $0x4, %r13d
jne 0x1276
pushq $0x18
jmp 0x1299
cmpb $0x2d, (%rsi)
jne 0x1510
cmpb $0x73, 0x1(%rsi)
jne 0x1510
cmpb $0x0, 0x2(%rsi)
jne 0x1510
movq 0x18(%r15), %rsi
pushq $0x20
popq %r14
leaq -0x440(%rbp), %r12
movq %r12, %rdi
callq 0x1040
movq (%r15,%r14), %rsi
leaq -0x640(%rbp), %rdi
callq 0x1040
leaq -0x540(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x1040
cmpb $0x2f, (%r15)
jne 0x146c
leaq -0x540(%rbp), %rdi
callq 0x1b9e
testl %eax, %eax
je 0x14bf
movl %eax, %r15d
movq 0x2dac(%rip), %rax      # 0x40a0
movslq %r15d, %r14
shlq $0x7, %r14
testb $0x40, -0x7f(%rax,%r14)
jne 0x1475
movq %rax, -0x30(%rbp)
movq %r13, -0x38(%rbp)
leaq -0x340(%rbp), %rdi
leaq -0x640(%rbp), %r12
movq %r12, %rsi
callq 0x1040
leaq -0x240(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1040
cmpb $0x2f, (%r13)
jne 0x148a
cmpb $0x0, -0x23f(%rbp)
je 0x1493
leaq -0x240(%rbp), %r12
movq %r12, %rdi
callq 0x1c9b
movq %r12, %rdi
callq 0x1b9e
testl %eax, %eax
je 0x14b3
movq 0x2d2b(%rip), %rcx      # 0x40a0
cltq
shlq $0x7, %rax
leaq (%rcx,%rax), %rbx
addq $-0x80, %rbx
leaq -0x140(%rbp), %r12
leaq -0x340(%rbp), %rsi
movq %r12, %rdi
callq 0x1c66
movq %rbx, %rdi
movq %r12, %rsi
callq 0x192c
testl %eax, %eax
jne 0x14dd
cmpl $0x4, -0x38(%rbp)
jne 0x13d9
movq -0x30(%rbp), %r12
addq %r14, %r12
pushq $0x1
popq %rdx
leaq -0x140(%rbp), %rcx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1b0b
incw -0x66(%r12)
jmp 0x1465
movl $0xa000, %edi           # imm = 0xA000
callq 0x16b9
movq 0x2cb6(%rip), %r12      # 0x40a0
movslq %eax, %r15
movq %r15, %r13
shlq $0x7, %r13
leaq (%r12,%r13), %r14
leaq -0x340(%rbp), %rdi
callq 0x1050
movl %eax, -0x7c(%r12,%r13)
andl $0x0, -0x64(%r12,%r13)
pushq $0x7
popq %rdx
leaq -0x140(%rbp), %rcx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1b0b
cmpl $0x3c, -0x7c(%r12,%r13)
ja 0x1444
addq $-0x58, %r14
leaq -0x440(%rbp), %rsi
pushq $0x3c
popq %rdx
movq %r14, %rdi
callq 0x1030
jmp 0x1465
callq 0x18dd
movl %eax, %ebx
movl %eax, %edi
callq 0x18c7
leaq -0x440(%rbp), %rsi
movq %rax, %rdi
callq 0x1040
movl %ebx, -0x58(%r14)
xorl %ebx, %ebx
jmp 0x14fc
leaq 0xbf9(%rip), %rdi       # 0x206c
jmp 0x14ba
movq 0x2b7c(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc17(%rip), %rdi       # 0x209d
pushq $0x17
jmp 0x14a6
leaq 0xc24(%rip), %rdi       # 0x20b5
jmp 0x14ba
movq 0x2b5e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc27(%rip), %rdi       # 0x20cb
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
pushq $0x15
jmp 0x14fb
leaq 0xc2b(%rip), %rdi       # 0x20e5
callq 0x10c0
movq 0x2b32(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xbb2(%rip), %rdi       # 0x2082
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x14fc
movq 0x2b14(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc0f(%rip), %rdi       # 0x20fd
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
pushq $0x11
popq %rbx
movl %ebx, %eax
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2ae1(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb3d(%rip), %rdi       # 0x205e
pushq $0xd
jmp 0x1538
movq 0x2acc(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xace(%rip), %rdi       # 0x2004
pushq $0x54
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x154f
leaq 0xb0f(%rip), %rdi       # 0x2059
callq 0x10c0
pushq $0x1
popq %rdi
callq 0x10e0
nop