NEOERR *ne_remove_dir (const char *path) { NEOERR *err; DIR *dp; struct stat s; struct dirent *de; char npath[PATH_BUF_SIZE]; if (stat(path, &s) == -1) { if (errno == ENOENT) return STATUS_OK; return nerr_raise_errno (NERR_SYSTEM, "Unable to stat file %s", path); } if (!S_ISDIR(s.st_mode)) { return nerr_raise (NERR_ASSERT, "Path %s is not a directory", path); } dp = opendir(path); if (dp == NULL) return nerr_raise_errno (NERR_IO, "Unable to open directory %s", path); while ((de = readdir (dp)) != NULL) { if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) { snprintf (npath, sizeof(npath), "%s/%s", path, de->d_name); if (stat(npath, &s) == -1) { if (errno == ENOENT) continue; closedir(dp); return nerr_raise_errno (NERR_SYSTEM, "Unable to stat file %s", npath); } if (S_ISDIR(s.st_mode)) { err = ne_remove_dir(npath); if (err) break; } else { if (unlink(npath) == -1) { if (errno == ENOENT) continue; closedir(dp); return nerr_raise_errno (NERR_SYSTEM, "Unable to unlink file %s", npath); } } } } closedir(dp); if (rmdir(path) == -1) { return nerr_raise_errno (NERR_SYSTEM, "Unable to rmdir %s", path); } return STATUS_OK; }
NEOERR *ne_remove_dir (const char *path) { #ifdef _MSC_VER SHFILEOPSTRUCT op; op.hwnd = NULL; op.wFunc = FO_DELETE; char* tmp = (char*)calloc(1, strlen(path) + 2); memcpy(tmp, path, strlen(path)); op.pFrom = tmp; op.pTo = NULL; op.fFlags = FOF_SILENT; op.hNameMappings = NULL; op.lpszProgressTitle = NULL; int res = SHFileOperation(&op); free(tmp); if (res != 0) return nerr_raise_errno(NERR_SYSTEM, "Unable to stat file %s", path); #else NEOERR *err; DIR *dp; struct stat s; struct dirent *de; char npath[PATH_BUF_SIZE]; if (stat(path, &s) == -1) { if (errno == ENOENT) return STATUS_OK; return nerr_raise_errno (NERR_SYSTEM, "Unable to stat file %s", path); } if (!S_ISDIR(s.st_mode)) { return nerr_raise (NERR_ASSERT, "Path %s is not a directory", path); } dp = opendir(path); if (dp == NULL) return nerr_raise_errno (NERR_IO, "Unable to open directory %s", path); while ((de = readdir (dp)) != NULL) { if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) { snprintf (npath, sizeof(npath), "%s/%s", path, de->d_name); if (stat(npath, &s) == -1) { if (errno == ENOENT) continue; closedir(dp); return nerr_raise_errno (NERR_SYSTEM, "Unable to stat file %s", npath); } if (S_ISDIR(s.st_mode)) { err = ne_remove_dir(npath); if (err) break; } else { if (unlink(npath) == -1) { if (errno == ENOENT) continue; closedir(dp); return nerr_raise_errno (NERR_SYSTEM, "Unable to unlink file %s", npath); } } } } closedir(dp); if (rmdir(path) == -1) { return nerr_raise_errno (NERR_SYSTEM, "Unable to rmdir %s", path); } #endif return STATUS_OK; }