void handle_extracted_image_file(char *filename, char *target_dir, const char *pak_type_name) { if (is_squashfs(filename)) { char unsquashed[100] = ""; construct_path(unsquashed, target_dir, pak_type_name, NULL); printf("unsquashfs %s to directory %s\n", filename, unsquashed); rmrf(unsquashed); unsquashfs(filename, unsquashed); } if (check_lzo_header(filename)) { char unpacked[100] = ""; construct_path(unpacked, target_dir, pak_type_name, ".unpacked"); printf("decompressing %s with modified LZO algorithm to %s\n", filename, unpacked); if (lzo_unpack((const char*) filename, (const char*) unpacked) != 0) { printf("sorry. decompression failed. aborting now.\n"); exit(1); } if (is_cramfs_image(unpacked)) { char uncram[100] = ""; construct_path(uncram, target_dir, pak_type_name, NULL); printf("uncramfs %s to directory %s\n", unpacked, uncram); rmrf(uncram); uncramfs(uncram, unpacked); } } if (is_uboot_image(filename)) { char deimaged[100] = ""; construct_path(deimaged, target_dir, pak_type_name, ".deimaged"); printf("extracting u-boot image %s to %s\n", filename, deimaged); extract_uboot_image(filename, deimaged); handle_extracted_image_file(deimaged, target_dir, pak_type_name); } }
static void single_process_2(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 ); t( sp_ctl(env, SPCMP, cmp, NULL) == 0 ); void *db = sp_open(env); t( db != NULL ); void *env2 = sp_env(); t( env != NULL ); t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep2) == 0 ); t( sp_ctl(env2, SPCMP, cmp, NULL) == 0 ); void *db2 = sp_open(env2); t( db2 != NULL ); uint32_t k = 1; t( sp_set(db2, &k, sizeof(k), &k, sizeof(k)) == 0 ); size_t vsize = 0; void *vp = NULL; t( sp_get(db2, &k, sizeof(k), &vp, &vsize) == 1 ); t( vsize == sizeof(k) ); t( *(uint32_t*)vp == k ); free(vp); k = 3; t( sp_set(db, &k, sizeof(k), &k, sizeof(k)) == 0 ); t( sp_get(db, &k, sizeof(k), &vp, &vsize) == 1 ); t( vsize == sizeof(k) ); t( *(uint32_t*)vp == k ); free(vp); t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); t( sp_destroy(db2) == 0 ); t( sp_destroy(env2) == 0 ); t( rmrf(dbrep1) == 0 ); t( rmrf(dbrep2) == 0 ); }
static void repository_test4(void) { rmrf("./sophia"); rmrf("./logdir"); rmrf("./dir"); void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", "sophia", 0) == 0 ); t( sp_setint(env, "sophia.path_create", 0) == 0 ); t( sp_setint(env, "log.enable", 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_open(env) == -1 ); t( sp_destroy(env) == 0 ); t( exists("sophia", "") == 0 ); mkdir("sophia", 0755); env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", "sophia", 0) == 0 ); t( sp_setint(env, "sophia.path_create", 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_setint(env, "log.enable", 0) == 0 ); t( sp_setstring(env, "db", "test", 0) == 0 ); t( sp_setint(env, "db.test.sync", 0) == 0 ); t( sp_open(env) == 0 ); t( exists("sophia", "test") == 1 ); t( sp_destroy(env) == 0 ); }
static void github_123(void) { rmrf("./abc"); rmrf("./abc_log"); void *env = sp_env(); t( env != NULL ); char path[] = { '.', '/', 'a', 'b', 'c' }; char path_log[] = { '.', '/', 'a', 'b', 'c', '_', 'l', 'o', 'g' }; char name[] = { 't', 'e', 's', 't' }; t( sp_setstring(env, "sophia.path", path, sizeof(path)) == 0 ); t( sp_setstring(env, "db", name, sizeof(name)) == 0 ); t( sp_setstring(env, "log.path", path_log, sizeof(path_log)) == 0 ); t( sp_open(env) == 0 ); t( exists("./", "abc") == 1 ); t( exists("./", "abc_log") == 1 ); void *db = sp_getobject(env, "db.test"); t( db != NULL ); t( sp_destroy(env) == 0 ); rmrf("./abc"); rmrf("./abc_log"); }
void st_scene_rmrf(stscene *g, stc *cx) { rmrf(cx->suite->shamdir); rmrf(cx->suite->backupdir); rmrf(cx->suite->logdir); rmrf(cx->suite->dir); }
int main(int argc, char *argv[]) { rmrf(dbrep1); rmrf(dbrep2); test(single_process); test(single_process_2); test(multi_process); test(multi_process_2); return 0; }
void processExtractedFile(char *filename, char *folderExtractTo, const char *PAKname) { char extractedFile[255] = ""; int extracted = 0; if (is_lz4(filename)) { constructPath(extractedFile, folderExtractTo, PAKname, ".unlz4"); printf("UnLZ4 %s to %s\n", filename, extractedFile); extracted = !decode_file(filename, extractedFile); } else { if (check_lzo_header(filename)) { constructPath(extractedFile, folderExtractTo, PAKname, ".unpacked"); printf("LZOunpack %s to %s\n", filename, extractedFile); extracted = !lzo_unpack((const char*) filename, (const char*) extractedFile); } else { if (is_cramfs_image(filename)) { constructPath(extractedFile, folderExtractTo, PAKname, NULL); printf("Uncramfs %s to folder %s\n", filename, extractedFile); rmrf(extractedFile); uncramfs(extractedFile, filename); return; } else { if (is_kernel(filename)) { constructPath(extractedFile, folderExtractTo, PAKname, ".unpaked"); printf("Extracting kernel %s to %s\n", filename, extractedFile); extract_kernel(filename, extractedFile); extracted = 1; } else { if (is_nfsb(filename)) { constructPath(extractedFile, folderExtractTo, PAKname, ".unnfsb"); printf("Unnfsb %s to %s\n", filename, extractedFile); unnfsb(filename, extractedFile); extracted = 1; } } } } } if (strcmp(PAKname, "patc") != 0 && strcmp(PAKname, "extr") != 0) { if (is_squashfs(filename)) { constructPath(extractedFile, folderExtractTo, PAKname, NULL); printf("Unsquashfs %s to folder %s\n", filename, extractedFile); rmrf(extractedFile); unsquashfs(filename, extractedFile); return; } } else { printf("!!!Skipping unsquashfs (%s) as it doesn't know how to handle it...\n", PAKname); return; } if (extracted) processExtractedFile(extractedFile, folderExtractTo, PAKname); }
static void repository_test0(void) { rmrf("./sophia"); rmrf("./logdir"); rmrf("./dir"); void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", "sophia", 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_open(env) == 0 ); t( exists("sophia", "log") == 1 ); t( sp_destroy(env) == 0 ); }
static void gh_29(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 ); void *db = sp_open(env); t( db != NULL ); int i; for (i = 1; i <= 10; i++) { char key[4]; int len = snprintf(key, sizeof(key), "%d", i); t( sp_set(db, key, len, key, len) == 0 ); } for (i = 1; i <= 10; i++) { char key[4]; int len = snprintf(key, sizeof(key), "%d", i); size_t vsize = 0; void *vp = NULL; t( sp_get(db, key, len, &vp, &vsize) == 1 ); t( vsize == len ); t( memcmp(key, vp, len) == 0 ); free(vp); } t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); t( rmrf(dbrep) == 0 ); }
int rmrf(char *path) { DIR *d = opendir(path); if (d == NULL) return -1; char file[1024]; struct dirent *de; while ((de = readdir(d))) { if (de->d_name[0] == '.') continue; snprintf(file, sizeof(file), "%s/%s", path, de->d_name); int rc; if (de->d_type == DT_DIR) { rc = rmrf(file); } else { rc = unlink(file); } if (rc == -1) { closedir(d); return -1; } } closedir(d); return rmdir(path); }
int main(int argc, char* argv[]) { if(argc==2) usage(argv[0]); // Get the trash variable TRASH = getenv("TRASH"); if(!TRASH){ fprintf(stderr,"Error: TRASH variable not set, please set $TRASH to a created directory."); exit(1); } // Get the current working directory struct stat* buff = malloc(sizeof(struct stat)); int s = stat(TRASH, buff); if(s) { fprintf(stderr, "ERROR: Couln't stat file: %s\n\t%s\n", TRASH, strerror(errno)); return EACCES == errno ? E_PERMISSION : E_NONEXIST; } rmrf(TRASH, 0); if(S_ISDIR(buff->st_mode)) { s = mkdir(TRASH, buff->st_mode); if(!s) chmod(TRASH, buff->st_mode); else{ fprintf(stderr, "ERROR: %s\n", strerror(errno)); return EACCES == errno ? E_PERMISSION : E_NONEXIST; } } free(buff); }
int main(int argc, char *argv[]) { DIR *path; int fd; int ret; if (argc == 1) return -rmrf(CACHE_PATH); if (argc < 2) exit(1); path = opendir(CACHE_PATH); if (!path) exit(2); fd = dirfd(path); if (fd < 0) { perror(argv[1]); exit(3); } ret = unlinkat(fd, argv[1], 0); if (ret < 0) { if (errno == ENOENT) { /* It's not an error if unlinkat fails, since that file */ exit(0); } perror(argv[1]); } return 0; }
static void gh_5(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 ); void *db = sp_open(env); t( db != NULL ); char k1[] = "key-1"; char v1[] = "val-1"; t( sp_set(db, k1, sizeof(k1), v1, sizeof(v1)) == 0); char k2[] = "key-10"; char v2[] = "val-10"; t( sp_set(db, k2, sizeof(k2), v2, sizeof(v2)) == 0); size_t ressize = 0; void *res = NULL; t( sp_get(db, k1, sizeof(k1), &res, &ressize) == 1 ); t( ressize == sizeof(v1) ); t( strcmp(v1, res) == 0 ); free(res); t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); t( rmrf(dbrep) == 0 ); }
static void multi_process(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 ); void *db = sp_open(env); t( db != NULL ); int pid = fork(); t( pid != -1 ); if (pid == 0) { /* new process */ void *env2 = sp_env(); t( env2 != NULL ); t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 ); void *db2 = sp_open(env2); t( db2 == NULL ); t( sp_destroy(env2) == 0 ); /* valgrind: parent db and env are unfreed here, and that * is correct otherwise destroy would * corrupt the database. */ exit(0); } else { int status = 0; t( waitpid(pid, &status, 0) == pid ); t( status == 0 ); } t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); rmrf(dbrep1); }
char * poe_init_playground(const char *base, const char *env) { struct stat s; if (stat(POE_TEMPORARY_BASE, &s) == -1) { NONNEGATIVE(mkdir(POE_TEMPORARY_BASE, 0755)); } // setup base (tmpfs) NONNEGATIVE(asprintf(&workbase, POE_TEMPORARY_BASE "/%ld", (long)getpid())); // pid_t is signed, not larger than long if (stat(workbase, &s) != -1) { NONNEGATIVE(rmrf(workbase)); } NONNEGATIVE(mkdir(workbase, 0755)); NONNEGATIVE(mount(NULL, workbase, "tmpfs", MS_NOSUID, "size=32m")); // TODO NONNEGATIVE(asprintf(&workdir, "%s/work", workbase)); NONNEGATIVE(mkdir(workdir, 0755)); NONNEGATIVE(asprintf(&upperdir, "%s/upper", workbase)); NONNEGATIVE(mkdir(upperdir, 0755)); NONNEGATIVE(asprintf(&mergeddir, "%s/merged", workbase)); NONNEGATIVE(mkdir(mergeddir, 0755)); char *opts = NULL; NONNEGATIVE(asprintf(&opts, "lowerdir=%s:%s,upperdir=%s,workdir=%s", env, base, upperdir, workdir)); NONNEGATIVE(mount(NULL, mergeddir, "overlay", MS_NOSUID, opts)); return mergeddir; }
static void multi_process_2(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 ); t( sp_ctl(env, SPCMP, cmp, NULL) == 0 ); void *db = sp_open(env); t( db != NULL ); void *env2 = sp_env(); t( env != NULL ); t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep2) == 0 ); t( sp_ctl(env2, SPCMP, cmp, NULL) == 0 ); uint32_t k = 1; size_t vsize = 0; void *vp = NULL; int pid = fork(); t( pid != -1 ); if (pid == 0) { void *db2 = sp_open(env2); t( db2 != NULL ); t( sp_set(db2, &k, sizeof(k), &k, sizeof(k)) == 0 ); t( sp_get(db2, &k, sizeof(k), &vp, &vsize) == 1 ); t( vsize == sizeof(k) ); t( *(uint32_t*)vp == k ); free(vp); t( sp_destroy(db2) == 0 ); t( sp_destroy(env2) == 0 ); t( rmrf(dbrep2) == 0 ); /* valgrind: parent db and env are unfreed here, and that * is correct otherwise destroy would * corrupt the database. */ exit(0); } k = 3; t( sp_set(db, &k, sizeof(k), &k, sizeof(k)) == 0 ); t( sp_get(db, &k, sizeof(k), &vp, &vsize) == 1 ); t( vsize == sizeof(k) ); t( *(uint32_t*)vp == k ); free(vp); t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); t( rmrf(dbrep1) == 0 ); int status = 0; t( waitpid(pid, &status, 0) == pid ); t( status == 0 ); }
int main(int argc, char *argv[]) { rmrf(dbrep); test(gh_5); test(gh_29); test(gh_37); return 0; }
int rmrf(char* file, int end) { char* oldPath = toAbsolute(file); char* orig = file; char* base = basename(file); struct stat* buff = malloc(sizeof(struct stat)); int i = stat(oldPath, buff); if(i) { fprintf(stderr, "ERROR: Couln't stat file: %s\n\t%s\n", oldPath, strerror(errno)); return EACCES == errno ? E_PERMISSION : E_NONEXIST; } if(S_ISDIR(buff->st_mode)) { DIR *dp; struct dirent *d; dp = opendir(oldPath); if (dp == NULL) { perror("open"); exit(1); } char* nPCP = malloc(sizeof(oldPath) + 250); strcpy(nPCP, oldPath); strcat(nPCP, "/"); char* suffix = nPCP+strlen(oldPath)+1; end = end == 0 ? strlen(oldPath) - strlen(base) : end; while (d = readdir(dp)) { if(!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) continue; strcpy(suffix,d->d_name); if(d->d_type == 4){ rmrf(nPCP, end); continue; } char* sop = malloc(strlen(file) + strlen(d->d_name) + 1); strcpy(sop, file); strcat(sop,"/"); strcat(sop, d->d_name); int l = unlink(sop); if(l){ fprintf(stderr, "ERROR: %s\n", strerror(errno)); return errno == ENOENT ? E_NONEXIST : E_PERMISSION; } free(sop); } closedir(dp); rmdir(oldPath); return SUCCESS; } rmf(oldPath); }
int main(int argc, char *argv[]) { rmrf(dbrep); test(limit_key); if (sizeof(size_t) > 4) test(limit_value); test(error_nonfatal); return 0; }
void poe_destroy_playground() { struct stat s; if (mergeddir) { umount(mergeddir); rmrf(mergeddir); free(mergeddir); } if (workdir && stat(workdir, &s) != -1) { rmrf(workdir); free(workdir); } if (upperdir && stat(upperdir, &s) != -1) { rmrf(upperdir); free(upperdir); } if (workbase && stat(workbase, &s) != -1) { umount(workbase); rmrf(workbase); free(workbase); } }
static void limit_value(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 ); t( sp_ctl(env, SPCMP, cmp, NULL) == 0 ); void *db = sp_open(env); t( db != NULL ); char buf[1]; t( sp_set(db, buf, sizeof(buf), buf, UINT32_MAX + 1ULL) == -1 ); t( sp_error(db) != NULL ); t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); rmrf(dbrep); }
int handle_file(const char *file, char *destination) { if (check_lzo_header(file)) { if (destination == NULL) { destination = "./lzounpack.out"; } printf("extracting lzo compressed file to: %s\n", destination); if (lzo_unpack(file, destination) == 0) { handle_file(destination, NULL); return EXIT_SUCCESS; } } else if (is_squashfs(file)) { if (destination == NULL) { destination = "./unsquashfs.out"; } printf("unsquashfs compressed file system to: %s\n", destination); rmrf(destination); unsquashfs(file, destination); return EXIT_SUCCESS; } else if (is_cramfs_image(file)) { if (destination == NULL) { destination = "./uncramfs.out"; } printf("uncramfs compressed file system to: %s\n", destination); rmrf(destination); uncramfs(destination, file); return EXIT_SUCCESS; } else if (is_epk2_file(file)) { printf("extracting firmware file...\n\n"); extract_epk2_file(file); return EXIT_SUCCESS; } printf("\n"); printf("unsupported file format:\n", file); exit(EXIT_FAILURE); }
void rmrf(const char *path) { struct stat st; if (kga_lstat_skip_enoent(path, &st)) return; if (S_ISDIR(st.st_mode)) { scope { char *sub_path = string_new(); DIR *dir = kga_opendir(path); for (struct dirent *dirent; (dirent = readdir(dir)); ) { if (dirent->d_name[0] == '.' && (dirent->d_name[1] == '\0' || (dirent->d_name[1] == '.' && dirent->d_name[2] == '\0'))) { continue; }; string_fmt(sub_path, "%s/%s", path, dirent->d_name); rmrf(sub_path); }; }; };
static void single_process(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 ); void *db = sp_open(env); t( db != NULL ); void *env2 = sp_env(); t( env2 != NULL ); t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 ); void *db2 = sp_open(env2); t( db2 == NULL ); t( sp_destroy(env2) == 0 ); t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); rmrf(dbrep1); }
static void gh_37(void) { const int size = 999; const int watermark = 3996; const int pagesize = 1022; char data[size]; memset(data, 0, sizeof(data)); void *env = sp_env(); int rc = sp_ctl(env, SPDIR, SPO_CREAT | SPO_RDWR, dbrep); t(rc == 0); if (rc == -1) rc = sp_ctl(env, SPMERGE, 0); t(rc == 0); rc = sp_ctl(env, SPMERGEWM, watermark); if (rc == -1) t(rc == 0); sp_ctl(env, SPPAGE, pagesize); t(rc == 0); void *db = sp_open(env); t(db != NULL); int byteCount = 0; int i; for (i = 0; i < 100000; ++i) { rc = sp_set(db, &i, sizeof(int), data, size); t(rc == 0); } byteCount += size; if (i != 0 && i % watermark == 0) { rc = sp_ctl(db, SPMERGEFORCE); t(rc == 0); } t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); t( rmrf(dbrep) == 0 ); }
static void ddl_open_online0(void) { rmrf("./logdir"); rmrf("./dir0"); rmrf("./dir1"); void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_setstring(env, "log.path", st_r.conf->log_dir, 0) == 0 ); t( sp_open(env) == 0 ); t( sp_setstring(env, "db", "s0", 0) == 0 ); t( sp_setstring(env, "db.s0.path", "dir0", 0) == 0 ); t( sp_setstring(env, "db.s0.index.key", "u32", 0) == 0 ); t( sp_setint(env, "db.s0.sync", 0) == 0 ); void *s0 = sp_getobject(env, "db.s0"); t( s0 != NULL ); t( sp_open(s0) == 0 ); int key = 7; void *o = sp_object(s0); sp_setstring(o, "key", &key, sizeof(key)); t( sp_set(s0, o) == 0 ); key = 8; o = sp_object(s0); sp_setstring(o, "key", &key, sizeof(key)); t( sp_set(s0, o) == 0 ); key = 9; o = sp_object(s0); sp_setstring(o, "key", &key, sizeof(key)); t( sp_set(s0, o) == 0 ); t( sp_destroy(s0) == 0 ); t( sp_destroy(s0) == 0 ); /* shutdown */ t( sp_setstring(env, "db", "s0", 0) == 0 ); t( sp_setstring(env, "db.s0.path", "dir0", 0) == 0 ); t( sp_setstring(env, "db.s0.index.key", "u32", 0) == 0 ); t( sp_setint(env, "db.s0.sync", 0) == 0 ); /* ban open existing databases */ s0 = sp_getobject(env, "db.s0"); t( s0 != NULL ); t( sp_open(s0) == -1 ); t( sp_destroy(env) == 0 ); rmrf("./logdir"); rmrf("./dir0"); rmrf("./dir1"); }
static void error_nonfatal(void) { void *env = sp_env(); t( env != NULL ); t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 ); t( sp_ctl(env, SPCMP, cmp, NULL) == 0 ); void *db = sp_open(env); t( db != NULL ); uint32_t k = 1, v = 1; t( sp_set(db, &k, sizeof(k), &v, sizeof(v)) == 0); t( sp_set(db, &k, UINT16_MAX + 1 , &v, sizeof(v)) == -1); t( sp_error(db) != NULL ); t( sp_set(db, &k, sizeof(k), &v, sizeof(v)) == 0); t( sp_error(db) == NULL ); t( sp_destroy(db) == 0 ); t( sp_destroy(env) == 0 ); t( rmrf(dbrep) == 0 ); }
/* does the same thing as 'rm -rf' */ int rmrf(const char *path) { int errflag = 0; struct dirent *dp; DIR *dirp; if(!unlink(path)) { return 0; } else { if(errno == ENOENT) { return 0; } else if(errno == EPERM) { /* fallthrough */ } else if(errno == EISDIR) { /* fallthrough */ } else if(errno == ENOTDIR) { return 1; } else { /* not a directory */ return 1; } dirp = opendir(path); if(!dirp) { return 1; } for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if(dp->d_name) { if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) { char name[PATH_MAX]; snprintf(name, PATH_MAX, "%s/%s", path, dp->d_name); errflag += rmrf(name); } } } closedir(dirp); if(rmdir(path)) { errflag++; } return errflag; } }
/* does the same thing as 'rm -rf' */ int rmrf(char *path) { int errflag = 0; struct dirent *dp; DIR *dirp; char name[PATH_MAX]; if(!unlink(path)) { return(0); } else { if(errno == ENOENT) { return(0); } else if(errno == EPERM) { /* fallthrough */ } else if(errno == EISDIR) { /* fallthrough */ } else if(errno == ENOTDIR) { return(1); } else { /* not a directory */ return(1); } if((dirp = opendir(path)) == (DIR *)-1) return(1); for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if(dp->d_ino) { sprintf(name, "%s/%s", path, dp->d_name); if(strcmp(dp->d_name, "..") && strcmp(dp->d_name, ".")) errflag += rmrf(name); } } closedir(dirp); if(rmdir(path)) errflag++; return(errflag); } return(0); }
int ABSHandler::Private::rmrf( const char *path ) { int errflag = 0; struct dirent *dp; DIR *dirp; if ( !unlink( path ) ) return( 0 ); else { if ( errno == ENOENT ) return( 0 ); else if ( errno == EPERM ) { } /* fallthrough */ else if ( errno == EISDIR ) { } /* fallthrough */ else if ( errno == ENOTDIR ) return( 1 ); else /* not a directory */ return( 1 ); if (( dirp = opendir( path ) ) == ( DIR * ) - 1 ) return( 1 ); for ( dp = readdir( dirp ); dp != NULL; dp = readdir( dirp ) ) { if ( dp->d_ino ) { char name[4096]; sprintf( name, "%s/%s", path, dp->d_name ); if ( strcmp( dp->d_name, ".." ) && strcmp( dp->d_name, "." ) ) errflag += rmrf( name ); } } closedir( dirp ); if ( rmdir( path ) ) errflag++; return( errflag ); } }