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

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: Fix Windows test 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 ASSERT(end == -1 || end > start);
112 HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(handle_->fd()));
113 OVERLAPPED overlapped;
114 ZeroMemory(&overlapped, sizeof(OVERLAPPED));
115
116 overlapped.Offset = Utils::Low32Bits(start);
117 overlapped.OffsetHigh = Utils::High32Bits(start);
118
119 int64_t length = end == -1 ? 0 : end - start;
120 if (length == 0) length = kMaxInt64;
121 int32_t length_low = Utils::Low32Bits(length);
122 int32_t length_high = Utils::High32Bits(length);
123
124
125 BOOL rc;
126 switch (lock) {
127 case File::kLockUnlock:
128 rc = UnlockFileEx(handle, 0, length_low, length_high, &overlapped);
129 break;
130 case File::kLockShared:
131 case File::kLockExclusive: {
132 DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
133 if (lock == File::kLockExclusive) {
134 flags |= LOCKFILE_EXCLUSIVE_LOCK;
135 }
136 rc = LockFileEx(handle, flags, 0,
137 length_low, length_high, &overlapped);
138 break;
139 }
140 default:
141 UNREACHABLE();
142 }
143 return rc;
144 }
145
146
108 int64_t File::Length() { 147 int64_t File::Length() {
109 ASSERT(handle_->fd() >= 0); 148 ASSERT(handle_->fd() >= 0);
110 struct __stat64 st; 149 struct __stat64 st;
111 if (_fstat64(handle_->fd(), &st) == 0) { 150 if (_fstat64(handle_->fd(), &st) == 0) {
112 return st.st_size; 151 return st.st_size;
113 } 152 }
114 return -1; 153 return -1;
115 } 154 }
116 155
117 156
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 SetLastError(ERROR_NOT_A_REPARSE_POINT); 469 SetLastError(ERROR_NOT_A_REPARSE_POINT);
431 return NULL; 470 return NULL;
432 } 471 }
433 472
434 target_offset /= sizeof(wchar_t); // Offset and length are in bytes. 473 target_offset /= sizeof(wchar_t); // Offset and length are in bytes.
435 target_length /= sizeof(wchar_t); 474 target_length /= sizeof(wchar_t);
436 target += target_offset; 475 target += target_offset;
437 // Remove "\??\" from beginning of target. 476 // Remove "\??\" from beginning of target.
438 if (target_length > 4 && wcsncmp(L"\\??\\", target, 4) == 0) { 477 if (target_length > 4 && wcsncmp(L"\\??\\", target, 4) == 0) {
439 target += 4; 478 target += 4;
440 target_length -=4; 479 target_length -= 4;
441 } 480 }
442 int utf8_length = WideCharToMultiByte(CP_UTF8, 481 int utf8_length = WideCharToMultiByte(CP_UTF8,
443 0, 482 0,
444 target, 483 target,
445 target_length, 484 target_length,
446 NULL, 485 NULL,
447 0, 486 0,
448 NULL, 487 NULL,
449 NULL); 488 NULL);
450 char* utf8_target = reinterpret_cast<char*>(malloc(utf8_length + 1)); 489 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; 684 return kIdentical;
646 } else { 685 } else {
647 return kDifferent; 686 return kDifferent;
648 } 687 }
649 } 688 }
650 689
651 } // namespace bin 690 } // namespace bin
652 } // namespace dart 691 } // namespace dart
653 692
654 #endif // defined(TARGET_OS_WINDOWS) 693 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW
« no previous file with comments | « runtime/bin/file_patch.dart ('k') | runtime/bin/io_service.h » ('j') | sdk/lib/io/file.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698