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_2 { 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 auto result = mMemoryFileSystem.find(fileName); 28 return result != mMemoryFileSystem.end(); 29 } 30 GetFileSize(const std::string & fileName) const31ssize_t MemoryFileSystem::GetFileSize(const std::string& fileName) const { 32 auto result = mMemoryFileSystem.find(fileName); 33 if (result != mMemoryFileSystem.end()) { 34 return static_cast<ssize_t>(result->second.getFileSize()); 35 } else { 36 ALOGE("Failed to get size for %s", fileName.c_str()); 37 return -1; 38 } 39 } 40 ListFiles() const41std::vector<std::string> MemoryFileSystem::ListFiles() const { 42 std::vector<std::string> list; 43 for (const auto& filename : mMemoryFileSystem) { 44 list.push_back(filename.first); 45 } 46 return list; 47 } 48 Read(const std::string & path,std::string * buffer)49size_t MemoryFileSystem::Read(const std::string& path, std::string* buffer) { 50 std::string key = GetFileName(path); 51 auto result = mMemoryFileSystem.find(key); 52 if (result != mMemoryFileSystem.end()) { 53 std::string serializedHashFile = result->second.getContent(); 54 buffer->assign(serializedHashFile); 55 return buffer->size(); 56 } else { 57 ALOGE("Failed to read from %s", path.c_str()); 58 return -1; 59 } 60 } 61 Write(const std::string & path,const MemoryFile & memoryFile)62size_t MemoryFileSystem::Write(const std::string& path, const MemoryFile& memoryFile) { 63 std::string key = GetFileName(path); 64 auto result = mMemoryFileSystem.find(key); 65 if (result != mMemoryFileSystem.end()) { 66 mMemoryFileSystem.erase(key); 67 } 68 mMemoryFileSystem.insert(std::pair<std::string, MemoryFile>(key, memoryFile)); 69 return memoryFile.getFileSize(); 70 } 71 RemoveFile(const std::string & fileName)72bool MemoryFileSystem::RemoveFile(const std::string& fileName) { 73 auto result = mMemoryFileSystem.find(fileName); 74 if (result != mMemoryFileSystem.end()) { 75 mMemoryFileSystem.erase(result); 76 return true; 77 } else { 78 ALOGE("Cannot find license to remove: %s", fileName.c_str()); 79 return false; 80 } 81 } 82 RemoveAllFiles()83bool MemoryFileSystem::RemoveAllFiles() { 84 mMemoryFileSystem.clear(); 85 return mMemoryFileSystem.empty(); 86 } 87 88 } // namespace clearkey 89 } // namespace V1_2 90 } // namespace drm 91 } // namespace hardware 92 } // namespace android 93