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

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

Issue 1103733002: Make unused InitializeUnsafe private and rename to DoInitialize. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@less-base
Patch Set: based on topic branch Created 5 years, 8 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
OLDNEW
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
159 } 159 }
160 160
161 // Default implementations of Protect/Unprotect hooks defined as weak symbols 161 // Default implementations of Protect/Unprotect hooks defined as weak symbols
162 // where possible. 162 // where possible.
163 void ProtectFileDescriptor(int fd) { 163 void ProtectFileDescriptor(int fd) {
164 } 164 }
165 165
166 void UnprotectFileDescriptor(int fd) { 166 void UnprotectFileDescriptor(int fd) {
167 } 167 }
168 168
169 // NaCl doesn't implement system calls to open files directly.
170 #if !defined(OS_NACL)
171 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
172 void File::InitializeUnsafe(const FilePath& name, uint32 flags) {
173 ThreadRestrictions::AssertIOAllowed();
174 DCHECK(!IsValid());
175
176 int open_flags = 0;
177 if (flags & FLAG_CREATE)
178 open_flags = O_CREAT | O_EXCL;
179
180 created_ = false;
181
182 if (flags & FLAG_CREATE_ALWAYS) {
183 DCHECK(!open_flags);
184 DCHECK(flags & FLAG_WRITE);
185 open_flags = O_CREAT | O_TRUNC;
186 }
187
188 if (flags & FLAG_OPEN_TRUNCATED) {
189 DCHECK(!open_flags);
190 DCHECK(flags & FLAG_WRITE);
191 open_flags = O_TRUNC;
192 }
193
194 if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) {
195 NOTREACHED();
196 errno = EOPNOTSUPP;
197 error_details_ = FILE_ERROR_FAILED;
198 return;
199 }
200
201 if (flags & FLAG_WRITE && flags & FLAG_READ) {
202 open_flags |= O_RDWR;
203 } else if (flags & FLAG_WRITE) {
204 open_flags |= O_WRONLY;
205 } else if (!(flags & FLAG_READ) &&
206 !(flags & FLAG_WRITE_ATTRIBUTES) &&
207 !(flags & FLAG_APPEND) &&
208 !(flags & FLAG_OPEN_ALWAYS)) {
209 NOTREACHED();
210 }
211
212 if (flags & FLAG_TERMINAL_DEVICE)
213 open_flags |= O_NOCTTY | O_NDELAY;
214
215 if (flags & FLAG_APPEND && flags & FLAG_READ)
216 open_flags |= O_APPEND | O_RDWR;
217 else if (flags & FLAG_APPEND)
218 open_flags |= O_APPEND | O_WRONLY;
219
220 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
221
222 int mode = S_IRUSR | S_IWUSR;
223 #if defined(OS_CHROMEOS)
224 mode |= S_IRGRP | S_IROTH;
225 #endif
226
227 int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
228
229 if (flags & FLAG_OPEN_ALWAYS) {
230 if (descriptor < 0) {
231 open_flags |= O_CREAT;
232 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
233 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
234
235 descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
236 if (descriptor >= 0)
237 created_ = true;
238 }
239 }
240
241 if (descriptor < 0) {
242 error_details_ = File::OSErrorToFileError(errno);
243 return;
244 }
245
246 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
247 created_ = true;
248
249 if (flags & FLAG_DELETE_ON_CLOSE)
250 unlink(name.value().c_str());
251
252 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
253 error_details_ = FILE_OK;
254 file_.reset(descriptor);
255 ProtectFileDescriptor(descriptor);
256 }
257 #endif // !defined(OS_NACL)
258
259 bool File::IsValid() const { 169 bool File::IsValid() const {
260 return file_.is_valid(); 170 return file_.is_valid();
261 } 171 }
262 172
263 PlatformFile File::GetPlatformFile() const { 173 PlatformFile File::GetPlatformFile() const {
264 return file_.get(); 174 return file_.get();
265 } 175 }
266 176
267 PlatformFile File::TakePlatformFile() { 177 PlatformFile File::TakePlatformFile() {
268 if (IsValid()) 178 if (IsValid())
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 void File::MemoryCheckingScopedFD::Check() const { 444 void File::MemoryCheckingScopedFD::Check() const {
535 unsigned int computed_checksum; 445 unsigned int computed_checksum;
536 ComputeMemoryChecksum(&computed_checksum); 446 ComputeMemoryChecksum(&computed_checksum);
537 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; 447 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory";
538 } 448 }
539 449
540 void File::MemoryCheckingScopedFD::UpdateChecksum() { 450 void File::MemoryCheckingScopedFD::UpdateChecksum() {
541 ComputeMemoryChecksum(&file_memory_checksum_); 451 ComputeMemoryChecksum(&file_memory_checksum_);
542 } 452 }
543 453
454 // NaCl doesn't implement system calls to open files directly.
455 #if !defined(OS_NACL)
456 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
457 void File::DoInitialize(const FilePath& name, uint32 flags) {
458 ThreadRestrictions::AssertIOAllowed();
459 DCHECK(!IsValid());
460
461 int open_flags = 0;
462 if (flags & FLAG_CREATE)
463 open_flags = O_CREAT | O_EXCL;
464
465 created_ = false;
466
467 if (flags & FLAG_CREATE_ALWAYS) {
468 DCHECK(!open_flags);
469 DCHECK(flags & FLAG_WRITE);
470 open_flags = O_CREAT | O_TRUNC;
471 }
472
473 if (flags & FLAG_OPEN_TRUNCATED) {
474 DCHECK(!open_flags);
475 DCHECK(flags & FLAG_WRITE);
476 open_flags = O_TRUNC;
477 }
478
479 if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) {
480 NOTREACHED();
481 errno = EOPNOTSUPP;
482 error_details_ = FILE_ERROR_FAILED;
483 return;
484 }
485
486 if (flags & FLAG_WRITE && flags & FLAG_READ) {
487 open_flags |= O_RDWR;
488 } else if (flags & FLAG_WRITE) {
489 open_flags |= O_WRONLY;
490 } else if (!(flags & FLAG_READ) &&
491 !(flags & FLAG_WRITE_ATTRIBUTES) &&
492 !(flags & FLAG_APPEND) &&
493 !(flags & FLAG_OPEN_ALWAYS)) {
494 NOTREACHED();
495 }
496
497 if (flags & FLAG_TERMINAL_DEVICE)
498 open_flags |= O_NOCTTY | O_NDELAY;
499
500 if (flags & FLAG_APPEND && flags & FLAG_READ)
501 open_flags |= O_APPEND | O_RDWR;
502 else if (flags & FLAG_APPEND)
503 open_flags |= O_APPEND | O_WRONLY;
504
505 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
506
507 int mode = S_IRUSR | S_IWUSR;
508 #if defined(OS_CHROMEOS)
509 mode |= S_IRGRP | S_IROTH;
510 #endif
511
512 int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
513
514 if (flags & FLAG_OPEN_ALWAYS) {
515 if (descriptor < 0) {
516 open_flags |= O_CREAT;
517 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
518 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
519
520 descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
521 if (descriptor >= 0)
522 created_ = true;
523 }
524 }
525
526 if (descriptor < 0) {
527 error_details_ = File::OSErrorToFileError(errno);
528 return;
529 }
530
531 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
532 created_ = true;
533
534 if (flags & FLAG_DELETE_ON_CLOSE)
535 unlink(name.value().c_str());
536
537 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
538 error_details_ = FILE_OK;
539 file_.reset(descriptor);
540 ProtectFileDescriptor(descriptor);
541 }
542 #endif // !defined(OS_NACL)
543
544 bool File::DoFlush() { 544 bool File::DoFlush() {
545 ThreadRestrictions::AssertIOAllowed(); 545 ThreadRestrictions::AssertIOAllowed();
546 DCHECK(IsValid()); 546 DCHECK(IsValid());
547 #if defined(OS_NACL) 547 #if defined(OS_NACL)
548 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. 548 NOTIMPLEMENTED(); // NaCl doesn't implement fsync.
549 return true; 549 return true;
550 #elif defined(OS_LINUX) || defined(OS_ANDROID) 550 #elif defined(OS_LINUX) || defined(OS_ANDROID)
551 return !HANDLE_EINTR(fdatasync(file_.get())); 551 return !HANDLE_EINTR(fdatasync(file_.get()));
552 #else 552 #else
553 return !HANDLE_EINTR(fsync(file_.get())); 553 return !HANDLE_EINTR(fsync(file_.get()));
554 #endif 554 #endif
555 } 555 }
556 556
557 void File::SetPlatformFile(PlatformFile file) { 557 void File::SetPlatformFile(PlatformFile file) {
558 CHECK(!file_.is_valid()); 558 CHECK(!file_.is_valid());
559 file_.reset(file); 559 file_.reset(file);
560 if (file_.is_valid()) 560 if (file_.is_valid())
561 ProtectFileDescriptor(GetPlatformFile()); 561 ProtectFileDescriptor(GetPlatformFile());
562 } 562 }
563 563
564 } // namespace base 564 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file.cc ('k') | base/files/file_win.cc » ('j') | base/files/file_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698