自己手痒不小心把/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;
}