Linux下检测指定目录下的所有打开的文件(包括子目录)并按照原有的目录结构保存一个副本

#define _GNU_SOURCE

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <dirent.h>
#include "sys/stat.h"
#include "sys/types.h"
#include <fcntl.h>

int SrcDirStrLongth;

/*----------debug---------------*/
#define DEBUG 0
#if DEBUG
#define db_msg_out(x...) printf(x)
#else
#define db_msg_out(x...)
#endif
/*------------------------------*/

enum
{
 OPEN_FILE_ERROR,
 FILE_BEEN_OPENED,
 FILE_NOT_BEEN_OPENED
};

int IsFileOpen(char *FileName)
{
 int fd;

 fd = open(FileName, O_RDWR);    
 if (fd < 0)
 {        
  db_out("Fail to open file:[%s]",FileName);        
  return OPEN_FILE_ERROR;    
 }
  
 if(fcntl(fd,F_SETLEASE,F_WRLCK) < 0)
 {
  db_out("File has been opened");
  close(fd); 
  return  FILE_BEEN_OPENED;
 }  
   else
 {      
       db_out("File has not been opened");
  close(fd);
  return FILE_NOT_BEEN_OPENED;
 }  
}

void GetNewDirName(char *parent_dir,char *new_dir_name,struct dirent *p_dirent)
{
 strcpy(new_dir_name,parent_dir);
 strcat(new_dir_name,"/");
 strcat(new_dir_name,p_dirent->d_name);
}
void GetLstatErrorMsg(int ret)
{
 switch(ret)
 {
 case EFAULT:
  printf("Error type = EFAULT/n");  break;

 case ENOTDIR:
  printf("Error type = ENOTDIR/n");break;
 case ENOENT:
  printf("Error type = ENOENT/n");break;
 case EBADF:
  printf("Error type = EBADF/n");break;
 case ELOOP:
  printf("Error type = ELOOP/n");break;
 case ENOMEM:
  printf("Error type = ENOMEM/n");break;
 case ENAMETOOLONG:
  printf("Error type = ENAMETOOLONG/n");break;
 default:
  printf("Error type = UnknowType/n");
 }
}

void ParseDir(DIR *dir,char *parent_dir ,char *target_dir)
{
 int ret;
 DIR *p_dir;
 struct stat buf;
 char * time;
 struct dirent *p_dirent;
 char new_dir_name[100];
 char new_src_dir[100];
 char new_target_dir[100] = "/0";
 char creat_dir_cmd_str[100] = "/0";
 char cp_cmd_str[100] = "cp ";

 strcat(creat_dir_cmd_str,"mkdir -p ");
 //strcat(cp_cmd_str,"cp ");
  
 while(p_dirent = readdir(dir))/*p_dirent->d_name: file_name or dir_name*/
 {  
  GetNewDirName(parent_dir,new_dir_name,p_dirent);

  if((ret = lstat(new_dir_name,&buf)) < 0 )
  {
   GetLstatErrorMsg(ret);
  }
    
  if(S_ISDIR(buf.st_mode))
  {
   if(!strcmp(".",p_dirent->d_name)||!strcmp("..",p_dirent->d_name))
   { 
    continue;
   }
 
   p_dir = opendir(new_dir_name);
   ParseDir(p_dir,new_dir_name,target_dir);
  }
  else
  {
   if(!(ret = IsFileOpen(new_dir_name)))
   {
    db_out("Fail to open file/n");
   }
   
   if(FILE_BEEN_OPENED == ret)
   {
    strcat(new_target_dir,target_dir);
    strcat(new_target_dir,parent_dir + SrcDirStrLongth);
    strcat(creat_dir_cmd_str,new_target_dir);
    db_msg_out("creat_dir_cmd_str = %s/n",creat_dir_cmd_str);
   
    system(creat_dir_cmd_str);

    strcat(cp_cmd_str,new_dir_name);
    strcat(cp_cmd_str," ");
    strcat(cp_cmd_str,new_target_dir);

    db_msg_out("cp_cmd_str = %s/n",cp_cmd_str);
    system(cp_cmd_str);

    strcpy(new_target_dir,"/0");
    strcpy(creat_dir_cmd_str,"mkdir -p ");
   }

  }
 }   
 
}

int main(int argc,char **argv)
{
 DIR *p_dir;
 struct stat buf;
 SrcDirStrLongth = strlen(argv[1]);
  
 if((p_dir = opendir(argv[1])) == NULL)/*Get Dir Description to operate*/
 {
  printf("Fail to open dir:[%s]/n",argv[2]);
  return 0;
 }
 ParseDir(p_dir,argv[1],argv[2]);
 
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值