Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/directory.h" | 5 #include "bin/directory.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 | 9 |
| 10 #include "bin/log.h" | 10 #include "bin/log.h" |
| 11 | 11 |
| 12 // Forward declaration. | 12 class PathBuffer { |
| 13 public: | |
| 14 PathBuffer() : length(0) { } | |
| 15 | |
| 16 wchar_t data[MAX_PATH + 1]; | |
| 17 int length; | |
| 18 | |
| 19 bool Add(const wchar_t* name) { | |
| 20 size_t written = _snprintf(data + length, | |
| 21 MAX_PATH - length, | |
| 22 L"%s", | |
| 23 name); | |
| 24 data[MAX_PATH] = L'\0'; | |
| 25 if (written == wcsnlen(name, MAX_PATH + 1)) { | |
| 26 length += written; | |
| 27 return true; | |
| 28 } else { | |
| 29 return false; | |
|
Søren Gjesse
2013/02/08 08:49:29
SetLastError or some other way of providing the er
Bill Hesse
2013/02/08 12:11:37
Done.
| |
| 30 } | |
| 31 } | |
| 32 | |
| 33 void Reset(int new_length) { | |
| 34 length = new_length; | |
| 35 data[length] = L'\0'; | |
| 36 } | |
| 37 }; | |
| 38 | |
| 39 | |
| 40 // Forward declarations. | |
| 13 static bool ListRecursively(const wchar_t* dir_name, | 41 static bool ListRecursively(const wchar_t* dir_name, |
| 14 bool recursive, | 42 bool recursive, |
| 15 DirectoryListing* listing); | 43 DirectoryListing* listing); |
| 16 static bool DeleteRecursively(const wchar_t* dir_name); | 44 static bool DeleteRecursively(const wchar_t* dir_name); |
| 17 | 45 |
| 18 | 46 |
| 19 static bool HandleDir(wchar_t* dir_name, | 47 static bool HandleDir(wchar_t* dir_name, |
| 20 wchar_t* path, | 48 PathBuffer* path, |
| 21 int path_length, | |
| 22 bool recursive, | 49 bool recursive, |
| 23 DirectoryListing* listing) { | 50 DirectoryListing* listing) { |
| 24 if (wcscmp(dir_name, L".") != 0 && | 51 if (wcscmp(dir_name, L".") == 0) return true; |
| 25 wcscmp(dir_name, L"..") != 0) { | 52 if (wcscmp(dir_name, L"..") == 0) return true; |
| 26 size_t written = _snwprintf(path + path_length, | 53 if (!path->Add(dir_name)) return false; |
| 27 MAX_PATH - path_length, | 54 char* utf8_path = StringUtils::WideToUtf8(path->data); |
| 28 L"%s", | 55 bool ok = listing->HandleDirectory(utf8_path); |
| 29 dir_name); | 56 free(utf8_path); |
| 30 if (written != wcslen(dir_name)) { | 57 if (!ok) return ok; |
| 31 return false; | 58 if (recursive) { |
| 32 } | 59 return ListRecursively(path->data, recursive, listing); |
| 33 char* utf8_path = StringUtils::WideToUtf8(path); | |
| 34 bool ok = listing->HandleDirectory(utf8_path); | |
| 35 free(utf8_path); | |
| 36 if (!ok) return ok; | |
| 37 if (recursive) { | |
| 38 return ListRecursively(path, recursive, listing); | |
| 39 } | |
| 40 } | 60 } |
| 41 return true; | |
| 42 } | 61 } |
| 43 | 62 |
| 44 | 63 |
| 45 static bool HandleFile(wchar_t* file_name, | 64 static bool HandleFile(wchar_t* file_name, |
| 46 wchar_t* path, | 65 PathBuffer* path, |
| 47 int path_length, | |
| 48 DirectoryListing* listing) { | 66 DirectoryListing* listing) { |
| 49 size_t written = _snwprintf(path + path_length, | 67 if (!path->Add(file_name)) { |
| 50 MAX_PATH - path_length, | |
| 51 L"%s", | |
| 52 file_name); | |
| 53 if (written != wcslen(file_name)) { | |
| 54 return false; | 68 return false; |
| 55 }; | 69 } |
| 56 char* utf8_path = StringUtils::WideToUtf8(path); | 70 char* utf8_path = StringUtils::WideToUtf8(path->data); |
| 57 bool ok = listing->HandleFile(utf8_path); | 71 bool ok = listing->HandleFile(utf8_path); |
| 58 free(utf8_path); | 72 free(utf8_path); |
| 59 return ok; | 73 return ok; |
| 60 } | 74 } |
| 61 | 75 |
| 62 | 76 |
| 63 static bool HandleEntry(LPWIN32_FIND_DATAW find_file_data, | 77 static bool HandleEntry(LPWIN32_FIND_DATAW find_file_data, |
| 64 wchar_t* path, | 78 PathBuffer* path, |
| 65 int path_length, | |
| 66 bool recursive, | 79 bool recursive, |
| 67 DirectoryListing* listing) { | 80 DirectoryListing* listing) { |
| 68 DWORD attributes = find_file_data->dwFileAttributes; | 81 DWORD attributes = find_file_data->dwFileAttributes; |
| 69 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 82 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 70 return HandleDir(find_file_data->cFileName, | 83 return HandleDir(find_file_data->cFileName, |
| 71 path, | 84 path, |
| 72 path_length, | |
| 73 recursive, | 85 recursive, |
| 74 listing); | 86 listing); |
| 75 } else { | 87 } else { |
| 76 return HandleFile(find_file_data->cFileName, path, path_length, listing); | 88 return HandleFile(find_file_data->cFileName, path, listing); |
| 77 } | 89 } |
| 78 } | 90 } |
| 79 | 91 |
| 80 | 92 |
| 81 // ComputeFullSearchPath must be called with a path array of size at | 93 static PathBuffer* ComputeFullSearchPath(const wchar_t* dir_name) { |
| 82 // least MAX_PATH. | |
| 83 static bool ComputeFullSearchPath(const wchar_t* dir_name, | |
| 84 wchar_t* path, | |
| 85 int* path_length) { | |
| 86 // GetFullPathName only works in a multi-threaded environment if | 94 // GetFullPathName only works in a multi-threaded environment if |
| 87 // SetCurrentDirectory is not used. We currently have no plan for | 95 // SetCurrentDirectory is not used. We currently have no plan for |
| 88 // exposing SetCurrentDirectory. | 96 // exposing SetCurrentDirectory. |
| 89 size_t written = GetFullPathNameW(dir_name, MAX_PATH, path, NULL); | 97 PathBuffer* path = new PathBuffer(); |
| 98 | |
| 99 size_t written = GetFullPathNameW(dir_name, MAX_PATH + 1, path->data, NULL); | |
| 90 // GetFullPathName only accepts input strings of size less than | 100 // GetFullPathName only accepts input strings of size less than |
| 91 // MAX_PATH and returns 0 to indicate failure for paths longer than | 101 // MAX_PATH and returns 0 to indicate failure for paths longer than |
| 92 // that. Therefore the path buffer is always big enough. | 102 // that. Therefore the path buffer is always big enough. |
| 93 if (written == 0 || written > MAX_PATH) { | 103 if (written == 0 || written > MAX_PATH) { |
| 94 return false; | 104 delete path; |
| 105 return NULL; | |
| 95 } | 106 } |
| 96 *path_length = written; | 107 path->length = written; |
| 97 written = _snwprintf(path + *path_length, | 108 if (path->Add(L"\\*")) { |
| 98 MAX_PATH - *path_length, | 109 return path; |
| 99 L"%s", | 110 } else { |
| 100 L"\\*"); | 111 delete path; |
| 101 if (written != 2) { | 112 return NULL; |
| 102 return false; | |
| 103 } | 113 } |
| 104 *path_length += written; | |
| 105 return true; | |
| 106 } | 114 } |
| 107 | 115 |
| 116 | |
| 108 static void PostError(DirectoryListing* listing, | 117 static void PostError(DirectoryListing* listing, |
| 109 const wchar_t* dir_name) { | 118 const wchar_t* dir_name) { |
| 110 const char* utf8_path = StringUtils::WideToUtf8(dir_name); | 119 const char* utf8_path = StringUtils::WideToUtf8(dir_name); |
| 111 listing->HandleError(utf8_path); | 120 listing->HandleError(utf8_path); |
| 112 free(const_cast<char*>(utf8_path)); | 121 free(const_cast<char*>(utf8_path)); |
| 113 } | 122 } |
| 114 | 123 |
| 115 | 124 |
| 116 static bool ListRecursively(const wchar_t* dir_name, | 125 static bool ListRecursively(const wchar_t* dir_name, |
| 117 bool recursive, | 126 bool recursive, |
| 118 DirectoryListing* listing) { | 127 DirectoryListing* listing) { |
| 119 // Compute full path for the directory currently being listed. The | 128 // Compute full path for the directory currently being listed. The |
| 120 // path buffer will be used to construct the current path in the | 129 // path buffer will be used to construct the current path in the |
| 121 // recursive traversal. path_length does not always equal | 130 // recursive traversal. path_length does not always equal |
| 122 // strlen(path) but indicates the current prefix of path that is the | 131 // strlen(path) but indicates the current prefix of path that is the |
| 123 // path of the current directory in the traversal. | 132 // path of the current directory in the traversal. |
| 124 wchar_t* path = static_cast<wchar_t*>(malloc(MAX_PATH * sizeof(wchar_t))); | 133 PathBuffer* path = ComputeFullSearchPath(dir_name, path, &path_length); |
| 125 int path_length = 0; | 134 if (path == NULL) { |
| 126 bool valid = ComputeFullSearchPath(dir_name, path, &path_length); | |
| 127 if (!valid) { | |
| 128 PostError(listing, dir_name); | 135 PostError(listing, dir_name); |
| 129 free(path); | 136 delete path; |
| 130 return false; | 137 return false; |
| 131 } | 138 } |
| 132 | 139 |
| 133 WIN32_FIND_DATAW find_file_data; | 140 WIN32_FIND_DATAW find_file_data; |
| 134 HANDLE find_handle = FindFirstFileW(path, &find_file_data); | 141 HANDLE find_handle = FindFirstFileW(path, &find_file_data); |
| 135 | 142 |
| 136 // Adjust the path by removing the '*' used for the search. | 143 // Adjust the path by removing the '*' used for the search. |
| 137 path_length -= 1; | 144 path->Reset(path->length - 1); |
| 138 path[path_length] = '\0'; | |
| 139 | 145 |
| 140 if (find_handle == INVALID_HANDLE_VALUE) { | 146 if (find_handle == INVALID_HANDLE_VALUE) { |
| 141 PostError(listing, path); | 147 PostError(listing, path); |
| 142 free(path); | 148 delete path; |
| 143 return false; | 149 return false; |
| 144 } | 150 } |
| 145 | 151 |
| 152 int path_length = path->length; | |
| 146 bool success = HandleEntry(&find_file_data, | 153 bool success = HandleEntry(&find_file_data, |
| 147 path, | 154 path, |
| 148 path_length, | |
| 149 recursive, | 155 recursive, |
| 150 listing); | 156 listing); |
| 151 | 157 |
| 152 while ((FindNextFileW(find_handle, &find_file_data) != 0)) { | 158 while ((FindNextFileW(find_handle, &find_file_data) != 0)) { |
| 159 path->Reset(path_length); // HandleEntry adds the entry name to path. | |
| 153 success = HandleEntry(&find_file_data, | 160 success = HandleEntry(&find_file_data, |
| 154 path, | 161 path, |
| 155 path_length, | |
| 156 recursive, | 162 recursive, |
| 157 listing) && success; | 163 listing) && success; |
| 158 } | 164 } |
| 159 | 165 |
| 160 if (GetLastError() != ERROR_NO_MORE_FILES) { | 166 if (GetLastError() != ERROR_NO_MORE_FILES) { |
| 161 success = false; | 167 success = false; |
| 162 PostError(listing, dir_name); | 168 PostError(listing, dir_name); |
| 163 } | 169 } |
| 164 | 170 |
| 165 if (FindClose(find_handle) == 0) { | 171 if (FindClose(find_handle) == 0) { |
| 166 success = false; | 172 success = false; |
| 167 PostError(listing, dir_name); | 173 PostError(listing, dir_name); |
| 168 } | 174 } |
| 169 free(path); | 175 delete path; |
| 170 | 176 |
| 171 return success; | 177 return success; |
| 172 } | 178 } |
| 173 | 179 |
| 174 | 180 |
| 175 static bool DeleteFile(wchar_t* file_name, | 181 static bool DeleteFile(PathBuffer* path, int path_length) { |
| 176 wchar_t* path, | 182 if (path.Add(file_name)) return false; |
| 177 int path_length) { | |
| 178 size_t written = _snwprintf(path + path_length, | |
| 179 MAX_PATH - path_length, | |
| 180 L"%s", | |
| 181 file_name); | |
| 182 if (written != wcslen(file_name)) { | |
| 183 return false; | |
| 184 } | |
| 185 | 183 |
| 186 if (DeleteFileW(path) != 0) { | 184 if (DeleteFileW(path->data) != 0) { |
| 187 return true; | 185 return true; |
| 188 } | 186 } |
| 189 | 187 |
| 190 // If we failed because the file is read-only, make it writeable and try | 188 // If we failed because the file is read-only, make it writeable and try |
| 191 // again. This mirrors Linux/Mac where a directory containing read-only files | 189 // again. This mirrors Linux/Mac where a directory containing read-only files |
| 192 // can still be recursively deleted. | 190 // can still be recursively deleted. |
| 193 if (GetLastError() == ERROR_ACCESS_DENIED) { | 191 if (GetLastError() == ERROR_ACCESS_DENIED) { |
| 194 DWORD attributes = GetFileAttributesW(path); | 192 DWORD attributes = GetFileAttributesW(path->data); |
| 195 if (attributes == INVALID_FILE_ATTRIBUTES) { | 193 if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 196 return false; | 194 return false; |
| 197 } | 195 } |
| 198 | 196 |
| 199 if ((attributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) { | 197 if ((attributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) { |
| 200 attributes &= ~FILE_ATTRIBUTE_READONLY; | 198 attributes &= ~FILE_ATTRIBUTE_READONLY; |
| 201 | 199 |
| 202 if (SetFileAttributesW(path, attributes) == 0) { | 200 if (SetFileAttributesW(path->data, attributes) == 0) { |
| 203 return false; | 201 return false; |
| 204 } | 202 } |
| 205 | 203 |
| 206 return DeleteFileW(path) != 0; | 204 return DeleteFileW(path->data) != 0; |
| 207 } | 205 } |
| 208 } | 206 } |
| 209 | 207 |
| 210 return false; | 208 return false; |
| 211 } | 209 } |
| 212 | 210 |
| 213 | 211 |
| 214 static bool DeleteDir(wchar_t* dir_name, | 212 static bool DeleteDir(PathBuffer* path, int path_length) { |
| 215 wchar_t* path, | 213 if (wcscmp(dir_name, L".") == 0) return true; |
| 216 int path_length) { | 214 if (wcscmp(dir_name, L"..") == 0) return true; |
| 217 if (wcscmp(dir_name, L".") != 0 && | 215 return path->Add(dir_name) && DeleteRecursively(path->data); |
| 218 wcscmp(dir_name, L"..") != 0) { | |
| 219 size_t written = _snwprintf(path + path_length, | |
| 220 MAX_PATH - path_length, | |
| 221 L"%s", | |
| 222 dir_name); | |
| 223 if (written != wcslen(dir_name)) { | |
| 224 return false; | |
| 225 } | |
| 226 return DeleteRecursively(path); | |
| 227 } | |
| 228 return true; | |
| 229 } | 216 } |
| 230 | 217 |
| 231 | 218 |
| 232 static bool DeleteEntry(LPWIN32_FIND_DATAW find_file_data, | 219 static bool DeleteEntry(LPWIN32_FIND_DATAW find_file_data, PathBuffer* path) { |
| 233 wchar_t* path, | |
| 234 int path_length) { | |
| 235 DWORD attributes = find_file_data->dwFileAttributes; | 220 DWORD attributes = find_file_data->dwFileAttributes; |
| 236 | 221 |
| 237 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 222 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 238 return DeleteDir(find_file_data->cFileName, path, path_length); | 223 return DeleteDir(find_file_data->cFileName, path); |
| 239 } else { | 224 } else { |
| 240 return DeleteFile(find_file_data->cFileName, path, path_length); | 225 return DeleteFile(find_file_data->cFileName, path); |
| 241 } | 226 } |
| 242 } | 227 } |
| 243 | 228 |
| 244 | 229 |
| 245 static bool DeleteRecursively(const wchar_t* dir_name) { | 230 static bool DeleteRecursively(const wchar_t* dir_name) { |
| 246 // If the directory is a junction, it's pointing to some other place in the | 231 // If the directory is a junction, it's pointing to some other place in the |
| 247 // filesystem that we do not want to recurse into. | 232 // filesystem that we do not want to recurse into. |
| 248 DWORD attributes = GetFileAttributesW(dir_name); | 233 DWORD attributes = GetFileAttributesW(dir_name); |
| 249 if ((attributes != INVALID_FILE_ATTRIBUTES) && | 234 if ((attributes != INVALID_FILE_ATTRIBUTES) && |
| 250 (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { | 235 (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { |
| 251 // Just delete the junction itself. | 236 // Just delete the junction itself. |
| 252 return RemoveDirectoryW(dir_name) != 0; | 237 return RemoveDirectoryW(dir_name) != 0; |
| 253 } | 238 } |
| 254 | 239 |
| 255 // Compute full path for the directory currently being deleted. The | 240 // Compute full path for the directory currently being deleted. The |
| 256 // path buffer will be used to construct the current path in the | 241 // path buffer will be used to construct the current path in the |
| 257 // recursive traversal. path_length does not always equal | 242 // recursive traversal. path_length does not always equal |
| 258 // strlen(path) but indicates the current prefix of path that is the | 243 // strlen(path) but indicates the current prefix of path that is the |
| 259 // path of the current directory in the traversal. | 244 // path of the current directory in the traversal. |
| 260 wchar_t* path = static_cast<wchar_t*>(malloc(MAX_PATH * sizeof(wchar_t))); | 245 PathBuffer* path = ComputeFullSearchPath(dir_name); |
| 261 int path_length = 0; | 246 if (path == NULL) return false; |
| 262 bool valid = ComputeFullSearchPath(dir_name, path, &path_length); | 247 |
| 263 if (!valid) { | 248 WIN32_FIND_DATAW find_file_data; |
| 264 free(path); | 249 HANDLE find_handle = FindFirstFileW(path->data, &find_file_data); |
| 250 | |
| 251 // Adjust the path by removing the '*' used for the search. | |
| 252 int path_length = path->length - 1; | |
| 253 path->Reset(path_length); | |
| 254 | |
| 255 if (find_handle == INVALID_HANDLE_VALUE) { | |
| 256 delete path; | |
| 265 return false; | 257 return false; |
| 266 } | 258 } |
| 267 | 259 |
| 268 WIN32_FIND_DATAW find_file_data; | 260 bool success = DeleteEntry(&find_file_data, path); |
| 269 HANDLE find_handle = FindFirstFileW(path, &find_file_data); | |
| 270 | 261 |
| 271 // Adjust the path by removing the '*' used for the search. | 262 while ((FindNextFileW(find_handle, &find_file_data) != 0) && success) { |
| 272 path_length -= 1; | 263 path->Reset(path_length); // DeleteEntry adds to the path. |
| 273 path[path_length] = '\0'; | 264 success = success && DeleteEntry(&find_file_data, path); |
| 274 | |
| 275 if (find_handle == INVALID_HANDLE_VALUE) { | |
| 276 free(path); | |
| 277 return false; | |
| 278 } | 265 } |
| 279 | 266 |
| 280 bool success = DeleteEntry(&find_file_data, path, path_length); | 267 delete path; |
| 281 | |
| 282 while ((FindNextFileW(find_handle, &find_file_data) != 0) && success) { | |
| 283 success = success && DeleteEntry(&find_file_data, path, path_length); | |
| 284 } | |
| 285 | |
| 286 free(path); | |
| 287 | 268 |
| 288 if ((GetLastError() != ERROR_NO_MORE_FILES) || | 269 if ((GetLastError() != ERROR_NO_MORE_FILES) || |
| 289 (FindClose(find_handle) == 0) || | 270 (FindClose(find_handle) == 0) || |
| 290 (RemoveDirectoryW(dir_name) == 0)) { | 271 (RemoveDirectoryW(dir_name) == 0)) { |
| 291 return false; | 272 return false; |
| 292 } | 273 } |
| 293 | 274 |
| 294 return success; | 275 return success; |
| 295 } | 276 } |
| 296 | 277 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 free(const_cast<wchar_t*>(system_name)); | 336 free(const_cast<wchar_t*>(system_name)); |
| 356 return (create_status != 0); | 337 return (create_status != 0); |
| 357 } | 338 } |
| 358 | 339 |
| 359 | 340 |
| 360 char* Directory::CreateTemp(const char* const_template) { | 341 char* Directory::CreateTemp(const char* const_template) { |
| 361 // Returns a new, unused directory name, modifying the contents of | 342 // Returns a new, unused directory name, modifying the contents of |
| 362 // dir_template. Creates this directory, with a default security | 343 // dir_template. Creates this directory, with a default security |
| 363 // descriptor inherited from its parent directory. | 344 // descriptor inherited from its parent directory. |
| 364 // The return value must be freed by the caller. | 345 // The return value must be freed by the caller. |
| 365 wchar_t* path = static_cast<wchar_t*>(malloc(MAX_PATH * sizeof(wchar_t))); | 346 PathBuffer path = new PathBuffer(); |
| 366 int path_length; | |
| 367 if (0 == strncmp(const_template, "", 1)) { | 347 if (0 == strncmp(const_template, "", 1)) { |
| 368 path_length = GetTempPathW(MAX_PATH, path); | 348 path->length = GetTempPathW(MAX_PATH, path->data); |
| 369 if (path_length == 0) { | 349 if (path->length == 0) { |
| 370 free(path); | 350 delete path; |
| 371 return NULL; | 351 return NULL; |
| 372 } | 352 } |
| 373 } else { | 353 } else { |
| 374 const wchar_t* system_template = StringUtils::Utf8ToWide(const_template); | 354 const wchar_t* system_template = StringUtils::Utf8ToWide(const_template); |
| 375 _snwprintf(path, MAX_PATH, L"%s", system_template); | 355 path->Add(system_template); |
| 376 free(const_cast<wchar_t*>(system_template)); | 356 free(const_cast<wchar_t*>(system_template)); |
| 377 path_length = wcslen(path); | |
| 378 } | 357 } |
| 379 // Length of tempdir-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 44. | 358 // Length of tempdir-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 44. |
| 380 if (path_length > MAX_PATH - 44) { | 359 if (path->length > MAX_PATH - 44) { |
| 381 free(path); | 360 delete path; |
| 382 return NULL; | 361 return NULL; |
| 383 } | 362 } |
| 384 if ((path)[path_length - 1] == L'\\') { | 363 if ((path->data)[path->length - 1] == L'\\') { |
| 385 // No base name for the directory - use "tempdir". | 364 // No base name for the directory - use "tempdir". |
| 386 _snwprintf(path + path_length, MAX_PATH - path_length, L"tempdir"); | 365 path->Add(L"tempdir"); |
| 387 path_length = wcslen(path); | |
| 388 } | 366 } |
| 389 | 367 |
| 390 UUID uuid; | 368 UUID uuid; |
| 391 RPC_STATUS status = UuidCreateSequential(&uuid); | 369 RPC_STATUS status = UuidCreateSequential(&uuid); |
| 392 if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) { | 370 if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) { |
| 393 free(path); | 371 delete path; |
| 394 return NULL; | 372 return NULL; |
| 395 } | 373 } |
| 396 RPC_WSTR uuid_string; | 374 RPC_WSTR uuid_string; |
| 397 status = UuidToStringW(&uuid, &uuid_string); | 375 status = UuidToStringW(&uuid, &uuid_string); |
| 398 if (status != RPC_S_OK) { | 376 if (status != RPC_S_OK) { |
| 399 free(path); | 377 delete path; |
| 400 return NULL; | 378 return NULL; |
| 401 } | 379 } |
| 402 | 380 |
| 403 _snwprintf(path + path_length, MAX_PATH - path_length, L"-%s", uuid_string); | 381 path->Add(L"-"); |
| 382 path->Add(uuid_string); | |
| 404 RpcStringFreeW(&uuid_string); | 383 RpcStringFreeW(&uuid_string); |
| 405 if (!CreateDirectoryW(path, NULL)) { | 384 if (!CreateDirectoryW(path->data, NULL)) { |
| 406 free(path); | 385 delete path; |
| 407 return NULL; | 386 return NULL; |
| 408 } | 387 } |
| 409 char* result = StringUtils::WideToUtf8(path); | 388 char* result = StringUtils::WideToUtf8(path->data); |
| 410 free(path); | 389 delete path; |
| 411 return result; | 390 return result; |
| 412 } | 391 } |
| 413 | 392 |
| 414 | 393 |
| 415 bool Directory::Delete(const char* dir_name, bool recursive) { | 394 bool Directory::Delete(const char* dir_name, bool recursive) { |
| 416 bool result = false; | 395 bool result = false; |
| 417 const wchar_t* system_dir_name = StringUtils::Utf8ToWide(dir_name); | 396 const wchar_t* system_dir_name = StringUtils::Utf8ToWide(dir_name); |
| 418 if (!recursive) { | 397 if (!recursive) { |
| 419 result = (RemoveDirectoryW(system_dir_name) != 0); | 398 result = (RemoveDirectoryW(system_dir_name) != 0); |
| 420 } else { | 399 } else { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 438 bool success = DeleteRecursively(system_new_path); | 417 bool success = DeleteRecursively(system_new_path); |
| 439 if (!success) return false; | 418 if (!success) return false; |
| 440 } | 419 } |
| 441 DWORD flags = MOVEFILE_WRITE_THROUGH; | 420 DWORD flags = MOVEFILE_WRITE_THROUGH; |
| 442 int move_status = | 421 int move_status = |
| 443 MoveFileExW(system_path, system_new_path, flags); | 422 MoveFileExW(system_path, system_new_path, flags); |
| 444 free(const_cast<wchar_t*>(system_path)); | 423 free(const_cast<wchar_t*>(system_path)); |
| 445 free(const_cast<wchar_t*>(system_new_path)); | 424 free(const_cast<wchar_t*>(system_new_path)); |
| 446 return (move_status != 0); | 425 return (move_status != 0); |
| 447 } | 426 } |
| OLD | NEW |