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

    if(argc!=4) {
        fprintf(stderr, "Usage: ext2_cp <image file name> <native path> <absolute path on the disk>\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();

    // open native file for read
    if((src_fd = fopen(argv[2], "r")) == NULL) {
		fprintf(stderr, "No such file or directory\n");
        return ENOENT;
	}

    // get the parent directory inode
    strcpy(parentDirPath, argv[3]);
    if (parentDirPath[0]!='/') {
        fprintf(stderr, "Must be absolute path\n");
        return ENOENT;
    } else if (parentDirPath[1]=='\0'){
        fprintf(stderr, "Destination cannot be root directory\n");
        return EEXIST;
    } 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[3]);
    childInodeNum = searchFileInDir(parentInode, fileName);
    if (childInodeNum != 0) {
        fprintf(stderr, "File or directory already exist\n");
        return EEXIST;
    }

    // create file and cp
    childInodeNum = initInode(EXT2_S_IFREG);
    childInode = &inodeTable[childInodeNum-1];
    unsigned int *singleIndirect = NULL;
    int nextBlockNum, byteRead;
    int fileSize = 0;
    int i = 0;
    while (!feof(src_fd)) {
        nextBlockNum = allocateNewBlock();
        if (i<12) {
            childInode->i_block[i] = nextBlockNum;
        } else if (i==12) {
            childInode->i_block[i] = nextBlockNum;
            singleIndirect = initSingleIndirect(nextBlockNum);
            i++;
            continue;
        } else {
            singleIndirect[i-13] = nextBlockNum;
        }

        byteRead = fread(getBlock(nextBlockNum), 1, 1024, src_fd);
        fileSize += byteRead;
        i++;
    }
    fclose(src_fd);
    // update inode fields
    childInode->i_size = fileSize;
    if (singleIndirect == NULL)
        childInode->i_blocks = ((fileSize+1023)/1024)*2;
    else
        childInode->i_blocks = ((fileSize+1023)/1024+1)*2;
    // add dir_entry fot this file into parent dir
    initNewDirent(parentInode, childInodeNum, EXT2_FT_REG_FILE, fileName);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
cmpl $0x4, %edi
jne 0x15bd
movq %rsi, %r15
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10e0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x3d29(%rip)      # 0x50b8
cmpq $-0x1, %rax
je 0x15c2
callq 0x16f1
movq %rax, %r12
movq 0x10(%r15), %rdi
leaq 0x1ca9(%rip), %rsi      # 0x3055
callq 0x10f0
testq %rax, %rax
je 0x1590
movq %rax, %r14
movq 0x18(%r15), %rsi
leaq -0x150(%rbp), %rbx
movq %rbx, %rdi
callq 0x1030
cmpb $0x2f, (%rbx)
jne 0x1597
cmpb $0x0, -0x14f(%rbp)
je 0x159e
movq %rbx, %rdi
callq 0x1e28
movq %rbx, %rdi
callq 0x1d0d
testl %eax, %eax
je 0x15a5
cltq
shlq $0x7, %rax
leaq (%r12,%rax), %rbx
addq $-0x80, %rbx
movq 0x18(%r15), %rsi
leaq -0x250(%rbp), %r15
movq %r15, %rdi
callq 0x1de1
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a2e
testl %eax, %eax
jne 0x15b1
movq %rbx, -0x38(%rbp)
movl $0x8000, %edi           # imm = 0x8000
callq 0x1737
movl %eax, %r15d
movslq %eax, %rbx
shlq $0x7, %rbx
leaq (%r12,%rbx), %r13
movq %r14, %rdi
callq 0x10c0
testl %eax, %eax
je 0x148b
movq %r14, %rdi
callq 0x1050
movl $0x0, -0x7c(%r13)
xorl %ebx, %ebx
movq -0x38(%rbp), %rdi
leal 0x3ff(%rbx), %eax
addl $0x7fe, %ebx            # imm = 0x7FE
testl %eax, %eax
cmovnsl %eax, %ebx
sarl $0xa, %ebx
addl %ebx, %ebx
jmp 0x1557
movq %r13, -0x40(%rbp)
movl %r15d, -0x2c(%rbp)
leaq (%rbx,%r12), %rax
addq $-0x58, %rax
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rax, -0x48(%rbp)
leaq (%rax,%r15,4), %r13
callq 0x19b4
movq %r13, %rcx
cmpq $0xc, %r15
jb 0x14c3
je 0x14f8
leaq (%r12,%r15,4), %rcx
addq $-0x34, %rcx
movl %eax, (%rcx)
movl %eax, %edi
callq 0x199e
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
movq %r14, %rcx
callq 0x1040
addl %eax, %ebx
incq %r15
movq %r14, %rdi
callq 0x10c0
addq $0x4, %r13
testl %eax, %eax
je 0x14ab
jmp 0x151f
movq -0x40(%rbp), %rcx
movl %eax, -0x28(%rcx)
movl %eax, %edi
callq 0x1cec
movq %rax, %r12
movq %r14, %rdi
callq 0x10c0
movl $0xd, %r15d
testl %eax, %eax
movq -0x48(%rbp), %rax
je 0x14a7
movq %r14, %rdi
callq 0x1050
movq -0x40(%rbp), %r13
movl %ebx, -0x7c(%r13)
testq %r12, %r12
je 0x1583
leal 0x3ff(%rbx), %eax
addl $0x7fe, %ebx            # imm = 0x7FE
testl %eax, %eax
cmovnsl %eax, %ebx
sarl $0xa, %ebx
leal 0x2(,%rbx,2), %ebx
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %r15d
movl %ebx, -0x64(%r13)
leaq -0x250(%rbp), %rcx
movl %r15d, %esi
movl $0x1, %edx
callq 0x1c81
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %r15d
jmp 0x1470
callq 0x1200
jmp 0x15aa
callq 0x1178
jmp 0x15aa
callq 0x11de
jmp 0x15b6
callq 0x11bc
movl $0x2, %eax
jmp 0x1571
callq 0x119a
movl $0x11, %eax
jmp 0x1571
callq 0x1150
callq 0x122f
nop