嵌入式 获取文件真正的大小示例,经典短小精悍,以及文件上锁

文件锁:

/************************************************\
* author : kj *
* function : store the param of the ipnc *
* time : 2013.10.11 *
\***********************************************/
#include "avserver.h"


/*
 * funtion:set the file lock
 * params:fd is file Num,type is F_WRLCk,F_RDLCK or F_UNLCK
 -----------------------
 * time:2012-08-22 22:17
 * version:1.0
 */
int lock_set(int fd, int type)
{
    struct flock  lock_file;
    lock_file.l_whence = SEEK_SET;
    lock_file.l_start = 0;
    lock_file.l_len = 0;
    lock_file.l_type = type;
    lock_file.l_pid = -1;
    fcntl(fd, F_GETLK, &lock_file);

    if (lock_file.l_type != F_UNLCK)
    {
 if (lock_file.l_type == F_RDLCK)
 {
     //printf("Read lock already set by %d\n", lock_file.l_pid);
 }
 else if (lock_file.l_type == F_WRLCK)
 {
     //printf("Write lock already set by %d\n", lock_file.l_pid);
 }   
    }
    lock_file.l_type = type;
    if ((fcntl(fd, F_SETLKW, &lock_file)) < 0)
    {
 //printf("Lock failed:type = %d\n", lock_file.l_type);
 return 1;
    }
    switch(lock_file.l_type)
    {
 case F_RDLCK:
     {
  //printf("Read lock set by %d\n", getpid());
     }
     break;
 case F_WRLCK:
     {
  //printf("Write lock set by %d\n", getpid());
     }
     break;
 case F_UNLCK:
     {
  //printf("Release lock by %d\n", getpid());
  return 1;
     }
     break;
 default:
     break;
    }/* end of switch  */
    return 0;
}

/*
 * Params:user is the user information
 * function:write the user information to a temp file
 * return: 0 is success,-1 is fail  
 -----------------------  
 * time:2013-08-22 22:17   
 * version:1.0
 */
int write_user_file(char *data,int length,int label)
{
    int fd;
    int tmp;
    FILE *fp;

   if(label)
   {
       fp = fopen(IPNC_CONF, "a+");
   }
   else
   {
       fp = fopen(IPNC_CONF, "w");
   }
  
#if 1

    if(fp == NULL) {
 printf("%s %d can't create  file  : %s\n",__FUNCTION__,__LINE__,IPNC_CONF);
 return -1;
    }
    fd=fileno(fp);
    //printf("%s %d write fd is %d\n",__FUNCTION__,__LINE__,fd);
    lock_set(fd, F_WRLCK);//set the write lock
    tmp = fwrite(data, 1, length,fp);
    if( tmp == 0)
    {
 return -1;
    }
    lock_set(fd, F_UNLCK);// unlock the file
    fclose(fp);

#else
    fd = open(IPNC_CONF, O_RDWR | O_CREAT, 0666);
    if(fd < 0)
    {
 printf("Open file error\n");
 return -1;
    }
    lock_set(fd, F_WRLCK);//set the write lock
    if(write(fd,data,length) == 0)//write to file
    {
 return -1; 
    }
    lock_set(fd, F_UNLCK);// unlock the file
    close(fd);
#endif

    return tmp;
}

/*
 * Params:user is the user information
 * function:read the user information from a temp file and save to the UserInfo struct
 * return: UserInfo struct pointer is success,NULL is fail  
 -----------------------  
 * time:2012-08-22 22:17   
 * version:1.0
 */
int read_user_file(char *param_name)
{
    int fd;
    int param_value;
    char buf_name[64];
    char buf[JOSEPH_CHAR_MAX_ROWLENGTH*30];
    FILE *fp;
    strcpy(buf_name,param_name);
    fp = fopen(IPNC_CONF, "r");
    if(fp == NULL) {
 //printf("%s %d can't open  file  : %s\n",__FUNCTION__,__LINE__,IPNC_CONF);
 return -1;
    }
    fd=fileno(fp);
    //printf("%s %d read fd is %d\n",__FUNCTION__,__LINE__,fd);
   lock_set(fd, F_RDLCK);//set the write lock
    while(fgets(buf, sizeof(buf), fp)){

  if(strstr(buf,buf_name)){
   //printf("%s %d %s is %s",__FUNCTION__,__LINE__,buf_name,(buf+strlen(buf_name)+1));
   if(strcmp(buf_name,"joseph_ipnc_nickname") == 0)
   {
    memset(joseph_ipnc_param.joseph_ipnc_network_attr.joseph_ipnc_nickname,0,JOSEPH_IPNC_NICKNAME_MAX);
    sprintf(joseph_ipnc_param.joseph_ipnc_network_attr.joseph_ipnc_nickname,"%s",(buf+strlen(buf_name)+1));
    return 1;
   }
   if(strcmp(buf_name,"joseph_ipnc_jds_ip") == 0)
   {
    memset(joseph_ipnc_param.joseph_ipnc_server_attr.joseph_ipnc_das_attr.joseph_ipnc_jds_ip,0,JOSEPH_IPNC_SERVER_IP_MAX);
    sprintf(joseph_ipnc_param.joseph_ipnc_server_attr.joseph_ipnc_das_attr.joseph_ipnc_jds_ip,"%s",(buf+strlen(buf_name)+1));
    return 1;
   }
   if(strcmp(buf_name,"joseph_ipnc_jds_port") == 0)
   {
    memset(joseph_ipnc_param.joseph_ipnc_server_attr.joseph_ipnc_das_attr.joseph_ipnc_jds_port,0,JOSEPH_IPNC_SERVER_PORT_MAX);
    sprintf(joseph_ipnc_param.joseph_ipnc_server_attr.joseph_ipnc_das_attr.joseph_ipnc_jds_port,"%s",(buf+strlen(buf_name)+1));
    return 1;
   }


   param_value = atoi(buf+strlen(buf_name)+1);
       
  }
    }

    lock_set(fd, F_UNLCK);// unlock the file
    fclose(fp);

    return param_value;
}


void alter_ipnc_param(char *joseph_param_name, char *joseph_param_values)
{
    char buf[1024];
    char conf_path[64] = IPNC_CONF;

    if((joseph_param_name == NULL) ||( joseph_param_values == NULL))
    {
 printf("The NULL point !!!\n");
 return;
    }

    if((strlen(joseph_param_name) == 0) ||( strlen(joseph_param_values) == 0))
    {
 printf("The NULL content !!!\n");
 return;
    }

    //printf("%s %d conf_path is :%s\n",__FUNCTION__,__LINE__,conf_path);
   
    //sprintf(buf,"sed -i '/%s/ s/%s/%s/g' %s",argv[1],argv[2],argv[3],conf_path);
    sprintf(buf,"sed -i 's/%s=.*/%s=%s/g' %s",joseph_param_name,joseph_param_name,joseph_param_values,conf_path);
#if 0
    printf("%s %d The content of sed %s",__FUNCTION__,__LINE__,buf);
    printf("%s %d joseph_param_values = %s",__FUNCTION__,__LINE__,joseph_param_values);
#endif
    system(buf);
    //printf("%s %d \n",__FUNCTION__,__LINE__);
    return;
}


 

获取文件真实大小:

int read_filesize(char *fileName)
{
 int fileSize;
 FILE *fPtr;
 fPtr = fopen(fileName, "rb");
 if(fPtr == NULL) return -2;
 fileSize = 0;
 fseek(fPtr,0,SEEK_END);
 fileSize = ftell(fPtr);
 fclose (fPtr);
 return fileSize;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值