// File: /workspace/github_cpp_makefile_star2_O2/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::head(char const*, unsigned int)
void FileSys::head(const char *name, unsigned int n)
{
  bool found = false;
  bool error = false;
  char buffer [MAX_FILE_SIZE + 256];

  datablock_t* file_contents = new datablock_t;
  dirblock_t dir_block;

  // read contents of current directory's directory node into dir_ptr
  bfs.read_block(curr_dir, (void *) &dir_block);

  // look through all elements of dir_entries held in current directory blocks
  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;
            }
          else if(!is_directory(dir_block.dir_entries[i].block_num))
            {
              inode_t file_inode;
              unsigned int bytes_left;
              //read inode data for targeted file
              bfs.read_block(dir_block.dir_entries[i].block_num, (void *) &file_inode);

              if(n > file_inode.size)
              {
                bytes_left == file_inode.size;
              }

              string size_str = to_string(file_inode.size);
              int byte_count = (size_str.length() + 1);
              char* file_size = new char[16];
              strcpy(file_size, 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);
                  if(bytes_left < BLOCK_SIZE)
                  {
                    strncat(buffer, file_contents->data, bytes_left);
                  }
                  else
                  {
                    strcat(buffer, file_contents->data);
                    bytes_left -= BLOCK_SIZE;
                  }
                }
            }
        }
    }
  // 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, 0x18(%rsp)
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), %r12
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq $0x0, (%rsp)
cmpq $0xa, %r13
je 0x521c
imulq $0xc, %r13, %rbp
leaq 0xc8(%rsp), %rax
addq %rax, %rbp
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x3350
testl %eax, %eax
jne 0x5214
movswl 0xa(%rbp), %esi
movq %rbx, %rdi
callq 0x3d22
testb %al, %al
je 0x510a
movups 0x30ed(%rip), %xmm0     # 0x81bf
movups %xmm0, 0x14b(%rsp)
movups 0x30d3(%rip), %xmm0     # 0x81b4
movaps %xmm0, 0x140(%rsp)
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32a0
movb $0x1, %al
movq %rax, 0x10(%rsp)
movb $0x1, %al
movq %rax, (%rsp)
jmp 0x5214
movswl 0xa(%rbp), %esi
movq %rbx, %rdi
callq 0x3d22
movb $0x1, %cl
movq %rcx, (%rsp)
testb %al, %al
jne 0x5214
movswl 0xa(%rbp), %esi
movq %rbx, %rdi
leaq 0x40(%rsp), %rdx
callq 0x380e
movl 0x44(%rsp), %esi
leaq 0x20(%rsp), %rdi
callq 0x5334
pushq $0x10
popq %rdi
callq 0x3030
movq %rax, %rbp
movq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x3260
movq %r12, %rdi
callq 0x3140
xorl %ecx, %ecx
movb %cl, 0x150(%rsp,%rax)
movups 0x3181(%rip), %xmm0     # 0x82f4
movups %xmm0, 0x140(%rsp,%rax)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3420
movq %r12, %rdi
callq 0x3140
movw $0xa0d, %cx             # imm = 0xA0D
movw %cx, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %r12, %rdi
callq 0x3140
movw $0xa0d, %cx             # imm = 0xA0D
movw %cx, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %rbp, %rdi
callq 0x3310
pushq $0x4
popq %rbp
cmpq $0x7c, %rbp
je 0x520a
movswl 0x40(%rsp,%rbp,2), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x380e
movq 0x8(%rsp), %rsi
cmpl $0x7f, %r14d
ja 0x51f9
movl %r14d, %edx
movq %r12, %rdi
callq 0x31c0
jmp 0x5205
movq %r12, %rdi
callq 0x3420
addl $-0x80, %r14d
incq %rbp
jmp 0x51cb
leaq 0x20(%rsp), %rdi
callq 0x3498
incq %r13
jmp 0x508d
movq (%rsp), %rax
orb 0x10(%rsp), %al
testb $0x1, %al
jne 0x5246
movups 0x30e1(%rip), %xmm0     # 0x8310
movups %xmm0, 0x14b(%rsp)
movups 0x30c7(%rip), %xmm0     # 0x8305
movaps %xmm0, 0x140(%rsp)
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 0x5280
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3498
movq %rbx, %rdi
callq 0x3410
nop