// File: 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
movq %rsp, %rbp
subq $0x20b0, %rsp           # imm = 0x20B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2078(%rbp)
movb $0x0, -0x15(%rbp)
movb $0x0, -0x16(%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 0x6937
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 0x6921
movq -0x2078(%rbp), %rdi
movb $0x1, -0x15(%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 0x66bc
jmp 0x66fd
leaq -0x1f20(%rbp), %rdi
leaq 0x5af3(%rip), %rsi      # 0xc1bd
callq 0x3320
movb $0x1, -0x16(%rbp)
movq -0x1f28(%rbp), %rax
movq %rax, -0x2080(%rbp)
cmpq $0x0, %rax
je 0x66f8
movq -0x2080(%rbp), %rdi
movl $0x80, %esi
callq 0x3370
jmp 0x691f
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 0x691d
movq -0x2078(%rbp), %rdi
leaq -0x1fa8(%rbp), %rax
addq $0x8, %rax
movslq -0x1fac(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
leaq -0x202c(%rbp), %rdx
movswl 0xa(%rax), %esi
callq 0x3c20
movl -0x14(%rbp), %eax
cmpl -0x2028(%rbp), %eax
jbe 0x6769
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)
movl $0x10, %edi
callq 0x3040
movq %rax, -0x2088(%rbp)
jmp 0x67a9
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 0x5b06(%rip), %rsi      # 0xc2f4
callq 0x3580
leaq -0x1f20(%rbp), %rdi
movq -0x2060(%rbp), %rsi
callq 0x3580
leaq -0x1f20(%rbp), %rdi
leaq 0x58d4(%rip), %rsi      # 0xc0e8
callq 0x3580
leaq -0x1f20(%rbp), %rdi
leaq 0x58c1(%rip), %rsi      # 0xc0e8
callq 0x3580
movq -0x2060(%rbp), %rax
movq %rax, -0x2098(%rbp)
cmpq $0x0, %rax
je 0x684c
movq -0x2098(%rbp), %rdi
callq 0x3460
movl $0x0, -0x2070(%rbp)
movslq -0x2070(%rbp), %rax
cmpq $0x78, %rax
jae 0x6911
movq -0x2078(%rbp), %rdi
movslq -0x2070(%rbp), %rax
movswl -0x2024(%rbp,%rax,2), %esi
leaq -0x1f28(%rbp), %rdx
callq 0x3c20
jmp 0x688b
cmpl $0x80, -0x2030(%rbp)
jae 0x68d7
leaq -0x1f20(%rbp), %rdi
movq -0x1f28(%rbp), %rsi
movl -0x2030(%rbp), %eax
movl %eax, %edx
callq 0x3250
jmp 0x68fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2068(%rbp)
movl %eax, -0x206c(%rbp)
leaq -0x2050(%rbp), %rdi
callq 0x3628
jmp 0x69a1
leaq -0x1f20(%rbp), %rdi
movq -0x1f28(%rbp), %rsi
callq 0x3580
movl -0x2030(%rbp), %eax
subl $0x80, %eax
movl %eax, -0x2030(%rbp)
jmp 0x68fd
movl -0x2070(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2070(%rbp)
jmp 0x6856
leaq -0x2050(%rbp), %rdi
callq 0x3628
jmp 0x691f
jmp 0x6921
jmp 0x6923
movl -0x1fac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1fac(%rbp)
jmp 0x6651
testb $0x1, -0x15(%rbp)
jne 0x6956
testb $0x1, -0x16(%rbp)
jne 0x6956
leaq -0x1f20(%rbp), %rdi
leaq 0x59b4(%rip), %rsi      # 0xc305
callq 0x3320
movq -0x1f28(%rbp), %rax
movq %rax, -0x20a8(%rbp)
cmpq $0x0, %rax
je 0x697b
movq -0x20a8(%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)