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> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_posix_hooks_internal.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
15 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
18 | 19 |
19 #if defined(OS_ANDROID) | 20 #if defined(OS_ANDROID) |
20 #include "base/os_compat_android.h" | 21 #include "base/os_compat_android.h" |
21 #endif | 22 #endif |
22 | 23 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 Time::FromTimeT(last_accessed_sec) + | 160 Time::FromTimeT(last_accessed_sec) + |
160 TimeDelta::FromMicroseconds(last_accessed_nsec / | 161 TimeDelta::FromMicroseconds(last_accessed_nsec / |
161 Time::kNanosecondsPerMicrosecond); | 162 Time::kNanosecondsPerMicrosecond); |
162 | 163 |
163 creation_time = | 164 creation_time = |
164 Time::FromTimeT(creation_time_sec) + | 165 Time::FromTimeT(creation_time_sec) + |
165 TimeDelta::FromMicroseconds(creation_time_nsec / | 166 TimeDelta::FromMicroseconds(creation_time_nsec / |
166 Time::kNanosecondsPerMicrosecond); | 167 Time::kNanosecondsPerMicrosecond); |
167 } | 168 } |
168 | 169 |
| 170 // Default implementations of Protect/Unprotect hooks defined as weak symbols |
| 171 // where possible. |
| 172 void ProtectFileDescriptor(int fd) { |
| 173 } |
| 174 |
| 175 void UnprotectFileDescriptor(int fd) { |
| 176 } |
| 177 |
169 // NaCl doesn't implement system calls to open files directly. | 178 // NaCl doesn't implement system calls to open files directly. |
170 #if !defined(OS_NACL) | 179 #if !defined(OS_NACL) |
171 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? | 180 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
172 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { | 181 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { |
173 base::ThreadRestrictions::AssertIOAllowed(); | 182 base::ThreadRestrictions::AssertIOAllowed(); |
174 DCHECK(!IsValid()); | 183 DCHECK(!IsValid()); |
175 | 184 |
176 int open_flags = 0; | 185 int open_flags = 0; |
177 if (flags & FLAG_CREATE) | 186 if (flags & FLAG_CREATE) |
178 open_flags = O_CREAT | O_EXCL; | 187 open_flags = O_CREAT | O_EXCL; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 254 |
246 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | 255 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
247 created_ = true; | 256 created_ = true; |
248 | 257 |
249 if (flags & FLAG_DELETE_ON_CLOSE) | 258 if (flags & FLAG_DELETE_ON_CLOSE) |
250 unlink(name.value().c_str()); | 259 unlink(name.value().c_str()); |
251 | 260 |
252 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | 261 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
253 error_details_ = FILE_OK; | 262 error_details_ = FILE_OK; |
254 file_.reset(descriptor); | 263 file_.reset(descriptor); |
| 264 ProtectFileDescriptor(descriptor); |
255 } | 265 } |
256 #endif // !defined(OS_NACL) | 266 #endif // !defined(OS_NACL) |
257 | 267 |
258 bool File::IsValid() const { | 268 bool File::IsValid() const { |
259 return file_.is_valid(); | 269 return file_.is_valid(); |
260 } | 270 } |
261 | 271 |
262 PlatformFile File::GetPlatformFile() const { | 272 PlatformFile File::GetPlatformFile() const { |
263 return file_.get(); | 273 return file_.get(); |
264 } | 274 } |
265 | 275 |
266 PlatformFile File::TakePlatformFile() { | 276 PlatformFile File::TakePlatformFile() { |
| 277 if (IsValid()) |
| 278 UnprotectFileDescriptor(GetPlatformFile()); |
267 return file_.release(); | 279 return file_.release(); |
268 } | 280 } |
269 | 281 |
270 void File::Close() { | 282 void File::Close() { |
271 if (!IsValid()) | 283 if (!IsValid()) |
272 return; | 284 return; |
273 | 285 |
274 base::ThreadRestrictions::AssertIOAllowed(); | 286 base::ThreadRestrictions::AssertIOAllowed(); |
| 287 UnprotectFileDescriptor(GetPlatformFile()); |
275 file_.reset(); | 288 file_.reset(); |
276 } | 289 } |
277 | 290 |
278 int64 File::Seek(Whence whence, int64 offset) { | 291 int64 File::Seek(Whence whence, int64 offset) { |
279 base::ThreadRestrictions::AssertIOAllowed(); | 292 base::ThreadRestrictions::AssertIOAllowed(); |
280 DCHECK(IsValid()); | 293 DCHECK(IsValid()); |
281 | 294 |
282 #if defined(OS_ANDROID) | 295 #if defined(OS_ANDROID) |
283 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); | 296 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); |
284 return lseek64(file_.get(), static_cast<off64_t>(offset), | 297 return lseek64(file_.get(), static_cast<off64_t>(offset), |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 default: | 490 default: |
478 #if !defined(OS_NACL) // NaCl build has no metrics code. | 491 #if !defined(OS_NACL) // NaCl build has no metrics code. |
479 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", | 492 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
480 saved_errno); | 493 saved_errno); |
481 #endif | 494 #endif |
482 return FILE_ERROR_FAILED; | 495 return FILE_ERROR_FAILED; |
483 } | 496 } |
484 } | 497 } |
485 | 498 |
486 void File::SetPlatformFile(PlatformFile file) { | 499 void File::SetPlatformFile(PlatformFile file) { |
487 DCHECK(!file_.is_valid()); | 500 CHECK(!file_.is_valid()); |
488 file_.reset(file); | 501 file_.reset(file); |
| 502 if (file_.is_valid()) |
| 503 ProtectFileDescriptor(GetPlatformFile()); |
489 } | 504 } |
490 | 505 |
491 } // namespace base | 506 } // namespace base |
OLD | NEW |