// File: /workspace/github_cpp_makefile_star2_O2/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::rmdir(char const*)
void FileSys::rmdir(const char *name)
{
  struct dirblock_t curr_block_ptr;
  char file_name[MAX_FNAME_SIZE + 1];
  char curr_file_name[MAX_FNAME_SIZE + 1];
  strcpy(file_name, name);
  bfs.read_block(curr_dir, (void*)&curr_block_ptr);

  bool error = false;
  char buffer[1024];
  short found_block = 0;
  unsigned int found_index = 0;
  bool found = false;
  dirblock_t found_dir_ptr;
  //finds the directory to remove wthin the current directory, sets found
  //bool to true if it exists in the directory
  for (unsigned int i = 0; i < MAX_DIR_ENTRIES; i++){
    strcpy(curr_file_name, curr_block_ptr.dir_entries[i].name);
    if (strcmp(curr_file_name, file_name) == 0){
      error = false;
      found = true;
      found_index = i;
      found_block = curr_block_ptr.dir_entries[found_index].block_num;
      bfs.read_block(found_block, (void*)&found_dir_ptr);
    }else{
      error = true;
    }
  }
  if(found){
    error = false;
  }
  //ensures the found directory is a directory and not a file
  if(!is_directory(found_block)){
    if(!error){
      error = true;
      strcpy(buffer, "500: File is not a directory\r\n");
    }
  }
  //if the found bool was not set to true then the filename passed in does
  //not exist and the error message is wrote into the buffer
  else if(!found && !error){
    error = true;
    strcpy(buffer ,"503: File does not exsit\r\n");
  }
  //if the found directory is a directory but still contains files it cannot
  //be removed and this error is wrote into the buffer
  else if(found_dir_ptr.num_entries != 0 && !error){
    error = true;
    strcpy(buffer, "507: Directory is not empty\r\n");
  }
  else{
    //if error is still set to false then remove the directory and set all
    //values back to null/0 as well as reclaiming its block num
    if(found && !error){
      bfs.reclaim_block(curr_block_ptr.dir_entries[found_index].block_num);
      curr_block_ptr.dir_entries[found_index].name[0] = '\0';
      curr_block_ptr.dir_entries[found_index].block_num = 0;
      curr_block_ptr.num_entries--;
      bfs.write_block(curr_dir, (void*)&curr_block_ptr);
      strcpy(buffer, "200 OK\r\n Length: 0\r\n\r\n");
    }
  }
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x538, %rsp            # imm = 0x538
movq %rdi, %rbx
leaq 0x1c(%rsp), %rdi
callq 0x3260
movswl 0x4(%rbx), %esi
leaq 0x30(%rsp), %rdx
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x380e
xorl %r13d, %r13d
leaq 0x42(%rsp), %rbx
leaq 0x26(%rsp), %r12
xorl %r14d, %r14d
xorl %ebp, %ebp
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
cmpq $0xa, %r13
je 0x3e27
leaq -0xa(%rbx), %rsi
movq %r12, %rdi
callq 0x3260
movq %r12, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x3350
movb $0x1, %r14b
testl %eax, %eax
jne 0x3e1e
movzwl (%rbx), %ebp
movswl %bp, %esi
movq 0x10(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x380e
movb $0x1, %r15b
xorl %r14d, %r14d
movl %r13d, 0xc(%rsp)
incq %r13
addq $0xc, %rbx
jmp 0x3dd5
movzbl %r14b, %eax
xorl %ebx, %ebx
testb $0x1, %r15b
cmovel %eax, %ebx
movswl %bp, %esi
movq 0x10(%rsp), %rdi
callq 0x3d22
testb %al, %al
je 0x3ed3
testb $0x1, %r15b
jne 0x3e59
testb $0x1, %r14b
je 0x3ef0
cmpl $0x0, 0xb4(%rsp)
je 0x3e6c
testb $0x1, %bl
je 0x3f08
testb $0x1, %r15b
je 0x3f26
movl 0xc(%rsp), %eax
imulq $0xc, %rax, %rbx
leaq 0x30(%rsp), %r15
movswl 0x12(%r15,%rbx), %esi
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x37b6
movb $0x0, 0x8(%r15,%rbx)
andw $0x0, 0x12(%r15,%rbx)
leaq 0x30(%rsp), %rdx
decl 0x4(%rdx)
movswl 0x4(%r14), %esi
movq %r14, %rdi
callq 0x3814
movabsq $0xa0d0a0d30203a, %rax  # imm = 0xA0D0A0D30203A
movq %rax, 0x13f(%rsp)
movups 0x4252(%rip), %xmm0     # 0x8123
jmp 0x3f1e
testb $0x1, %bl
jne 0x3f26
movups 0x426a(%rip), %xmm0     # 0x8149
movups %xmm0, 0x13f(%rsp)
movups 0x424c(%rip), %xmm0     # 0x813a
jmp 0x3f1e
movups 0x426d(%rip), %xmm0     # 0x8164
movups %xmm0, 0x13b(%rsp)
movups 0x4253(%rip), %xmm0     # 0x8159
jmp 0x3f1e
movups 0x4273(%rip), %xmm0     # 0x8182
movups %xmm0, 0x13e(%rsp)
movups 0x4256(%rip), %xmm0     # 0x8174
movaps %xmm0, 0x130(%rsp)
movq 0x10(%rsp), %rax
movl 0x8(%rax), %edi
leaq 0x130(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x3170
addq $0x538, %rsp            # imm = 0x538
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq