// File: /workspace/github_c_makefile_star2_O2/sheheryarbutt[P]Ext2-File-System/ext2_mkdir.c
// Function: main
int main(int argc, char **argv) {

    if(argc != 3) {
        fprintf(stderr, "Usage: ext2_mkdir <image file name> <dest path>\n");
        exit(1);
    }
    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);
    }

    // setting global vars
    sb = (struct ext2_super_block *)(disk + EXT2_BLOCK_SIZE);
    gd = (struct ext2_group_desc *)(disk + (2*EXT2_BLOCK_SIZE)); 
	inode_table = (struct ext2_inode *)(disk + (5*EXT2_BLOCK_SIZE));

	char * path = argv[2];
	remove_trailing_slash(path);
	// need for pathTraversal function
	unsigned int * parent_block= malloc(sizeof(unsigned int *));
	int cur_inode=pathTraversal(path,disk,parent_block);
	
	if (cur_inode!=0){
		// file/directory already exists at path
		free(parent_block);
		fprintf(stderr, "ERROR: %s\n", strerror(EEXIST));     
		exit(EEXIST);
	}

	free(parent_block);
	char *cpyPath=malloc((unsigned int)strlen(path) + 1);
	strcpy(cpyPath,path);
	parent_block= malloc(sizeof(unsigned int *));
	// name of dir we want to create
	char *name;
	name=basename(cpyPath);

	if (strlen(name)>EXT2_NAME_LEN){
		fprintf(stderr, "ERROR: %s\n", strerror(ENAMETOOLONG));     
		exit(ENAMETOOLONG);
	}
	path[strlen(path)-(strlen(name))]='\0';
	cur_inode=pathTraversal(path,disk,parent_block);

	if (cur_inode==0){
		// path to directory we want to create new dir in, doesnt exist
		fprintf(stderr, "ERROR: %s\n", strerror(ENOENT));     
		exit(ENOENT);
	}
	if ((gd->bg_free_blocks_count<2) || (gd->bg_free_inodes_count<1) ){
		// no space left to create new dir
		fprintf(stderr, "ERROR: %s\n", strerror(ENOSPC));     
		exit(ENOSPC);
	}

	//var for available datablocks and inodes
	int avail_db,avail_db2;
	int avail_ib;

	//cannot create directory in reserved inodes, except root
	if(cur_inode==EXT2_ROOT_INO || cur_inode > EXT2_GOOD_OLD_FIRST_INO){

		struct ext2_inode inode = inode_table[cur_inode-1];

		if(!((inode.i_mode & EXT2_S_IFDIR) == EXT2_S_IFDIR)){
			// path to create directory in is not to a directory
			fprintf(stderr, "ERROR: %s\n", strerror(ENOTDIR));     
			exit(ENOTDIR);
		}
		else { 
			int block_pointer=0; 
			//find next block pointer to use for new dir entry
			while(inode_table[cur_inode-1].i_block[block_pointer]!=0){
				block_pointer++;
			}
			//datablock to store new dir entry
			avail_db= set_avail_db();
			//update dir info 
			inode_table[cur_inode-1].i_block[block_pointer]=avail_db;
			inode_table[cur_inode-1].i_blocks+=2;
			inode_table[cur_inode-1].i_size+=EXT2_BLOCK_SIZE;
			inode_table[cur_inode-1].i_links_count+=1;

			//datablock and inode for new dir
			avail_ib= set_avail_ib();
			avail_db2= set_avail_db();

			unsigned int * blocks=malloc(sizeof(unsigned int)*15);

			blocks[0]=avail_db2;
			// create inode for new dir
			create_inode(avail_ib,EXT2_S_IFDIR,EXT2_BLOCK_SIZE,2,2,blocks);
			// create dir entry for new dir
			create_entry((EXT2_BLOCK_SIZE*avail_db),(unsigned int)avail_ib,(unsigned short)EXT2_BLOCK_SIZE,(unsigned char)strlen(name),(unsigned char)EXT2_FT_DIR,name);
			// create dir entries for . and ..
			create_entry((EXT2_BLOCK_SIZE*avail_db2),(unsigned int)avail_ib,(unsigned short)12,(unsigned char)1,(unsigned char)EXT2_FT_DIR,".");
			create_entry((EXT2_BLOCK_SIZE*avail_db2)+12,(unsigned int)cur_inode,(unsigned short)1012,(unsigned char)2,(unsigned char)EXT2_FT_DIR,"..");

		}

	}else{
		fprintf(stderr, "ERROR: %s\n", strerror(EPERM));     
		exit(EPERM);
		
	}
					   	   
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x3, %edi
jne 0x14aa
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10e0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2e3d(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x14c8
leaq 0x400(%rax), %rcx
movq %rcx, 0x2e2d(%rip)      # 0x40b8
leaq 0x800(%rax), %rcx
movq %rcx, 0x2e27(%rip)      # 0x40c0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2e22(%rip)      # 0x40c8
movq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x1548
pushq $0x8
popq %rdi
callq 0x10d0
movq %rax, %rbx
movq 0x2dec(%rip), %rsi      # 0x40b0
movq %r14, %rdi
movq %rax, %rdx
callq 0x1679
movl %eax, %r15d
movq %rbx, %rdi
callq 0x1030
testl %r15d, %r15d
jne 0x14dc
movq %r14, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10d0
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
pushq $0x8
popq %rdi
callq 0x10d0
movq %rax, %rbx
movq %r15, %rdi
callq 0x10b0
movq %rax, %r13
movq %rax, %rdi
callq 0x1060
cmpq $0x100, %rax            # imm = 0x100
jae 0x14ea
movq %rax, %r15
movq %r14, %rdi
callq 0x1060
subq %r15, %rax
movb $0x0, (%r14,%rax)
movq 0x2d6b(%rip), %rsi      # 0x40b0
movq %r14, %rdi
movq %rbx, %rdx
callq 0x1679
testl %eax, %eax
je 0x14f8
movq 0x2d61(%rip), %rcx      # 0x40c0
cmpw $0x2, 0xc(%rcx)
jb 0x149c
cmpw $0x0, 0xe(%rcx)
je 0x149c
cmpl $0x2, %eax
setne %dl
cmpl $0xc, %eax
setl %cl
testb %cl, %dl
jne 0x1506
movq 0x2d38(%rip), %rcx      # 0x40c8
leal -0x1(%rax), %r15d
movq %r15, %r12
shlq $0x7, %r12
testb $0x40, 0x1(%rcx,%r12)
je 0x1514
movq %rax, -0x30(%rbp)
movq %r13, %r14
cmpl $0x0, 0x28(%rcx,%r12)
leaq 0x4(%r12), %r12
jne 0x13ae
xorl %eax, %eax
callq 0x1878
movl %eax, %ebx
movq 0x2cfd(%rip), %rax      # 0x40c8
movl %ebx, 0x24(%rax,%r12)
shlq $0x7, %r15
addl $0x2, 0x1c(%rax,%r15)
addl $0x400, 0x4(%rax,%r15)  # imm = 0x400
incw 0x1a(%rax,%r15)
xorl %eax, %eax
callq 0x18e5
movl %eax, %r12d
xorl %eax, %eax
callq 0x1878
movl %eax, %r15d
pushq $0x3c
popq %rdi
callq 0x10d0
movl %r15d, (%rax)
pushq $0x2
popq %r13
movl %r12d, %edi
movl $0x4000, %esi           # imm = 0x4000
movl $0x400, %edx            # imm = 0x400
movl %r13d, %ecx
movl %r13d, %r8d
movq %rax, %r9
callq 0x193e
shll $0xa, %ebx
movq %r14, %rdi
callq 0x1060
movzbl %al, %ecx
movl %ebx, %edi
movl %r12d, %esi
movl $0x400, %edx            # imm = 0x400
movl %r13d, %r8d
movq %r14, %r9
callq 0x19fd
shll $0xa, %r15d
leaq 0xbf1(%rip), %r9        # 0x2046
pushq $0xc
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
movl %r12d, %esi
movl %r13d, %r8d
callq 0x19fd
orl $0xc, %r15d
leaq 0xbd1(%rip), %r9        # 0x2045
movl %r15d, %edi
movq -0x30(%rbp), %rsi
movl $0x3f4, %edx            # imm = 0x3F4
movl %r13d, %ecx
movl %r13d, %r8d
callq 0x19fd
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2b55(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x1c
jmp 0x1520
movq 0x2b47(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb49(%rip), %rdi       # 0x2004
pushq $0x30
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
jmp 0x14d4
leaq 0xb66(%rip), %rdi       # 0x2035
callq 0x10f0
pushq $0x1
popq %rdi
callq 0x1100
movq 0x2b15(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x11
jmp 0x1520
movq 0x2b07(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x24
jmp 0x1520
movq 0x2af9(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x2
jmp 0x1520
movq 0x2aeb(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x1
jmp 0x1520
movq 0x2add(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x14
popq %r14
movl %r14d, %edi
callq 0x1120
leaq 0xb09(%rip), %rsi       # 0x203a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1090
movl %r14d, %edi
callq 0x1100
nop