OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file.h" | 5 #include "base/files/file.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 Time::FromTimeT(last_accessed_sec) + | 159 Time::FromTimeT(last_accessed_sec) + |
160 TimeDelta::FromMicroseconds(last_accessed_nsec / | 160 TimeDelta::FromMicroseconds(last_accessed_nsec / |
161 Time::kNanosecondsPerMicrosecond); | 161 Time::kNanosecondsPerMicrosecond); |
162 | 162 |
163 creation_time = | 163 creation_time = |
164 Time::FromTimeT(creation_time_sec) + | 164 Time::FromTimeT(creation_time_sec) + |
165 TimeDelta::FromMicroseconds(creation_time_nsec / | 165 TimeDelta::FromMicroseconds(creation_time_nsec / |
166 Time::kNanosecondsPerMicrosecond); | 166 Time::kNanosecondsPerMicrosecond); |
167 } | 167 } |
168 | 168 |
169 // Define empty hooks for blacklisting file descriptors used in base::File. | |
170 // These functions should be declared 'weak', i.e. the functions declared in | |
171 // a default way would have precedence over the weak ones at link time. This | |
172 // works for both static and dynamic linking. | |
173 // | |
174 // With compilers other than GCC define strong no-op symbols for simplicity. | |
Fabrice (no longer in Chrome)
2014/11/04 15:37:21
Nit: other than GCC and Clang (so msvc, essentiall
pasko
2014/11/04 17:48:05
Thanks, I did not realize COMPILER_GCC is defined
| |
175 #if defined(COMPILER_GCC) | |
176 #define ATTRIBUTE_WEAK __attribute__ ((weak)) | |
177 #else | |
178 #define ATTRIBUTE_WEAK | |
179 #endif | |
180 BASE_EXPORT void ProtectFileDescriptor(int fd) ATTRIBUTE_WEAK; | |
181 BASE_EXPORT void UnprotectFileDescriptor(int fd) ATTRIBUTE_WEAK; | |
182 #undef ATTRIBUTE_WEAK | |
183 | |
184 void ProtectFileDescriptor(int fd) { | |
185 } | |
186 | |
187 void UnprotectFileDescriptor(int fd) { | |
188 } | |
189 | |
169 // NaCl doesn't implement system calls to open files directly. | 190 // NaCl doesn't implement system calls to open files directly. |
170 #if !defined(OS_NACL) | 191 #if !defined(OS_NACL) |
171 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? | 192 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
172 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { | 193 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { |
173 base::ThreadRestrictions::AssertIOAllowed(); | 194 base::ThreadRestrictions::AssertIOAllowed(); |
174 DCHECK(!IsValid()); | 195 DCHECK(!IsValid()); |
175 | 196 |
176 int open_flags = 0; | 197 int open_flags = 0; |
177 if (flags & FLAG_CREATE) | 198 if (flags & FLAG_CREATE) |
178 open_flags = O_CREAT | O_EXCL; | 199 open_flags = O_CREAT | O_EXCL; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 | 266 |
246 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | 267 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
247 created_ = true; | 268 created_ = true; |
248 | 269 |
249 if (flags & FLAG_DELETE_ON_CLOSE) | 270 if (flags & FLAG_DELETE_ON_CLOSE) |
250 unlink(name.value().c_str()); | 271 unlink(name.value().c_str()); |
251 | 272 |
252 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | 273 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
253 error_details_ = FILE_OK; | 274 error_details_ = FILE_OK; |
254 file_.reset(descriptor); | 275 file_.reset(descriptor); |
276 ProtectFileDescriptor(descriptor); | |
255 } | 277 } |
256 #endif // !defined(OS_NACL) | 278 #endif // !defined(OS_NACL) |
257 | 279 |
258 bool File::IsValid() const { | 280 bool File::IsValid() const { |
259 return file_.is_valid(); | 281 return file_.is_valid(); |
260 } | 282 } |
261 | 283 |
262 PlatformFile File::GetPlatformFile() const { | 284 PlatformFile File::GetPlatformFile() const { |
263 return file_.get(); | 285 return file_.get(); |
264 } | 286 } |
265 | 287 |
266 PlatformFile File::TakePlatformFile() { | 288 PlatformFile File::TakePlatformFile() { |
289 UnprotectFileDescriptor(GetPlatformFile()); | |
267 return file_.release(); | 290 return file_.release(); |
268 } | 291 } |
269 | 292 |
270 void File::Close() { | 293 void File::Close() { |
271 if (!IsValid()) | 294 if (!IsValid()) |
272 return; | 295 return; |
273 | 296 |
274 base::ThreadRestrictions::AssertIOAllowed(); | 297 base::ThreadRestrictions::AssertIOAllowed(); |
298 UnprotectFileDescriptor(GetPlatformFile()); | |
275 file_.reset(); | 299 file_.reset(); |
276 } | 300 } |
277 | 301 |
278 int64 File::Seek(Whence whence, int64 offset) { | 302 int64 File::Seek(Whence whence, int64 offset) { |
279 base::ThreadRestrictions::AssertIOAllowed(); | 303 base::ThreadRestrictions::AssertIOAllowed(); |
280 DCHECK(IsValid()); | 304 DCHECK(IsValid()); |
281 | 305 |
282 #if defined(OS_ANDROID) | 306 #if defined(OS_ANDROID) |
283 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); | 307 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); |
284 return lseek64(file_.get(), static_cast<off64_t>(offset), | 308 return lseek64(file_.get(), static_cast<off64_t>(offset), |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 default: | 501 default: |
478 #if !defined(OS_NACL) // NaCl build has no metrics code. | 502 #if !defined(OS_NACL) // NaCl build has no metrics code. |
479 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", | 503 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
480 saved_errno); | 504 saved_errno); |
481 #endif | 505 #endif |
482 return FILE_ERROR_FAILED; | 506 return FILE_ERROR_FAILED; |
483 } | 507 } |
484 } | 508 } |
485 | 509 |
486 void File::SetPlatformFile(PlatformFile file) { | 510 void File::SetPlatformFile(PlatformFile file) { |
487 DCHECK(!file_.is_valid()); | 511 CHECK(!file_.is_valid()); |
488 file_.reset(file); | 512 file_.reset(file); |
513 if (file_.is_valid()) | |
514 ProtectFileDescriptor(GetPlatformFile()); | |
489 } | 515 } |
490 | 516 |
491 } // namespace base | 517 } // namespace base |
OLD | NEW |