// File: /workspace/github_c_makefile_star2_O2/evanwire[P]GitClone/server.c
// Function: update
void update(char* command, int connfd){
  //Parse project name from the message received from the client
  char project_name[strlen(command) - 1];
  memset(project_name, 0, sizeof(project_name));
  int i;
  for(i = 2; i < strlen(command); i++){
      if(command[i]){
        project_name[i - 2] = command[i];
      }
      
  }

  //Set up buffer for responding to client
  char sendBuff[1024];
  memset(sendBuff, '\0', sizeof(sendBuff));

  //Attempt to open directory with the name specified by the client
  DIR* dir = opendir(project_name);

  //Report an error assuming the project does not exist
  if(ENOENT == errno){
    closedir(dir);
    strcpy(sendBuff, "x");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;

  //project does exist, so continue
  }else if (dir){
    //do nothing and continue

  //opendir() failed
  }else{
    printf("\nERROR: opendir() failed.\n");
    strcpy(sendBuff, "e");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;
  }

  //Open the needed Manifest file
  char* manifestPath = (char*)malloc(sizeof(project_name)+11);
  strcpy(manifestPath,project_name);
  strcat(manifestPath,"/.Manifest");

  int manifest_fd = open(manifestPath, O_RDONLY);

    /*
    TODO: Communicate errors like these to clients in an efficient way
        -Make a method for only communicating errors
    */
  if(manifest_fd < 0){
    printf("\nError: Was unable to open/create the .Manifest file. Please try again.\n");
    exit(1);
  }
  char* fileContents=readFile(manifestPath);
  printf("Message: -%s- size: %d\n", fileContents, strlen(fileContents));
  write(connfd, fileContents, strlen(fileContents));
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movl %esi, %ebx
movq %rdi, %r12
callq 0x10b0
movq %rax, %r14
leaq -0x1(%rax), %rdx
movq %rsp, %r15
addq $0xe, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
pushq $0x2
popq %rax
cmpq %rax, %r14
jbe 0x28bd
movb (%r12,%rax), %cl
testb %cl, %cl
je 0x28b8
movb %cl, -0x2(%r15,%rax)
incq %rax
jmp 0x28a6
leaq -0x420(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
callq 0x1210
movq %rax, %r12
callq 0x1030
cmpl $0x2, (%rax)
jne 0x28ff
movq %r12, %rdi
callq 0x1260
leaq -0x420(%rbp), %r14
movw $0x78, (%r14)
jmp 0x29b6
testq %r12, %r12
je 0x299d
addq $0xa, %r14
movq %r14, %rdi
callq 0x11d0
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x1150
movq %r14, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%r14,%rax)
movl $0x747365, 0x7(%r14,%rax) # imm = 0x747365
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1100
testl %eax, %eax
js 0x29f4
movq %r14, %rdi
callq 0x320a
movq %rax, %r14
movq %rax, %rdi
callq 0x10b0
leaq 0x196a(%rip), %rdi      # 0x42d9
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
leaq -0x20(%rbp), %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10a0
leaq 0x1cf5(%rip), %rdi      # 0x4699
callq 0x1230
leaq -0x420(%rbp), %r14
movw $0x65, (%r14)
movq %r14, %rdi
callq 0x10b0
leaq 0x1914(%rip), %rdi      # 0x42d9
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
leaq -0x20(%rbp), %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1cb8(%rip), %rdi      # 0x46b3
callq 0x1230
pushq $0x1
popq %rdi
callq 0x11a0