| 53 | |
| 54 | bool remove_all ( const char *path ) { |
| 55 | DIR *dir; |
| 56 | dirent *de; |
| 57 | |
| 58 | bool rv = true; |
| 59 | if ( ( dir = opendir ( path ) ) != 0 ) { |
| 60 | try { |
| 61 | std::string top ( path ); |
| 62 | top += "/"; |
| 63 | |
| 64 | while ( ( de = readdir ( dir ) ) != 0 ) { |
| 65 | if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) { |
| 66 | std::string subpath ( top ); |
| 67 | subpath += de->d_name; |
| 68 | remove_all ( subpath.c_str() ); |
| 69 | } |
| 70 | } |
| 71 | } catch ( ... ) { |
| 72 | closedir ( dir ); |
| 73 | throw; |
| 74 | } |
| 75 | |
| 76 | closedir ( dir ); |
| 77 | |
| 78 | if ( rmdir ( path ) ) { |
| 79 | std::string msg = "can't remove dir "; |
| 80 | msg += path; |
| 81 | throw std::runtime_error ( msg ); |
| 82 | } |
| 83 | } else { |
| 84 | if ( ( errno == ENOTDIR ) || // Linux |
| 85 | ( errno == EINVAL ) ) { // Windows |
| 86 | if ( unlink ( path ) ) { |
| 87 | std::string msg = "can't remove file "; |
| 88 | msg += path; |
| 89 | throw std::runtime_error ( msg ); |
| 90 | } |
| 91 | } else { |
| 92 | if ( errno != ENOENT ) { |
| 93 | std::string msg = "can't remove "; |
| 94 | msg += path; |
| 95 | throw std::runtime_error ( msg ); |
| 96 | } else { |
| 97 | // it wasn't there in the first place |
| 98 | rv = false; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return rv; |
| 104 | } |