// File: /workspace/github_c_makefile_star2_O0/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
subq $0xb0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x23a1
movq 0x2c72(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xcf2(%rip), %rsi       # 0x3082
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x10f0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2080
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1060
cmpq $0x100, %rax            # imm = 0x100
jbe 0x23ee
movq 0x2c2e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xcf0(%rip), %rsi       # 0x30c4
movb $0x0, %al
callq 0x10a0
movq -0x18(%rbp), %rdi
callq 0x1030
movl $0x1, %edi
callq 0x10f0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0x2420
movq 0x2bf3(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xcc8(%rip), %rsi       # 0x30d7
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x10f0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x2f, %esi
callq 0x1090
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rdi
xorl %ecx, %ecx
movl $0x1, %edx
cmpq %rdi, %rsi
cmovel %edx, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x1200
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10d0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, 0x2beb(%rip)      # 0x5098
movq $-0x1, %rax
cmpq %rax, 0x2bdd(%rip)      # 0x5098
jne 0x24d3
leaq 0xc2d(%rip), %rdi       # 0x30f1
callq 0x10e0
movl $0x1, %edi
callq 0x10f0
movq 0x2bbe(%rip), %rax      # 0x5098
addq $0x400, %rax            # imm = 0x400
movq %rax, -0x38(%rbp)
movq 0x2bad(%rip), %rax      # 0x5098
addq $0x800, %rax            # imm = 0x800
movq -0x38(%rbp), %rcx
movzwl 0x5e(%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x2b85(%rip), %rdi      # 0x5098
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17c0
movq -0x70(%rbp), %rdi
movq %rax, %rsi
movq -0x18(%rbp), %rdx
callq 0x1370
cmpl $0x0, %eax
je 0x255d
movq 0x2ab8(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xbac(%rip), %rsi       # 0x30f6
movb $0x0, %al
callq 0x10a0
movl $0x11, -0x4(%rbp)
jmp 0x2844
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x80(%rbp)
movq 0x2b28(%rip), %rdi      # 0x5098
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17c0
movq -0x80(%rbp), %rdi
movq %rax, %rsi
callq 0x13c0
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
movq 0x2af4(%rip), %rdi      # 0x5098
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17c0
movq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x13c0
movq %rax, -0x50(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x25ef
movq 0x2a26(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xb3b(%rip), %rsi       # 0x3117
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x2844
movsbl -0x50(%rbp), %eax
cmpl $0x64, %eax
je 0x261c
movq 0x29f9(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xb37(%rip), %rsi       # 0x3140
movb $0x0, %al
callq 0x10a0
movl $0x15, -0x4(%rbp)
jmp 0x2844
jmp 0x261e
cmpl $0x0, -0x44(%rbp)
jne 0x2648
movq 0x29cd(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xb2f(%rip), %rsi       # 0x3164
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x2844
movsbl -0x48(%rbp), %eax
cmpl $0x66, %eax
je 0x2675
movq 0x29a0(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xb22(%rip), %rsi       # 0x3184
movb $0x0, %al
callq 0x10a0
movl $0x15, -0x4(%rbp)
jmp 0x2844
jmp 0x2677
movq -0x18(%rbp), %rdi
callq 0x1060
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
addl $0x8, %eax
movl -0x54(%rbp), %edx
addl $0x8, %edx
andl $0x3, %edx
movl $0x4, %ecx
subl %edx, %ecx
andl $0x3, %ecx
addl %ecx, %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %edi
callq 0x10c0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x26cc
leaq 0x967(%rip), %rdi       # 0x3024
callq 0x10e0
movl $0x1, %edi
callq 0x10f0
movl -0x44(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0x1060
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x6(%rax)
movl -0x58(%rbp), %eax
movw %ax, %cx
movq -0x60(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x60(%rbp), %rax
movb $0x1, 0x7(%rax)
movq -0x60(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1060
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movq 0x295b(%rip), %rdi      # 0x5098
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17c0
movq %rax, -0x98(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x40(%rbp), %rax
movzwl 0xc(%rax), %eax
movl %eax, -0x8c(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
movq 0x291a(%rip), %rdi      # 0x5098
movq -0x40(%rbp), %rax
movl (%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17c0
movq -0x98(%rbp), %rdi
movl -0x90(%rbp), %esi
movl -0x8c(%rbp), %edx
movq -0x88(%rbp), %rcx
movq %rax, %r8
callq 0x1e60
cmpl $-0x1, %eax
jne 0x27cd
movq 0x283c(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x9e6(%rip), %rsi       # 0x31ac
movb $0x0, %al
callq 0x10a0
movq -0x60(%rbp), %rdi
callq 0x1030
movq 0x28bb(%rip), %rdi      # 0x5098
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17c0
movq %rax, %rdi
movl -0x44(%rbp), %esi
callq 0x17a0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movw 0x1a(%rax), %cx
addw $0x1, %cx
movw %cx, 0x1a(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq 0x9c9(%rip), %rdi       # 0x31ed
movb $0x0, %al
callq 0x1080
movq -0x18(%rbp), %rdi
callq 0x1030
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq