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