从目录

时间:2019-06-17 22:09:50

标签: c linux

我试图递归地删除dir中所有悬空的链接,但是在每个链接上我都得到“ lstat:没有这样的文件或目录”。

void deletelinks(char *name, int indent)
{
    DIR *dir;
    FILE *a;
    struct stat sb;
    char *buf;
    ssize_t nbytes, bufsiz;
    struct dirent *entry;

    if (!(dir = opendir(name)))
        return;

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            char path[1024];
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
           // printf("%*s[%s]\n", indent, "", entry->d_name);
            deletelinks(path, indent + 2);
        } else {
            if(entry->d_type == DT_LNK){
                if (lstat(("%s/%s", name, entry->d_name), &sb) == -1) {
                printf("%s/%s", name, entry->d_name);
               perror("lstat");
               exit(EXIT_FAILURE);
           }
           bufsiz = sb.st_size + 1;
           if (sb.st_size == 0)
               bufsiz = PATH_MAX;

           buf = malloc(bufsiz);
           if (buf == NULL) {
               perror("malloc");
               exit(EXIT_FAILURE);
           }

           nbytes = readlink(("%s%s", name, entry->d_name), buf, bufsiz);
           if (nbytes == -1) {
               perror("readlink");
               exit(EXIT_FAILURE);
           }
                a=fopen(("%s/%*s",name, (int) nbytes, buf), "r");
                if(a==NULL){

                    printf("%s/%*s\n",name, (int) nbytes, buf);
                  }
            }
        }
    }
    closedir(dir);
}

我希望此功能删除dir中所有悬空或断开的链接。这个函数成功地递归地找到了所有链接,但是它无法识别它是否挂接,因为我有一个错误“ lstat:没有这样的文件或目录”

1 个答案:

答案 0 :(得分:0)

void deletelinks(char *name, int indent)
{
    DIR *dir;
    FILE *a;
    struct stat sb;
    char *buf;
    ssize_t nbytes, bufsiz;
    struct dirent *entry;

    if (!(dir = opendir(name)))         
        return;

    while ((entry = readdir(dir)) != NULL) {            
        if (entry->d_type == DT_DIR) {                  
            char path[1024];
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
           // printf("%*s[%s]\n", indent, "", entry->d_name);
            deletelinks(path, indent + 2);              
        } else {
            if(entry->d_type == DT_LNK){                
              char path[1024];
                snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
                if (lstat(path, &sb) == -1) {
                printf("%s", path);
               perror("lstat");
               exit(EXIT_FAILURE);
           }
           bufsiz = sb.st_size + 1;
           if (sb.st_size == 0)
               bufsiz = PATH_MAX;

           buf = malloc(bufsiz);
           if (buf == NULL) {
               perror("malloc");
               exit(EXIT_FAILURE);
           }

           nbytes = readlink(path, buf, bufsiz);
           if (nbytes == -1) {
               perror("readlink");
               exit(EXIT_FAILURE);
           }
                char path2[1024];
                snprintf(path2, sizeof(path2), "%s/%*s", name, (int) nbytes, buf);  
                a=fopen(path2, "r");                                                
                if(a==NULL){
                  fclose(a);
                  if(unlink(path)!=0) printf("error on deleting symlink!\n"); 
                  printf("%s\n", path);                                       
                  }
            }
        }
    }
    closedir(dir);          
}

忘了,谢谢用户3386109