blob: 245debf6009ea5ec40e4b085155ba44807b49765 [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001/*
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"
Ethan Yonker1b67fff2015-01-27 08:50:48 -060031#include "../tw_atomic.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040032
33class MtpDatabase;
that9e0593e2014-10-08 00:01:24 +020034struct inotify_event;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040035
36class MtpStorage {
37
38private:
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;
47 MtpServer* mServer;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040048 typedef std::map<int, Tree*> maptree;
49 typedef maptree::iterator iter;
50 maptree mtpmap;
51 std::string mtpstorageparent;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040052 android::Mutex mMutex;
53
54public:
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();
59
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; }
that9e0593e2014-10-08 00:01:24 +020070
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 bigbiffc7eee6f2014-09-02 18:59:01 -040080 int createDB();
81 MtpObjectHandleList* getObjectList(MtpStorageID storageID, MtpObjectHandle parent);
82 int getObjectInfo(MtpObjectHandle handle, MtpObjectInfo& info);
that9e0593e2014-10-08 00:01:24 +020083 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 bigbiffc7eee6f2014-09-02 18:59:01 -040085 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);
that9e0593e2014-10-08 00:01:24 +020089 int getObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, PropEntry& prop);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040090 void lockMutex(int thread_type);
91 void unlockMutex(int thread_type);
92
93private:
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040094 pthread_t inotify();
95 int inotify_t();
96 typedef int (MtpStorage::*ThreadPtr)(void);
97 typedef void* (*PThreadPtr)(void *);
that9e0593e2014-10-08 00:01:24 +020098 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400104 bool sendEvents;
that9e0593e2014-10-08 00:01:24 +0200105 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400114 bool use_mutex;
115 pthread_mutex_t inMutex; // inotify mutex
116 pthread_mutex_t mtpMutex; // main mtp mutex
Ethan Yonker1b67fff2015-01-27 08:50:48 -0600117 TWAtomicInt inotify_thread_kill;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400118};
119
120#endif // _MTP_STORAGE_H