// File: /workspace/github_cpp_makefile_star2_O2/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::cat(char const*)
void FileSys::cat(const char *name)
{
  bool error = false;
  bool found = false;

  // may need to increase buffer size to account for terminal messages
  char buffer [MAX_FILE_SIZE + 256];

  datablock_t* file_contents = new datablock_t;
  dirblock_t dir_block;
  bfs.read_block(curr_dir, (void *) &dir_block);
  for(int i = 0; i < MAX_DIR_ENTRIES; i++)
    {
      //target file found in current element in dir_entries
      if(strcmp(name, dir_block.dir_entries[i].name)==0)
        {
          found = true;
          if(is_directory(dir_block.dir_entries[i].block_num))
            {
              strcpy(buffer, "501: File is a directory\r\n");
              error = true;
              delete file_contents;
              break;
            }
          else if(!is_directory(dir_block.dir_entries[i].block_num))
            {
              inode_t file_inode;
              bfs.read_block(dir_block.dir_entries[i].block_num, (void *) &file_inode);

              string file_size_str = to_string(file_inode.size);
              int byte_count = (file_size_str.length() + 1);
              char* file_size = new char[byte_count];
              strcpy(file_size, file_size_str.c_str());

              strcat(buffer, "200 OK\r\n Length:");
              strcat(buffer, file_size);
              strcat(buffer, "\r\n");
              strcat(buffer, "\r\n");
              delete[] file_size;

              //append each data block pointed to by inode_t.blocks[] to our buffer
              for(int j = 0; j < sizeof(file_inode.blocks); j++)
                {
                  bfs.read_block(file_inode.blocks[j], (void *) &file_contents);
                  strcat(buffer, file_contents->data);
                }
              delete file_contents;
              // send buffer to socket here
            }
        }
    }

    // if point reached, file not found.
    // ERROR 503 File does not exist
    if(!found && !error)
    {
      strcpy(buffer, "503: File does not exist\r\n");
    }

  delete file_contents;
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2048, %rsp           # imm = 0x2048
movq %rsi, %r14
movq %rdi, %rbx
movl $0x80, %edi
callq 0x3280
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
movswl 0x4(%rbx), %esi
leaq 0xc0(%rsp), %rdx
movq %rbx, %rdi
callq 0x380e
leaq 0x140(%rsp), %rbp
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq %r14, 0x18(%rsp)
cmpq $0xa, %r13
je 0x4f8f
imulq $0xc, %r13, %r12
leaq 0xc8(%rsp), %rax
addq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3350
testl %eax, %eax
jne 0x4f87
movswl 0xa(%r12), %esi
movq %rbx, %rdi
callq 0x3d22
testb %al, %al
jne 0x4fb6
movswl 0xa(%r12), %esi
movq %rbx, %rdi
callq 0x3d22
movb $0x1, %cl
movq %rcx, 0x10(%rsp)
testb %al, %al
jne 0x4f87
movswl 0xa(%r12), %esi
movq %rbx, %rdi
leaq 0x40(%rsp), %rdx
callq 0x380e
movl 0x44(%rsp), %esi
leaq 0x20(%rsp), %rdi
callq 0x5334
movl 0x28(%rsp), %eax
incl %eax
movslq %eax, %rdi
callq 0x3030
movq %rax, %r12
movq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x3260
movq %rbp, %rdi
callq 0x3140
xorl %ecx, %ecx
movb %cl, 0x150(%rsp,%rax)
movups 0x340c(%rip), %xmm0     # 0x82f4
movups %xmm0, 0x140(%rsp,%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3420
movq %rbp, %rdi
callq 0x3140
movw $0xa0d, %r14w           # imm = 0xA0D
movw %r14w, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %rbp, %rdi
callq 0x3140
movw %r14w, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %r12, %rdi
callq 0x3310
pushq $0x4
popq %r12
movq 0x18(%rsp), %r14
cmpq $0x7c, %r12
je 0x4f6e
movswl 0x40(%rsp,%r12,2), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x380e
movq 0x8(%rsp), %rsi
movq %rbp, %rdi
callq 0x3420
incq %r12
jmp 0x4f45
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32a0
leaq 0x20(%rsp), %rdi
callq 0x3498
incq %r13
jmp 0x4e32
testb $0x1, 0x10(%rsp)
jne 0x4fe3
movups 0x3373(%rip), %xmm0     # 0x8310
movups %xmm0, 0x14b(%rsp)
movups 0x3359(%rip), %xmm0     # 0x8305
movaps %xmm0, 0x140(%rsp)
jmp 0x4fe3
movups 0x3202(%rip), %xmm0     # 0x81bf
movups %xmm0, 0x14b(%rsp)
movups 0x31e8(%rip), %xmm0     # 0x81b4
movaps %xmm0, 0x140(%rsp)
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32a0
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32a0
movl 0x8(%rbx), %edi
leaq 0x140(%rsp), %rsi
movl $0x1f00, %edx           # imm = 0x1F00
xorl %ecx, %ecx
callq 0x3170
addq $0x2048, %rsp           # imm = 0x2048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x501d
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3498
movq %rbx, %rdi
callq 0x3410