// File: /workspace/github_c_makefile_star2_O2/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 0x514d
movl %eax, 0xc(%rbx)
cmpl $-0x1, %eax
je 0x57c9
leaq 0x818(%rbx), %rdi
pushq $0x41
popq %rsi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x3450
cmpl $-0x1, %eax
je 0x5940
movl %eax, %ebp
movl %eax, %edi
callq 0x3947
cmpl $-0x1, %eax
je 0x57ec
movq 0xc30(%rbx), %rsi
testq %rsi, %rsi
je 0x5816
movl %ebp, %edi
xorl %edx, %edx
jmp 0x5827
leaq 0x1de7(%rip), %rdx      # 0x75b7
movq %rbx, %rdi
movl $0x1a9, %esi            # imm = 0x1A9
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x489c
leaq 0x1e4d(%rip), %rdx      # 0x7640
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
callq 0x489c
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 0x591e
pushq $0x2
popq %rdx
movl %ebp, %edi
xorl %esi, %esi
callq 0x31f0
leaq 0x1e2a(%rip), %rdx      # 0x765d
movq %rbx, %rdi
movl $0x96, %esi
callq 0x489c
cmpl $0x0, 0x10(%rbx)
je 0x5888
leaq 0x1dcd(%rip), %rdx      # 0x761a
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x489c
movq %rbx, %rdi
callq 0x4f9c
movl %ebp, %edi
callq 0x396c
movl %ebp, %edi
callq 0x3260
movq %rbx, %rdi
callq 0x434f
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rsp, %r14
leaq 0x5a5a(%rip), %r12      # 0xb2ec
movq %rbx, %rdi
callq 0x43c7
callq 0x4e1e
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 0x5900
testl %r15d, %r15d
je 0x5846
movl $0x1, 0x5a46(%rip)      # 0xb320
movl (%r12), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x4400
movslq %r15d, %r15
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x401f
cmpq %rax, %r15
je 0x5892
jmp 0x592c
callq 0x3060
cmpl $0x4, (%rax)
je 0x5892
leaq 0x1d5c(%rip), %rdx      # 0x766d
movq %rbx, %rdi
movl $0x1aa, %esi            # imm = 0x1AA
jmp 0x5855
movl %ebp, %edi
xorl %esi, %esi
callq 0x31e0
jmp 0x582c
leaq 0x1d50(%rip), %rdx      # 0x7683
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
jmp 0x5855
leaq 0x1cf9(%rip), %rdx      # 0x7640
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
callq 0x489c
leaq 0x1cfd(%rip), %rdi      # 0x7658
callq 0x3470
pushq $0x1
popq %rdi
callq 0x34e0