34 | | } |
35 | | |
36 | | bool remove_all ( const char *path ) { |
37 | | DIR *dir; |
38 | | dirent *de; |
39 | | |
40 | | bool rv = true; |
41 | | if ( ( dir = opendir ( path ) ) != 0 ) { |
42 | | try { |
43 | | std::string top ( path ); |
44 | | top += "/"; |
45 | | |
46 | | while ( ( de = readdir ( dir ) ) != 0 ) { |
47 | | if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) { |
48 | | std::string subpath ( top ); |
49 | | subpath += de->d_name; |
50 | | remove_all ( subpath.c_str() ); |
51 | | } |
52 | | } |
53 | | } catch ( ... ) { |
54 | | closedir ( dir ); |
55 | | throw; |
56 | | } |
57 | | |
58 | | closedir ( dir ); |
59 | | |
60 | | if ( rmdir ( path ) ) { |
61 | | std::string msg = "can't remove dir "; |
62 | | msg += path; |
63 | | throw std::runtime_error ( msg ); |
64 | | } |
65 | | } else { |
66 | | if ( ( errno == ENOTDIR ) || // Linux |
67 | | ( errno == EINVAL ) ) { // Windows |
68 | | if ( unlink ( path ) ) { |
69 | | std::string msg = "can't remove file "; |
70 | | msg += path; |
71 | | throw std::runtime_error ( msg ); |
72 | | } |
73 | | } else { |
74 | | if ( errno != ENOENT ) { |
75 | | std::string msg = "can't remove "; |
76 | | msg += path; |
77 | | throw std::runtime_error ( msg ); |
78 | | } else { |
79 | | // it wasn't there in the first place |
80 | | rv = false; |
81 | | } |
82 | | } |
83 | | } |
84 | | |
85 | | return rv; |