Shaka Packager SDK
memory_file.cc
1 // Copyright 2015 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/file/memory_file.h"
8 
9 #include <string.h> // for memcpy
10 
11 #include <algorithm>
12 #include <map>
13 #include <memory>
14 
15 #include "packager/base/logging.h"
16 
17 namespace shaka {
18 namespace {
19 
20 // A helper filesystem object. This holds the data for the memory files.
21 class FileSystem {
22  public:
23  ~FileSystem() {}
24 
25  static FileSystem* Instance() {
26  if (!g_file_system_)
27  g_file_system_.reset(new FileSystem());
28 
29  return g_file_system_.get();
30  }
31 
32  bool Exists(const std::string& file_name) const {
33  return files_.find(file_name) != files_.end();
34  }
35 
36  std::vector<uint8_t>* GetFile(const std::string& file_name) {
37  return &files_[file_name];
38  }
39 
40  void Delete(const std::string& file_name) { files_.erase(file_name); }
41 
42  void DeleteAll() { files_.clear(); }
43 
44  private:
45  FileSystem() {}
46 
47  static std::unique_ptr<FileSystem> g_file_system_;
48 
49  std::map<std::string, std::vector<uint8_t> > files_;
50  DISALLOW_COPY_AND_ASSIGN(FileSystem);
51 };
52 
53 std::unique_ptr<FileSystem> FileSystem::g_file_system_;
54 
55 } // namespace
56 
57 MemoryFile::MemoryFile(const std::string& file_name, const std::string& mode)
58  : File(file_name), mode_(mode), file_(NULL), position_(0) {}
59 
60 MemoryFile::~MemoryFile() {}
61 
63  delete this;
64  return true;
65 }
66 
67 int64_t MemoryFile::Read(void* buffer, uint64_t length) {
68  const uint64_t size = Size();
69  DCHECK_LE(position_, size);
70  if (position_ >= size)
71  return 0;
72 
73  const uint64_t bytes_to_read = std::min(length, size - position_);
74  memcpy(buffer, &(*file_)[position_], bytes_to_read);
75  position_ += bytes_to_read;
76  return bytes_to_read;
77 }
78 
79 int64_t MemoryFile::Write(const void* buffer, uint64_t length) {
80  // If length is zero, we won't resize the buffer and it is possible for
81  // |position| to equal the length of the buffer. This will cause a segfault
82  // when indexing into the buffer for the memcpy.
83  if (length == 0) {
84  return 0;
85  }
86 
87  const uint64_t size = Size();
88  if (size < position_ + length) {
89  file_->resize(position_ + length);
90  }
91 
92  memcpy(&(*file_)[position_], buffer, length);
93  position_ += length;
94  return length;
95 }
96 
97 int64_t MemoryFile::Size() {
98  DCHECK(file_);
99  return file_->size();
100 }
101 
103  return true;
104 }
105 
106 bool MemoryFile::Seek(uint64_t position) {
107  if (Size() < static_cast<int64_t>(position))
108  return false;
109 
110  position_ = position;
111  return true;
112 }
113 
114 bool MemoryFile::Tell(uint64_t* position) {
115  *position = position_;
116  return true;
117 }
118 
120  FileSystem* file_system = FileSystem::Instance();
121  if (mode_ == "r") {
122  if (!file_system->Exists(file_name()))
123  return false;
124  } else if (mode_ == "w") {
125  file_system->Delete(file_name());
126  } else {
127  NOTIMPLEMENTED() << "File mode " << mode_ << " not supported by MemoryFile";
128  return false;
129  }
130 
131  file_ = file_system->GetFile(file_name());
132  DCHECK(file_);
133  position_ = 0;
134  return true;
135 }
136 
138  FileSystem::Instance()->DeleteAll();
139 }
140 
141 void MemoryFile::Delete(const std::string& file_name) {
142  FileSystem::Instance()->Delete(file_name);
143 }
144 
145 } // namespace shaka
static void Delete(const std::string &file_name)
Definition: memory_file.cc:141
static void DeleteAll()
Definition: memory_file.cc:137
bool Open() override
Internal open. Should not be used directly.
Definition: memory_file.cc:119
bool Close() override
Definition: memory_file.cc:62
bool Seek(uint64_t position) override
Definition: memory_file.cc:106
const std::string & file_name() const
Definition: file.h:94
All the methods that are virtual are virtual for mocking.
int64_t Write(const void *buffer, uint64_t length) override
Definition: memory_file.cc:79
int64_t Size() override
Definition: local_file.cc:61
int64_t Size() override
Definition: memory_file.cc:97
bool Tell(uint64_t *position) override
Definition: memory_file.cc:114
bool Flush() override
Definition: memory_file.cc:102
int64_t Read(void *buffer, uint64_t length) override
Definition: memory_file.cc:67