Add kTruncate in FileBase. BUG=6841435.

And remove O_TRUNC from the default flags to OutputFile.

Change-Id: Id2ff6d5bac319d8be498e438eb3ef84e85573218
diff --git a/include/bcc/Support/File.h b/include/bcc/Support/File.h
index 69b7201..6367359 100644
--- a/include/bcc/Support/File.h
+++ b/include/bcc/Support/File.h
@@ -40,7 +40,7 @@
 // FileAttribute for accessing writable file
 template<>
 struct FileAttribute<FileBase::kWriteMode> {
-  enum { kOpenFlags       = O_RDWR | O_CREAT | O_TRUNC };
+  enum { kOpenFlags       = O_RDWR | O_CREAT };
   enum { kDefaultLockMode = FileBase::kWriteLock };
 };
 
diff --git a/include/bcc/Support/FileBase.h b/include/bcc/Support/FileBase.h
index afe6768..5e722df 100644
--- a/include/bcc/Support/FileBase.h
+++ b/include/bcc/Support/FileBase.h
@@ -40,6 +40,9 @@
   enum FlagEnum {
     // The openning file is a binary file.
     kBinary = 1 << 0,
+
+    // The openning file will be truncated to length 0.
+    kTruncate = 1 << 1,
   };
 
   enum LockModeEnum {
diff --git a/lib/RenderScript/RSCompilerDriver.cpp b/lib/RenderScript/RSCompilerDriver.cpp
index a59a7f9..60a6d9d 100644
--- a/lib/RenderScript/RSCompilerDriver.cpp
+++ b/lib/RenderScript/RSCompilerDriver.cpp
@@ -228,7 +228,8 @@
   //===--------------------------------------------------------------------===//
   // Open the output file for write.
   //===--------------------------------------------------------------------===//
-  OutputFile *output_file = new (std::nothrow) OutputFile(pOutputPath);
+  OutputFile *output_file =
+      new (std::nothrow) OutputFile(pOutputPath, FileBase::kTruncate);
 
   if ((output_file == NULL) || output_file->hasError()) {
     ALOGE("Unable to open the %s for write! (%s)", pOutputPath,
diff --git a/lib/RenderScript/RSExecutable.cpp b/lib/RenderScript/RSExecutable.cpp
index bbb31db..e18167a 100644
--- a/lib/RenderScript/RSExecutable.cpp
+++ b/lib/RenderScript/RSExecutable.cpp
@@ -128,7 +128,7 @@
   }
 
   android::String8 info_path = RSInfo::GetPath(*mObjFile);
-  OutputFile info_file(info_path.string());
+  OutputFile info_file(info_path.string(), FileBase::kTruncate);
 
   if (info_file.hasError()) {
     ALOGE("Failed to open the info file %s for write! (%s)", info_path.string(),
diff --git a/lib/Support/FileBase.cpp b/lib/Support/FileBase.cpp
index 2d50d08..1cc4283 100644
--- a/lib/Support/FileBase.cpp
+++ b/lib/Support/FileBase.cpp
@@ -40,6 +40,9 @@
     mOpenFlags |= O_BINARY;
   }
 #endif
+  if (pFlags & kTruncate) {
+    mOpenFlags |= O_TRUNC;
+  }
 
   // Open the file.
   open();
diff --git a/lib/Support/OutputFile.cpp b/lib/Support/OutputFile.cpp
index 7d1c057..6d969d0 100644
--- a/lib/Support/OutputFile.cpp
+++ b/lib/Support/OutputFile.cpp
@@ -54,8 +54,9 @@
     return NULL;
   }
 
-  // Create result OutputFile.
-  result = new (std::nothrow) OutputFile(tmp_filename, pFlags);
+  // Create result OutputFile. Temporary file is always truncated.
+  result = new (std::nothrow) OutputFile(tmp_filename,
+                                         pFlags | FileBase::kTruncate);
   if (result == NULL) {
     ALOGE("Out of memory when creates OutputFile for %s!", tmp_filename);
     // Fall through to the clean-up codes.
diff --git a/tools/bcc/Main.cpp b/tools/bcc/Main.cpp
index 44da31c..585dda0 100644
--- a/tools/bcc/Main.cpp
+++ b/tools/bcc/Main.cpp
@@ -265,7 +265,7 @@
 bool CompileScript(Compiler &pCompiler, Script &pScript,
                    const std::string &pOutputPath) {
   // Open the output file.
-  OutputFile output_file(pOutputPath);
+  OutputFile output_file(pOutputPath, FileBase::kTruncate);
 
   if (output_file.hasError()) {
     llvm::errs() << "Failed to open the output file `" << pOutputPath