Replace CreateTemporaryFileInDir with a custom implementation

CreateTemporaryFileInDir uses mkstemp which works only on local files.
This change enables the use of other types of files, like network file
as temporary directory.

Bug: 19246995

Change-Id: Ic752725e56b65eec2cd10a4e5d760c6f76ba4f18
This commit is contained in:
KongQun Yang 2015-02-03 15:21:55 -08:00 committed by Gerrit Code Review
parent dddff135cf
commit e76b20a282
1 changed files with 23 additions and 8 deletions

View File

@ -6,7 +6,11 @@
#include "packager/media/formats/mp4/single_segment_segmenter.h" #include "packager/media/formats/mp4/single_segment_segmenter.h"
#include <inttypes.h>
#include "packager/base/file_util.h" #include "packager/base/file_util.h"
#include "packager/base/strings/stringprintf.h"
#include "packager/base/time/time.h"
#include "packager/media/base/buffer_writer.h" #include "packager/media/base/buffer_writer.h"
#include "packager/media/base/media_stream.h" #include "packager/media/base/media_stream.h"
#include "packager/media/base/muxer_options.h" #include "packager/media/base/muxer_options.h"
@ -17,6 +21,15 @@
namespace edash_packager { namespace edash_packager {
namespace media { namespace media {
namespace mp4 { namespace mp4 {
namespace {
// Create a temp file name using process/thread id and current time.
std::string TempFileName() {
int32_t tid = static_cast<int32_t>(pthread_self());
int32_t pid = static_cast<int32_t>(getpid());
int64_t now = base::Time::Now().ToInternalValue();
return base::StringPrintf("packager-tempfile-%x-%d-%" PRIx64, tid, pid, now);
}
} // namespace
SingleSegmentSegmenter::SingleSegmentSegmenter(const MuxerOptions& options, SingleSegmentSegmenter::SingleSegmentSegmenter(const MuxerOptions& options,
scoped_ptr<FileType> ftyp, scoped_ptr<FileType> ftyp,
@ -40,15 +53,17 @@ bool SingleSegmentSegmenter::GetIndexRange(size_t* offset, size_t* size) {
} }
Status SingleSegmentSegmenter::DoInitialize() { Status SingleSegmentSegmenter::DoInitialize() {
base::FilePath temp_file_path; if (options().temp_dir.empty()) {
if (options().temp_dir.empty() ? base::FilePath temp_file_path;
!base::CreateTemporaryFile(&temp_file_path) : if (!base::CreateTemporaryFile(&temp_file_path)) {
!base::CreateTemporaryFileInDir(base::FilePath(options().temp_dir), LOG(ERROR) << "Failed to create temporary file.";
&temp_file_path)) { return Status(error::FILE_FAILURE, "Unable to create temporary file.");
return Status(error::FILE_FAILURE, "Unable to create temporary file."); }
temp_file_name_ = temp_file_path.value();
} else {
temp_file_name_ =
base::FilePath(options().temp_dir).Append(TempFileName()).value();
} }
temp_file_name_ = temp_file_path.value();
temp_file_.reset(File::Open(temp_file_name_.c_str(), "w")); temp_file_.reset(File::Open(temp_file_name_.c_str(), "w"));
return temp_file_ return temp_file_
? Status::OK ? Status::OK