// File: /workspace/github_c_makefile_star2_O1/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 0x15a3
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 0x15a8
callq 0x16d9
movq %rax, %r13
movq 0x10(%r15), %rdi
leaq 0x1ca9(%rip), %rsi      # 0x3055
callq 0x10f0
testq %rax, %rax
je 0x1574
movq %rax, %rbx
movq 0x18(%r15), %rsi
leaq -0x150(%rbp), %r14
movq %r14, %rdi
callq 0x1030
cmpb $0x2f, (%r14)
jne 0x157b
cmpb $0x0, -0x14f(%rbp)
je 0x1582
movq %r14, %rdi
callq 0x1e21
movq %r14, %rdi
callq 0x1d06
testl %eax, %eax
je 0x1589
cltq
shlq $0x7, %rax
leaq (%rax,%r13), %r14
addq $-0x80, %r14
movq 0x18(%r15), %rsi
leaq -0x250(%rbp), %r15
movq %r15, %rdi
callq 0x1dda
movq %r14, %rdi
movq %r15, %rsi
callq 0x1a16
testl %eax, %eax
jne 0x1596
movq %r14, -0x48(%rbp)
movl $0x8000, %edi           # imm = 0x8000
callq 0x171f
movl %eax, -0x2c(%rbp)
movslq %eax, %r14
shlq $0x7, %r14
leaq (%r14,%r13), %rax
movq %rax, -0x38(%rbp)
movq %rbx, %rdi
callq 0x10c0
xorl %r15d, %r15d
testl %eax, %eax
je 0x146d
xorl %r13d, %r13d
xorl %r14d, %r14d
jmp 0x150d
leaq (%r14,%r13), %rax
addq $-0x58, %rax
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %rax, -0x40(%rbp)
leaq (%rax,%r12,4), %r15
callq 0x199c
movq %r15, %rcx
cmpq $0xc, %r12
jb 0x14a1
je 0x14d7
leaq -0x34(,%r12,4), %rcx
addq %r13, %rcx
movl %eax, (%rcx)
movl %eax, %edi
callq 0x1986
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
movq %rbx, %rcx
callq 0x1040
addl %eax, %r14d
incq %r12
movq %rbx, %rdi
callq 0x10c0
addq $0x4, %r15
testl %eax, %eax
je 0x1486
jmp 0x150a
movq -0x38(%rbp), %rcx
movl %eax, -0x28(%rcx)
movl %eax, %edi
callq 0x1ce5
movq %rax, %r13
movq %rbx, %rdi
callq 0x10c0
movl $0xd, %r12d
testl %eax, %eax
movl $0x0, %r15d
movq -0x40(%rbp), %rax
je 0x1482
jmp 0x150d
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x1050
movq -0x38(%rbp), %rdx
movl %r14d, -0x7c(%rdx)
leal 0x3ff(%r14), %eax
addl $0x7fe, %r14d           # imm = 0x7FE
testl %eax, %eax
cmovnsl %eax, %r14d
sarl $0xa, %r14d
leal (%r14,%r14), %eax
testq %r13, %r13
leal 0x2(%r14,%r14), %ecx
cmovel %eax, %ecx
movl %ecx, -0x64(%rdx)
leaq -0x250(%rbp), %rcx
movq -0x48(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl $0x1, %edx
callq 0x1c7a
movl %r15d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1200
jmp 0x158e
callq 0x1178
jmp 0x158e
callq 0x11de
jmp 0x159b
callq 0x11bc
movl $0x2, %r15d
jmp 0x155f
callq 0x119a
movl $0x11, %r15d
jmp 0x155f
callq 0x1150
callq 0x122f
nopl (%rax)