000
29.07.2003, 10:05 Uhr
xodiak
|
Hallo Leute!
ich habe hier zwei Funktionen, process_dir und read_dir. Das was ich mit den Funktionen machen will funktioniert zuverlässig, solange ich Zugriff auf das Verzeichnis habe. Habe ich auf ein Verzeichnis keine Zugriffsrechte, so bricht er mir mit einem SIGSEGV in der markierten Zeile ab (while(*dir)). der GDB sagt mir vor dem Absturz, dass dir auf 0x0 zeigt -> Logisch, Schutzverletzung. Der Denkfehler muss also irgendwo in read_dir begraben sein. Kann mir jemand auf die Sprünge helfen? Danke schonmal,
Gruß xodi
C++: |
float process_dir(char *directory, char *tname, int *filestotal, float *sizetotal, unsigned int level) { struct dirnames **dir, **sav; char *path = malloc(1024); float dsize = 0; int dfiles = *filestotal; sav = dir = read_dir(directory, &dsize, filestotal, tname); if(debug) printf("process_dir %d\n", debug++);
while(*dir) // <-------------------- Hier kommt der SIGSEGV { if((strlen(directory)+strlen((*dir)->name)) > 1022) path = realloc(path, strlen(directory)+strlen((*dir)->name) + 2); if(strlen(directory) == 1) snprintf(path,strlen((*dir)->name)+3,"/%s",(*dir)->name); else snprintf(path,strlen(directory)+strlen((*dir)->name)+2,"%s/%s",directory,(*dir)->name); dsize += process_dir(path,tname, filestotal, sizetotal, (level + 1)); free((*dir)->name); free(*(dir++)); } insert_level(((float)dsize)/1048576, directory, ((*filestotal)-dfiles)); if(level == 1) check_legal(tname, directory, (float)(dsize)/1048576, ((*filestotal)-dfiles), strlen(getvalue(tname, "transdir"))); free(path); if(debug) printf("process_dir Ende\n"); return dsize; }
struct dirnames **read_dir(char *dir, float *thisdirsize, int *thisdirfiles, char *tname) { static char *path; struct dirnames **dl; struct dirent *ent; struct stat lst; DIR *d; int ne, p = 0, bigsize = 1048576 * atoi(getvalue(tname, "bigsize")), olddate = time((time_t *)NULL) - atoi(getvalue(tname, "olddate"))*3600*24;
if(debug) printf("read_dir %d\n", debug++);
if ((d=opendir(dir)) == NULL) return NULL;
dl = (struct dirnames **) malloc(sizeof(struct dirnames *) * (ne = 400)); path = malloc(1024);
while((ent = (struct dirent *)readdir(d))) { if (!(strcmp("..",ent->d_name) && strcmp(".",ent->d_name) && strncmp(".snapshot",ent->d_name, 9))) continue;
if((strlen(dir)+strlen(ent->d_name)) > 1022) path = realloc(path, (strlen(dir)+strlen(ent->d_name)+2)); snprintf(path,strlen(dir)+strlen(ent->d_name)+2,"%s/%s",dir,ent->d_name);
if (lstat(path,&lst) < 0 || ((lst.st_mode & S_IFMT) == S_IFLNK)) { (*thisdirfiles)++; continue; } if((lst.st_mode & S_IFMT) == S_IFDIR) { dl[p] = (struct dirnames*) malloc(sizeof(struct dirnames)); if(ent->d_name == NULL) dl[p++]->name = scopy(ent->d_name); else dl[p++]->name = scopy("Error opening directory"); if(p>ne) dl = (struct dirnames **) realloc(dl,sizeof(struct dirnames *) * (ne += 100)); continue; } (*thisdirsize) += lst.st_size; (*thisdirfiles)++;
} closedir(d); free(path); dl[p] = NULL; if(debug) printf("read_dir Ende\n"); return dl; }
|
-- There's always one more bug. |