C语言文件操作


#ifndef _FILE_OPR_H
#define _FILE_OPR_H

#include 

#define CONFIGUER_FILE_PATH    "/tmp"
#define MCU_UPGRADE_FILE_PATH	         "/tmp/upgrade"
#define YyFree(addr) do { \
                        if(addr) { \
                            free(addr); \
                            addr = NULL; \
                        } \
                     }while(0)

typedef struct
{
    int (*write)(const char const *fileName, char *data, int len, bool isRelativePath);
    int (*append)(const char const *fileName, char *data, int len, bool isRelativePath);
    int (*read)(const char const *fileName, char *out, int len, bool isRelativePath);
    int (*readFromOffset)(const char const *fileName, char *out, int offset, int len, bool isRelativePath);
    int (*size)(const char const *fileName, bool isRelativePath);
    bool (*exist)(const char const *fileName, bool isRelativePath);
    void (*decrypt)(char *filePath, char *rc4, bool isRelativePath);
} FileOprS;

extern void createFileOps();
extern FileOprS *getFileOps();
extern void destoryFileOps();

#endif /* _FILE_OPR_H */

```cpp
#include 
#include 
#include 
#include "fileOpr.h"
#include 


static FileOprS *pFileOps = NULL;
static bool isInitComplete = false;


static int fileDeInit(void)
{

    //todo
    if(!isInitComplete)
    {
        return 0;
    }

//    isInitComplete = false;
//    return 0;
}


static FILE* fileOpen(const char const *fileName, const char *flag, bool isRelativePath)
{
    FILE *fp = NULL;
    char path[64] = {0};

    if(!fileName)
    {
        printf("Bad parameter...\n");
        return NULL;
    }
    if(isRelativePath)
    {
        sprintf(path, "%s/%s", CONFIGUER_FILE_PATH, fileName);
    }
    else
    {
        sprintf(path, "%s", fileName);
    }

    fp = fopen(path, flag);

    return fp;
}

static bool fileClose(FILE *fp)
{

    if(fp)
    {
		fflush(fp);
		fsync(fileno(fp));
		fclose(fp);
		fp = NULL;
    }

    return true;
}

static int fileWrite(const char const *fileName, char *data, int len, bool isRelativePath)
{
    int ret = 0;
    FILE *fp = fileOpen(fileName, "wb+", isRelativePath);

    if(!data || len == 0)
    {
        printf("Bad parameter...\n");
        return -1;
    }

    if(!fp)
    {
        printf("Open file %s failed...\n", fileName);
        return -1;
    }

    ret = fwrite(data, sizeof(char), len, fp);

    fileClose(fp);

    return ret;
}


static int fileWriteAppend(const char const *fileName, char *data, int len, bool isRelativePath)
{
    int ret = 0;
    FILE *fp = fileOpen(fileName, "ab+", isRelativePath);

    if(!data || len == 0)
    {
        printf("Bad parameter...\n");
        return -1;
    }

    if(!fp)
    {
        printf("Open file %s failed...\n", fileName);
        return -1;
    }

    ret = fwrite(data, sizeof(char), len, fp);

    fileClose(fp);

    return ret;

}


static int fileSize(const char const *fileName, bool isRelativePath)
{
    int len = 0;

    FILE *fp = fileOpen(fileName, "r", isRelativePath);

    if(!fp)
    {
        printf("Open file %s failed...\n", fileName);
        return 0;
    }

    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    fileClose(fp);
    return len;
}

static int fileRead(const char const *fileName, char *out, int len, bool isRelativePath)
{
    int ret = 0;
    FILE *fp = fileOpen(fileName, "r", isRelativePath);

    if(!out || len == 0)
    {
        printf("Bad parameter...\n");
        return -1;
    }

    if(!fp)
    {
        printf("Open file %s failed...\n", fileName);
        return -1;
    }

    ret = fread(out, sizeof(char), len, fp);

    fileClose(fp);

    return ret;
}

static int fileReadFromOffset(const char const *fileName, char *out, int offset, int len, bool isRelativePath)
{
    int ret = 0;
    int size = fileSize(fileName, isRelativePath);
    FILE *fp = fileOpen(fileName, "r", isRelativePath);

    if(!out || len == 0 || offset > size)
    {
        printf("[%s, %d]Bad parameter...\n", __func__, __LINE__);
        return -1;
    }

    if(!fp)
    {
        printf("Open file %s failed...\n", fileName);
        return -1;
    }
    
    fseek(fp, offset, SEEK_SET); 
    ret = fread(out, sizeof(char), len, fp);

    fileClose(fp);

    return ret;
}

static bool fileExist(const char const *fileName, bool isRelativePath)
{

    FILE *fp = fileOpen(fileName, "r", isRelativePath);

    if (fp)
    {
        fclose(fp);
        return true;
    }
    return false;
}

static void fileDecrypt(char *filePath, char *rc4, bool isRelativePath)
{
    char filePathtemp[256];
    char filePathlast[256];
    char cmd[256];
    char fileName[256];
    int ret = -1;
    int retryNum = 0;

    if(!filePath)
    {
        printf("file path is NULL");
        return;
    }

    printf("Going to Decrypt file ");

    char *ptr = strrchr(filePath, '/');
    sprintf(fileName, "%s", ptr+1);

    /* temp output path*/
    memset(filePathtemp, 0x00, sizeof(filePathtemp));
    memset(filePathlast, 0x00, sizeof(filePathlast));
    sprintf(filePathtemp, "%s/%s", MCU_UPGRADE_FILE_PATH, "temp.bin");
    sprintf(filePathlast, "%s/%s", MCU_UPGRADE_FILE_PATH, fileName);

    printf("filePathlast = %s",filePathlast);

    //decrytp_file
    memset(cmd, 0, sizeof(cmd));
    sprintf(cmd, "openssl enc -rc4 -md md5 -d -in %s -out %s -pass pass:%s -p", filePath, filePathtemp, rc4);
    ret = system(cmd);

    printf("Decrypt file is over , ret = %d ",ret);

    memset(cmd, 0, sizeof(cmd));
    sprintf(cmd, "/bin/rm %s ",filePath);
    system(cmd);

    while(retryNum++ < 5)
    {
        if(access(filePath, F_OK) == 0)
        {
            break;
        }
        else
        {
            memset(cmd, 0, sizeof(cmd));
            sprintf(cmd, "/bin/mv %s %s ", filePathtemp, filePath);
//            LOGD("%s\n", cmd);
            system(cmd);
            usleep(500);
        }
    }

    printf("file rename over");

}

void createFileOps()
{

    if(!pFileOps)
    {
        pFileOps = (FileOprS *)malloc(sizeof(FileOprS));
    }
    if(!pFileOps)
    {
        printf("[%s, %d]not enough memory to alloc\n", __func__, __LINE__);
        return;
    }
    //fileInit();

    pFileOps->write = fileWrite;
    pFileOps->read = fileRead;
    pFileOps->readFromOffset = fileReadFromOffset;
    pFileOps->size = fileSize;
    pFileOps->exist = fileExist;
    pFileOps->append = fileWriteAppend;
    pFileOps->decrypt = fileDecrypt;
}

void destoryFileOps()
{
    fileDeInit();
    YyFree(pFileOps);
}

FileOprS *getFileOps()
{
    return pFileOps;
}


你可能感兴趣的:(c语言)