// File: /workspace/github_c_makefile_star2_O2/Sidd0107[P]EXT2-File-System/ext2_ln.c
// Function: main
int main(int argc, char **argv) {
    //Makes sure correct number of arguments
    if(argc != 4) {
        fprintf(stderr, "Usage: ext2_cp <image file name> <file location> <link location>\n");
        exit(1);
    }
    //Extracts filename and checks if it is not too long
	char *linked_filename = extract_filename(argv[3]);
	if(strlen(linked_filename) > 256){
		fprintf(stderr, "filename too long!");
		free(linked_filename);
		exit(1);
	}
    //Gets path from arg
	char *file_path;
	if(argv[3][0] != '/'){
		fprintf(stderr, "Invalid link destination!");
		exit(1);
	}
	file_path = strrchr(argv[3], '/');
	file_path[(file_path == argv[3])? 1 : 0] = '\0';
	char *link_full_path = combine_path_and_file(argv[3], linked_filename);
	
	//Opens and maps diskimage to memory
    int fd = open(argv[1], O_RDWR);
    disk = mmap(NULL, 128 * BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
		perror("mmap");
		exit(1);
    }
    //Gets superblock and group descriptor
	struct ext2_super_block *sb = (struct ext2_super_block *)(disk + BLOCK_SIZE);
	struct ext2_group_desc *gd = (struct ext2_group_desc *)  (disk + 2*BLOCK_SIZE + ((sb -> s_block_group_nr) *  sizeof(struct ext2_group_desc)));
	
    //Makes sure the file exists
	if(file_exists(link_full_path, get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), linked_filename)){
		fprintf(stderr, "ln: file at link location exists");
		return EEXIST;
	};
	
	struct basic_fileinfo file_to_link = find_file(argv[2], get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
	struct basic_fileinfo destination_dir = find_file(argv[3], get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
    //Checks validity of destination and file to be linked
	if(destination_dir.inode == 0){
		fprintf(stderr, "ln: destination directory does not exist");
		return ENOENT;
	}
	else if (destination_dir.type != 'd'){
		fprintf(stderr, "ln: destination is not a directory!");
		return EISDIR;
	}
	
	if(file_to_link.inode == 0){
		fprintf(stderr, "ln: file to link does not exist");
		return ENOENT;
	}
	else if (file_to_link.type != 'f'){
		fprintf(stderr, "ln: trying to link to non-regular file!");
		return EISDIR;
	}

	unsigned int filename_length = strlen(linked_filename);
	unsigned int dir_entry_size = (8 + filename_length) + (4 - ((8 + filename_length) % 4)) % 4;


	//CHECK FOR DIRECTORY TO RESERVE
    //Creates new dir entry element with specific details
	struct ext2_dir_entry_2  * new_file = malloc(dir_entry_size);
	if(!new_file){
		perror("malloc");
		exit(1);
	}
	new_file -> inode = file_to_link.inode;
	new_file -> name_len = strlen(linked_filename);
	new_file -> rec_len = dir_entry_size;
	new_file -> file_type = 1;
	memcpy(new_file -> name, linked_filename, strlen(linked_filename));
    
	// Gets space and adds directory entry to it
	if (reserve_directory_entry(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), destination_dir.inode, gd -> bg_free_blocks_count, new_file, get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE)) == -1){
		fprintf(stderr, "Failed to create directory: not enough space for directory entry");
	}
	free(new_file);
    // Gets inode of original file being linked and increments the link count
    // As a new hard link has been created.
    struct ext2_inode *inode = get_inode(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), file_to_link.inode);
    inode -> i_links_count++;
	printf("link to %s at %s successfully created! %s", argv[2], link_full_path, linked_filename);
	free(linked_filename);
	free(link_full_path);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x4, %edi
jne 0x1fd8
movq %rsi, %r14
movq 0x18(%rsi), %rdi
callq 0x1aa9
movq %rax, %r15
movq %rax, %rdi
callq 0x1070
cmpq $0x101, %rax            # imm = 0x101
jae 0x1fed
movq 0x18(%r14), %rbx
cmpb $0x2f, (%rbx)
jne 0x2013
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x10a0
xorl %ecx, %ecx
cmpq %rbx, %rax
sete %cl
movb $0x0, (%rax,%rcx)
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x120c
movq %rax, %rbx
movq 0x8(%r14), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10d0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1080
movq %rax, 0x338b(%rip)      # 0x50a0
cmpq $-0x1, %rax
je 0x2031
movq %rax, %r12
movzwl 0x45e(%rax), %r13d
shll $0x5, %r13d
movl 0x808(%rax,%r13), %esi
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
callq 0x15c2
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x12b7
testl %eax, %eax
jne 0x1f1b
movq %rbx, -0x48(%rbp)
movq 0x10(%r14), %rbx
movq 0x3338(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%r13), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rbx, %rdi
movq %rax, %rsi
callq 0x12ed
movq %r14, %rcx
movq %rax, %r14
movq %rcx, -0x50(%rbp)
movq 0x18(%rcx), %rbx
movq 0x3306(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%r13), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rbx, %rdi
movq %rax, %rsi
callq 0x12ed
movq %rax, %rcx
shrq $0x20, %rcx
je 0x1f3b
cmpb $0x64, %al
jne 0x1f50
movq %r14, %rax
shrq $0x20, %rax
je 0x1f65
cmpb $0x66, %r14b
jne 0x1f85
movq %rcx, -0x40(%rbp)
movq %rax, -0x30(%rbp)
movq %r15, %rdi
callq 0x1070
movl %eax, %ecx
negl %ecx
andl $0x3, %ecx
leal (%rax,%rcx), %ebx
addl $0x8, %ebx
movq %rbx, %rdi
callq 0x10c0
testq %rax, %rax
je 0x203a
movq %rax, %r14
movq -0x30(%rbp), %rax
movl %eax, (%r14)
movq %r15, %rdi
callq 0x1070
movb %al, 0x6(%r14)
movw %bx, 0x4(%r14)
movb $0x1, 0x7(%r14)
movq %r14, %rbx
addq $0x8, %rbx
movq %r15, %rdi
callq 0x1070
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x10b0
movq 0x324b(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%r13), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %rbx
movq %r15, -0x38(%rbp)
movzwl 0x80c(%r12,%r13), %r15d
movq 0x3222(%rip), %rdi      # 0x50a0
movl 0x800(%r12,%r13), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
movl %r15d, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x1942
cmpl $-0x1, %eax
je 0x1fb7
movq %r14, %rdi
callq 0x1030
movq 0x31e3(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%r13), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x15b2
incw 0x1a(%rax)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x12dc(%rip), %rdi      # 0x31ca
xorl %r14d, %r14d
movq -0x48(%rbp), %rbx
movq %rbx, %rdx
movq -0x38(%rbp), %r15
movq %r15, %rcx
xorl %eax, %eax
callq 0x1090
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
jmp 0x1fa5
movq 0x30d6(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x11a7(%rip), %rdi      # 0x30d3
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $0x11
jmp 0x1fa3
movq 0x30b6(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x11a8(%rip), %rdi      # 0x30f4
pushq $0x28
jmp 0x1f78
movq 0x30a1(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x11bc(%rip), %rdi      # 0x311d
pushq $0x23
jmp 0x1f98
movq 0x308c(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x11cb(%rip), %rdi      # 0x3141
pushq $0x1f
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $0x2
jmp 0x1fa3
movq 0x306c(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x11cb(%rip), %rdi      # 0x3161
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $0x15
popq %r14
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x303a(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x11c1(%rip), %rdi      # 0x3189
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0x1eae
movq 0x3019(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1076(%rip), %rdi      # 0x305f
pushq $0x41
jmp 0x2026
movq 0x3004(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x10a3(%rip), %rdi      # 0x30a1
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
movq %r15, %rdi
callq 0x1030
jmp 0x2046
movq 0x2fde(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1090(%rip), %rdi      # 0x30b4
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0x2046
leaq 0x1096(%rip), %rdi      # 0x30ce
jmp 0x2041
leaq 0xfc3(%rip), %rdi       # 0x3004
callq 0x10e0
pushq $0x1
popq %rdi
callq 0x10f0