2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2015 Google LLC. All rights reserved.
|
2015-12-22 00:20:34 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/file/memory_file.h>
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2016-08-19 22:32:27 +00:00
|
|
|
#include <algorithm>
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <cstring> // for memcpy
|
2015-12-22 00:20:34 +00:00
|
|
|
#include <map>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2018-08-06 23:09:11 +00:00
|
|
|
#include <set>
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
#include <absl/synchronization/mutex.h>
|
|
|
|
|
|
|
|
#include <packager/macros/logging.h>
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-12-22 00:20:34 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// A helper filesystem object. This holds the data for the memory files.
|
|
|
|
class FileSystem {
|
|
|
|
public:
|
|
|
|
~FileSystem() {}
|
|
|
|
|
|
|
|
static FileSystem* Instance() {
|
2018-08-06 23:09:11 +00:00
|
|
|
static FileSystem instance;
|
|
|
|
return &instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delete(const std::string& file_name) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock auto_lock(&mutex_);
|
2018-08-06 23:09:11 +00:00
|
|
|
|
|
|
|
if (open_files_.find(file_name) != open_files_.end()) {
|
|
|
|
LOG(ERROR) << "File '" << file_name
|
|
|
|
<< "' is still open. Deleting an open MemoryFile is not "
|
|
|
|
"allowed. Exit without deleting the file.";
|
|
|
|
return;
|
|
|
|
}
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2018-08-06 23:09:11 +00:00
|
|
|
files_.erase(file_name);
|
2015-12-22 00:20:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 23:09:11 +00:00
|
|
|
void DeleteAll() {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock auto_lock(&mutex_);
|
2018-08-06 23:09:11 +00:00
|
|
|
if (!open_files_.empty()) {
|
|
|
|
LOG(ERROR) << "There are still files open. Deleting an open MemoryFile "
|
|
|
|
"is not allowed. Exit without deleting the file.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
files_.clear();
|
2015-12-22 00:33:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 23:09:11 +00:00
|
|
|
std::vector<uint8_t>* Open(const std::string& file_name,
|
|
|
|
const std::string& mode) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock auto_lock(&mutex_);
|
2018-08-06 23:09:11 +00:00
|
|
|
|
|
|
|
if (open_files_.find(file_name) != open_files_.end()) {
|
|
|
|
NOTIMPLEMENTED() << "File '" << file_name
|
|
|
|
<< "' is already open. MemoryFile does not support "
|
2023-12-01 17:32:19 +00:00
|
|
|
"opening the same file before it is closed.";
|
2018-08-06 23:09:11 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = files_.find(file_name);
|
|
|
|
if (mode == "r") {
|
|
|
|
if (iter == files_.end())
|
|
|
|
return nullptr;
|
|
|
|
} else if (mode == "w") {
|
|
|
|
if (iter != files_.end())
|
|
|
|
iter->second.clear();
|
|
|
|
} else {
|
|
|
|
NOTIMPLEMENTED() << "File mode '" << mode
|
|
|
|
<< "' not supported by MemoryFile";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
open_files_[file_name] = mode;
|
2015-12-22 00:20:34 +00:00
|
|
|
return &files_[file_name];
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:09:11 +00:00
|
|
|
bool Close(const std::string& file_name) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock auto_lock(&mutex_);
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2018-08-06 23:09:11 +00:00
|
|
|
auto iter = open_files_.find(file_name);
|
|
|
|
if (iter == open_files_.end()) {
|
|
|
|
LOG(ERROR) << "Cannot close file '" << file_name
|
|
|
|
<< "' which is not open.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
open_files_.erase(iter);
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-22 00:20:34 +00:00
|
|
|
|
|
|
|
private:
|
2018-08-06 23:09:11 +00:00
|
|
|
FileSystem(const FileSystem&) = delete;
|
|
|
|
FileSystem& operator=(const FileSystem&) = delete;
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2018-08-06 23:09:11 +00:00
|
|
|
FileSystem() = default;
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2018-08-06 23:09:11 +00:00
|
|
|
// Filename to file data map.
|
2023-12-01 17:32:19 +00:00
|
|
|
std::map<std::string, std::vector<uint8_t>> files_ ABSL_GUARDED_BY(mutex_);
|
2018-08-06 23:09:11 +00:00
|
|
|
// Filename to file open modes map.
|
2023-12-01 17:32:19 +00:00
|
|
|
std::map<std::string, std::string> open_files_ ABSL_GUARDED_BY(mutex_);
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::Mutex mutex_;
|
2018-08-06 23:09:11 +00:00
|
|
|
};
|
2015-12-22 00:20:34 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-12-22 00:33:55 +00:00
|
|
|
MemoryFile::MemoryFile(const std::string& file_name, const std::string& mode)
|
|
|
|
: File(file_name), mode_(mode), file_(NULL), position_(0) {}
|
2015-12-22 00:20:34 +00:00
|
|
|
|
|
|
|
MemoryFile::~MemoryFile() {}
|
|
|
|
|
|
|
|
bool MemoryFile::Close() {
|
2018-08-06 23:09:11 +00:00
|
|
|
if (!FileSystem::Instance()->Close(file_name()))
|
|
|
|
return false;
|
2015-12-22 00:20:34 +00:00
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t MemoryFile::Read(void* buffer, uint64_t length) {
|
|
|
|
const uint64_t size = Size();
|
|
|
|
DCHECK_LE(position_, size);
|
|
|
|
if (position_ >= size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const uint64_t bytes_to_read = std::min(length, size - position_);
|
|
|
|
memcpy(buffer, &(*file_)[position_], bytes_to_read);
|
|
|
|
position_ += bytes_to_read;
|
|
|
|
return bytes_to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t MemoryFile::Write(const void* buffer, uint64_t length) {
|
2018-01-03 00:35:38 +00:00
|
|
|
// If length is zero, we won't resize the buffer and it is possible for
|
|
|
|
// |position| to equal the length of the buffer. This will cause a segfault
|
|
|
|
// when indexing into the buffer for the memcpy.
|
|
|
|
if (length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:20:34 +00:00
|
|
|
const uint64_t size = Size();
|
|
|
|
if (size < position_ + length) {
|
|
|
|
file_->resize(position_ + length);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&(*file_)[position_], buffer, length);
|
|
|
|
position_ += length;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
void MemoryFile::CloseForWriting() {}
|
|
|
|
|
2015-12-22 00:20:34 +00:00
|
|
|
int64_t MemoryFile::Size() {
|
2015-12-22 00:33:55 +00:00
|
|
|
DCHECK(file_);
|
2015-12-22 00:20:34 +00:00
|
|
|
return file_->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryFile::Flush() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryFile::Seek(uint64_t position) {
|
|
|
|
if (Size() < static_cast<int64_t>(position))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
position_ = position;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryFile::Tell(uint64_t* position) {
|
|
|
|
*position = position_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryFile::Open() {
|
2018-08-06 23:09:11 +00:00
|
|
|
file_ = FileSystem::Instance()->Open(file_name(), mode_);
|
|
|
|
if (!file_)
|
2015-12-22 00:33:55 +00:00
|
|
|
return false;
|
|
|
|
|
2015-12-22 00:20:34 +00:00
|
|
|
position_ = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryFile::DeleteAll() {
|
|
|
|
FileSystem::Instance()->DeleteAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryFile::Delete(const std::string& file_name) {
|
|
|
|
FileSystem::Instance()->Delete(file_name);
|
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|