• 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 #pragma once
17 
18 #include <map>
19 #include <string>
20 
21 #include "ClearKeyTypes.h"
22 
23 namespace clearkeydrm {
24 
25 // Using android file system requires clearkey plugin to update
26 // its sepolicy. However, we are unable to update sepolicy for
27 // older vendor partitions. To provide backward compatibility,
28 // clearkey plugin implements a very simple file system in memory.
29 // This memory file system does not support directory structure.
30 class MemoryFileSystem {
31   public:
32     struct MemoryFile {
33         std::string fileName;  // excludes path
34         std::string content;
35         size_t fileSize;
36 
getContentMemoryFile37         std::string getContent() const { return content; }
getFileSizeMemoryFile38         size_t getFileSize() const { return fileSize; }
setContentMemoryFile39         void setContent(const std::string& file) { content = file; }
setFileNameMemoryFile40         void setFileName(const std::string& name) { fileName = name; }
setFileSizeMemoryFile41         void setFileSize(size_t size) {
42             content.resize(size);
43             fileSize = size;
44         }
45     };
46 
MemoryFileSystem()47     MemoryFileSystem(){};
~MemoryFileSystem()48     virtual ~MemoryFileSystem(){};
49 
50     bool FileExists(const std::string& fileName) const;
51     ssize_t GetFileSize(const std::string& fileName) const;
52     std::vector<std::string> ListFiles() const;
53     size_t Read(const std::string& pathName, std::string* buffer);
54     bool RemoveAllFiles();
55     bool RemoveFile(const std::string& fileName);
56     size_t Write(const std::string& pathName, const MemoryFile& memoryFile);
57 
58   private:
59     // License file name is made up of a unique keySetId, therefore,
60     // the filename can be used as the key to locate licenses in the
61     // memory file system.
62     std::map<std::string, MemoryFile> mMemoryFileSystem;
63 
64     std::string GetFileName(const std::string& path);
65 
66     CLEARKEY_DISALLOW_COPY_AND_ASSIGN(MemoryFileSystem);
67 };
68 
69 }  // namespace clearkeydrm
70