blob: cdbb73b50bea0be38efdb80269338a7e28298613 [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"
31
32class MtpDatabase;
that9e0593e2014-10-08 00:01:24 +020033struct inotify_event;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040034
35class MtpStorage {
36
37private:
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 bigbiffc7eee6f2014-09-02 18:59:01 -040047 typedef std::map<int, Tree*> maptree;
48 typedef maptree::iterator iter;
49 maptree mtpmap;
50 std::string mtpstorageparent;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040051 android::Mutex mMutex;
52
53public:
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; }
that9e0593e2014-10-08 00:01:24 +020069
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 bigbiffc7eee6f2014-09-02 18:59:01 -040079 int createDB();
80 MtpObjectHandleList* getObjectList(MtpStorageID storageID, MtpObjectHandle parent);
81 int getObjectInfo(MtpObjectHandle handle, MtpObjectInfo& info);
that9e0593e2014-10-08 00:01:24 +020082 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 bigbiffc7eee6f2014-09-02 18:59:01 -040084 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);
that9e0593e2014-10-08 00:01:24 +020088 int getObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, PropEntry& prop);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040089 void lockMutex(int thread_type);
90 void unlockMutex(int thread_type);
91
92private:
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040093 pthread_t inotify();
94 int inotify_t();
95 typedef int (MtpStorage::*ThreadPtr)(void);
96 typedef void* (*PThreadPtr)(void *);
that9e0593e2014-10-08 00:01:24 +020097 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400103 bool sendEvents;
that9e0593e2014-10-08 00:01:24 +0200104 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400113 bool use_mutex;
114 pthread_mutex_t inMutex; // inotify mutex
115 pthread_mutex_t mtpMutex; // main mtp mutex
Ethan Yonker726a0202014-12-16 20:01:38 -0600116 int inotify_thread_kill;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400117};
118
119#endif // _MTP_STORAGE_H