// File: /workspace/github_c_makefile_star2_O1/Sidd0107[P]EXT2-File-System/ext2_mkdir.c
// Function: main
int main(int argc, char **argv) {

    //Checks valid no of argument
    if(argc != 3) {
        fprintf(stderr, "Usage: ext2_mkdir <image file name> <file path on ext2 image>\n");
        exit(1);
    }
    //Gets arguments
    int fd = open(argv[1], O_RDWR);
    char *path = argv[2];
    char *dirName = extract_filename(argv[2]);
    
    //maps disk in memory and checks for error
    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 groupd desc.
    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)));
    // Checks if file already exists and returns if yes.
	char *directory = strrchr(argv[2], '/');
	directory[(argv[2] == directory)? 1:0] = '\0';
    if(file_exists(path, get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), dirName)!=0){
        fprintf(stderr, "mkdir: file exists\n");
        return EEXIST;
    }
    //CHECK FOR INODE TO RESERVE
    int free_inode;
    if((free_inode = find_first_free_bit(get_block(disk, gd -> bg_inode_bitmap, BLOCK_SIZE), 0, 128)) == -1){
        fprintf(stderr, "mkdir: operation failed: No free inodes");
        exit(1);
    }
    unsigned int dirname_length = strlen(dirName);
    unsigned int dir_entry_size = (8 + dirname_length) + (4 - (8 + dirname_length) % 4) % 4;
    //Creates new directory entry
    struct ext2_dir_entry_2  * new_dir = malloc(dir_entry_size);
	if(!new_dir){
		perror("malloc");
		exit(1);
	}
    new_dir -> inode = free_inode + 1;
    new_dir -> name_len = strlen(dirName);
    new_dir -> rec_len = dir_entry_size;
    new_dir -> file_type = EXT2_FT_DIR;
    memcpy(new_dir -> name, dirName, strlen(dirName));
    int new_capacity = gd -> bg_free_blocks_count;
    
    //Getting info to check that destination is a directory and not reserved
    struct basic_fileinfo destination = find_file(argv[2], get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
    if(destination.inode < 11 && destination.inode != 2){
        fprintf(stderr, "Invalid destination");
        free(new_dir);
        return ENOENT;
    }//Checks if destination is a directory
    if(destination.type != 'd'){
        fprintf(stderr, "mkdir: destination is not a directory");
        free(new_dir);
        return ENOENT;
   }//Checks if directory has space for the new directory
	if(new_capacity < 1){
		fprintf(stderr, "mkdir: not enough capacity");
		return ENOSPC;
	}
    //Reserves location for new directory entry
    if ((new_capacity = reserve_directory_entry(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), destination.inode, new_capacity, new_dir, get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE))) == -1){
        fprintf(stderr, "mkdir: Not enough capacity for file");
        exit(-1);
    }
    //freeing memory and reducing the free inode count
    free(new_dir);
    gd -> bg_free_inodes_count --;
    set_inode(get_inode(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1),
              free_inode, 0, NULL,
              0,
              get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE),
              get_block(disk, gd -> bg_inode_bitmap, BLOCK_SIZE),
		EXT2_S_IFDIR);
    gd -> bg_free_blocks_count = new_capacity;
    printf("Success: Directory entry created.");
    //Adds . directory to the new directory entry
	new_dir = malloc(12);
	if (!new_dir){
		perror("malloc");
		exit(1);
	}
	new_dir -> inode = free_inode + 1;
	new_dir -> name_len = 1; 
	new_dir -> rec_len = 12;
	new_dir -> file_type = EXT2_FT_DIR;
	new_dir -> name[0] = '.';

	struct ext2_inode *inode = get_inode(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1);
	inode -> i_block[0] = 0;
    // Adds .. to the new directory entry
	new_capacity = reserve_directory_entry(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1, new_capacity, new_dir, get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE));
	new_dir -> inode = destination.inode;
	new_dir -> name_len = 2;
	new_dir -> name[0] = '.';
	new_dir -> name[1] = '.';
	reserve_directory_entry(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1, new_capacity, new_dir, get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE));
	gd -> bg_free_blocks_count = new_capacity;
	gd -> bg_used_dirs_count += 3;
	free(new_dir);
	free(dirName);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpl $0x3, %edi
jne 0x23de
movq %rsi, %r15
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10d0
movl %eax, %r14d
movq 0x10(%r15), %rbx
movq %rbx, %rdi
callq 0x1d8e
movq %rax, %r13
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x1080
movq %rax, 0x30aa(%rip)      # 0x50a0
cmpq $-0x1, %rax
je 0x23e3
movq %rax, %r14
movzwl 0x45e(%rax), %r12d
shll $0x5, %r12d
movq %r15, -0x30(%rbp)
movq 0x10(%r15), %r15
movq %r15, %rdi
movl $0x2f, %esi
callq 0x10a0
xorl %ecx, %ecx
cmpq %rax, %r15
sete %cl
movb $0x0, (%rax,%rcx)
movq 0x3069(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x1492
testl %eax, %eax
jne 0x23ad
movq %r13, %r15
movq 0x3037(%rip), %rdi      # 0x50a0
movq %r12, -0x40(%rbp)
movl 0x804(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rax, %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1a05
cmpl $-0x1, %eax
je 0x23e8
movl %eax, %r12d
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 0x23ed
movq %rax, %r13
movq %r12, -0x58(%rbp)
leal 0x1(%r12), %eax
movl %eax, -0x34(%rbp)
movl %eax, (%r13)
movq %r15, %rdi
callq 0x1070
movb %al, 0x6(%r13)
movw %bx, 0x4(%r13)
movb $0x2, 0x7(%r13)
movq %r13, %rbx
addq $0x8, %rbx
movq %r15, %rdi
callq 0x1070
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x10b0
movq -0x40(%rbp), %rax
movzwl 0x80c(%r14,%rax), %ebx
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %r12
movq 0x2f7e(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %r12, %rdi
movq %rax, %rsi
callq 0x14c8
movq %rax, %r12
shrq $0x20, %r12
cmpq $0xb, %r12
setae %cl
cmpq $0x2, %r12
sete %dl
orb %cl, %dl
je 0x23b9
cmpb $0x64, %al
jne 0x23c3
testl %ebx, %ebx
je 0x23d2
movq %r15, -0x50(%rbp)
movq 0x2f29(%rip), %rdi      # 0x50a0
movl %ebx, -0x30(%rbp)
movq -0x40(%rbp), %rbx
movl 0x808(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rax, %r15
movq 0x2f06(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %r15, %rdi
movq %r12, -0x48(%rbp)
movl %r12d, %esi
movl -0x30(%rbp), %edx
movq %r13, %rcx
movq %rax, %r8
callq 0x1be8
movl %eax, -0x30(%rbp)
cmpl $-0x1, %eax
je 0x23f2
movq %r13, %rdi
callq 0x1030
decw 0x80e(%r14,%rbx)
movq 0x2eb8(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rax, %rdi
movl -0x34(%rbp), %r12d
movl %r12d, %esi
callq 0x17f3
movq %rax, %r15
movq 0x2e8d(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rax, %r13
movq 0x2e71(%rip), %rdi      # 0x50a0
movl 0x804(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %r15, %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r13, %r9
pushq $0x4000                 # imm = 0x4000
pushq %rax
callq 0x1a61
addq $0x10, %rsp
movl -0x30(%rbp), %eax
movw %ax, 0x80c(%r14,%rbx)
leaq 0xf1d(%rip), %rdi       # 0x3191
xorl %eax, %eax
callq 0x1090
movl $0xc, %edi
callq 0x10c0
testq %rax, %rax
je 0x23f7
movq %rax, %r13
movl %r12d, (%rax)
movb $0x1, 0x6(%rax)
movw $0xc, 0x4(%rax)
movw $0x2e02, 0x7(%rax)      # imm = 0x2E02
movq 0x2df5(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rax, %rdi
movl %r12d, %esi
callq 0x17f3
movl $0x0, 0x28(%rax)
movq 0x2dca(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rax, %r15
movq 0x2dae(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %r15, %rdi
movl %r12d, %esi
movl -0x30(%rbp), %edx
movq %r13, %rcx
movq %rax, %r8
callq 0x1be8
movl %eax, %r12d
movq -0x48(%rbp), %rax
movl %eax, (%r13)
movb $0x2, 0x6(%r13)
movw $0x2e2e, 0x8(%r13)      # imm = 0x2E2E
movq 0x2d6a(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %rax, %r15
movq 0x2d4e(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%rbx), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1803
movq %r15, %rdi
movl -0x34(%rbp), %esi
movl %r12d, %edx
movq %r13, %rcx
movq %rax, %r8
callq 0x1be8
movw %r12w, 0x80c(%r14,%rbx)
addw $0x3, 0x810(%r14,%rbx)
movq %r13, %rdi
callq 0x1030
movq -0x50(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1190
movl $0x11, %eax
jmp 0x239e
movq %r13, %rdi
callq 0x1261
jmp 0x23cb
movq %r13, %rdi
callq 0x11bf
movl $0x2, %eax
jmp 0x239e
callq 0x1232
movl $0x1c, %eax
jmp 0x239e
callq 0x1168
callq 0x12d4
callq 0x12ac
callq 0x1294
callq 0x120a
callq 0x11f2