DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations 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 #if defined(OS_WIN)
11 #include <windows.h>
12 #endif // defined(OS_WIN)
13 #include "packager/base/files/file_util.h"
14 #include "packager/base/logging.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 // Always open files in binary mode.
20 const char kAdditionalFileMode[] = "b";
21 
22 LocalFile::LocalFile(const char* file_name, const char* mode)
23  : File(file_name),
24  file_mode_(mode),
25  internal_file_(NULL) {
26  if (file_mode_.find(kAdditionalFileMode) == std::string::npos)
27  file_mode_ += kAdditionalFileMode;
28 }
29 
31  bool result = true;
32  if (internal_file_) {
33  result = base::CloseFile(internal_file_);
34  internal_file_ = NULL;
35  }
36  delete this;
37  return result;
38 }
39 
40 int64_t LocalFile::Read(void* buffer, uint64_t length) {
41  DCHECK(buffer != NULL);
42  DCHECK(internal_file_ != NULL);
43  return fread(buffer, sizeof(char), length, internal_file_);
44 }
45 
46 int64_t LocalFile::Write(const void* buffer, uint64_t length) {
47  DCHECK(buffer != NULL);
48  DCHECK(internal_file_ != NULL);
49  return fwrite(buffer, sizeof(char), length, internal_file_);
50 }
51 
52 int64_t LocalFile::Size() {
53  DCHECK(internal_file_ != NULL);
54 
55  // Flush any buffered data, so we get the true file size.
56  if (!Flush()) {
57  LOG(ERROR) << "Cannot flush file.";
58  return -1;
59  }
60 
61  int64_t file_size;
62  if (!base::GetFileSize(base::FilePath::FromUTF8Unsafe(file_name()),
63  &file_size)) {
64  LOG(ERROR) << "Cannot get file size.";
65  return -1;
66  }
67  return file_size;
68 }
69 
71  DCHECK(internal_file_ != NULL);
72  return ((fflush(internal_file_) == 0) && !ferror(internal_file_));
73 }
74 
75 bool LocalFile::Seek(uint64_t position) {
76 #if defined(OS_WIN)
77  return _fseeki64(internal_file_, static_cast<__int64>(position),
78  SEEK_SET) == 0;
79 #else
80  return fseeko(internal_file_, position, SEEK_SET) >= 0;
81 #endif // !defined(OS_WIN)
82 }
83 
84 bool LocalFile::Tell(uint64_t* position) {
85 #if defined(OS_WIN)
86  __int64 offset = _ftelli64(internal_file_);
87 #else
88  off_t offset = ftello(internal_file_);
89 #endif // !defined(OS_WIN)
90  if (offset < 0)
91  return false;
92  *position = static_cast<uint64_t>(offset);
93  return true;
94 }
95 
96 LocalFile::~LocalFile() {}
97 
99  internal_file_ =
100  base::OpenFile(base::FilePath::FromUTF8Unsafe(file_name()), file_mode_.c_str());
101  return (internal_file_ != NULL);
102 }
103 
104 bool LocalFile::Delete(const char* file_name) {
105  return base::DeleteFile(base::FilePath::FromUTF8Unsafe(file_name), false);
106 }
107 
108 } // namespace media
109 } // namespace shaka
int64_t Read(void *buffer, uint64_t length) override
Definition: local_file.cc:40
bool Close() override
Definition: local_file.cc:30
bool Seek(uint64_t position) override
Definition: local_file.cc:75
Define an abstract file interface.
Definition: file.h:24
LocalFile(const char *file_name, const char *mode)
Definition: local_file.cc:22
bool Tell(uint64_t *position) override
Definition: local_file.cc:84
int64_t Write(const void *buffer, uint64_t length) override
Definition: local_file.cc:46
const std::string & file_name() const
Definition: file.h:91
bool Open() override
Internal open. Should not be used directly.
Definition: local_file.cc:98
bool Flush() override
Definition: local_file.cc:70
int64_t Size() override
Definition: local_file.cc:52
static bool Delete(const char *file_name)
Definition: local_file.cc:104