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

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

Issue 833623004: Add support for file locking (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Comment/doc fixes Created 5 years, 11 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 | Annotate | Revision Log
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(TARGET_OS_WINDOWS) 6 #if defined(TARGET_OS_WINDOWS)
7 7
8 #include "bin/file.h" 8 #include "bin/file.h"
9 9
10 #include <fcntl.h> // NOLINT 10 #include <fcntl.h> // NOLINT
11 #include <io.h> // NOLINT 11 #include <io.h> // NOLINT
12 #include <stdio.h> // NOLINT 12 #include <stdio.h> // NOLINT
13 #include <string.h> // NOLINT 13 #include <string.h> // NOLINT
14 #include <sys/stat.h> // NOLINT 14 #include <sys/stat.h> // NOLINT
15 #include <WinIoCtl.h> // NOLINT 15 #include <WinIoCtl.h> // NOLINT
16 16
17 #include "bin/builtin.h" 17 #include "bin/builtin.h"
18 #include "bin/log.h" 18 #include "bin/log.h"
19 #include "bin/utils.h" 19 #include "bin/utils.h"
20 #include "platform/utils.h"
20 21
21 22
22 namespace dart { 23 namespace dart {
23 namespace bin { 24 namespace bin {
24 25
25 class FileHandle { 26 class FileHandle {
26 public: 27 public:
27 explicit FileHandle(int fd) : fd_(fd) { } 28 explicit FileHandle(int fd) : fd_(fd) { }
28 ~FileHandle() { } 29 ~FileHandle() { }
29 int fd() const { return fd_; } 30 int fd() const { return fd_; }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 return _chsize_s(handle_->fd(), length) == 0; 99 return _chsize_s(handle_->fd(), length) == 0;
99 } 100 }
100 101
101 102
102 bool File::Flush() { 103 bool File::Flush() {
103 ASSERT(handle_->fd()); 104 ASSERT(handle_->fd());
104 return _commit(handle_->fd()) != -1; 105 return _commit(handle_->fd()) != -1;
105 } 106 }
106 107
107 108
109 bool File::Lock(File::LockType lock, int64_t start, int64_t end) {
110 ASSERT(handle_->fd() >= 0);
111 HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(handle_->fd()));
112 OVERLAPPED overlapped;
113 ZeroMemory(&overlapped, sizeof(OVERLAPPED));
114
115 overlapped.Offset = Utils::Low32Bits(start);
116 overlapped.OffsetHigh = Utils::High32Bits(start);
117
118 int64_t length = end == -1 ? 0 : end - start;
119 if (length == 0) length = kMaxInt64;
120 int32_t length_low = Utils::Low32Bits(length);
121 int32_t length_high = Utils::High32Bits(length);
122
123
124 BOOL rc;
125 switch (lock) {
126 case File::kLockUnlock:
127 rc = UnlockFileEx(handle, 0, length_low, length_high, &overlapped);
128 break;
129 case File::kLockShared:
130 rc = LockFileEx(handle,
131 LOCKFILE_FAIL_IMMEDIATELY,
132 0, length_low, length_high, &overlapped);
133 break;
134 case File::kLockExclusive:
135 rc = LockFileEx(handle,
136 LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK,
137 0, length_low, length_high, &overlapped);
138 break;
139 default:
Lasse Reichstein Nielsen 2015/01/08 11:59:22 Could these two cases be combined - they only diff
Søren Gjesse 2015/01/09 13:06:20 Done.
140 return false;
kustermann 2015/01/08 12:54:27 This should be UNREACHABLE()
Søren Gjesse 2015/01/09 13:06:20 Done.
141 }
kustermann 2015/01/08 12:54:27 Would it make sense to use GetLastError() if rc ==
Søren Gjesse 2015/01/09 13:06:20 When this results in an OS error in Dart the error
142 return rc;
143 }
144
145
108 int64_t File::Length() { 146 int64_t File::Length() {
109 ASSERT(handle_->fd() >= 0); 147 ASSERT(handle_->fd() >= 0);
110 struct __stat64 st; 148 struct __stat64 st;
111 if (_fstat64(handle_->fd(), &st) == 0) { 149 if (_fstat64(handle_->fd(), &st) == 0) {
112 return st.st_size; 150 return st.st_size;
113 } 151 }
114 return -1; 152 return -1;
115 } 153 }
116 154
117 155
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 SetLastError(ERROR_NOT_A_REPARSE_POINT); 468 SetLastError(ERROR_NOT_A_REPARSE_POINT);
431 return NULL; 469 return NULL;
432 } 470 }
433 471
434 target_offset /= sizeof(wchar_t); // Offset and length are in bytes. 472 target_offset /= sizeof(wchar_t); // Offset and length are in bytes.
435 target_length /= sizeof(wchar_t); 473 target_length /= sizeof(wchar_t);
436 target += target_offset; 474 target += target_offset;
437 // Remove "\??\" from beginning of target. 475 // Remove "\??\" from beginning of target.
438 if (target_length > 4 && wcsncmp(L"\\??\\", target, 4) == 0) { 476 if (target_length > 4 && wcsncmp(L"\\??\\", target, 4) == 0) {
439 target += 4; 477 target += 4;
440 target_length -=4; 478 target_length -= 4;
441 } 479 }
442 int utf8_length = WideCharToMultiByte(CP_UTF8, 480 int utf8_length = WideCharToMultiByte(CP_UTF8,
443 0, 481 0,
444 target, 482 target,
445 target_length, 483 target_length,
446 NULL, 484 NULL,
447 0, 485 0,
448 NULL, 486 NULL,
449 NULL); 487 NULL);
450 char* utf8_target = reinterpret_cast<char*>(malloc(utf8_length + 1)); 488 char* utf8_target = reinterpret_cast<char*>(malloc(utf8_length + 1));
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 return kIdentical; 683 return kIdentical;
646 } else { 684 } else {
647 return kDifferent; 685 return kDifferent;
648 } 686 }
649 } 687 }
650 688
651 } // namespace bin 689 } // namespace bin
652 } // namespace dart 690 } // namespace dart
653 691
654 #endif // defined(TARGET_OS_WINDOWS) 692 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698