bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | #ifndef _MTP_STORAGE_H |
| 18 | #define _MTP_STORAGE_H |
| 19 | |
| 20 | #include "MtpObjectInfo.h" |
| 21 | #include "MtpServer.h" |
| 22 | #include "MtpStringBuffer.h" |
| 23 | #include "MtpTypes.h" |
| 24 | #include "mtp.h" |
| 25 | #include "btree.hpp" |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 26 | #include "tw_atomic.hpp" |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 27 | |
| 28 | class MtpDatabase; |
| 29 | |
| 30 | class MtpStorage { |
| 31 | |
| 32 | public: |
| 33 | struct PropEntry { |
| 34 | MtpObjectHandle handle; |
| 35 | uint16_t property; |
| 36 | uint16_t datatype; |
| 37 | uint64_t intvalue; |
| 38 | std::string strvalue; |
| 39 | }; |
| 40 | |
| 41 | private: |
| 42 | MtpStorageID mStorageID; |
| 43 | MtpStringBuffer mFilePath; |
| 44 | MtpStringBuffer mDescription; |
| 45 | uint64_t mMaxCapacity; |
| 46 | uint64_t mMaxFileSize; |
| 47 | bool mRemovable; |
| 48 | typedef std::map<int, Tree*> maptree; |
| 49 | typedef maptree::iterator iter; |
| 50 | maptree mtpmap; |
| 51 | std::string mtpstorageparent; |
| 52 | MtpObjectHandle handleCurrentlySending; |
| 53 | int inotify_fd; |
| 54 | std::map<int, Tree*> inotifymap; // inotify wd -> tree |
| 55 | bool sendEvents; |
| 56 | MtpServer* mServer; |
| 57 | typedef int (MtpStorage::*ThreadPtr)(void); |
| 58 | typedef void* (*PThreadPtr)(void *); |
| 59 | bool use_mutex; |
| 60 | pthread_mutex_t inMutex; // inotify mutex |
| 61 | pthread_mutex_t mtpMutex; // main mtp mutex |
| 62 | TWAtomicInt inotify_thread_kill; |
| 63 | pthread_t inotify_thread; |
| 64 | Node* findNode(MtpObjectHandle handle); |
| 65 | std::string getNodePath(Node* node); |
| 66 | Node* addNewNode(bool isDir, Tree* tree, const std::string& name); |
| 67 | void queryNodeProperties(std::vector<PropEntry>& results, Node* node, uint32_t property, int groupCode, MtpStorageID storageID); |
| 68 | int addInotify(Tree* tree); |
| 69 | void handleInotifyEvent(struct inotify_event* event); |
| 70 | |
| 71 | public: |
| 72 | MtpStorage(MtpStorageID id, const char* filePath, |
| 73 | const char* description, |
| 74 | bool removable, uint64_t maxFileSize, MtpServer* refserver); |
| 75 | virtual ~MtpStorage(); |
| 76 | inline MtpStorageID getStorageID() const { return mStorageID; } |
| 77 | int getType() const; |
| 78 | int getFileSystemType() const; |
| 79 | int getAccessCapability() const; |
| 80 | uint64_t getMaxCapacity(); |
| 81 | uint64_t getFreeSpace(); |
| 82 | const char* getDescription() const; |
| 83 | inline const char* getPath() const { return (const char *)mFilePath; } |
| 84 | inline bool isRemovable() const { return mRemovable; } |
| 85 | inline uint64_t getMaxFileSize() const { return mMaxFileSize; } |
| 86 | int renameObject(MtpObjectHandle handle, std::string newName); |
| 87 | MtpObjectHandle beginSendObject(const char* path, MtpObjectFormat format, MtpObjectHandle parent, uint64_t size, time_t modified); |
| 88 | MtpObjectHandleList* getObjectList(MtpStorageID storageID, MtpObjectHandle parent); |
| 89 | int getObjectPropertyList(MtpObjectHandle handle, uint32_t format, uint32_t property, int groupCode, int depth, MtpDataPacket& packet); |
| 90 | int readDir(const std::string& path, Tree* tree); |
| 91 | int getObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, PropEntry& prop); |
| 92 | int getObjectInfo(MtpObjectHandle handle, MtpObjectInfo& info); |
| 93 | void endSendObject(const char* path, MtpObjectHandle handle, MtpObjectFormat format, bool succeeded); |
| 94 | int getObjectFilePath(MtpObjectHandle handle, MtpStringBuffer& outFilePath, int64_t& outFileLength, MtpObjectFormat& outFormat); |
| 95 | int deleteFile(MtpObjectHandle handle); |
| 96 | int createDB(); |
| 97 | pthread_t inotify(); |
| 98 | int inotify_t(); |
| 99 | void lockMutex(int thread_type); |
| 100 | void unlockMutex(int thread_type); |
| 101 | }; |
| 102 | |
| 103 | #endif // _MTP_STORAGE_H |