⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp_file.c

📁 周立功公司2410开发板上所有基于UCOS开发的源代码
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzou ZLG-MCU Development Co.,LTD.
**                                      graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:           ftp_file.c
** Last modified Date:  2005-06-03
** Last Version:        1.0
** Descriptions:        interface functions between ZLG/FPT-S and ZLG/FS.
**
**------------------------------------------------------------------------------------------------------
** Created by:          Chenmingji
** Created date:        2005-06-03
** Version:             1.0
** Descriptions:        The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#define IN_FTP_FILE

#include "config.h"

/*********************************************************************************************************
**                  "全局和静态变量在这里定义"         
**        global variables and static variables define here
********************************************************************************************************/
const char ListStr[] = "rw-rw-rw-   1 user     group";
const char * const Month[12] = {"Jan ", "Feb ", "Mar ", "Apr ", "may ", "Jun ", 
                                "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "};


/*********************************************************************************************************
**                  "函数定义"         
**              functions here
********************************************************************************************************/


/*********************************************************************************************************
** Function name: FtpDele
** Descriptions: delete file
** Input:Fip:FPT info
** 
** Output TRUE:  OK
**        FALSE: NOK
** Created by:   chenmingji
** Created Date: 2005-06-03
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        uint8 FtpDele(FTP_INFO *Fip)
{
    char *cp;
    char temp;
    int temp1;
    
    cp = (char *)(Fip->Buf + 5);
    temp = 1;

    if (*cp == '/')
    {
        cp++;
    }    
    while ( temp != 0)
    {
        temp = *cp;
        if (temp == '/')
        {
            *cp = '\\';
        }
        cp++;
    }

    temp1 = OSRemoveFile((char *)(Fip->Buf + 5));

    cp = (char *)(Fip->Buf + 5);
    temp = 1;
    
    while ( temp != 0)
    {
        temp = *cp;
        if (temp == '\\')
        {
            *cp = '/';
        }
        cp++;
    }

    if ( temp1 == RETURN_OK)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

/*********************************************************************************************************
** Function name: FtpMkd
** Descriptions: make dir
** Input:Fip:FPT info
** 
** Output TRUE:  OK
**        FALSE: NOK
** Created by:   chenmingji
** Created Date: 2005-06-03
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        uint8 FtpMkd(FTP_INFO *Fip)
{
    char *cp;
    char temp;
    int temp1;
    
    cp = (char *)(Fip->Buf + 6);
    temp = 1;
    
    if (*cp == '/')
    {
        cp++;
    }    
    while ( temp != 0)
    {
        temp = *cp;
        if (temp == '/')
        {
            *cp = '\\';
        }
        cp++;
    }

    temp1 = OSMakeDir((char *)(Fip->Buf + 6)); 

    cp = (char *)(Fip->Buf + 5);
    temp = 1;

    while ( temp != 0)
    {
        temp = *cp;
        if (temp == '\\')
        {
            *cp = '/';
        }
        cp++;
    }
    
    if ( temp1 == RETURN_OK)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

/*********************************************************************************************************
** Function name: FtpRmd
** Descriptions: remove dir
** Input:Fip:FPT info
** 
** Output TRUE:  OK
**        FALSE: NOK
** Created by:   chenmingji
** Created Date: 2005-06-03
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        uint8 FtpRmd(FTP_INFO *Fip)
{
    char *cp;
    char temp;
    int temp1;
    
    cp = (char *)(Fip->Buf + 6);
    temp = 1;
    
    if (*cp == '/')
    {
        cp++;
    }    
    while ( temp != 0)
    {
        temp = *cp;
        if (temp == '/')
        {
            *cp = '\\';
        }
        cp++;
    }

    temp1 = OSRemoveDir((char *)(Fip->Buf + 6));

    cp = (char *)(Fip->Buf + 5);
    temp = 1;
    
    while ( temp != 0)
    {
        temp = *cp;
        if (temp == '\\')
        {
            *cp = '/';
        }
        cp++;
    }

    if ( temp1== RETURN_OK)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

/*********************************************************************************************************
** Function name: FtpRetr
** Descriptions: send file to user
** Input:Fip:FPT info
** 
** Output TRUE:  OK
**        FALSE: NOK
** Created by:   chenmingji
** Created Date: 2005-06-03
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

        uint8 FtpRetr(FTP_INFO *Fip, uint16 LinkHandle)
{
    HANDLE Handle;
    uint32 Length;
    uint8 *Buf, *cp;
    uint8 FileName[FTP_PATH_LEGTH];
    unsigned int i;
    uint8 temp;

    Buf = Fip->Buf;
    cp = FileName;
    if (*Buf == '/')
    {
        Buf++;
    }
    for (i = 0; i < FTP_PATH_LEGTH; i++)
    {
        temp = *Buf++;
        if (temp == '/')
        {
            temp = '\\';
        }
        *cp++ = temp;
    }

    Buf = Fip->Buf;
    
    Handle = OSFileOpen((char *)FileName, "R");
    if (Handle == Not_Open_FILE)
    {
        SendStr(Fip->LinkHandle, (char *)FileNOpen);
        return FALSE;
    }
    OSFileSeek(Handle, Fip->temp1, SEEK_SET);
    while (OSFileEof(Handle) == 0)
    {
        if (OSTaskDelReq(OS_PRIO_SELF) == OS_TASK_DEL_REQ)
        {
            OSFileClose(Handle);
            return TRUE;
        }
        Length = OSFileRead(Buf, FTP_BUF_LENGTH, Handle);
        send(LinkHandle, Buf, Length, 0);
        if (TCP_Status(LinkHandle) != TCP_STATE_ESTABLISHED)
        {
            break;
        }
    }
    OSFileClose(Handle);
    SendStr(Fip->LinkHandle, (char *)TransferOk);
    return TRUE;
}

/*********************************************************************************************************
** Function name: FtpStor
** Descriptions: save file from user
** Input:Fip:FPT info
** 
** Output TRUE:  OK
**        FALSE: NOK
** Created by:   chenmingji
** Created Date: 2005-06-03
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

        uint8 FtpStor(FTP_INFO *Fip, uint16 LinkHandle)
{
    HANDLE Handle;
    uint32 Length;
    uint8 *Buf, *cp;
    uint8 FileName[FTP_PATH_LEGTH];
    unsigned int i;
    uint8 temp;

    Buf = Fip->Buf;
    cp = FileName;
    if (*Buf == '/')
    {
        Buf++;
    }
    for (i = 0; i < FTP_PATH_LEGTH; i++)
    {
        temp = *Buf++;
        if (temp == '/')
        {
            temp = '\\';
        }
        *cp++ = temp;
    }

    Buf = Fip->Buf;
    if (Fip->temp1 == 0)
    {
        OSRemoveFile((char *)FileName);
    }
    Handle = OSFileOpen((char *)FileName, "RW");
    if (Handle == Not_Open_FILE)
    {
        SendStr(Fip->LinkHandle, (char *)FileNOpen);
        return FALSE;
    }
    OSFileSeek(Handle, Fip->temp1, SEEK_SET);
    while (1)
    {
        Length = recv(LinkHandle, Buf, FTP_BUF_LENGTH, 0);

        if(Length == 0 || Length == SOCKET_RCV_ERROR)
        {
            if (OSTaskDelReq(OS_PRIO_SELF) == OS_TASK_DEL_REQ)
            {
                OSFileClose(Handle);
                OSAllCacheWriteBack();
                return TRUE;
            }

            if (TCP_Status(LinkHandle) != TCP_STATE_ESTABLISHED)
            {
            #if 0
                Length = recv(LinkHandle, Buf, FTP_BUF_LENGTH, 0);
                OSFileWrite(Buf, Length, Handle);
            #endif
                break;
            }
        }
        else
        {
            OSFileWrite(Buf, Length, Handle);
        }
    }
    OSFileClose(Handle);
    OSAllCacheWriteBack();
    SendStr(Fip->LinkHandle, (char *)TransferOk);
    return TRUE;
}
/*********************************************************************************************************
** Function name: FtpList
** Descriptions: list dir
** Input:Fip:FPT info
** 
** Output TRUE:  OK
**        FALSE: NOK
** Created by:   chenmingji
** Created Date: 2005-06-03
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        uint8 FtpList(FTP_INFO *Fip, uint16 LinkHandle)
{
    char *Path;
    uint8 *Buf;
    uint32 i, j, temp;
    uint32 PathClus;
    FDT FdtInfo;
    uint8 Drive, k;
    char FileName[FTP_PATH_LEGTH];

    Path = Fip->Path + 1;
    Buf = (uint8 *)FileName;
    for (i = 0; i < FTP_PATH_LEGTH; i++)
    {
        temp = *Path++;
        if (temp == '/')
        {
            temp = '\\';
        }
        *Buf++ = temp;
    }
    
    Buf = Fip->Buf;
    PathClus = OSGetDirClusIndex(FileName);
    if (PathClus == BAD_CLUS)
    {
        return FALSE;
    }
    Drive = OSGetDrive(FileName);
    
    i = 0;
    while (OSGetFDTInfo(&FdtInfo, Drive, PathClus, i) == RETURN_OK)
    {
        if (TCP_Status(LinkHandle) != TCP_STATE_ESTABLISHED)
        {
            return TRUE;
        }

        if (OSTaskDelReq(OS_PRIO_SELF) == OS_TASK_DEL_REQ)
        {
            return TRUE;
        }
        i++;
        if (FdtInfo.Name[0] == DEL_FDT)
        {
            continue;
        }
        if (FdtInfo.Name[0] == EMPTY_FDT)
        {
            break;
        }
        if ((FdtInfo.Attr & ATTR_VOLUME_ID) == 0)
        {
            
            /* 属性  */
            if ((FdtInfo.Attr & ATTR_DIRECTORY) == 0)
            {
                *Buf++ = '-';
            }
            else
            {
                *Buf++ = 'd';
            }
            memcpy(Buf, ListStr, 28);
            Buf += 28;
            
            /* 文件大小 */
            sprintf((void *)Buf, "%12u ", FdtInfo.FileSize);
            Buf += 13;
            
            /* 日期时间 */
            k = (FdtInfo.WrtDate >> 5) & 0x0f;
            if (k == 0)
            {
                k = 1;
            }
            memcpy(Buf, Month[k - 1] , 4);              /* 月 */
            Buf += 4;
            k = (FdtInfo.WrtDate & 0x1f);
            if (k == 0)
            {
                k = 1;
            }
            sprintf((void *)Buf, "%2u ", k);            /* 日 */
            Buf += 3;

/* 获取当前日期,如果在过去一年内,发送时间 否则如下*/
            temp = (FdtInfo.WrtDate >> 9) + 1980;
            sprintf((void *)Buf, "%5u ", temp);                             /*  年 */
            Buf += 6;

            if (FdtInfo.Name[0] == ESC_FDT)
            {
                *Buf++ = DEL_FDT;
            }
            else
            {
                *Buf++ = FdtInfo.Name[0];
            }
            for (k = 1; k < 8; k++)
            {
                if (FdtInfo.Name[k] != ' ')
                {
                    *Buf++ = FdtInfo.Name[k];
                    j++;
                }
                else
                {
                    break;
                }
            }
            if (FdtInfo.Name[8] != ' ')
            {
                *Buf++ = '.';
            }
            for (k = 8; k < 11; k++)
            {
                if (FdtInfo.Name[k] != ' ')
                {
                    *Buf++ = FdtInfo.Name[k];
                    j++;
                }
                else
                {
                    break;
                }
            }
            *Buf++ = '\r';
            *Buf++ = '\n';
            j = Buf - Fip->Buf;
            if (j >= FTP_BUF_LENGTH - 57 - 11)
            {
                Buf = Fip->Buf;
                send(LinkHandle, Buf, j, 0);
                j = 0;
            }
        }
    }
    if (j > 0)
    {
        send(LinkHandle, Fip->Buf, j, 0);
    }
    SendStr(Fip->LinkHandle, (char *)TransferOk);
    return TRUE;   
}

/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -