bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [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 | * |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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 | * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++ |
| 17 | */ |
| 18 | |
| 19 | #ifndef _MTP_STORAGE_H |
| 20 | #define _MTP_STORAGE_H |
| 21 | |
| 22 | #include "mtp.h" |
| 23 | #include "MtpObjectInfo.h" |
| 24 | #include <string> |
| 25 | #include <deque> |
| 26 | #include <map> |
| 27 | #include <libgen.h> |
| 28 | #include <pthread.h> |
| 29 | #include "btree.hpp" |
| 30 | #include "MtpServer.h" |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 31 | #include "../../tw_atomic.hpp" |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 32 | |
| 33 | class MtpDatabase; |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 34 | struct inotify_event; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 35 | |
| 36 | class MtpStorage { |
| 37 | |
| 38 | private: |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 39 | MtpStorageID mStorageID; |
| 40 | MtpString mFilePath; |
| 41 | MtpString mDescription; |
| 42 | uint64_t mMaxCapacity; |
| 43 | uint64_t mMaxFileSize; |
| 44 | // amount of free space to leave unallocated |
| 45 | uint64_t mReserveSpace; |
| 46 | bool mRemovable; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 47 | MtpServer* mServer; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 48 | typedef std::map<int, Tree*> maptree; |
| 49 | typedef maptree::iterator iter; |
| 50 | maptree mtpmap; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 51 | std::string mtpstorageparent; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 52 | android::Mutex mMutex; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 53 | |
| 54 | public: |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 55 | MtpStorage(MtpStorageID id, const char* filePath, |
| 56 | const char* description, uint64_t reserveSpace, |
| 57 | bool removable, uint64_t maxFileSize, MtpServer* refserver); |
| 58 | virtual ~MtpStorage(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 59 | |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 60 | inline MtpStorageID getStorageID() const { return mStorageID; } |
| 61 | int getType() const; |
| 62 | int getFileSystemType() const; |
| 63 | int getAccessCapability() const; |
| 64 | uint64_t getMaxCapacity(); |
| 65 | uint64_t getFreeSpace(); |
| 66 | const char* getDescription() const; |
| 67 | inline const char* getPath() const { return (const char *)mFilePath; } |
| 68 | inline bool isRemovable() const { return mRemovable; } |
| 69 | inline uint64_t getMaxFileSize() const { return mMaxFileSize; } |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 70 | |
| 71 | struct PropEntry { |
| 72 | MtpObjectHandle handle; |
| 73 | uint16_t property; |
| 74 | uint16_t datatype; |
| 75 | uint64_t intvalue; |
| 76 | std::string strvalue; |
| 77 | }; |
| 78 | |
| 79 | int readDir(const std::string& path, Tree* tree); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 80 | int createDB(); |
| 81 | MtpObjectHandleList* getObjectList(MtpStorageID storageID, MtpObjectHandle parent); |
| 82 | int getObjectInfo(MtpObjectHandle handle, MtpObjectInfo& info); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 83 | MtpObjectHandle beginSendObject(const char* path, MtpObjectFormat format, MtpObjectHandle parent, uint64_t size, time_t modified); |
| 84 | void endSendObject(const char* path, MtpObjectHandle handle, MtpObjectFormat format, bool succeeded); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 85 | int getObjectPropertyList(MtpObjectHandle handle, uint32_t format, uint32_t property, int groupCode, int depth, MtpDataPacket& packet); |
| 86 | int getObjectFilePath(MtpObjectHandle handle, MtpString& outFilePath, int64_t& outFileLength, MtpObjectFormat& outFormat); |
| 87 | int deleteFile(MtpObjectHandle handle); |
| 88 | int renameObject(MtpObjectHandle handle, std::string newName); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 89 | int getObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, PropEntry& prop); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 90 | void lockMutex(int thread_type); |
| 91 | void unlockMutex(int thread_type); |
| 92 | |
| 93 | private: |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 94 | pthread_t inotify(); |
| 95 | int inotify_t(); |
| 96 | typedef int (MtpStorage::*ThreadPtr)(void); |
| 97 | typedef void* (*PThreadPtr)(void *); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 98 | std::map<int, Tree*> inotifymap; // inotify wd -> tree |
| 99 | pthread_t inotify_thread; |
| 100 | int inotify_fd; |
| 101 | int addInotify(Tree* tree); |
| 102 | void handleInotifyEvent(struct inotify_event* event); |
| 103 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 104 | bool sendEvents; |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 105 | MtpObjectHandle handleCurrentlySending; |
| 106 | |
| 107 | Node* addNewNode(bool isDir, Tree* tree, const std::string& name); |
| 108 | Node* findNode(MtpObjectHandle handle); |
| 109 | Node* findNodeByPath(const std::string& path); |
| 110 | std::string getNodePath(Node* node); |
| 111 | |
| 112 | void queryNodeProperties(std::vector<PropEntry>& results, Node* node, uint32_t property, int groupCode, MtpStorageID storageID); |
| 113 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 114 | bool use_mutex; |
| 115 | pthread_mutex_t inMutex; // inotify mutex |
| 116 | pthread_mutex_t mtpMutex; // main mtp mutex |
Ethan Yonker | 1b67fff | 2015-01-27 08:50:48 -0600 | [diff] [blame] | 117 | TWAtomicInt inotify_thread_kill; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | #endif // _MTP_STORAGE_H |