我正在编写一个dotfile存储库管理器,但删除存储库的命令不起作用。 它进入存储库文件夹,然后它必须列出所有文件和目录,以便我可以删除它们。麻烦在于,它列出了我需要删除的每个文件或目录,但它排除了非空的.git
。我对其他存储库进行了进一步测试,结论是,每个非空目录的名称以点开头都会被忽略,而“普通”点文件则可以。 下面是我会很快描述的有问题的代码。readdir忽略以点开头的非空存储库
rm_dotfiles_repository时调用库的名称,repo_dir(repo)
获取到存储库,然后readdir
循环启动。我需要递归删除文件夹,这就是为什么我要过滤文件夹和普通的旧文件。请注意,我不排除文件夹.
和..
,但我会尽快添加。
#define _XOPEN_SOURCE 500
#include "repository.h"
#include "helpers.h"
#include "nftwcallbacks.h"
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <error.h>
void rm_dotfiles_repository(char* repo)
{
repo_dir(repo);
/* Remove the repository's files recursively
* TODO: Remove the symbolic links in ~ before removing the repo
* We remove the repository, target by target */
DIR* dir = NULL;
struct dirent* file = NULL;
struct stat stat_data;
dir = opendir(".");
if (dir == NULL)
{
perror("Error:");
exit(EXIT_FAILURE);
}
file = readdir(dir);
while ((file = readdir(dir)) != NULL)
{
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
{
/* TODO: why isn't .git listed, even if .gitmodules is listed ? After tests, it seems that .something repositories which are non-empty
* aren't listed*/
if(stat(file->d_name, &stat_data))
{
perror("Error");
exit(EXIT_FAILURE);
}
if (S_ISDIR(stat_data.st_mode))
{
remove_target(repo, file->d_name);
}
else
{
printf("Remove file %s\n", file->d_name);
}
}
}
if (closedir(dir))
{
perror("Error:");
exit(EXIT_FAILURE);
}
}
void install_target(char* repo, char* target)
{
repo_dir(repo);
if (nftw(target, install, 4, 0))
{
exit(EXIT_FAILURE);
}
}
void remove_target(char* repo, char* target)
{
printf("Remove target %s from repo %s\n", target, repo);
}
你能帮我找到问题的原因吗?在此先感谢
编辑:由于垫皮特森问:here的完整代码,我已经给段是repository.c
任何机会,你可以写一个完整的独立的例子,而不是一个包含很多包含非标准文件的代码片段。例如它应该有一个“主”。 –
我有一个独立的例子,是的,但有很多代码。尽管如此,我会做一个主意。 – Mathuin
嗯,我只是删除了代码中的“我不需要它”,创建了一个名为'.git'的目录并运行了你的代码,它说,在“删除”所有其他文件的过程中,“从回购中删除目标.git“。所以只有两种可能的情况是你没有'.git'目录的读权限,或者你的系统和我的系统有一些区别...... –