// File: /workspace/github_c_makefile_star2_O3/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 0x2254
movq %rsi, %r15
movq 0x18(%rsi), %rdi
callq 0x1d65
movq %rax, %r13
movq %rax, %rdi
callq 0x1070
cmpq $0x101, %rax            # imm = 0x101
jae 0x2259
movq 0x18(%r15), %r14
cmpb $0x2f, (%r14)
jne 0x2261
movq %r14, %rdi
movl $0x2f, %esi
callq 0x10a0
xorl %ecx, %ecx
cmpq %r14, %rax
sete %cl
movb $0x0, (%rax,%rcx)
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0x142c
movq %rax, %r14
movq 0x8(%r15), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10d0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1080
movq %rax, 0x309a(%rip)      # 0x50a0
cmpq $-0x1, %rax
je 0x2266
movq %rax, %r12
movzwl 0x45e(%rax), %ebx
shll $0x5, %ebx
movl 0x808(%rax,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
callq 0x183a
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x14c8
testl %eax, %eax
jne 0x2215
movq %r14, -0x48(%rbp)
movq 0x10(%r15), %r14
movq 0x304a(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x183a
movq %r14, %rdi
movq %rax, %rsi
callq 0x14fe
movq %rax, %r14
movq %r15, -0x50(%rbp)
movq 0x18(%r15), %r15
movq 0x301b(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x183a
movq %r15, %rdi
movq %rax, %rsi
callq 0x14fe
movq %rax, %rcx
shrq $0x20, %rcx
je 0x2222
cmpb $0x64, %al
jne 0x2229
movq %r14, %r15
shrq $0x20, %r15
je 0x2230
cmpb $0x66, %r14b
jne 0x223d
movq %rcx, -0x40(%rbp)
movq %r13, %rdi
callq 0x1070
movl %eax, %ecx
negl %ecx
andl $0x3, %ecx
movq %r13, -0x30(%rbp)
leal (%rax,%rcx), %r13d
addl $0x8, %r13d
movq %r13, %rdi
callq 0x10c0
testq %rax, %rax
je 0x226b
movq %rax, %r14
movl %r15d, (%rax)
movq %r15, -0x38(%rbp)
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0x1070
movb %al, 0x6(%r14)
movw %r13w, 0x4(%r14)
movb $0x1, 0x7(%r14)
movq %r14, %r13
addq $0x8, %r13
movq %r15, %rdi
callq 0x1070
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x10b0
movq 0x2f5a(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x183a
movq %rax, %r13
movzwl 0x80c(%r12,%rbx), %r15d
movq 0x2f35(%rip), %rdi      # 0x50a0
movl 0x800(%r12,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x183a
movq %r13, %rdi
movq -0x40(%rbp), %rsi
movl %r15d, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x1c06
cmpl $-0x1, %eax
je 0x224a
movq %r14, %rdi
callq 0x1030
movq 0x2ef6(%rip), %rdi      # 0x50a0
movl 0x808(%r12,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x183a
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x182a
incw 0x1a(%rax)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x1028(%rip), %rdi      # 0x3203
xorl %r14d, %r14d
movq -0x48(%rbp), %rbx
movq %rbx, %rdx
movq -0x30(%rbp), %r15
movq %r15, %rcx
xorl %eax, %eax
callq 0x1090
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11b8
movl $0x11, %r14d
jmp 0x2203
callq 0x12bb
jmp 0x2235
callq 0x11e7
jmp 0x2242
callq 0x128c
movl $0x2, %r14d
jmp 0x2203
callq 0x1216
movl $0x15, %r14d
jmp 0x2203
callq 0x1245
jmp 0x219b
callq 0x1168
movq %r13, %rdi
callq 0x1302
callq 0x1190
callq 0x12ea
callq 0x1274