// File: /workspace/github_cpp_makefile_star2_O2/hyp1231[P]FMSS/filesystem.cpp
// Function: FileSystem::DeleteFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool FileSystem::DeleteFile(const string &filepath, const string param) {
    /* 
        step 1: find the right directory
    */

	string filename;
    int dir_inodeNum = Analysis_path(filepath, filename);
    if(dir_inodeNum == -1) {
    	return false;
    }

	if(filename == "." || filename == "..") {
        cout << "[Error] '.' or '..' cannot be removed" << endl;
        return false;
    }

    /*
        step 2: make sure file exists; otherwise, return FALSE
    */

    int file_inodeNum = Get_file_inodeNum_from_dir(dir_inodeNum, filename, false);
    if (file_inodeNum == -1) {
        cout << "[Error] File doesn't exist (cannot delete file \""
        << filename << "\")" << endl;
        return false;
    }

    /*
        step 3: delete data in file's data_BLK
        (set corresponding bit in data bitmap to 0)
    */

    bool isFile;
    char file_buf[64];
    Get_inode_from_inodeNum(file_buf, file_inodeNum);
    // judge if inode is a file_inode
    if (byte2int(file_buf, 0, 2) == 0)
        isFile = true;
    else 
        isFile = false;

    if (!isFile) {  // if it's dir, data should be deleted recursively
        if(param != "-r") {
            cout << "[Error] Cannot delete directory" << endl;
            return false;
        }

        char fileBLK[BLKsize];
        int dataNum = byte2int(file_buf, 42, 44);
        Fill_data_block_bitmap(dataNum, false);
        D.Getblk(fileBLK, Get_actual_dataBLKnumber(dataNum));
        for (int i = 24; i + 12 < BLKsize; i += 12) {
            if (byte2int(fileBLK, i + 8, i + 10) == 0) 
                continue;
            string tmp_filename = byte2string(fileBLK, i, i + 8);
            // delete all files in dir recursively
            DeleteFile(filepath + '/' + tmp_filename, param);
        }
    } else {    // if it's a file, just delete data block one by one
        for (int i = 42; i < 58; i += 2) {
            char fileBLK[BLKsize];
            int dataNum = byte2int(file_buf, i, i + 2);
            Fill_data_block_bitmap(dataNum, false);
            // if EOF, break
            D.Getblk(fileBLK, Get_actual_dataBLKnumber(dataNum));
            if (FindEOF(fileBLK) != -1)
                break;
        }
    }

    /*
        step 4: modify dir's data block to remove this file's inode
        (set valid to 0x0)
     */
    Get_file_inodeNum_from_dir(dir_inodeNum, filename, true);

    /*
        step 5: remove file's inode
        (set corresponding bit in inode bitmap to 0)
    */

    Fill_inode_bitmap(file_inodeNum, false);

    /*
        step 6: save the super block
    */

    Save_superBLK();

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x50(%rbp), %rdx
callq 0x3c86
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x3961
leaq 0x3ba5(%rip), %rsi      # 0x74ca
leaq -0x50(%rbp), %rdi
callq 0x54ac
testb %al, %al
jne 0x3946
leaq 0x3b90(%rip), %rsi      # 0x74c9
leaq -0x50(%rbp), %rdi
callq 0x54ac
testb %al, %al
je 0x397f
movq 0x7683(%rip), %rdi      # 0xafd0
leaq 0x3bab(%rip), %rsi      # 0x74ff
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
xorl %r14d, %r14d
leaq -0x50(%rbp), %rdi
callq 0x20e0
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x50(%rbp), %rdx
movq %rbx, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0x2ff2
movl %eax, %r15d
cmpl $-0x1, %eax
je 0x39fa
movl %r14d, -0x30(%rbp)
leaq -0x110(%rbp), %rsi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x3176
leaq -0x110(%rbp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x2917
testl %eax, %eax
je 0x3a35
leaq 0x3b8f(%rip), %rsi      # 0x7559
movq %r12, %rdi
callq 0x54bf
testb %al, %al
je 0x3ab7
movq 0x75ef(%rip), %rdi      # 0xafd0
leaq 0x3b74(%rip), %rsi      # 0x755c
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
jmp 0x3961
movq 0x75cf(%rip), %rdi      # 0xafd0
leaq 0x3b1d(%rip), %rsi      # 0x7525
callq 0x21d0
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x21b0
leaq 0x3b36(%rip), %rsi      # 0x7556
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
jmp 0x3961
movl %r15d, -0x2c(%rbp)
pushq $0x2a
popq %rsi
leaq 0x8(%rbx), %r14
cmpl $0x39, %esi
ja 0x3be3
movq %rsp, %r15
movl (%rbx), %eax
movq %rsp, %r13
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leal 0x2(%rsi), %r12d
leaq -0x110(%rbp), %rdi
movl %r12d, %edx
callq 0x2917
movl %eax, %r8d
cltd
pushq $0x50
popq %rcx
idivl %ecx
movl %edx, %ecx
pushq $0x1
popq %rdx
shll %cl, %edx
cltq
notb %dl
andb %dl, 0x5a(%rbx,%rax)
addl $0x6, %r8d
movq %r14, %rdi
movq %r13, %rsi
movl %r8d, %edx
callq 0x2648
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3344
movq %r15, %rsp
movl %r12d, %esi
cmpl $-0x1, %eax
je 0x3a40
jmp 0x3be3
movq %r13, -0x68(%rbp)
movq %r12, -0x60(%rbp)
movl %r15d, -0x2c(%rbp)
movq %rsp, -0x58(%rbp)
movl (%rbx), %eax
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
leaq -0x110(%rbp), %rdi
pushq $0x2a
popq %rsi
pushq $0x2c
popq %rdx
callq 0x2917
movl %eax, %r8d
pushq $0x50
popq %rcx
cltd
idivl %ecx
movl %edx, %ecx
pushq $0x1
popq %rdx
shll %cl, %edx
cltq
notb %dl
andb %dl, 0x5a(%rbx,%rax)
leaq 0x8(%rbx), %rdi
addl $0x6, %r8d
movq %r15, %rsi
movl %r8d, %edx
callq 0x2648
pushq $0x18
popq %r12
leal 0xc(%r12), %r13d
cmpl (%rbx), %r13d
jge 0x3bdf
leal 0x8(%r12), %r14d
leal 0xa(%r12), %edx
movq %r15, %rdi
movl %r14d, %esi
callq 0x2917
testl %eax, %eax
je 0x3bd7
leaq -0x88(%rbp), %rdi
movq %r15, %rsi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x2967
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
movq -0x68(%rbp), %rsi
pushq $0x2f
popq %rdx
callq 0x54e8
leaq -0xc8(%rbp), %rdi
movq %r14, %rsi
leaq -0x88(%rbp), %rdx
callq 0x54c9
leaq -0x130(%rbp), %r12
movq %r12, %rdi
movq -0x60(%rbp), %rsi
callq 0x22b0
movq %rbx, %rdi
leaq -0xc8(%rbp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x38e0
movq %r12, %rdi
callq 0x20e0
movq %r14, %rdi
callq 0x20e0
leaq -0xa8(%rbp), %rdi
callq 0x20e0
leaq -0x88(%rbp), %rdi
callq 0x20e0
movl %r13d, %r12d
jmp 0x3b1b
movq -0x58(%rbp), %rsp
leaq -0x50(%rbp), %rdx
pushq $0x1
popq %r14
movq %rbx, %rdi
movl -0x30(%rbp), %esi
movl %r14d, %ecx
callq 0x2ff2
movl -0x2c(%rbp), %eax
pushq $0xa
popq %rcx
cltd
idivl %ecx
movl %edx, %ecx
shll %cl, %r14d
cltq
notb %r14b
andb %r14b, 0x50(%rbx,%rax)
movb $0x1, %r14b
movq %rbx, %rdi
callq 0x32ca
jmp 0x3964
jmp 0x3c71
jmp 0x3c71
jmp 0x3c71
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x20e0
jmp 0x3c3b
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x20e0
jmp 0x3c51
movq %rax, %rbx
jmp 0x3c5d
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x20e0
leaq -0x88(%rbp), %rdi
callq 0x20e0
jmp 0x3c74
jmp 0x3c71
jmp 0x3c71
jmp 0x3c71
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x2340
nop