// File: 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
movq %rsp, %rbp
subq $0x20b0, %rsp           # imm = 0x20B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2078(%rbp)
movb $0x0, -0x11(%rbp)
movb $0x0, -0x12(%rbp)
movl $0x80, %edi
callq 0x3350
movq %rax, %rcx
movq -0x2078(%rbp), %rax
movq %rcx, -0x1f28(%rbp)
leaq -0x1fa8(%rbp), %rdx
movq %rax, %rdi
movswl 0x4(%rax), %esi
callq 0x3c20
movl $0x0, -0x1fac(%rbp)
cmpl $0xa, -0x1fac(%rbp)
jge 0x6573
movq -0x10(%rbp), %rdi
leaq -0x1fa8(%rbp), %rsi
addq $0x8, %rsi
movslq -0x1fac(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rsi
callq 0x3480
cmpl $0x0, %eax
jne 0x655d
movq -0x2078(%rbp), %rdi
movb $0x1, -0x12(%rbp)
leaq -0x1fa8(%rbp), %rax
addq $0x8, %rax
movslq -0x1fac(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %esi
callq 0x4470
testb $0x1, %al
jne 0x6329
jmp 0x636a
leaq -0x1f20(%rbp), %rdi
leaq 0x5e86(%rip), %rsi      # 0xc1bd
callq 0x3320
movb $0x1, -0x11(%rbp)
movq -0x1f28(%rbp), %rax
movq %rax, -0x2080(%rbp)
cmpq $0x0, %rax
je 0x6365
movq -0x2080(%rbp), %rdi
movl $0x80, %esi
callq 0x3370
jmp 0x6573
movq -0x2078(%rbp), %rdi
leaq -0x1fa8(%rbp), %rax
addq $0x8, %rax
movslq -0x1fac(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %esi
callq 0x4470
testb $0x1, %al
jne 0x6559
movq -0x2078(%rbp), %rdi
movslq -0x1fac(%rbp), %rax
leaq (%rax,%rax,2), %rax
movswl -0x1f96(%rbp,%rax,4), %esi
leaq -0x202c(%rbp), %rdx
callq 0x3c20
movl -0x2028(%rbp), %esi
leaq -0x2050(%rbp), %rdi
movq %rdi, -0x2090(%rbp)
callq 0x6b70
movq -0x2090(%rbp), %rdi
callq 0x34c0
incl %eax
movl %eax, -0x2054(%rbp)
movslq -0x2054(%rbp), %rdi
callq 0x3040
movq %rax, -0x2088(%rbp)
jmp 0x6403
movq -0x2088(%rbp), %rax
movq %rax, -0x2060(%rbp)
movq -0x2060(%rbp), %rax
movq %rax, -0x20a0(%rbp)
leaq -0x2050(%rbp), %rdi
callq 0x3190
movq -0x20a0(%rbp), %rdi
movq %rax, %rsi
callq 0x3320
leaq -0x1f20(%rbp), %rdi
leaq 0x5eac(%rip), %rsi      # 0xc2f4
callq 0x3580
leaq -0x1f20(%rbp), %rdi
movq -0x2060(%rbp), %rsi
callq 0x3580
leaq -0x1f20(%rbp), %rdi
leaq 0x5c7a(%rip), %rsi      # 0xc0e8
callq 0x3580
leaq -0x1f20(%rbp), %rdi
leaq 0x5c67(%rip), %rsi      # 0xc0e8
callq 0x3580
movq -0x2060(%rbp), %rax
movq %rax, -0x2098(%rbp)
cmpq $0x0, %rax
je 0x64a6
movq -0x2098(%rbp), %rdi
callq 0x3460
movl $0x0, -0x2070(%rbp)
movslq -0x2070(%rbp), %rax
cmpq $0x78, %rax
jae 0x6528
movq -0x2078(%rbp), %rdi
movslq -0x2070(%rbp), %rax
movswl -0x2024(%rbp,%rax,2), %esi
leaq -0x1f28(%rbp), %rdx
callq 0x3c20
jmp 0x64e1
leaq -0x1f20(%rbp), %rdi
movq -0x1f28(%rbp), %rsi
callq 0x3580
movl -0x2070(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2070(%rbp)
jmp 0x64b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2068(%rbp)
movl %eax, -0x206c(%rbp)
leaq -0x2050(%rbp), %rdi
callq 0x3628
jmp 0x65dd
movq -0x1f28(%rbp), %rax
movq %rax, -0x20a8(%rbp)
cmpq $0x0, %rax
je 0x654d
movq -0x20a8(%rbp), %rdi
movl $0x80, %esi
callq 0x3370
leaq -0x2050(%rbp), %rdi
callq 0x3628
jmp 0x655b
jmp 0x655d
jmp 0x655f
movl -0x1fac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1fac(%rbp)
jmp 0x62be
testb $0x1, -0x12(%rbp)
jne 0x6592
testb $0x1, -0x11(%rbp)
jne 0x6592
leaq -0x1f20(%rbp), %rdi
leaq 0x5d78(%rip), %rsi      # 0xc305
callq 0x3320
movq -0x1f28(%rbp), %rax
movq %rax, -0x20b0(%rbp)
cmpq $0x0, %rax
je 0x65b7
movq -0x20b0(%rbp), %rdi
movl $0x80, %esi
callq 0x3370
movq -0x2078(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x1f20(%rbp), %rsi
movl $0x1f00, %edx           # imm = 0x1F00
xorl %ecx, %ecx
callq 0x31d0
addq $0x20b0, %rsp           # imm = 0x20B0
popq %rbp
retq
movq -0x2068(%rbp), %rdi
callq 0x3560
nopl (%rax)