2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/file/local_file.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2015-05-20 17:14:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/file_util.h"
|
|
|
|
#include "packager/base/logging.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-01-24 00:26:00 +00:00
|
|
|
LocalFile::LocalFile(const char* file_name, const char* mode)
|
|
|
|
: File(file_name), file_mode_(mode), internal_file_(NULL) {}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
bool LocalFile::Close() {
|
|
|
|
bool result = true;
|
|
|
|
if (internal_file_) {
|
2014-02-26 23:55:01 +00:00
|
|
|
result = base::CloseFile(internal_file_);
|
2013-10-11 21:44:55 +00:00
|
|
|
internal_file_ = NULL;
|
|
|
|
}
|
|
|
|
delete this;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t LocalFile::Read(void* buffer, uint64_t length) {
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(buffer != NULL);
|
|
|
|
DCHECK(internal_file_ != NULL);
|
|
|
|
return fread(buffer, sizeof(char), length, internal_file_);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t LocalFile::Write(const void* buffer, uint64_t length) {
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(buffer != NULL);
|
|
|
|
DCHECK(internal_file_ != NULL);
|
|
|
|
return fwrite(buffer, sizeof(char), length, internal_file_);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t LocalFile::Size() {
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(internal_file_ != NULL);
|
|
|
|
|
|
|
|
// Flush any buffered data, so we get the true file size.
|
|
|
|
if (!Flush()) {
|
|
|
|
LOG(ERROR) << "Cannot flush file.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t file_size;
|
2014-02-26 23:55:01 +00:00
|
|
|
if (!base::GetFileSize(base::FilePath(file_name()), &file_size)) {
|
2013-10-11 21:44:55 +00:00
|
|
|
LOG(ERROR) << "Cannot get file size.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return file_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalFile::Flush() {
|
|
|
|
DCHECK(internal_file_ != NULL);
|
|
|
|
return ((fflush(internal_file_) == 0) && !ferror(internal_file_));
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:14:36 +00:00
|
|
|
bool LocalFile::Seek(uint64_t position) {
|
|
|
|
return fseeko(internal_file_, position, SEEK_SET) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalFile::Tell(uint64_t* position) {
|
|
|
|
off_t offset = ftello(internal_file_);
|
|
|
|
if (offset < 0)
|
|
|
|
return false;
|
|
|
|
*position = offset;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-24 00:26:00 +00:00
|
|
|
LocalFile::~LocalFile() {}
|
|
|
|
|
|
|
|
bool LocalFile::Open() {
|
|
|
|
internal_file_ =
|
|
|
|
base::OpenFile(base::FilePath(file_name()), file_mode_.c_str());
|
|
|
|
return (internal_file_ != NULL);
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
bool LocalFile::Delete(const char* file_name) {
|
|
|
|
return base::DeleteFile(base::FilePath(file_name), false);
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|