#include #include #include #include #include #include #include #include #include #include #include "dir.h" #define CHUNKSIZE 5 #define TOTALFILES 4000 static void SeekDir(DIR *dirp, long loc) { printf("Seeking back to location %ld\n", loc); xseekdir(dirp, loc); } static long TellDir(DIR *dirp) { long loc; loc = xtelldir(dirp); printf("telldir assigned location %ld\n", loc); return (loc); } int main(int argc, char *argv[]) { DIR *dirp; int i; int j; long offset = 0, prev_offset = 0; char *files[100]; char filename[100]; int fd; struct dirent *dp = NULL; if (chdir("./test2") != 0) { err(EX_OSERR, "chdir"); } /*****************************************************/ /* Put a set of sample files in the target directory */ /*****************************************************/ for (i=1; i < TOTALFILES ; i++) { sprintf(filename, "file-%d", i); fd = open(filename, O_CREAT, 0666); if (fd == -1) { err(EX_OSERR, "open"); } close(fd); } dirp = xopendir("."); if (dirp == NULL) { err(EX_OSERR, "opendir"); } offset = TellDir(dirp); for (i = 0; i < 20; i++) files[i] = malloc(20); /*******************************************************/ /* enumerate and delete small sets of files, one group */ /* at a time. */ /*******************************************************/ do { /*****************************************/ /* Read in up to CHUNKSIZE file names */ /* i will be the number of files we hold */ /*****************************************/ for (i = 0; i < CHUNKSIZE; i++) { if ((dp = xreaddir(dirp)) != NULL) { strcpy(files[i], dp->d_name); printf("readdir (%ld) returned file %s\n", offset, files[i]); prev_offset = offset; offset = TellDir(dirp); } else { printf("readdir returned null\n"); break; } } #ifdef SEEKFIRST /****************************************************************/ /* Simuate the last entry not fitting into our (samba's) buffer */ /* If we read someting in on the last slot, push it back */ /* Pretend it didn't fit. This is approximately what SAMBA does.*/ /****************************************************************/ if (dp != NULL) { /* Step back */ SeekDir(dirp, prev_offset); offset = TellDir(dirp); i--; printf("file %s returned\n", files[i]); } #else if (dp != NULL) { /* don't delete teh last one */ i--; } #endif /*****************************************/ /* i is the number of names we have left.*/ /* Delete them. */ /*****************************************/ for (j = 0; j < i; j++) { if (*files[j] == '.') { printf ("skipping %s\n", files[j]); } else { #if 1 printf("Unlinking file %s\n", files[j]); if (unlink(files[j]) != 0) { err(EX_OSERR, "unlink"); } #endif } } #ifndef SEEKFIRST /****************************************************************/ /* Simuate the last entry not fitting into our (samba's) buffer */ /* If we read someting in on the last slot, push it back */ /* Pretend it didn't fit. This is approximately what SAMBA does.*/ /****************************************************************/ if (dp != NULL) { /* Step back */ SeekDir(dirp, prev_offset); offset = TellDir(dirp); printf("file %s returned\n", files[i]); } #endif SeekDir(dirp, offset); offset = TellDir(dirp); } while (dp != NULL); xclosedir(dirp); //chdir(".."); }