Roll forward 8722,8721

Review URL: http://codereview.chromium.org/19028

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


CrOS-Libchrome-Original-Commit: c2c998ce396ec7fd77d8e1277afb3f029f859341
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj
index e347a84..5c9ab7a 100644
--- a/base/base.xcodeproj/project.pbxproj
+++ b/base/base.xcodeproj/project.pbxproj
@@ -644,6 +644,7 @@
 		ABF68B280EB0F93100E72835 /* field_trial.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = field_trial.cc; sourceTree = "<group>"; };
 		B290BFCBD30E45A63758BFC7 /* waitable_event_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = waitable_event_posix.cc; sourceTree = "<group>"; };
 		B52C916B0E9428F500208D01 /* clipboard_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clipboard_unittest.cc; sourceTree = "<group>"; };
+		B57D788E0F26983200685566 /* scoped_file.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = scoped_file.h; sourceTree = "<group>"; };
 		B57E4D770E9C26340090055D /* idletimer_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = idletimer_unittest.cc; sourceTree = "<group>"; };
 		B5A8618D0EC1257900B332C2 /* clipboard.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clipboard.cc; sourceTree = "<group>"; };
 		B5D544AA0EAFB7E000272A1C /* sys_string_conversions_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sys_string_conversions_unittest.cc; sourceTree = "<group>"; };
@@ -1002,6 +1003,7 @@
 				7B5AD60D0D9DD8050012BCF1 /* scoped_cftyperef.h */,
 				B5E8F6CB0EBFB38E008DD1E9 /* scoped_clipboard_writer.cc */,
 				B5E8F6CA0EBFB38E008DD1E9 /* scoped_clipboard_writer.h */,
+				B57D788E0F26983200685566 /* scoped_file.h */,
 				7BA35DD10E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.h */,
 				7BA35DD20E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm */,
 				825403610D92D27C0006B936 /* scoped_ptr.h */,
diff --git a/base/file_util.cc b/base/file_util.cc
index 078e3af..8ecb1cc 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -4,6 +4,9 @@
 
 #include "base/file_util.h"
 
+#if defined(OS_WIN)
+#include <io.h>
+#endif
 #include <stdio.h>
 
 #include <fstream>
@@ -276,6 +279,24 @@
   return fclose(file) == 0;
 }
 
+bool TruncateFile(FILE* file) {
+  if (file == NULL)
+    return false;
+  long current_offset = ftell(file);
+  if (current_offset == -1)
+    return false;
+#if defined(OS_WIN)
+  int fd = _fileno(file);
+  if (_chsize(fd, current_offset) != 0)
+    return false;
+#else
+  int fd = fileno(file);
+  if (ftruncate(fd, current_offset) != 0)
+    return false;
+#endif
+  return true;
+}
+
 bool ContainsPath(const FilePath &parent, const FilePath& child) {
   FilePath abs_parent = FilePath(parent);
   FilePath abs_child = FilePath(child);
diff --git a/base/file_util.h b/base/file_util.h
index 4154856..c546b9a 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -24,6 +24,7 @@
 #include <vector>
 
 #include "base/basictypes.h"
+#include "base/scoped_ptr.h"
 #include "base/file_path.h"
 
 namespace file_util {
@@ -318,6 +319,10 @@
 // Closes file opened by OpenFile. Returns true on success.
 bool CloseFile(FILE* file);
 
+// Truncates an open file to end at the location of the current file pointer.
+// This is a cross-platform analog to Windows' SetEndOfFile() function.
+bool TruncateFile(FILE* file);
+
 // Reads the given number of bytes from the file into the buffer.  Returns
 // the number of read bytes, or -1 on error.
 int ReadFile(const std::wstring& filename, char* data, int size);
@@ -336,6 +341,18 @@
 // Deprecated temporary compatibility function.
 bool SetCurrentDirectory(const std::wstring& current_directory);
 
+// A class to handle auto-closing of FILE*'s.
+class ScopedFILEClose {
+ public:
+  inline void operator()(FILE* x) const {
+    if (x) {
+      fclose(x);
+    }
+  }
+};
+
+typedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE;
+
 // A class for enumerating the files in a provided path. The order of the
 // results is not guaranteed.
 //