// File: 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
subq $0x200, %rsp            # imm = 0x200
movq %rdx, -0x170(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x33e0
movq -0x168(%rbp), %rdi
movq -0x160(%rbp), %rdx
movq -0x18(%rbp), %rsi
callq 0x65b0
movl %eax, -0x158(%rbp)
jmp 0x5fa8
movl -0x158(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x5fd8
movb $0x0, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x657e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x6591
leaq 0x9506(%rip), %rsi      # 0xf4e5
leaq -0x40(%rbp), %rdi
callq 0x9320
movb %al, -0x171(%rbp)
jmp 0x5ff0
movb -0x171(%rbp), %al
testb $0x1, %al
jne 0x6020
jmp 0x5ffc
leaq 0x94e1(%rip), %rsi      # 0xf4e4
leaq -0x40(%rbp), %rdi
callq 0x9320
movb %al, -0x172(%rbp)
jmp 0x6014
movb -0x172(%rbp), %al
testb $0x1, %al
jne 0x6020
jmp 0x6061
movq 0xefa9(%rip), %rdi      # 0x14fd0
leaq 0x94ec(%rip), %rsi      # 0xf51a
callq 0x3250
movq %rax, -0x180(%rbp)
jmp 0x603c
movq -0x180(%rbp), %rdi
movq 0xef76(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x6051
movb $0x0, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x657e
movq -0x168(%rbp), %rdi
movl -0x44(%rbp), %esi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4d30
movl %eax, -0x184(%rbp)
jmp 0x607e
movl -0x184(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $-0x1, -0x5c(%rbp)
jne 0x6103
movq 0xef3c(%rip), %rdi      # 0x14fd0
leaq 0x94a5(%rip), %rsi      # 0xf540
callq 0x3250
movq %rax, -0x190(%rbp)
jmp 0x60a9
movq -0x190(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3230
movq %rax, -0x198(%rbp)
jmp 0x60c2
movq -0x198(%rbp), %rdi
leaq 0x94a1(%rip), %rsi      # 0xf571
callq 0x3250
movq %rax, -0x1a0(%rbp)
jmp 0x60de
movq -0x1a0(%rbp), %rdi
movq 0xeed4(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x60f3
movb $0x0, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x657e
movq -0x168(%rbp), %rdi
movl -0x5c(%rbp), %edx
leaq -0xa0(%rbp), %rsi
callq 0x4ff0
jmp 0x611b
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x40e0
movl %eax, -0x1a4(%rbp)
jmp 0x6136
movl -0x1a4(%rbp), %eax
cmpl $0x0, %eax
jne 0x6147
movb $0x1, -0x5d(%rbp)
jmp 0x614b
movb $0x0, -0x5d(%rbp)
testb $0x1, -0x5d(%rbp)
jne 0x6409
movq -0x170(%rbp), %rdi
leaq 0x9411(%rip), %rsi      # 0xf574
callq 0x94b0
movb %al, -0x1a5(%rbp)
jmp 0x6170
movb -0x1a5(%rbp), %al
testb $0x1, %al
jne 0x617c
jmp 0x61bd
movq 0xee4d(%rip), %rdi      # 0x14fd0
leaq 0x93ed(%rip), %rsi      # 0xf577
callq 0x3250
movq %rax, -0x1b0(%rbp)
jmp 0x6198
movq -0x1b0(%rbp), %rdi
movq 0xee1a(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x61ad
movb $0x0, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x657e
movq -0x168(%rbp), %rax
movl (%rax), %eax
movq %rsp, %rcx
movq %rcx, -0xa8(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1c0(%rbp)
movq %rcx, %rsp
movq %rax, -0xb0(%rbp)
leaq -0xa0(%rbp), %rdi
movl $0x2a, %esi
movl $0x2c, %edx
callq 0x40e0
movl %eax, -0x1b4(%rbp)
jmp 0x6210
movq -0x168(%rbp), %rdi
movl -0x1b4(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %esi
xorl %edx, %edx
callq 0x5270
movq -0x168(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x1d0(%rbp)
movl -0xb4(%rbp), %esi
callq 0x9390
movl %eax, -0x1c4(%rbp)
jmp 0x6258
movl -0x1c4(%rbp), %edx
movq -0x1c0(%rbp), %rsi
movq -0x1d0(%rbp), %rdi
callq 0x3900
jmp 0x6273
movl $0x18, -0xb8(%rbp)
movq -0x168(%rbp), %rcx
movl -0xb8(%rbp), %eax
addl $0xc, %eax
cmpl (%rcx), %eax
jge 0x63fa
movq -0x1c0(%rbp), %rdi
movl -0xb8(%rbp), %edx
movl %edx, %esi
addl $0x8, %esi
addl $0xa, %edx
callq 0x40e0
movl %eax, -0x1d4(%rbp)
jmp 0x62b7
movl -0x1d4(%rbp), %eax
cmpl $0x0, %eax
jne 0x62c7
jmp 0x637b
movq -0x1c0(%rbp), %rsi
movl -0xb8(%rbp), %edx
movl %edx, %ecx
addl $0x8, %ecx
leaq -0xd8(%rbp), %rdi
callq 0x4180
jmp 0x62e7
movq -0x18(%rbp), %rsi
leaq -0x118(%rbp), %rdi
movl $0x2f, %edx
callq 0x9530
jmp 0x62fe
leaq -0xf8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
callq 0x94e0
jmp 0x631a
movq -0x170(%rbp), %rsi
leaq -0x138(%rbp), %rdi
callq 0x30f0
jmp 0x632f
movq -0x168(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
leaq -0x138(%rbp), %rdx
callq 0x5f50
jmp 0x634b
leaq -0x138(%rbp), %rdi
callq 0x3160
leaq -0xf8(%rbp), %rdi
callq 0x3160
leaq -0x118(%rbp), %rdi
callq 0x3160
leaq -0xd8(%rbp), %rdi
callq 0x3160
movl -0xb8(%rbp), %eax
addl $0xc, %eax
movl %eax, -0xb8(%rbp)
jmp 0x627d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x63e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x63dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x63d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x3160
leaq -0xf8(%rbp), %rdi
callq 0x3160
leaq -0x118(%rbp), %rdi
callq 0x3160
leaq -0xd8(%rbp), %rdi
callq 0x3160
jmp 0x6591
movq -0xa8(%rbp), %rax
movq %rax, %rsp
jmp 0x653a
movl $0x2a, -0x13c(%rbp)
cmpl $0x3a, -0x13c(%rbp)
jge 0x6538
movq -0x168(%rbp), %rax
movl (%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x148(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1e0(%rbp)
movq %rcx, %rsp
movq %rax, -0x150(%rbp)
movl -0x13c(%rbp), %esi
movl %esi, %edx
addl $0x2, %edx
leaq -0xa0(%rbp), %rdi
callq 0x40e0
movl %eax, -0x1d8(%rbp)
jmp 0x6474
movq -0x168(%rbp), %rdi
movl -0x1d8(%rbp), %eax
movl %eax, -0x154(%rbp)
movl -0x154(%rbp), %esi
xorl %edx, %edx
callq 0x5270
movq -0x168(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x1f0(%rbp)
movl -0x154(%rbp), %esi
callq 0x9390
movl %eax, -0x1e4(%rbp)
jmp 0x64bc
movl -0x1e4(%rbp), %edx
movq -0x1e0(%rbp), %rsi
movq -0x1f0(%rbp), %rdi
callq 0x3900
jmp 0x64d7
movq -0x1e0(%rbp), %rsi
movq -0x168(%rbp), %rdi
callq 0x53d0
movl %eax, -0x1f4(%rbp)
jmp 0x64f2
movl -0x1f4(%rbp), %eax
cmpl $-0x1, %eax
je 0x6506
movl $0x5, -0x58(%rbp)
jmp 0x650d
movl $0x0, -0x58(%rbp)
movq -0x148(%rbp), %rax
movq %rax, %rsp
movl -0x58(%rbp), %eax
testl %eax, %eax
je 0x6522
jmp 0x6520
jmp 0x6538
jmp 0x6524
movl -0x13c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x13c(%rbp)
jmp 0x6413
jmp 0x653a
movq -0x168(%rbp), %rdi
movl -0x44(%rbp), %esi
leaq -0x40(%rbp), %rdx
movl $0x1, %ecx
callq 0x4d30
jmp 0x6554
movq -0x168(%rbp), %rdi
movl -0x5c(%rbp), %esi
xorl %edx, %edx
callq 0x51e0
movq -0x168(%rbp), %rdi
callq 0x5300
jmp 0x6573
movb $0x1, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3160
movb -0x1(%rbp), %al
andb $0x1, %al
movq %rbp, %rsp
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x3160
movq -0x50(%rbp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)