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

Side by Side Diff: base/shared_memory_posix.cc

Issue 174482: Use ftruncate() instead of fwrite() to extend files.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/shared_memory.h" 5 #include "base/shared_memory.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/mman.h> 9 #include <sys/mman.h>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 } 187 }
188 188
189 fp = file_util::OpenFile(mem_filename, mode.c_str()); 189 fp = file_util::OpenFile(mem_filename, mode.c_str());
190 } 190 }
191 191
192 if (fp == NULL) 192 if (fp == NULL)
193 return false; 193 return false;
194 file_closer.reset(fp); // close when we go out of scope 194 file_closer.reset(fp); // close when we go out of scope
195 195
196 // Make sure the (new) file is the right size. 196 // Make sure the (new) file is the right size.
197 // According to the man page, "Use of truncate() to extend a file is
198 // not portable."
199 if (size && (posix_flags & (O_RDWR | O_CREAT))) { 197 if (size && (posix_flags & (O_RDWR | O_CREAT))) {
200 // Get current size. 198 // Get current size.
201 size_t current_size = 0; 199 size_t current_size = 0;
Mark Mentovai 2009/08/26 01:22:51 While you're here, move the declaration down to wh
pink (ping after 24hrs) 2009/08/26 15:16:57 Done.
202 struct stat stat; 200 struct stat stat;
203 if (fstat(fileno(fp), &stat) != 0) 201 if (fstat(fileno(fp), &stat) != 0)
204 return false; 202 return false;
205 current_size = stat.st_size; 203 current_size = stat.st_size;
206 // Possibly grow. 204 if (current_size != size) {
207 if (current_size < size) { 205 if (ftruncate(fileno(fp), size) != 0)
208 if (fseeko(fp, current_size, SEEK_SET) != 0)
209 return false; 206 return false;
210 size_t writesize = size - current_size; 207 if (fseeko(fp, size, SEEK_SET) != 0)
211 scoped_array<char> buf(new char[writesize]);
212 memset(buf.get(), 0, writesize);
213 if (fwrite(buf.get(), 1, writesize, fp) != writesize) {
214 return false;
215 }
216 if (fflush(fp) != 0)
217 return false; 208 return false;
218 } else if (current_size > size) {
219 // possibly shrink.
220 if ((ftruncate(fileno(fp), size) != 0) ||
221 (fflush(fp) != 0)) {
222 return false;
223 }
224 } 209 }
225 } 210 }
226 211
227 mapped_file_ = dup(fileno(fp)); 212 mapped_file_ = dup(fileno(fp));
228 if (mapped_file_ == -1) { 213 if (mapped_file_ == -1) {
229 if (errno == EMFILE) { 214 if (errno == EMFILE) {
230 LOG(WARNING) << "Shared memory creation failed; out of file descriptors"; 215 LOG(WARNING) << "Shared memory creation failed; out of file descriptors";
231 return false; 216 return false;
232 } else { 217 } else {
233 NOTREACHED() << "Call to dup failed, errno=" << errno; 218 NOTREACHED() << "Call to dup failed, errno=" << errno;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 301
317 void SharedMemory::Unlock() { 302 void SharedMemory::Unlock() {
318 LockOrUnlockCommon(F_ULOCK); 303 LockOrUnlockCommon(F_ULOCK);
319 } 304 }
320 305
321 SharedMemoryHandle SharedMemory::handle() const { 306 SharedMemoryHandle SharedMemory::handle() const {
322 return FileDescriptor(mapped_file_, false); 307 return FileDescriptor(mapped_file_, false);
323 } 308 }
324 309
325 } // namespace base 310 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698