遍历链接文件

   自己手痒不小心把/etc/apache2 删掉了,结果用apt-get install 安装apache2,subversion,libapache2-svn的时候,报一堆错,还好之前apache2备份,于是把apache2放到/etc/下,但是安装时,还是会报错,原因是apache2/mods-enabled/下的文件本来应该是apache2/mods-available/下的链接,但这个备份的apache2是通过winScp从ubuntu上拖到windows桌面上的,拖过去后,链接属性被破坏了,apache2/mods-enabled/下的文件全成了普通文件,把apache2/mods-enabled/下的现有文件做成apache2/mods-available/下的软链接,手动工作量有点大,写个程序来实现。

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

#include <iostream>
using namespace std;
void listDir(const char *path)
{
        DIR              *pDir ;
        struct dirent    *ent  ;
        int               i=0  ;
        char              childpath[512];
 
        pDir=opendir(path);
        memset(childpath,0,sizeof(childpath));
 
 
        while((ent=readdir(pDir))!=NULL)
        {
 
                if(ent->d_type & DT_DIR)
                {
 
                        if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
                                continue;
 
                        sprintf(childpath,"%s/%s",path,ent->d_name);
                        printf("path:%s/n",childpath);
 
                        listDir(childpath);
 
                }
                                else
                                {
                                                struct stat buf;
                                                //这里需包含绝对路径,才能识别出文件的属性,否则,只判断出该文件上层目录的属性
                                            string fileHead =  path;
                                            string fileTail =  ent->d_name;
                                            string filePath = fileHead + fileTail;
                                            //cout<<filePath<<" S_IFLNK: "<<S_IFLNK<<" S_IFMT: "<<S_IFMT<<endl;
                                            //cout<<filePath.c_str()<<endl;
                                            if(!lstat(filePath.c_str(),&buf))//成功返回0
                                                {
                                                    cout<<filePath.c_str()<<" S_IFLNK: "<<S_IFLNK<<" S_IFMT: "<<S_IFMT<<" "<<S_ISLNK(buf.st_mode)<<": "<<buf.st_mode<<endl;
                                                    //链接文件
                                                    if(S_ISLNK(buf.st_mode))
                                                    {
                                                        cout<<"111111111111 linkFile "<<filePath.c_str()<<endl;
                                                    }
                                
                                                    //一般文件
                                                    else if(S_ISREG(buf.st_mode))
                                                    {
                                                        cout<<"222 ordinaryFile "<<filePath.c_str()<<endl;
                                                        char cmd[128] = {0};
                                                        //删除原先存在的文件
                                                        sprintf(cmd,"rm -rf %s",filePath.c_str());
                                                        system(cmd);
                                                        
                                                        //把/etc/apache2/mods-enabled/下的文件链接到/etc/apache2/mods-available/下
                                                        string fileHead1 = "/etc/apache2/mods-available/";
                                                        string fileTail1 = ent->d_name;
                                                        string filePath1 = fileHead1 + fileTail1;
                                                        memset(cmd,0,sizeof(cmd));
                                                        sprintf(cmd,"ln -s %s %s",filePath1.c_str(),filePath.c_str());
                                                        system(cmd);
                                                    }
                                                    else
                                                  {
                                                      cout<<"333333"<<endl;
                                                  }
                                                    
                        
                                                }
                                                else
                                                {
                                                    //输出错误原因
                                                    cout<<strerror(errno)<<endl;
                                                }
                                                
                                }
        }
 
}
 
int main(int argc,char *argv[])
{
        string filePath = "/etc/apache2/mods-enabled/";
        listDir(filePath.c_str());
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值