我正在使用Linux Ubuntu 16.04上的C++文件搜尋器。
基本上它應該通過一個目錄,將文件字節大小添加到鏈接列表並相應地創建新節點。所以如果我有一個文件夾裏面有好幾個文件,它就可以算好了
我的問題是,如果我有一個文件夾中有另一個文件夾,我得到一個segementation錯誤,當用GNU-debugger測試時如下所示:
Segfault用於嵌套文件夾調用
Program received signal SIGSEGV, Segmentation fault. __strcpy_sse2_unaligned() at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:714 714 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
根據stack overflow post 1我需要以某種方式安裝庫,但沒有與安裝了正確的版本問題。
我不認爲這是我的問題,因爲發生故障的位置,根據調試器,它發生在遞歸函數內部時,它應該挖掘目錄並獲取數據。在我的主循環中調用相同的函數來遍歷頂層目錄,它在單層上工作正常,當我有嵌套的目錄時,我遇到了問題。附件爲基準的完整源代碼,這是一個有點長,但如果調用相同參數的錯誤應該是很容易複製:
// Directory crawler
// Written by Kaz
/*
1) Start at a user provided directory
2) Descend the file tree while tracking each file
3) Groups each file by it's size based off user argument
4) Print a histogram of file sizes in a bin wide groupings
*/
#include<iostream>
#include <dirent.h>
#include<string.h>
#include <errno.h>
#include <stdio.h>
#include<string>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<stdlib.h>
using namespace std;
int binCount = 0; // count of total bins
struct node{
node* next, *prev;
int count, name, min, max;
node(){
prev = NULL;
next = NULL;
count = 0;
name = binCount;
min = 0;
max = 0;
}
};
node *nextNode(node* previousNode){
node *nextLink = new node;
nextLink ->name = binCount;
nextLink->prev = previousNode;
nextLink->next = NULL;
nextLink->count = 1;
nextLink->min = previousNode->max + 1;
nextLink->max = ((previousNode->max)*2) + 1;
previousNode ->next = nextLink;
return nextLink;
}
void traverseNewDirectory(node * here, char *dirName){
DIR * nwd;
struct dirent *dip;
node * current;
current = here;
bool isadirectory,isHidden;
if((nwd = opendir(dirName))== NULL){
perror("Can't open derived directory");
return;
}
while ((dip = readdir(nwd)) != NULL){
isadirectory = false;
isHidden = false;
if((dip -> d_type) == DT_UNKNOWN){
struct stat stbuf;
stat(dip->d_name, &stbuf);
isadirectory = S_ISDIR(stbuf.st_mode);
}
else if((dip -> d_type) == DT_DIR){
if((strcmp(dip->d_name, ".") == 0) || (strcmp(dip->d_name, "..")) == 0){
isHidden = true;
isadirectory = true;
}
else{
isadirectory = true;
}
}
else{
if((dip-> d_reclen <= current->max)&&(dip->d_reclen >=current->min)){
current->count = current->count+1;
}
else if(dip->d_reclen < current->min){
node*temp = current->prev;
while(temp->prev != NULL){
if((dip-> d_reclen <= current->max)&&(dip->d_reclen >=current->min)){
current->count = current->count+1;
break;
}
else if(dip->d_reclen < current->min){
temp = current->prev;
}
}
}
else{
current -> next = nextNode(current);
current = current -> next;
binCount++;
}
}
if(isadirectory){
char *path;
strcpy(path,dirName);
strcat(path, "/");
strcat(path,dip->d_name);
strcat(path, "\0");
if(isHidden == true){
}
else{
traverseNewDirectory(current, path);
}
}
}
while ((closedir (nwd) == -1) && (errno == EINTR));
}
void printHistogram(node *head){
node*temp;
temp = head;
while(temp!=NULL){
cout << "[B " << temp->name << "] from " << temp->min << " to " << temp->max << " : ";
for(int i = 0; i < temp->count; i++){
cout << "x";
}
cout << endl;
temp = temp->next;
}
}
int main(int argc,char *argv[]){
// Ensures that a valid directory is provided by the cmd line argument
if (argc != 3){
if(argc == 1){
fprintf (stderr, " argc = %d no directory given \n", argc);
return 1;
}
else if(argc == 2){
fprintf (stderr, " argc = %d no size given \n", argc);
return 2;
}
else{
fprintf(stderr, "argc = %d invalid parameters \n", argc);
return 3;
}
}
DIR * cwd; // current working directory pointer
struct dirent *cwdP; // pointer to dirent struct
int binWidth; // variable for the width of the grouping in the histogram
binWidth = atoi(argv[2]);
node *first = new node;
binCount++;
first->max = binWidth - 1;
node * current;
current = first;
bool isadirectory,isHidden;
if((cwd = opendir(argv[1]))== NULL){
perror("Can't open main directory");
return 2;
}
while ((cwdP = readdir(cwd)) != NULL){
isadirectory = false;
isHidden = false;
if((cwdP -> d_type) == DT_UNKNOWN){
struct stat stbuf;
stat(cwdP->d_name, &stbuf);
isadirectory = S_ISDIR(stbuf.st_mode);
}
else if((cwdP -> d_type) == DT_DIR){
if((strcmp(cwdP->d_name, ".") == 0) || (strcmp(cwdP->d_name, "..")) == 0){
isHidden = true;
isadirectory = true;
}
else{
isadirectory = true;
}
}
else{
if((cwdP-> d_reclen <= current->max)&&(cwdP->d_reclen >=current->min)){
current->count = current->count+1;
}
else if(cwdP->d_reclen < current->min){
node*temp = current->prev;
while(temp->prev != NULL){
if((cwdP-> d_reclen <= current->max)&&(cwdP->d_reclen >=current->min)){
current->count = current->count+1;
break;
}
else if(cwdP->d_reclen < current->min){
temp = current->prev;
}
}
}
else{
current -> next = nextNode(current);
current = current -> next;
binCount++;
}
}
if(isadirectory){
char *fullPath;
strcpy(fullPath,argv[1]);
strcat(fullPath,"/");
strcat(fullPath,cwdP->d_name);
strcat(fullPath, "\0");
if(isHidden == true){
}
else{
traverseNewDirectory(current, fullPath);
}
}
}
while ((closedir (cwd) == -1) && (errno == EINTR));
printHistogram(first);
return 0;
}
'char * path; strcat(路徑,「/」);'你期望做什麼? 'path'是單元化的並且包含垃圾。所以你正在寫垃圾場。在嘗試寫入之前,'path'需要指向一個有效的內存緩衝區。 – kaylum
@kaylum對不起,請忽略'strcat(path,「/」);'我把它放進來測試我是否先串聯,如果這樣可以消除'strcpy()'發生故障的問題。我只是編輯了我的問題 – Callat
同樣的事情,但。 'strcpy'不會爲你分配內存。它期望你已經做到了。 – paddy