• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <utils/Log.h>
17 #include <string>
18 #include <vector>
19 
20 #include "MemoryFileSystem.h"
21 
22 namespace clearkeydrm {
23 
GetFileName(const std::string & path)24 std::string MemoryFileSystem::GetFileName(const std::string& path) {
25     size_t index = path.find_last_of('/');
26     if (index != std::string::npos) {
27         return path.substr(index + 1);
28     } else {
29         return path;
30     }
31 }
32 
FileExists(const std::string & fileName) const33 bool MemoryFileSystem::FileExists(const std::string& fileName) const {
34     auto result = mMemoryFileSystem.find(fileName);
35     return result != mMemoryFileSystem.end();
36 }
37 
GetFileSize(const std::string & fileName) const38 ssize_t MemoryFileSystem::GetFileSize(const std::string& fileName) const {
39     auto result = mMemoryFileSystem.find(fileName);
40     if (result != mMemoryFileSystem.end()) {
41         return static_cast<ssize_t>(result->second.getFileSize());
42     } else {
43         ALOGE("Failed to get size for %s", fileName.c_str());
44         return -1;
45     }
46 }
47 
ListFiles() const48 std::vector<std::string> MemoryFileSystem::ListFiles() const {
49     std::vector<std::string> list;
50     for (const auto& filename : mMemoryFileSystem) {
51         list.push_back(filename.first);
52     }
53     return list;
54 }
55 
Read(const std::string & path,std::string * buffer)56 size_t MemoryFileSystem::Read(const std::string& path, std::string* buffer) {
57     std::string key = GetFileName(path);
58     auto result = mMemoryFileSystem.find(key);
59     if (result != mMemoryFileSystem.end()) {
60         std::string serializedHashFile = result->second.getContent();
61         buffer->assign(serializedHashFile);
62         return buffer->size();
63     } else {
64         ALOGE("Failed to read from %s", path.c_str());
65         return -1;
66     }
67 }
68 
Write(const std::string & path,const MemoryFile & memoryFile)69 size_t MemoryFileSystem::Write(const std::string& path, const MemoryFile& memoryFile) {
70     std::string key = GetFileName(path);
71     auto result = mMemoryFileSystem.find(key);
72     if (result != mMemoryFileSystem.end()) {
73         mMemoryFileSystem.erase(key);
74     }
75     mMemoryFileSystem.insert(std::pair<std::string, MemoryFile>(key, memoryFile));
76     return memoryFile.getFileSize();
77 }
78 
RemoveFile(const std::string & fileName)79 bool MemoryFileSystem::RemoveFile(const std::string& fileName) {
80     auto result = mMemoryFileSystem.find(fileName);
81     if (result != mMemoryFileSystem.end()) {
82         mMemoryFileSystem.erase(result);
83         return true;
84     } else {
85         ALOGE("Cannot find license to remove: %s", fileName.c_str());
86         return false;
87     }
88 }
89 
RemoveAllFiles()90 bool MemoryFileSystem::RemoveAllFiles() {
91     mMemoryFileSystem.clear();
92     return mMemoryFileSystem.empty();
93 }
94 
95 }  // namespace clearkeydrm
96