Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: runtime/bin/directory_win.cc

Issue 3001963002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Fuchsia fix Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/directory_test.cc ('k') | runtime/bin/file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(HOST_OS_WINDOWS) 6 #if defined(HOST_OS_WINDOWS)
7 7
8 #include "bin/directory.h" 8 #include "bin/directory.h"
9 #include "bin/file.h"
10 #include "bin/utils.h"
11 #include "bin/utils_win.h"
12 9
13 #include <errno.h> // NOLINT 10 #include <errno.h> // NOLINT
14 #include <sys/stat.h> // NOLINT 11 #include <sys/stat.h> // NOLINT
15 12
16 #include "bin/dartutils.h" 13 #include "bin/dartutils.h"
14 #include "bin/file.h"
17 #include "bin/log.h" 15 #include "bin/log.h"
16 #include "bin/namespace.h"
17 #include "bin/utils.h"
18 #include "bin/utils_win.h"
18 19
19 #undef DeleteFile 20 #undef DeleteFile
20 21
21 #define MAX_LONG_PATH 32767 22 #define MAX_LONG_PATH 32767
22 23
23 namespace dart { 24 namespace dart {
24 namespace bin { 25 namespace bin {
25 26
26 PathBuffer::PathBuffer() : length_(0) { 27 PathBuffer::PathBuffer() : length_(0) {
27 data_ = calloc(MAX_LONG_PATH + 1, sizeof(wchar_t)); // NOLINT 28 data_ = calloc(MAX_LONG_PATH + 1, sizeof(wchar_t)); // NOLINT
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 // reasons such as lack of permissions. In that case we do 338 // reasons such as lack of permissions. In that case we do
338 // not know if the directory exists. 339 // not know if the directory exists.
339 return Directory::UNKNOWN; 340 return Directory::UNKNOWN;
340 } 341 }
341 } 342 }
342 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; 343 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
343 exists = exists && !IsBrokenLink(dir_name); 344 exists = exists && !IsBrokenLink(dir_name);
344 return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST; 345 return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST;
345 } 346 }
346 347
347 Directory::ExistsResult Directory::Exists(const char* dir_name) { 348 Directory::ExistsResult Directory::Exists(Namespace* namespc, const char* dir_na me) {
348 Utf8ToWideScope system_name(dir_name); 349 Utf8ToWideScope system_name(dir_name);
349 return ExistsHelper(system_name.wide()); 350 return ExistsHelper(system_name.wide());
350 } 351 }
351 352
352 char* Directory::CurrentNoScope() { 353 char* Directory::CurrentNoScope() {
353 int length = GetCurrentDirectoryW(0, NULL); 354 int length = GetCurrentDirectoryW(0, NULL);
354 if (length == 0) { 355 if (length == 0) {
355 return NULL; 356 return NULL;
356 } 357 }
357 wchar_t* current = new wchar_t[length + 1]; 358 wchar_t* current = new wchar_t[length + 1];
358 GetCurrentDirectoryW(length + 1, current); 359 GetCurrentDirectoryW(length + 1, current);
359 int utf8_len = 360 int utf8_len =
360 WideCharToMultiByte(CP_UTF8, 0, current, -1, NULL, 0, NULL, NULL); 361 WideCharToMultiByte(CP_UTF8, 0, current, -1, NULL, 0, NULL, NULL);
361 char* result = reinterpret_cast<char*>(malloc(utf8_len)); 362 char* result = reinterpret_cast<char*>(malloc(utf8_len));
362 WideCharToMultiByte(CP_UTF8, 0, current, -1, result, utf8_len, NULL, NULL); 363 WideCharToMultiByte(CP_UTF8, 0, current, -1, result, utf8_len, NULL, NULL);
363 delete[] current; 364 delete[] current;
364 return result; 365 return result;
365 } 366 }
366 367
367 const char* Directory::Current() { 368 bool Directory::Create(Namespace* namespc, const char* dir_name) {
368 int length = GetCurrentDirectoryW(0, NULL);
369 if (length == 0) {
370 return NULL;
371 }
372 wchar_t* current;
373 current = reinterpret_cast<wchar_t*>(
374 Dart_ScopeAllocate((length + 1) * sizeof(*current)));
375 GetCurrentDirectoryW(length + 1, current);
376 return StringUtilsWin::WideToUtf8(current);
377 }
378
379 bool Directory::SetCurrent(const char* path) {
380 Utf8ToWideScope system_path(path);
381 bool result = SetCurrentDirectoryW(system_path.wide()) != 0;
382 return result;
383 }
384
385 bool Directory::Create(const char* dir_name) {
386 Utf8ToWideScope system_name(dir_name); 369 Utf8ToWideScope system_name(dir_name);
387 int create_status = CreateDirectoryW(system_name.wide(), NULL); 370 int create_status = CreateDirectoryW(system_name.wide(), NULL);
388 // If the directory already existed, treat it as a success. 371 // If the directory already existed, treat it as a success.
389 if ((create_status == 0) && (GetLastError() == ERROR_ALREADY_EXISTS) && 372 if ((create_status == 0) && (GetLastError() == ERROR_ALREADY_EXISTS) &&
390 (ExistsHelper(system_name.wide()) == EXISTS)) { 373 (ExistsHelper(system_name.wide()) == EXISTS)) {
391 return true; 374 return true;
392 } 375 }
393 return (create_status != 0); 376 return (create_status != 0);
394 } 377 }
395 378
396 const char* Directory::SystemTemp() { 379 const char* Directory::SystemTemp(Namespace* namespc) {
397 PathBuffer path; 380 PathBuffer path;
398 // Remove \ at end. 381 // Remove \ at end.
399 path.Reset(GetTempPathW(MAX_LONG_PATH, path.AsStringW()) - 1); 382 path.Reset(GetTempPathW(MAX_LONG_PATH, path.AsStringW()) - 1);
400 return path.AsScopedString(); 383 return path.AsScopedString();
401 } 384 }
402 385
403 const char* Directory::CreateTemp(const char* prefix) { 386 const char* Directory::CreateTemp(Namespace* namespc, const char* prefix) {
404 // Returns a new, unused directory name, adding characters to the 387 // Returns a new, unused directory name, adding characters to the
405 // end of prefix. 388 // end of prefix.
406 // Creates this directory, with a default security 389 // Creates this directory, with a default security
407 // descriptor inherited from its parent directory. 390 // descriptor inherited from its parent directory.
408 // The return value is Dart_ScopeAllocated. 391 // The return value is Dart_ScopeAllocated.
409 PathBuffer path; 392 PathBuffer path;
410 Utf8ToWideScope system_prefix(prefix); 393 Utf8ToWideScope system_prefix(prefix);
411 if (!path.AddW(system_prefix.wide())) { 394 if (!path.AddW(system_prefix.wide())) {
412 return NULL; 395 return NULL;
413 } 396 }
(...skipping 18 matching lines...) Expand all
432 if (!path.AddW(reinterpret_cast<wchar_t*>(uuid_string))) { 415 if (!path.AddW(reinterpret_cast<wchar_t*>(uuid_string))) {
433 return NULL; 416 return NULL;
434 } 417 }
435 RpcStringFreeW(&uuid_string); 418 RpcStringFreeW(&uuid_string);
436 if (!CreateDirectoryW(path.AsStringW(), NULL)) { 419 if (!CreateDirectoryW(path.AsStringW(), NULL)) {
437 return NULL; 420 return NULL;
438 } 421 }
439 return path.AsScopedString(); 422 return path.AsScopedString();
440 } 423 }
441 424
442 bool Directory::Delete(const char* dir_name, bool recursive) { 425 bool Directory::Delete(Namespace* namespc, const char* dir_name, bool recursive) {
443 bool result = false; 426 bool result = false;
444 Utf8ToWideScope system_dir_name(dir_name); 427 Utf8ToWideScope system_dir_name(dir_name);
445 if (!recursive) { 428 if (!recursive) {
446 if (File::GetType(dir_name, true) == File::kIsDirectory) { 429 if (File::GetType(namespc, dir_name, true) == File::kIsDirectory) {
447 result = (RemoveDirectoryW(system_dir_name.wide()) != 0); 430 result = (RemoveDirectoryW(system_dir_name.wide()) != 0);
448 } else { 431 } else {
449 SetLastError(ERROR_FILE_NOT_FOUND); 432 SetLastError(ERROR_FILE_NOT_FOUND);
450 } 433 }
451 } else { 434 } else {
452 PathBuffer path; 435 PathBuffer path;
453 if (path.AddW(system_dir_name.wide())) { 436 if (path.AddW(system_dir_name.wide())) {
454 result = DeleteRecursively(&path); 437 result = DeleteRecursively(&path);
455 } 438 }
456 } 439 }
457 return result; 440 return result;
458 } 441 }
459 442
460 bool Directory::Rename(const char* path, const char* new_path) { 443 bool Directory::Rename(Namespace* namespc, const char* path, const char* new_pat h) {
461 Utf8ToWideScope system_path(path); 444 Utf8ToWideScope system_path(path);
462 Utf8ToWideScope system_new_path(new_path); 445 Utf8ToWideScope system_new_path(new_path);
463 ExistsResult exists = ExistsHelper(system_path.wide()); 446 ExistsResult exists = ExistsHelper(system_path.wide());
464 if (exists != EXISTS) { 447 if (exists != EXISTS) {
465 return false; 448 return false;
466 } 449 }
467 ExistsResult new_exists = ExistsHelper(system_new_path.wide()); 450 ExistsResult new_exists = ExistsHelper(system_new_path.wide());
468 // MoveFile does not allow replacing existing directories. Therefore, 451 // MoveFile does not allow replacing existing directories. Therefore,
469 // if the new_path is currently a directory we need to delete it 452 // if the new_path is currently a directory we need to delete it
470 // first. 453 // first.
471 if (new_exists == EXISTS) { 454 if (new_exists == EXISTS) {
472 bool success = Delete(new_path, true); 455 bool success = Delete(namespc, new_path, true);
473 if (!success) { 456 if (!success) {
474 return false; 457 return false;
475 } 458 }
476 } 459 }
477 DWORD flags = MOVEFILE_WRITE_THROUGH; 460 DWORD flags = MOVEFILE_WRITE_THROUGH;
478 int move_status = 461 int move_status =
479 MoveFileExW(system_path.wide(), system_new_path.wide(), flags); 462 MoveFileExW(system_path.wide(), system_new_path.wide(), flags);
480 return (move_status != 0); 463 return (move_status != 0);
481 } 464 }
482 465
483 } // namespace bin 466 } // namespace bin
484 } // namespace dart 467 } // namespace dart
485 468
486 #endif // defined(HOST_OS_WINDOWS) 469 #endif // defined(HOST_OS_WINDOWS)
OLDNEW
« no previous file with comments | « runtime/bin/directory_test.cc ('k') | runtime/bin/file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698