// File: /workspace/github_c_makefile_star2_O2/icysunlight[P]miniftpd/ftpproto.c
// Function: do_retr
void do_retr(session_t *sess)
{
    //打开数据连接
    int data_fd = get_datafd(sess);
    sess->datafd = data_fd;
    if ( -1 == data_fd )
    {
        ftp_reply(sess,FTP_BADSENDCONN,"Use port or pasv first");
        return;
    }

    //打开文件并加锁
    if ( is_dir(sess->arg) )
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Read local file fail.");
        return;
    }
    int filefd = open(sess->arg,O_RDONLY);
    if ( -1 == filefd )
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Read local file fail.");
        return;
    }
    if ( -1 == file_rdlock(filefd)) 
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Read local file fail.");
        close(filefd);
        return;
    }

    //读取文件大小
    off_t filesize = lseek(filefd,0,SEEK_END);
    lseek(filefd,sess->rest_pos,SEEK_SET);
    
    //发送开始信息
    char buf[2048] = {0};
    sprintf(buf,"Openning BINARY mode data connect for %s (%lld bytes)",sess->arg,(long long)filesize);
    ftp_reply(sess,FTP_DATACONN,buf);

    int flag = 0;
    //发送文件
    if ( 0 == sess->bASCII)
    {
        off_t sizetosend = filesize - sess->rest_pos;
        while ( 0 != sizetosend )
        {
            ratelim_init(sess);
            data_disconn_start();
            ssize_t sizecur = sizetosend > 2048 ? 2048 : sizetosend;
            ssize_t ret = sendfile(data_fd,filefd,NULL,sizecur);
            if ( -1 == ret )
            {
                flag = 1;
                break; 
            } 
            sizetosend -= ret;
            ratelim_do(sess,tunable_download_max_rate,ret);
            b_dataconn = 1; 
        }
    }

    switch (flag)
    {
        case 0:
            ftp_reply(sess,FTP_TRANSFEROK,"Transfer complete.");
            break;
        case 1:
            ftp_reply(sess,FTP_BADSENDNET,"Send file failure.");
            break;
    }

    is_urg(sess);
    file_unlock(filefd);
    close(data_fd);
    close(filefd);
    clear_transfer(sess);
    sess->rest_pos = 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x808, %rsp            # imm = 0x808
movq %rdi, %rbx
callq 0x514d
movl %eax, 0xc(%rbx)
cmpl $-0x1, %eax
je 0x55c3
movl %eax, %ebp
leaq 0x818(%rbx), %r12
movq %r12, %rdi
callq 0x4d80
testl %eax, %eax
je 0x55e8
leaq 0x2015(%rip), %rdx      # 0x75ce
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
jmp 0x55d2
leaq 0x1fed(%rip), %rdx      # 0x75b7
movq %rbx, %rdi
movl $0x1a9, %esi            # imm = 0x1A9
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x489c
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3450
cmpl $-0x1, %eax
je 0x55b2
movl %eax, %r14d
movl %eax, %edi
callq 0x3925
cmpl $-0x1, %eax
je 0x56c5
pushq $0x2
popq %rdx
movl %r14d, %edi
xorl %esi, %esi
callq 0x31f0
movq %rax, %r15
movq 0xc30(%rbx), %rsi
movl %r14d, %edi
xorl %edx, %edx
callq 0x31f0
movq %rsp, %r13
movl $0x800, %edx            # imm = 0x800
movq %r13, %rdi
xorl %esi, %esi
callq 0x3220
leaq 0x1f9e(%rip), %rsi      # 0x75e4
movq %r13, %rdi
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x34d0
movq %rbx, %rdi
movl $0x96, %esi
movq %r13, %rdx
callq 0x489c
cmpl $0x0, 0x10(%rbx)
je 0x56f2
leaq 0x1fa3(%rip), %rdx      # 0x761a
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x489c
movq %rbx, %rdi
callq 0x4f9c
movl %r14d, %edi
callq 0x396c
movl %ebp, %edi
callq 0x3260
movl %r14d, %edi
callq 0x3260
movq %rbx, %rdi
callq 0x434f
andq $0x0, 0xc30(%rbx)
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f02(%rip), %rdx      # 0x75ce
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
callq 0x489c
movl %r14d, %edi
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3260
subq 0xc30(%rbx), %r15
leaq 0x5bf0(%rip), %r12      # 0xb2f0
testq %r15, %r15
je 0x5670
movq %rbx, %rdi
callq 0x43c7
callq 0x4e1e
movl $0x800, %eax            # imm = 0x800
movq %r15, %rcx
cmpq %rax, %r15
jl 0x5728
movl $0x800, %ecx            # imm = 0x800
movl %ebp, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0x33e0
cmpq $-0x1, %rax
je 0x5757
subq %rax, %r15
movl (%r12), %esi
movq %rbx, %rdi
movl %eax, %edx
callq 0x4400
movl $0x1, 0x5bcb(%rip)      # 0xb320
jmp 0x5700
leaq 0x1ecf(%rip), %rdx      # 0x762d
movq %rbx, %rdi
movl $0x1aa, %esi            # imm = 0x1AA
jmp 0x567f