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

    //打开文件并加锁
    int filefd = open(sess->arg,O_WRONLY | O_CREAT , 0666);
    if ( -1 == filefd )
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Create local file fail.");
        ERR_EXIT("open");
        return;
    }
    if ( -1 == file_wrlock(filefd)) 
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Create local file fail.");
        close(filefd);
        return;
    }

    //定位ref
    if ( sess->rest_pos != 0)
        lseek(filefd,sess->rest_pos,SEEK_SET);
    else if ( sess->bappe ) 
    {
        lseek(filefd,0,SEEK_END);
    }
    else 
    {
        ftruncate(filefd,0);
    }
    
    //发送开始信息
    ftp_reply(sess,FTP_DATACONN,"OK to send data");

    int flag = 0;
    //发送文件
    if ( 0 == sess->bASCII)
    {
        while (1)
        {
            ratelim_init(sess);
            data_disconn_start();
            char buf[1024] = {0};
            int ret = read(sess->datafd,buf,1024);
            if ( -1 == ret && EINTR == errno )
                continue;
            if ( -1 == ret )
            {
                flag = 1;
                break;
            }
            if ( 0 == ret )
            {
                flag = 0;
                break;
            }

            b_dataconn = 1;
            ratelim_do(sess,tunable_upload_max_rate,ret); 
    
            if ( ret != writen(filefd,buf,ret) )
            {
                flag = 2;
                break;
            }
        }
    }

    switch (flag)
    {
        case 0:
            ftp_reply(sess,FTP_TRANSFEROK,"Transfer complete.");
            break;
        case 1:
            ftp_reply(sess,FTP_BADSENDNET,"Receive file failure.");
            break;
        case 2:
            ftp_reply(sess,FTP_BADSENDFILE,"Write local file failure.");
            break;
    }

    is_urg(sess);
    file_unlock(filefd);
    close(filefd);
    clear_transfer(sess);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movq %rdi, %rbx
callq 0x559c
movl %eax, 0xc(%rbx)
cmpl $-0x1, %eax
je 0x5c6c
leaq 0x818(%rbx), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x3450
cmpl $-0x1, %eax
je 0x5e04
movl %eax, %ebp
movl %eax, %edi
callq 0x3cfc
cmpl $-0x1, %eax
je 0x5c8f
movq 0xc30(%rbx), %rsi
testq %rsi, %rsi
je 0x5cb9
movl %ebp, %edi
xorl %edx, %edx
jmp 0x5ccc
leaq 0x2944(%rip), %rdx      # 0x85b7
movq %rbx, %rdi
movl $0x1a9, %esi            # imm = 0x1A9
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4cf4
leaq 0x29aa(%rip), %rdx      # 0x8640
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
callq 0x4cf4
movl %ebp, %edi
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3260
cmpl $0x0, 0x14(%rbx)
je 0x5de2
movl %ebp, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x31f0
leaq 0x2985(%rip), %rdx      # 0x865d
movq %rbx, %rdi
movl $0x96, %esi
callq 0x4cf4
cmpl $0x0, 0x10(%rbx)
je 0x5d4c
leaq 0x2928(%rip), %rdx      # 0x861a
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x4cf4
cmpl $0x1, 0xc40(%rbx)
jne 0x5d26
movl $0x0, 0xc40(%rbx)
leaq 0x260c(%rip), %rdx      # 0x8325
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x4cf4
movl %ebp, %edi
callq 0x3d23
movl %ebp, %edi
callq 0x3260
movq %rbx, %rdi
callq 0x479a
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rsp, %r14
leaq 0x6596(%rip), %r12      # 0xc2ec
movq %rbx, %rdi
callq 0x4828
callq 0x5264
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
xorl %esi, %esi
callq 0x3220
movl 0xc(%rbx), %edi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rsi
callq 0x3280
movq %rax, %r15
cmpl $-0x1, %r15d
je 0x5dc4
testl %r15d, %r15d
je 0x5ceb
movl $0x1, 0x6582(%rip)      # 0xc320
movl (%r12), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x4863
movslq %r15d, %r15
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4466
cmpq %rax, %r15
je 0x5d56
jmp 0x5df0
callq 0x3060
cmpl $0x4, (%rax)
je 0x5d56
leaq 0x2898(%rip), %rdx      # 0x866d
movq %rbx, %rdi
movl $0x1aa, %esi            # imm = 0x1AA
jmp 0x5cfa
movl %ebp, %edi
xorl %esi, %esi
callq 0x31e0
jmp 0x5cd1
leaq 0x288c(%rip), %rdx      # 0x8683
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
jmp 0x5cfa
movq %rbx, %rdi
callq 0x381a