// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rdi, %rbx
callq 0x55ed
movl %eax, 0xc(%rbx)
cmpl $-0x1, %eax
je 0x5ce2
leaq 0x818(%rbx), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x3440
cmpl $-0x1, %eax
je 0x5ebf
movl %eax, %ebp
movl %eax, %edi
callq 0x3cec
cmpl $-0x1, %eax
je 0x5d07
movq 0xc30(%rbx), %rsi
testq %rsi, %rsi
je 0x5d33
movl %ebp, %edi
xorl %edx, %edx
jmp 0x5d46
leaq 0x28d2(%rip), %rdx      # 0x85bb
movq %rbx, %rdi
movl $0x1a9, %esi            # imm = 0x1A9
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4d4c
leaq 0x2936(%rip), %rdx      # 0x8644
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
callq 0x4d4c
movl %ebp, %edi
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3260
cmpl $0x0, 0x14(%rbx)
je 0x5eb1
movl %ebp, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x31f0
leaq 0x290f(%rip), %rdx      # 0x8661
movq %rbx, %rdi
movl $0x96, %esi
callq 0x4d4c
xorl %r15d, %r15d
cmpl $0x0, 0x10(%rbx)
jne 0x5e20
xorl %r13d, %r13d
movq %rsp, %r14
movl $0x2, %r12d
movq %rbx, %rdi
callq 0x4875
callq 0x52b0
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
testl %r15d, %r15d
je 0x5dcd
cmpl $-0x1, %r15d
jne 0x5dd4
callq 0x3060
xorl %ecx, %ecx
cmpl $0x4, (%rax)
sete %cl
movl $0x1, %eax
cmovnel %eax, %r13d
xorl $0x5, %ecx
jmp 0x5e11
movl $0x5, %ecx
jmp 0x5e14
movl $0x1, 0x6532(%rip)      # 0xc310
leaq 0x64ff(%rip), %rax      # 0xc2e4
movl (%rax), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x48b9
movslq %r15d, %r15
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4465
xorl %ecx, %ecx
cmpq %rax, %r15
setne %cl
cmovnel %r12d, %r13d
leal (%rcx,%rcx,4), %ecx
movl %r13d, %r15d
movl %r15d, %r13d
cmpl $0x5, %ecx
jne 0x5d78
cmpl $0x2, %r15d
je 0x5e3d
cmpl $0x1, %r15d
jne 0x5e4e
leaq 0x283e(%rip), %rdx      # 0x8671
movq %rbx, %rdi
movl $0x1aa, %esi            # imm = 0x1AA
jmp 0x5e5d
leaq 0x2843(%rip), %rdx      # 0x8687
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
jmp 0x5e5d
leaq 0x27c9(%rip), %rdx      # 0x861e
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x4d4c
cmpl $0x1, 0xc40(%rbx)
jne 0x5e89
movl $0x0, 0xc40(%rbx)
leaq 0x24a9(%rip), %rdx      # 0x8325
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x4d4c
movl %ebp, %edi
callq 0x3d13
movl %ebp, %edi
callq 0x3260
movq %rbx, %rdi
callq 0x47e7
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %edi
xorl %esi, %esi
callq 0x31e0
jmp 0x5d4b
movq %rbx, %rdi
callq 0x380a