// File: /workspace/github_c_makefile_star2_O1/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 0x165f
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10e0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2c97(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x1664
leaq 0x400(%rax), %rcx
movq %rcx, 0x2c87(%rip)      # 0x40b8
leaq 0x800(%rax), %rcx
movq %rcx, 0x2c81(%rip)      # 0x40c0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2c7c(%rip)      # 0x40c8
movq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x1684
movl $0x8, %edi
callq 0x10d0
movq %rax, %rbx
movq 0x2c44(%rip), %rsi      # 0x40b0
movq %r14, %rdi
movq %rax, %rdx
callq 0x17e4
movl %eax, %r15d
movq %rbx, %rdi
callq 0x1030
testl %r15d, %r15d
jne 0x1669
movq %r14, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10d0
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
movl $0x8, %edi
callq 0x10d0
movq %rax, %r15
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
movq %rax, %rdi
callq 0x1060
cmpq $0x100, %rax            # imm = 0x100
jae 0x166e
movq %rax, %r12
movq %r14, %rdi
callq 0x1060
subq %r12, %rax
movb $0x0, (%r14,%rax)
movq 0x2bc1(%rip), %rsi      # 0x40b0
movq %r14, %rdi
movq %r15, %rdx
callq 0x17e4
testl %eax, %eax
je 0x1673
movl %eax, %r14d
movq 0x2bb4(%rip), %rax      # 0x40c0
cmpw $0x2, 0xc(%rax)
jb 0x165a
cmpw $0x0, 0xe(%rax)
je 0x165a
cmpl $0x2, %r14d
setne %al
cmpl $0xc, %r14d
setl %cl
testb %cl, %al
jne 0x1678
movq 0x2b89(%rip), %rax      # 0x40c8
movslq %r14d, %r15
decq %r15
movq %r15, %r12
shlq $0x7, %r12
testb $0x40, 0x1(%rax,%r12)
je 0x167d
cmpl $0x0, 0x28(%rax,%r12)
leaq 0x4(%r12), %r12
jne 0x1558
xorl %eax, %eax
callq 0x1a16
movl %eax, %r13d
movq 0x2b52(%rip), %rax      # 0x40c8
movl %r13d, 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 0x1aae
movl %eax, %r12d
xorl %eax, %eax
callq 0x1a16
movl %eax, %r15d
movl $0x3c, %edi
callq 0x10d0
movl %r15d, (%rax)
movl %r12d, %edi
movl $0x4000, %esi           # imm = 0x4000
movl $0x400, %edx            # imm = 0x400
movl $0x2, %ecx
movl $0x2, %r8d
movq %rax, %r9
callq 0x1b0b
shll $0xa, %r13d
movq %rbx, %rdi
callq 0x1060
movzbl %al, %ecx
movl %r13d, %edi
movl %r12d, %esi
movl $0x400, %edx            # imm = 0x400
movl $0x2, %r8d
movq %rbx, %r9
callq 0x1bf8
shll $0xa, %r15d
leaq 0xa3e(%rip), %r9        # 0x2046
movl %r15d, %edi
movl %r12d, %esi
movl $0xc, %edx
movl $0x1, %ecx
movl $0x2, %r8d
callq 0x1bf8
orl $0xc, %r15d
leaq 0xa17(%rip), %r9        # 0x2045
movl %r15d, %edi
movl %r14d, %esi
movl $0x3f4, %edx            # imm = 0x3F4
movl $0x2, %ecx
movl $0x2, %r8d
callq 0x1bf8
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1213
callq 0x1140
callq 0x12be
callq 0x1168
callq 0x1285
callq 0x124c
callq 0x11a1
callq 0x11da
nop