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

Side by Side Diff: base/files/memory_mapped_file_posix.cc

Issue 2404823002: Fix error handling in POSIX version of the base::File::GetLength. (Closed)
Patch Set: Fix error handling in POSIX version of the base::File::GetLength. Created 4 years 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 | « base/files/file_posix.cc ('k') | chrome/browser/media/webrtc/webrtc_browsertest_audio.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/memory_mapped_file.h" 5 #include "base/files/memory_mapped_file.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <sys/mman.h> 9 #include <sys/mman.h>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
(...skipping 13 matching lines...) Expand all
24 const MemoryMappedFile::Region& region, 24 const MemoryMappedFile::Region& region,
25 Access access) { 25 Access access) {
26 ThreadRestrictions::AssertIOAllowed(); 26 ThreadRestrictions::AssertIOAllowed();
27 27
28 off_t map_start = 0; 28 off_t map_start = 0;
29 size_t map_size = 0; 29 size_t map_size = 0;
30 int32_t data_offset = 0; 30 int32_t data_offset = 0;
31 31
32 if (region == MemoryMappedFile::Region::kWholeFile) { 32 if (region == MemoryMappedFile::Region::kWholeFile) {
33 int64_t file_len = file_.GetLength(); 33 int64_t file_len = file_.GetLength();
34 if (file_len == -1) { 34 if (file_len < 0) {
35 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile(); 35 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile();
36 return false; 36 return false;
37 } 37 }
38 map_size = static_cast<size_t>(file_len); 38 map_size = static_cast<size_t>(file_len);
39 length_ = map_size; 39 length_ = map_size;
40 } else { 40 } else {
41 // The region can be arbitrarily aligned. mmap, instead, requires both the 41 // The region can be arbitrarily aligned. mmap, instead, requires both the
42 // start and size to be page-aligned. Hence, we map here the page-aligned 42 // start and size to be page-aligned. Hence, we map here the page-aligned
43 // outer region [|aligned_start|, |aligned_start| + |size|] which contains 43 // outer region [|aligned_start|, |aligned_start| + |size|] which contains
44 // |region| and then add up the |data_offset| displacement. 44 // |region| and then add up the |data_offset| displacement.
(...skipping 26 matching lines...) Expand all
71 case READ_ONLY: 71 case READ_ONLY:
72 flags |= PROT_READ; 72 flags |= PROT_READ;
73 break; 73 break;
74 case READ_WRITE: 74 case READ_WRITE:
75 flags |= PROT_READ | PROT_WRITE; 75 flags |= PROT_READ | PROT_WRITE;
76 break; 76 break;
77 case READ_WRITE_EXTEND: 77 case READ_WRITE_EXTEND:
78 // POSIX won't auto-extend the file when it is written so it must first 78 // POSIX won't auto-extend the file when it is written so it must first
79 // be explicitly extended to the maximum size. Zeros will fill the new 79 // be explicitly extended to the maximum size. Zeros will fill the new
80 // space. 80 // space.
81 file_.SetLength(std::max(file_.GetLength(), region.offset + region.size)); 81 auto file_len = file_.GetLength();
82 if (file_len < 0) {
83 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile();
84 return false;
85 }
86 file_.SetLength(std::max(file_len, region.offset + region.size));
82 flags |= PROT_READ | PROT_WRITE; 87 flags |= PROT_READ | PROT_WRITE;
83 break; 88 break;
84 } 89 }
85 data_ = static_cast<uint8_t*>(mmap(NULL, map_size, flags, MAP_SHARED, 90 data_ = static_cast<uint8_t*>(mmap(NULL, map_size, flags, MAP_SHARED,
86 file_.GetPlatformFile(), map_start)); 91 file_.GetPlatformFile(), map_start));
87 if (data_ == MAP_FAILED) { 92 if (data_ == MAP_FAILED) {
88 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile(); 93 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile();
89 return false; 94 return false;
90 } 95 }
91 96
92 data_ += data_offset; 97 data_ += data_offset;
93 return true; 98 return true;
94 } 99 }
95 #endif 100 #endif
96 101
97 void MemoryMappedFile::CloseHandles() { 102 void MemoryMappedFile::CloseHandles() {
98 ThreadRestrictions::AssertIOAllowed(); 103 ThreadRestrictions::AssertIOAllowed();
99 104
100 if (data_ != NULL) 105 if (data_ != NULL)
101 munmap(data_, length_); 106 munmap(data_, length_);
102 file_.Close(); 107 file_.Close();
103 108
104 data_ = NULL; 109 data_ = NULL;
105 length_ = 0; 110 length_ = 0;
106 } 111 }
107 112
108 } // namespace base 113 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_posix.cc ('k') | chrome/browser/media/webrtc/webrtc_browsertest_audio.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698