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

Side by Side Diff: third_party/ijar/mapped_file_windows.cc

Issue 1901473003: 🐞 Update version of third_party/ijar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 | « third_party/ijar/mapped_file_unix.cc ('k') | third_party/ijar/zip.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Bazel Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdio.h>
16 #include <windows.h>
17 #include <sys/cygwin.h>
18
19 #include "third_party/ijar/mapped_file.h"
20
21 #define MAX_ERROR 2048
22
23 namespace devtools_ijar {
24
25 static char errmsg[MAX_ERROR] = "";
26
27 void PrintLastError(const char* op) {
28 char *message;
29 DWORD err = GetLastError();
30 FormatMessage(
31 FORMAT_MESSAGE_ALLOCATE_BUFFER
32 | FORMAT_MESSAGE_FROM_SYSTEM
33 | FORMAT_MESSAGE_IGNORE_INSERTS,
34 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
35 reinterpret_cast<char *>(&message),
36 0, NULL);
37 snprintf(errmsg, MAX_ERROR, "%s: %s", op, message);
38 LocalFree(message);
39 }
40
41 char* ToUnicodePath(const char* path) {
42 // Add \\?\ as prefix to enable unicode path which allows path length longer
43 // than 260
44 int length = strlen(path) + 5;
45 char* unicode_path = reinterpret_cast<char*>(malloc(length));
46 snprintf(unicode_path, length, "\\\\?\\%s", path);
47 return unicode_path;
48 }
49
50 struct MappedInputFileImpl {
51 HANDLE file_;
52 HANDLE mapping_;
53
54 MappedInputFileImpl(HANDLE file, HANDLE mapping) {
55 file_ = file;
56 mapping_ = mapping;
57 }
58 };
59
60 MappedInputFile::MappedInputFile(const char* name) {
61 impl_ = NULL;
62 opened_ = false;
63 errmsg_ = errmsg;
64
65 char* path = reinterpret_cast<char*>(
66 cygwin_create_path(CCP_POSIX_TO_WIN_A, name));
67 char* unicode_path = ToUnicodePath(path);
68 free(path);
69 HANDLE file = CreateFile(unicode_path, GENERIC_READ, FILE_SHARE_READ, NULL,
70 OPEN_EXISTING, 0, NULL);
71 free(unicode_path);
72 if (file == INVALID_HANDLE_VALUE) {
73 PrintLastError("CreateFile()");
74 return;
75 }
76
77 LARGE_INTEGER size;
78 if (!GetFileSizeEx(file, &size)) {
79 PrintLastError("GetFileSizeEx()");
80 CloseHandle(file);
81 return;
82 }
83
84 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY,
85 size.HighPart, size.LowPart, NULL);
86 if (mapping == NULL) {
87 PrintLastError("CreateFileMapping()");
88 CloseHandle(file);
89 return;
90 }
91
92 void *view = MapViewOfFileEx(mapping, FILE_MAP_READ, 0, 0, 0, NULL);
93 if (view == NULL) {
94 PrintLastError("MapViewOfFileEx()");
95 CloseHandle(mapping);
96 CloseHandle(file);
97 return;
98 }
99
100 impl_ = new MappedInputFileImpl(file, mapping);
101 length_ = size.QuadPart;
102 buffer_ = reinterpret_cast<u1*>(view);
103 opened_ = true;
104 }
105
106 MappedInputFile::~MappedInputFile() {
107 delete impl_;
108 }
109
110 void MappedInputFile::Discard(size_t bytes) {
111 // This is not supported on Windows for now. I'm not sure if we can unmap
112 // parts of an existing view and that this is necessary for Windows at all.
113 // At any rate, this only matters for >2GB (or maybe >4GB?) input files.
114 }
115
116 int MappedInputFile::Close() {
117 if (!UnmapViewOfFile(buffer_)) {
118 PrintLastError("UnmapViewOfFile()");
119 return -1;
120 }
121
122 if (!CloseHandle(impl_->mapping_)) {
123 PrintLastError("CloseHandle(mapping)");
124 return -1;
125 }
126
127 if (!CloseHandle(impl_->file_)) {
128 PrintLastError("CloseHandle(file)");
129 return -1;
130 }
131
132 return 0;
133 }
134
135 struct MappedOutputFileImpl {
136 HANDLE file_;
137 HANDLE mapping_;
138
139 MappedOutputFileImpl(HANDLE file, HANDLE mapping) {
140 file_ = file;
141 mapping_ = mapping;
142 }
143 };
144
145 MappedOutputFile::MappedOutputFile(const char* name, u8 estimated_size) {
146 impl_ = NULL;
147 opened_ = false;
148 errmsg_ = errmsg;
149
150 char* path = reinterpret_cast<char*>(
151 cygwin_create_path(CCP_POSIX_TO_WIN_A, name));
152 char* unicode_path = ToUnicodePath(path);
153 free(path);
154 HANDLE file = CreateFile(unicode_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
155 CREATE_ALWAYS, 0, NULL);
156 free(unicode_path);
157 if (file == INVALID_HANDLE_VALUE) {
158 PrintLastError("CreateFile()");
159 return;
160 }
161
162 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READWRITE,
163 estimated_size >> 32, estimated_size & 0xffffffffUL, NULL);
164 if (mapping == NULL) {
165 PrintLastError("CreateFileMapping()");
166 CloseHandle(file);
167 return;
168 }
169
170 void *view = MapViewOfFileEx(mapping, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
171 if (view == NULL) {
172 PrintLastError("MapViewOfFileEx()");
173 CloseHandle(mapping);
174 CloseHandle(file);
175 return;
176 }
177
178 impl_ = new MappedOutputFileImpl(file, mapping);
179 buffer_ = reinterpret_cast<u1*>(view);
180 opened_ = true;
181 }
182
183 MappedOutputFile::~MappedOutputFile() {
184 delete impl_;
185 }
186
187 int MappedOutputFile::Close(int size) {
188 if (!UnmapViewOfFile(buffer_)) {
189 PrintLastError("UnmapViewOfFile()");
190 return -1;
191 }
192
193 if (!CloseHandle(impl_->mapping_)) {
194 PrintLastError("CloseHandle(mapping)");
195 return -1;
196 }
197
198 if (!SetFilePointer(impl_->file_, size, NULL, FILE_BEGIN)) {
199 PrintLastError("SetFilePointer()");
200 return -1;
201 }
202
203 if (!SetEndOfFile(impl_->file_)) {
204 PrintLastError("SetEndOfFile()");
205 return -1;
206 }
207
208 if (!CloseHandle(impl_->file_)) {
209 PrintLastError("CloseHandle(file)");
210 return -1;
211 }
212
213 return 0;
214 }
215
216 } // namespace devtools_ijar
OLDNEW
« no previous file with comments | « third_party/ijar/mapped_file_unix.cc ('k') | third_party/ijar/zip.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698