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/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
17 | 17 |
18 #if defined(OS_ANDROID) | 18 #if defined(OS_ANDROID) |
19 #include "base/os_compat_android.h" | 19 #include "base/os_compat_android.h" |
20 #endif | 20 #endif |
21 | 21 |
22 namespace base { | 22 namespace base { |
23 | 23 |
24 // Make sure our Whence mappings match the system headers. | 24 // Make sure our Whence mappings match the system headers. |
25 COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET && | 25 static_assert(File::FROM_BEGIN == SEEK_SET && File::FROM_CURRENT == SEEK_CUR && |
26 File::FROM_CURRENT == SEEK_CUR && | 26 File::FROM_END == SEEK_END, |
27 File::FROM_END == SEEK_END, whence_matches_system); | 27 "whence mapping must match the system headers"); |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) | 31 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
32 static int CallFstat(int fd, stat_wrapper_t *sb) { | 32 static int CallFstat(int fd, stat_wrapper_t *sb) { |
33 ThreadRestrictions::AssertIOAllowed(); | 33 ThreadRestrictions::AssertIOAllowed(); |
34 return fstat(fd, sb); | 34 return fstat(fd, sb); |
35 } | 35 } |
36 #else | 36 #else |
37 static int CallFstat(int fd, stat_wrapper_t *sb) { | 37 static int CallFstat(int fd, stat_wrapper_t *sb) { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 file_.reset(); | 177 file_.reset(); |
178 } | 178 } |
179 | 179 |
180 int64 File::Seek(Whence whence, int64 offset) { | 180 int64 File::Seek(Whence whence, int64 offset) { |
181 ThreadRestrictions::AssertIOAllowed(); | 181 ThreadRestrictions::AssertIOAllowed(); |
182 DCHECK(IsValid()); | 182 DCHECK(IsValid()); |
183 | 183 |
184 SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset); | 184 SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset); |
185 | 185 |
186 #if defined(OS_ANDROID) | 186 #if defined(OS_ANDROID) |
187 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); | 187 static_assert(sizeof(int64) == sizeof(off64_t), "off64_t must be 64 bits"); |
188 return lseek64(file_.get(), static_cast<off64_t>(offset), | 188 return lseek64(file_.get(), static_cast<off64_t>(offset), |
189 static_cast<int>(whence)); | 189 static_cast<int>(whence)); |
190 #else | 190 #else |
191 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 191 static_assert(sizeof(int64) == sizeof(off_t), "off_t must be 64 bits"); |
192 return lseek(file_.get(), static_cast<off_t>(offset), | 192 return lseek(file_.get(), static_cast<off_t>(offset), |
193 static_cast<int>(whence)); | 193 static_cast<int>(whence)); |
194 #endif | 194 #endif |
195 } | 195 } |
196 | 196 |
197 int File::Read(int64 offset, char* data, int size) { | 197 int File::Read(int64 offset, char* data, int size) { |
198 ThreadRestrictions::AssertIOAllowed(); | 198 ThreadRestrictions::AssertIOAllowed(); |
199 DCHECK(IsValid()); | 199 DCHECK(IsValid()); |
200 if (size < 0) | 200 if (size < 0) |
201 return -1; | 201 return -1; |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 } | 464 } |
465 | 465 |
466 if (flags & FLAG_TERMINAL_DEVICE) | 466 if (flags & FLAG_TERMINAL_DEVICE) |
467 open_flags |= O_NOCTTY | O_NDELAY; | 467 open_flags |= O_NOCTTY | O_NDELAY; |
468 | 468 |
469 if (flags & FLAG_APPEND && flags & FLAG_READ) | 469 if (flags & FLAG_APPEND && flags & FLAG_READ) |
470 open_flags |= O_APPEND | O_RDWR; | 470 open_flags |= O_APPEND | O_RDWR; |
471 else if (flags & FLAG_APPEND) | 471 else if (flags & FLAG_APPEND) |
472 open_flags |= O_APPEND | O_WRONLY; | 472 open_flags |= O_APPEND | O_WRONLY; |
473 | 473 |
474 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); | 474 static_assert(O_RDONLY == 0, "O_RDONLY must equal zero"); |
475 | 475 |
476 int mode = S_IRUSR | S_IWUSR; | 476 int mode = S_IRUSR | S_IWUSR; |
477 #if defined(OS_CHROMEOS) | 477 #if defined(OS_CHROMEOS) |
478 mode |= S_IRGRP | S_IROTH; | 478 mode |= S_IRGRP | S_IROTH; |
479 #endif | 479 #endif |
480 | 480 |
481 int descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode)); | 481 int descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode)); |
482 | 482 |
483 if (flags & FLAG_OPEN_ALWAYS) { | 483 if (flags & FLAG_OPEN_ALWAYS) { |
484 if (descriptor < 0) { | 484 if (descriptor < 0) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 return !HANDLE_EINTR(fsync(file_.get())); | 522 return !HANDLE_EINTR(fsync(file_.get())); |
523 #endif | 523 #endif |
524 } | 524 } |
525 | 525 |
526 void File::SetPlatformFile(PlatformFile file) { | 526 void File::SetPlatformFile(PlatformFile file) { |
527 DCHECK(!file_.is_valid()); | 527 DCHECK(!file_.is_valid()); |
528 file_.reset(file); | 528 file_.reset(file); |
529 } | 529 } |
530 | 530 |
531 } // namespace base | 531 } // namespace base |
OLD | NEW |