DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
local_file.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/file/local_file.h"
8 
9 #include <stdio.h>
10 
11 #include "packager/base/files/file_util.h"
12 #include "packager/base/logging.h"
13 
14 namespace edash_packager {
15 namespace media {
16 
17 LocalFile::LocalFile(const char* file_name, const char* mode)
18  : File(file_name), file_mode_(mode), internal_file_(NULL) {}
19 
21  bool result = true;
22  if (internal_file_) {
23  result = base::CloseFile(internal_file_);
24  internal_file_ = NULL;
25  }
26  delete this;
27  return result;
28 }
29 
30 int64_t LocalFile::Read(void* buffer, uint64_t length) {
31  DCHECK(buffer != NULL);
32  DCHECK(internal_file_ != NULL);
33  return fread(buffer, sizeof(char), length, internal_file_);
34 }
35 
36 int64_t LocalFile::Write(const void* buffer, uint64_t length) {
37  DCHECK(buffer != NULL);
38  DCHECK(internal_file_ != NULL);
39  return fwrite(buffer, sizeof(char), length, internal_file_);
40 }
41 
42 int64_t LocalFile::Size() {
43  DCHECK(internal_file_ != NULL);
44 
45  // Flush any buffered data, so we get the true file size.
46  if (!Flush()) {
47  LOG(ERROR) << "Cannot flush file.";
48  return -1;
49  }
50 
51  int64_t file_size;
52  if (!base::GetFileSize(base::FilePath(file_name()), &file_size)) {
53  LOG(ERROR) << "Cannot get file size.";
54  return -1;
55  }
56  return file_size;
57 }
58 
60  DCHECK(internal_file_ != NULL);
61  return ((fflush(internal_file_) == 0) && !ferror(internal_file_));
62 }
63 
64 bool LocalFile::Seek(uint64_t position) {
65  return fseeko(internal_file_, position, SEEK_SET) >= 0;
66 }
67 
68 bool LocalFile::Tell(uint64_t* position) {
69  off_t offset = ftello(internal_file_);
70  if (offset < 0)
71  return false;
72  *position = offset;
73  return true;
74 }
75 
76 LocalFile::~LocalFile() {}
77 
79  internal_file_ =
80  base::OpenFile(base::FilePath(file_name()), file_mode_.c_str());
81  return (internal_file_ != NULL);
82 }
83 
84 bool LocalFile::Delete(const char* file_name) {
85  return base::DeleteFile(base::FilePath(file_name), false);
86 }
87 
88 } // namespace media
89 } // namespace edash_packager
Define an abstract file interface.
Definition: file.h:22
LocalFile(const char *file_name, const char *mode)
Definition: local_file.cc:17
bool Tell(uint64_t *position) override
Definition: local_file.cc:68
const std::string & file_name() const
Definition: file.h:89
int64_t Write(const void *buffer, uint64_t length) override
Definition: local_file.cc:36
int64_t Read(void *buffer, uint64_t length) override
Definition: local_file.cc:30
bool Seek(uint64_t position) override
Definition: local_file.cc:64
static bool Delete(const char *file_name)
Definition: local_file.cc:84
bool Open() override
Internal open. Should not be used directly.
Definition: local_file.cc:78