1 // Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary 2 // source code may only be used and distributed under the Widevine Master 3 // License Agreement. 4 5 #include <utils/Log.h> 6 #include <string> 7 8 #include "MemoryFileSystem.h" 9 #include "Utils.h" 10 11 namespace android { 12 namespace hardware { 13 namespace drm { 14 namespace V1_4 { 15 namespace clearkey { 16 GetFileName(const std::string & path)17std::string MemoryFileSystem::GetFileName(const std::string& path) { 18 size_t index = path.find_last_of('/'); 19 if (index != std::string::npos) { 20 return path.substr(index+1); 21 } else { 22 return path; 23 } 24 } 25 FileExists(const std::string & fileName) const26bool MemoryFileSystem::FileExists(const std::string& fileName) const { 27 std::lock_guard<std::mutex> lock(mMemoryFileSystemLock); 28 auto result = mMemoryFileSystem.find(fileName); 29 return result != mMemoryFileSystem.end(); 30 } 31 GetFileSize(const std::string & fileName) const32ssize_t MemoryFileSystem::GetFileSize(const std::string& fileName) const { 33 std::lock_guard<std::mutex> lock(mMemoryFileSystemLock); 34 auto result = mMemoryFileSystem.find(fileName); 35 if (result != mMemoryFileSystem.end()) { 36 return static_cast<ssize_t>(result->second.getFileSize()); 37 } else { 38 ALOGE("Failed to get size for %s", fileName.c_str()); 39 return -1; 40 } 41 } 42 ListFiles() const43std::vector<std::string> MemoryFileSystem::ListFiles() const { 44 std::vector<std::string> list; 45 std::lock_guard<std::mutex> lock(mMemoryFileSystemLock); 46 for (const auto& filename : mMemoryFileSystem) { 47 list.push_back(filename.first); 48 } 49 return list; 50 } 51 Read(const std::string & path,std::string * buffer)52size_t MemoryFileSystem::Read(const std::string& path, std::string* buffer) { 53 std::string key = GetFileName(path); 54 std::lock_guard<std::mutex> lock(mMemoryFileSystemLock); 55 auto result = mMemoryFileSystem.find(key); 56 if (result != mMemoryFileSystem.end()) { 57 std::string serializedHashFile = result->second.getContent(); 58 buffer->assign(serializedHashFile); 59 return buffer->size(); 60 } else { 61 ALOGE("Failed to read from %s", path.c_str()); 62 return -1; 63 } 64 } 65 Write(const std::string & path,const MemoryFile & memoryFile)66size_t MemoryFileSystem::Write(const std::string& path, const MemoryFile& memoryFile) { 67 std::string key = GetFileName(path); 68 std::lock_guard<std::mutex> lock(mMemoryFileSystemLock); 69 auto result = mMemoryFileSystem.find(key); 70 if (result != mMemoryFileSystem.end()) { 71 mMemoryFileSystem.erase(key); 72 } 73 mMemoryFileSystem.insert(std::pair<std::string, MemoryFile>(key, memoryFile)); 74 return memoryFile.getFileSize(); 75 } 76 RemoveFile(const std::string & fileName)77bool MemoryFileSystem::RemoveFile(const std::string& fileName) { 78 std::lock_guard<std::mutex> lock(mMemoryFileSystemLock); 79 auto result = mMemoryFileSystem.find(fileName); 80 if (result != mMemoryFileSystem.end()) { 81 mMemoryFileSystem.erase(result); 82 return true; 83 } else { 84 ALOGE("Cannot find license to remove: %s", fileName.c_str()); 85 return false; 86 } 87 } 88 RemoveAllFiles()89bool MemoryFileSystem::RemoveAllFiles() { 90 std::lock_guard<std::mutex> lock(mMemoryFileSystemLock); 91 mMemoryFileSystem.clear(); 92 return mMemoryFileSystem.empty(); 93 } 94 95 } // namespace clearkey 96 } // namespace V1_4 97 } // namespace drm 98 } // namespace hardware 99 } // namespace android 100