rule.cpp

来自「包含客户和服务器的升级程序,在linux下可以运行的.」· C++ 代码 · 共 169 行

CPP
169
字号
/* * Copyright (C) 2006, Binary Ma * Licence: GNU GPL 1991 - version 2 * Bug report: binary@eniak.org*/#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include "rule.h"#include "parse.h"#include "calmd5.h"const int INFO_COMPART = 28;static char* trim_end_slash( char* str ){    if( NULL == str )        return NULL;    int len = strlen( str );        while( len-- )    {        if( '/' != str[len] )        {            str[ len + 1 ] = 0;            break;        }    }        return str;}int extmap( char* info, struct filemap* fmap ){    if( NULL == info || NULL == fmap )        return -__LINE__;	    char* point = NULL;    int count = 0;    while( NULL != ( point = strchr( info, INFO_COMPART ) ) )    {        *point = 0;        switch( count++ )        {        case 0:            fmap->st.st_size = atoll( info );            break;        case 1:            fmap->st.st_mode = atoi( info );            break;        case 2:            fmap->st.st_uid = atoi( info );            break;        case 3:            fmap->st.st_gid = atoi( info );            break;        case 4:            fmap->st.st_rdev = atoi( info );            break;        case 5:            strncpy( fmap->device, info, sizeof( fmap->device ) - 1 );            break;        case 6:            fmap->act = atoi( info );            break;        case 7:            strncpy( fmap->md5, info, sizeof( fmap->md5 ) - 1 );            break;        // reserve extend        default:            return 0;        }        info = point + 1;    }    return 0;}const filemap* fillmap( const char* file ){    if( NULL == file )        return NULL;    const char* SUFFIX = "rule";    char rulefile[strlen( file ) + strlen( SUFFIX ) + 8 ];    sprintf( rulefile, "%s.%s", file, SUFFIX );    if( -1 == access( rulefile, F_OK ) )        return NULL;        static struct filemap fmap;    bzero( &fmap, sizeof( fmap ) );    lstat( file, &fmap.st );    const char* conf = parse( rulefile, "name" );    if( NULL != conf )        strncpy( fmap.name, conf, sizeof( fmap.name ) - 1 );    if( 0 != *fmap.name && '/' == fmap.name[strlen( fmap.name ) - 1] )    {        fmap.st.st_mode -= S_IFMT & fmap.st.st_mode;        fmap.st.st_mode += S_IFDIR;        trim_end_slash( fmap.name );    }    conf = parse( rulefile, "dev" );    if( NULL != conf )        strncpy( fmap.device, conf, sizeof( fmap.device ) - 1 );    fmap.act = ACT_CREATE;    conf = parse( rulefile, "act" );    if( NULL != conf )    {        if( 0 == strcasecmp( conf, "remove" ) )        {            fmap.act = ACT_REMOVE;        }        else        if( 0 == strcasecmp( conf, "update" ) )        {            fmap.act = ACT_UPDATE;        }        else        if( 0 == strcasecmp( conf, "unique" ) )        {            fmap.act = ACT_UNIQUE;        }        else        if( 0 == strcasecmp( conf, "create" ) )        {            fmap.act = ACT_CREATE;        }        else        {            // unknow action, maybe write error            return NULL;        }    }    conf = parse( rulefile, "md5" );    if( NULL == conf )    {        if( S_ISDIR( fmap.st.st_mode ) )        {            conf = "0";        }        else        {            conf = calculate_md5( file );        }    }        if( NULL != conf )        strncpy( fmap.md5, conf, sizeof( fmap.md5 ) - 1 );    return &fmap;}

⌨️ 快捷键说明

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