// File: /workspace/github_c_makefile_star2_O0/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
subq $0x640, %rsp            # imm = 0x640
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x614(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x1251
cmpl $0x5, -0x8(%rbp)
je 0x1251
movq 0x3dc2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1dc4(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x10f0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10c0
movl %eax, -0x63c(%rbp)
movl -0x63c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1060
movq %rax, 0x3e03(%rip)      # 0x5098
movq $-0x1, %rax
cmpq %rax, 0x3df5(%rip)      # 0x5098
jne 0x12bb
leaq 0x1dad(%rip), %rdi      # 0x3059
callq 0x10d0
movl $0x1, %edi
callq 0x10f0
callq 0x1980
movq %rax, 0x3dd9(%rip)      # 0x50a0
cmpl $0x4, -0x8(%rbp)
jne 0x12f7
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1040
leaq -0x210(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x1040
jmp 0x1364
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x1d58(%rip), %rsi      # 0x305e
callq 0x1090
cmpl $0x0, %eax
je 0x1332
movq 0x3ce1(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d40(%rip), %rsi      # 0x3061
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x10f0
movl $0x1, -0x614(%rbp)
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x1040
leaq -0x210(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x1040
leaq -0x310(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x1040
movsbl -0x310(%rbp), %eax
cmpl $0x2f, %eax
je 0x13b3
leaq 0x1ce5(%rip), %rdi      # 0x306f
callq 0x10d0
movq 0x3c62(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1ce5(%rip), %rsi      # 0x3085
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x16cd
leaq -0x310(%rbp), %rdi
callq 0x2190
movl %eax, -0x618(%rbp)
cmpl $0x0, -0x618(%rbp)
jne 0x13f2
movq 0x3c23(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1ca6(%rip), %rsi      # 0x3085
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x16cd
movq 0x3ca7(%rip), %rax      # 0x50a0
movl -0x618(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x628(%rbp)
movq -0x628(%rbp), %rax
movzwl (%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x144b
movq 0x3bca(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c68(%rip), %rsi      # 0x30a0
movb $0x0, %al
callq 0x10a0
movl $0x15, -0x4(%rbp)
jmp 0x16cd
leaq -0x410(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x1040
leaq -0x510(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x1040
movsbl -0x510(%rbp), %eax
cmpl $0x2f, %eax
je 0x14ad
leaq 0x1c34(%rip), %rdi      # 0x30b8
callq 0x10d0
movq 0x3b68(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1beb(%rip), %rsi      # 0x3085
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x16cd
movsbl -0x50f(%rbp), %eax
cmpl $0x0, %eax
jne 0x14dd
movq 0x3b38(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c04(%rip), %rsi      # 0x30ce
movb $0x0, %al
callq 0x10a0
movl $0x15, -0x4(%rbp)
jmp 0x16cd
leaq -0x510(%rbp), %rdi
callq 0x2320
jmp 0x14eb
leaq -0x510(%rbp), %rdi
callq 0x2190
movl %eax, -0x61c(%rbp)
cmpl $0x0, -0x61c(%rbp)
jne 0x1536
leaq 0x1bdb(%rip), %rdi      # 0x30e8
callq 0x10d0
movq 0x3adf(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1b62(%rip), %rsi      # 0x3085
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x16cd
movq 0x3b63(%rip), %rax      # 0x50a0
movl -0x61c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x630(%rbp)
leaq -0x610(%rbp), %rdi
leaq -0x410(%rbp), %rsi
callq 0x22c0
movq -0x630(%rbp), %rdi
leaq -0x610(%rbp), %rsi
callq 0x1ce0
movl %eax, -0x620(%rbp)
cmpl $0x0, -0x620(%rbp)
je 0x15b0
movq 0x3a65(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1b63(%rip), %rsi      # 0x3100
movb $0x0, %al
callq 0x10a0
movl $0x11, -0x4(%rbp)
jmp 0x16cd
cmpl $0x0, -0x614(%rbp)
jne 0x15ef
movq -0x630(%rbp), %rdi
movl -0x618(%rbp), %esi
leaq -0x610(%rbp), %rcx
movl $0x1, %edx
callq 0x20b0
movq -0x628(%rbp), %rax
movw 0x1a(%rax), %cx
addw $0x1, %cx
movw %cx, 0x1a(%rax)
jmp 0x16c6
movl $0xa000, %edi           # imm = 0xA000
callq 0x19e0
movl %eax, -0x620(%rbp)
movq 0x3a9a(%rip), %rax      # 0x50a0
movl -0x620(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x638(%rbp)
leaq -0x410(%rbp), %rdi
callq 0x1050
movl %eax, %ecx
movq -0x638(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x638(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x630(%rbp), %rdi
movl -0x620(%rbp), %esi
leaq -0x610(%rbp), %rcx
movl $0x7, %edx
callq 0x20b0
movq -0x638(%rbp), %rax
cmpl $0x3c, 0x4(%rax)
ja 0x168f
movq -0x638(%rbp), %rdi
addq $0x28, %rdi
leaq -0x110(%rbp), %rsi
movl $0x3c, %edx
callq 0x1030
jmp 0x16c4
callq 0x1c90
movl %eax, -0x640(%rbp)
movl -0x640(%rbp), %edi
callq 0x1c70
movq %rax, %rdi
leaq -0x110(%rbp), %rsi
callq 0x1040
movl -0x640(%rbp), %ecx
movq -0x638(%rbp), %rax
movl %ecx, 0x28(%rax)
jmp 0x16c6
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x640, %rsp            # imm = 0x640
popq %rbp
retq
nopl (%rax)