blob: d967b4b9a453ed92549bb1a745562cd08106fc96 [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 *
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05008 * http://www.apache.org/licenses/LICENSE-2.0
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04009 *
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 bigbiffaf32bb92018-12-18 18:39:53 -050031#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:
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050039 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 bigbiffc7eee6f2014-09-02 18:59:01 -040047 MtpServer* mServer;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050048 typedef std::map<int, Tree*> maptree;
49 typedef maptree::iterator iter;
50 maptree mtpmap;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040051 std::string mtpstorageparent;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050052 android::Mutex mMutex;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040053
54public:
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050055 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 bigbiffc7eee6f2014-09-02 18:59:01 -040059
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050060 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