Revert 257577 "Revert 257562 "Base: Make base::File use ScopedFD..."

Reland this: turns out not responsible.

> Revert 257562 "Base: Make base::File use ScopedFD on POSIX."
> 
> Might caused a bunch of layout test failures, at least on Linux
> 
> fast/dom/Window/property-access-on-cached-properties-after-frame-navigated.html,
> fast/dom/Window/property-access-on-cached-properties-after-frame-removed.html,
> fast/dom/Window/property-access-on-cached-window-after-frame-navigated.html,
> fast/dom/Window/property-access-on-cached-window-after-frame-removed.html
> 
> > Base: Make base::File use ScopedFD on POSIX.
> > 
> > Now that ScopedFD lives outside of file_util, File can use it to remove manual
> > lifetime management.
> > 
> > This also standarizes the behavior on Close failures for all platforms.
> > 
> > BUG=none
> > 
> > Review URL: https://codereview.chromium.org/201083002
> 
> TBR=rvargas@chromium.org
> 
> Review URL: https://codereview.chromium.org/202583005

TBR=zmo@chromium.org

Review URL: https://codereview.chromium.org/202493007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257581 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 49ec031c22cdccd490e5ea328c2c01a98e793e21
diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc
index a62ad98..f62491d 100644
--- a/base/files/file_posix.cc
+++ b/base/files/file_posix.cc
@@ -202,18 +202,20 @@
   else
     error_details_ = File::OSErrorToFileError(errno);
 
-  file_ = descriptor;
+  file_.reset(descriptor);
 }
 #endif  // !defined(OS_NACL)
 
 bool File::IsValid() const {
-  return file_ >= 0;
+  return file_.is_valid();
+}
+
+PlatformFile File::GetPlatformFile() const {
+  return file_.get();
 }
 
 PlatformFile File::TakePlatformFile() {
-  PlatformFile file = file_;
-  file_ = kInvalidPlatformFileValue;
-  return file;
+  return file_.release();
 }
 
 void File::Close() {
@@ -221,17 +223,17 @@
     return;
 
   base::ThreadRestrictions::AssertIOAllowed();
-  if (!IGNORE_EINTR(close(file_)))
-    file_ = kInvalidPlatformFileValue;
+  file_.reset();
 }
 
 int64 File::Seek(Whence whence, int64 offset) {
   base::ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
-  if (file_ < 0 || offset < 0)
+  if (offset < 0)
     return -1;
 
-  return lseek(file_, static_cast<off_t>(offset), static_cast<int>(whence));
+  return lseek(file_.get(), static_cast<off_t>(offset),
+               static_cast<int>(whence));
 }
 
 int File::Read(int64 offset, char* data, int size) {
@@ -243,7 +245,7 @@
   int bytes_read = 0;
   int rv;
   do {
-    rv = HANDLE_EINTR(pread(file_, data + bytes_read,
+    rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read,
                             size - bytes_read, offset + bytes_read));
     if (rv <= 0)
       break;
@@ -263,7 +265,7 @@
   int bytes_read = 0;
   int rv;
   do {
-    rv = HANDLE_EINTR(read(file_, data, size));
+    rv = HANDLE_EINTR(read(file_.get(), data, size));
     if (rv <= 0)
       break;
 
@@ -277,7 +279,7 @@
   base::ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
 
-  return HANDLE_EINTR(pread(file_, data, size, offset));
+  return HANDLE_EINTR(pread(file_.get(), data, size, offset));
 }
 
 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
@@ -286,13 +288,13 @@
   if (size < 0)
     return -1;
 
-  return HANDLE_EINTR(read(file_, data, size));
+  return HANDLE_EINTR(read(file_.get(), data, size));
 }
 
 int File::Write(int64 offset, const char* data, int size) {
   base::ThreadRestrictions::AssertIOAllowed();
 
-  if (IsOpenAppend(file_))
+  if (IsOpenAppend(file_.get()))
     return WriteAtCurrentPos(data, size);
 
   DCHECK(IsValid());
@@ -302,7 +304,7 @@
   int bytes_written = 0;
   int rv;
   do {
-    rv = HANDLE_EINTR(pwrite(file_, data + bytes_written,
+    rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written,
                              size - bytes_written, offset + bytes_written));
     if (rv <= 0)
       break;
@@ -322,7 +324,7 @@
   int bytes_written = 0;
   int rv;
   do {
-    rv = HANDLE_EINTR(write(file_, data, size));
+    rv = HANDLE_EINTR(write(file_.get(), data, size));
     if (rv <= 0)
       break;
 
@@ -338,14 +340,14 @@
   if (size < 0)
     return -1;
 
-  return HANDLE_EINTR(write(file_, data, size));
+  return HANDLE_EINTR(write(file_.get(), data, size));
 }
 
 int64 File::GetLength() {
   DCHECK(IsValid());
 
   stat_wrapper_t file_info;
-  if (CallFstat(file_, &file_info))
+  if (CallFstat(file_.get(), &file_info))
     return false;
 
   return file_info.st_size;
@@ -354,13 +356,13 @@
 bool File::SetLength(int64 length) {
   base::ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
-  return !CallFtruncate(file_, length);
+  return !CallFtruncate(file_.get(), length);
 }
 
 bool File::Flush() {
   base::ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
-  return !CallFsync(file_);
+  return !CallFsync(file_.get());
 }
 
 bool File::SetTimes(Time last_access_time, Time last_modified_time) {
@@ -371,14 +373,14 @@
   times[0] = last_access_time.ToTimeVal();
   times[1] = last_modified_time.ToTimeVal();
 
-  return !CallFutimes(file_, times);
+  return !CallFutimes(file_.get(), times);
 }
 
 bool File::GetInfo(Info* info) {
   DCHECK(IsValid());
 
   stat_wrapper_t file_info;
-  if (CallFstat(file_, &file_info))
+  if (CallFstat(file_.get(), &file_info))
     return false;
 
   info->is_directory = S_ISDIR(file_info.st_mode);
@@ -432,11 +434,11 @@
 }
 
 File::Error File::Lock() {
-  return CallFctnlFlock(file_, true);
+  return CallFctnlFlock(file_.get(), true);
 }
 
 File::Error File::Unlock() {
-  return CallFctnlFlock(file_, false);
+  return CallFctnlFlock(file_.get(), false);
 }
 
 // Static.
@@ -473,8 +475,8 @@
 }
 
 void File::SetPlatformFile(PlatformFile file) {
-  DCHECK_EQ(file_, kInvalidPlatformFileValue);
-  file_ = file;
+  DCHECK(!file_.is_valid());
+  file_.reset(file);
 }
 
 }  // namespace base