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