blob: 8a0a4514f88f259ce0962ff6d2cd29649442bba8 [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#include "MtpDebug.h"
20#include "MtpStorage.h"
21#include "MtpDataPacket.h"
22#include "MtpServer.h"
23#include "MtpEventPacket.h"
24
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/statfs.h>
28#include <unistd.h>
29#include <dirent.h>
30#include <errno.h>
31#include <string.h>
32#include <stdio.h>
33#include <limits.h>
34#include <pthread.h>
35#include <signal.h>
36#include <sys/inotify.h>
37#include <fcntl.h>
38#include <sstream>
39
40#define WATCH_FLAGS ( IN_CREATE | IN_DELETE | IN_MOVE | IN_MODIFY )
41
42static int mtpid = 0;
43
44MtpStorage::MtpStorage(MtpStorageID id, const char* filePath,
45 const char* description, uint64_t reserveSpace,
46 bool removable, uint64_t maxFileSize, MtpServer* refserver)
47 : mStorageID(id),
48 mFilePath(filePath),
49 mDescription(description),
50 mMaxCapacity(0),
51 mMaxFileSize(maxFileSize),
52 mReserveSpace(reserveSpace),
53 mRemovable(removable),
54 mServer(refserver)
55{
56 MTPI("MtpStorage id: %d path: %s\n", id, filePath);
57 mtpparentid = 0;
58 inotify_thread = 0;
59 sendEvents = false;
60 use_mutex = true;
61 if (pthread_mutex_init(&mtpMutex, NULL) != 0) {
62 MTPE("Failed to init mtpMutex\n");
63 use_mutex = false;
64 }
65 if (pthread_mutex_init(&inMutex, NULL) != 0) {
66 MTPE("Failed to init inMutex\n");
67 use_mutex = false;
68 }
69
70}
71
72MtpStorage::~MtpStorage() {
73 if (inotify_thread) {
74 pthread_kill(inotify_thread, 0);
75 for (std::map<int, std::string>::iterator i = inotifymap.begin(); i != inotifymap.end(); i++) {
76 inotify_rm_watch(inotify_fd, i->first);
77 }
78 close(inotify_fd);
79 }
80 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
81 delete i->second;
82 }
83 if (use_mutex) {
84 use_mutex = false;
85 pthread_mutex_destroy(&mtpMutex);
86 pthread_mutex_destroy(&inMutex);
87 }
88}
89
90int MtpStorage::getType() const {
91 return (mRemovable ? MTP_STORAGE_REMOVABLE_RAM : MTP_STORAGE_FIXED_RAM);
92}
93
94int MtpStorage::getFileSystemType() const {
95 return MTP_STORAGE_FILESYSTEM_HIERARCHICAL;
96}
97
98int MtpStorage::getAccessCapability() const {
99 return MTP_STORAGE_READ_WRITE;
100}
101
102uint64_t MtpStorage::getMaxCapacity() {
103 if (mMaxCapacity == 0) {
104 struct statfs stat;
105 if (statfs(getPath(), &stat))
106 return -1;
107 mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize;
108 }
109 return mMaxCapacity;
110}
111
112uint64_t MtpStorage::getFreeSpace() {
113 struct statfs stat;
114 if (statfs(getPath(), &stat))
115 return -1;
116 uint64_t freeSpace = (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
117 return (freeSpace > mReserveSpace ? freeSpace - mReserveSpace : 0);
118}
119
120const char* MtpStorage::getDescription() const {
121 return (const char *)mDescription;
122}
123
124int MtpStorage::createDB() {
125 std::string mtpParent = "";
126 mtpstorageparent = getPath();
127 readParentDirs(getPath());
128 while (!mtpParentList.empty()) {
129 mtpParent = mtpParentList.front();
130 mtpParentList.pop_front();
131 readParentDirs(mtpParent);
132 }
133 MTPD("MtpStorage::createDB DONE\n");
134 if (use_mutex) {
135 MTPD("Starting inotify thread\n");
136 sendEvents = true;
137 inotify_thread = inotify();
138 } else {
139 MTPD("NOT starting inotify thread\n");
140 }
141 return 0;
142}
143
144MtpObjectHandleList* MtpStorage::getObjectList(MtpStorageID storageID, MtpObjectHandle parent) {
145 std::vector<int> mtpids;
146 int local_mtpparentid;
147 MTPD("MtpStorage::getObjectList\n");
148 MTPD("parent: %d\n", parent);
149 //append object id (numerical #s) of database to int array
150 MtpObjectHandleList* list = new MtpObjectHandleList();
151 if (parent == MTP_PARENT_ROOT) {
152 MTPD("parent == MTP_PARENT_ROOT\n");
153 local_mtpparentid = 1;
154 }
155 else {
156 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
157 MTPD("root: %d\n", i->second->Root());
158 Node* node = i->second->findNode(parent, i->second->Root());
159 if (node != NULL) {
160 local_mtpparentid = i->second->getMtpParentId(node);
161 MTPD("path: %s\n", i->second->getPath(node).c_str());
162 MTPD("mtpparentid: %d going to endloop\n", local_mtpparentid);
163 goto endloop;
164 }
165 }
166 }
167 MTPD("got to endloop\n");
168 endloop:
169
170 if (mtpmap[local_mtpparentid] == NULL) {
171 MTPD("mtpmap[mtpparentid] == NULL, returning\n");
172 return list;
173 }
174
175 MTPD("root: %d\n", mtpmap[local_mtpparentid]->Root());
176 mtpmap[local_mtpparentid]->getmtpids(mtpmap[local_mtpparentid]->Root(), &mtpids);
177 MTPD("here, mtpids->size(): %i\n", mtpids.size());
178
179 for (unsigned index = 0; index < mtpids.size(); index++) {
180 MTPD("mtpidhere[%i]: %d\n", index, mtpids.at(index));
181 list->push(mtpids.at(index));
182 }
183 return list;
184}
185
186int MtpStorage::getObjectInfo(MtpObjectHandle handle, MtpObjectInfo& info) {
187 struct stat st;
188 uint64_t size;
189 MTPD("MtpStorage::getObjectInfo handle: %d\n", handle);
190 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
191 Node* node = i->second->findNode(handle, i->second->Root());
192 MTPD("node returned: %d\n", node);
193 if (node != NULL) {
194 MTPD("found mtpid: %d\n", node->Mtpid());
195 info.mStorageID = getStorageID();
196 MTPD("info.mStorageID: %d\n", info.mStorageID);
197 info.mParent = node->getMtpParentId();
198 MTPD("mParent: %d\n", info.mParent);
199 lstat(node->getPath().c_str(), &st);
200 size = st.st_size;
201 MTPD("size is: %llu\n", size);
202 info.mCompressedSize = size;//(size > 0xFFFFFFFFLL ? 0xFFFFFFFF : size);
203 info.mDateModified = st.st_mtime;
204 if (S_ISDIR(st.st_mode)) {
205 info.mFormat = MTP_FORMAT_ASSOCIATION;
206 }
207 else {
208 info.mFormat = MTP_FORMAT_UNDEFINED;
209 }
210 info.mName = strdup(basename(node->getPath().c_str()));
211 MTPD("MtpStorage::getObjectInfo found, Exiting getObjectInfo()\n");
212 return 0;
213 }
214 }
Ethan Yonker5e083dc2014-09-03 14:46:41 -0500215 // Item is not on this storage device
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400216 return -1;
217}
218
219MtpObjectHandle MtpStorage::beginSendObject(const char* path,
220 MtpObjectFormat format,
221 MtpObjectHandle parent,
222 MtpStorageID storage,
223 uint64_t size,
224 time_t modified) {
225 MTPD("MtpStorage::beginSendObject(), path: '%s', parent: %d, storage: %d, format: %04x\n", path, parent, storage, format);
226 Node* node;
227 std::string parentdir;
228 std::string pathstr(path);
229 int parent_id;
230 parentdir = pathstr.substr(0, pathstr.find_last_of('/'));
231 MTPD("MtpStorage::beginSendObject() parentdir: %s\n", parentdir.c_str());
232 if (parentdir.compare(mtpstorageparent) == 0) {
233 // root directory
234 MTPD("MtpStorage::beginSendObject() root dir\n");
235 parent_id = 1;
236 ++mtpid;
237 node = mtpmap[parent_id]->addNode(mtpid, path);
238 MTPD("node: %d\n", node);
239 node->addProperties(storage, 0);
240 if (format == MTP_FORMAT_ASSOCIATION) {
241 createEmptyDir(path);
242 }
243 return mtpid;
244 } else {
245 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
246 node = i->second->findNodePath(parentdir, i->second->Root());
247 if (node != NULL) {
248 MTPD("mtpid: %d\n", mtpid);
249 MTPD("path: %s\n", i->second->getPath(node).c_str());
250 parentdir = i->second->getPath(node);
251 parent = i->second->getMtpParentId(node);
252 if (parent == 0) {
253 MTPD("MtpStorage::beginSendObject parent is 0, error.\n");
254 return -1;
255 } else {
256 ++mtpid;
257 node = mtpmap[parent]->addNode(mtpid, path);
258 node->addProperties(getStorageID(), getParentObject(parentdir));
259 for (iter i2 = mtpmap.begin(); i2 != mtpmap.end(); i2++) {
260 node = i2->second->findNodePath(path, i2->second->Root());
261 if (node != NULL) {
262 i2->second->setMtpParentId(parent, node);
263 }
264 }
265 if (format == MTP_FORMAT_ASSOCIATION) {
266 createEmptyDir(path);
267 }
268 }
269 return mtpid;
270 }
271 }
272 }
273 MTPE("MtpStorage::beginSendObject(), path: '%s', parent: %d, storage: %d, format: %04x\n", path, parent, storage, format);
274 return -1;
275}
276
277int MtpStorage::getObjectFilePath(MtpObjectHandle handle, MtpString& outFilePath, int64_t& outFileLength, MtpObjectFormat& outFormat) {
278 struct stat st;
279 Node* node;
280 MTPD("MtpStorage::getObjectFilePath handle: %i\n", handle);
281 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
282 MTPD("handle: %d\n", handle);
283 node = i->second->findNode(handle, i->second->Root());
284 MTPD("node returned: %d\n", node);
285 if (node != NULL) {
286 lstat(node->getPath().c_str(), &st);
287 outFileLength = st.st_size;
288 outFilePath = strdup(node->getPath().c_str());
289 MTPD("outFilePath: %s\n", node->getPath().c_str());
290 goto end;
291 }
292 }
Ethan Yonker5e083dc2014-09-03 14:46:41 -0500293 // Item is not on this storage
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400294 return -1;
295end:
296 outFormat = MTP_FORMAT_ASSOCIATION;
297 return 0;
298}
299
300int MtpStorage::readParentDirs(std::string path) {
301 struct dirent *de;
302 struct stat st;
303 DIR *d;
304 std::string parent, item, prevparent = "";
305 Node* node;
306 int storageID = getStorageID();
307
308 d = opendir(path.c_str());
309 MTPD("opening '%s'\n", path.c_str());
310 if (d == NULL) {
311 MTPD("error opening '%s' -- error: %s\n", path.c_str(), strerror(errno));
312 closedir(d);
313 }
314 while ((de = readdir(d)) != NULL) {
315 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") == 0)
316 continue;
317 if (de->d_type == DT_DIR && strcmp(de->d_name, "..") != 0) {
318 // Handle dirs
319 item = path + "/" + de->d_name;
320 MTPD("dir: %s\n", item.c_str());
321 mtpParentList.push_back(item);
322 parent = item.substr(0, item.find_last_of('/'));
323 ++mtpid;
324 MTPD("parent: %s\n", parent.c_str());
325 MTPD("mtpid: %d\n", mtpid);
326 if (prevparent != parent) {
327 mtpparentid++;
328 MTPD("Handle dirs, prevparent != parent, mtpparentid: %d\n", mtpparentid);
329 mtpmap[mtpparentid] = new Tree();
330 MTPD("prevparent addNode\n");
331 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
332 node = i->second->findNodePath(parent, i->second->Root());
333 if (node != NULL) {
334 i->second->setMtpParentId(mtpparentid, node);
335 }
336 }
337 node = mtpmap[mtpparentid]->addNode(mtpid, item);
338 node->addProperties(storageID, getParentObject(path));
339 if (sendEvents)
340 mServer->sendObjectAdded(mtpid);
341 }
342 else {
343 MTPD("add node\n");
344 mtpmap[mtpparentid]->addNode(mtpid, item);
345 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
346 node = i->second->findNodePath(item, i->second->Root());
347 if (node != NULL) {
348 i->second->setMtpParentId(mtpparentid, node);
349 node->addProperties(storageID, getParentObject(path));
350 }
351 }
352 if (sendEvents)
353 mServer->sendObjectAdded(mtpid);
354 }
355 prevparent = parent;
356 }
357 else {
358 if (strcmp(de->d_name, "..") != 0) {
359 // Handle files
360 item = path + "/" + de->d_name;
361 MTPD("file: %s\n", item.c_str());
362 parent = item.substr(0, item.find_last_of('/'));
363 MTPD("parent: %s\n", parent.c_str());
364 ++mtpid;
365 MTPD("mtpid: %d\n", mtpid);
366 if (prevparent != parent) {
367 mtpparentid++;
368 MTPD("mtpparentid1: %d\n", mtpparentid);
369 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
370 node = i->second->findNodePath(path, i->second->Root());
371 if (node != NULL) {
372 i->second->setMtpParentId(mtpparentid, node);
373 node->addProperties(storageID, getParentObject(path));
374 }
375 }
376 }
377 if (mtpmap[mtpparentid] == NULL) {
378 mtpmap[mtpparentid] = new Tree();
379 }
380 MTPD("blank addNode\n");
381 node = mtpmap[mtpparentid]->addNode(mtpid, item);
382 node->addProperties(storageID, getParentObject(path));
383 prevparent = parent;
384 if (sendEvents)
385 mServer->sendObjectAdded(mtpid);
386 }
387 else {
388 // Handle empty dirs?
389 MTPD("checking for empty dir '%s'\n", path.c_str());
390 int count = 0;
391 DIR *dirc;
392 struct dirent *ep;
393 dirc = opendir(path.c_str());
394 if (dirc != NULL) {
395 while ((ep = readdir(dirc)))
396 ++count;
397 MTPD("count: %d\n", count);
398 closedir(dirc);
399 }
400 if (count == 2) {
401 MTPD("'%s' is an empty dir\n", path.c_str());
402 createEmptyDir(path.c_str());
403 goto end;
404 }
405 }
406 }
407 }
408 end:
409 closedir(d);
410 return 0;
411}
412
413void MtpStorage::deleteTrees(int parent) {
414 Node* node = mtpmap[parent]->Root();
415 MTPD("MtpStorage::deleteTrees deleting %i\n", parent);
416 while (node != NULL) {
417 if (node->getIntProperty(MTP_PROPERTY_OBJECT_FORMAT) == MTP_FORMAT_ASSOCIATION) {
418 deleteTrees(node->getMtpParentId());
419 }
420 node = mtpmap[parent]->getNext(node);
421 }
422 delete mtpmap[parent];
423 mtpmap.erase(parent);
424 MTPD("MtpStorage::deleteTrees deleted %i\n", parent);
425}
426
427int MtpStorage::deleteFile(MtpObjectHandle handle) {
428 int local_parent_id = 0;
429 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
430 MTPD("MtpStorage::deleteFile handle: %d\n", handle);
431 Node* node = i->second->findNode(handle, i->second->Root());
432 MTPD("MtpStorage::deleteFile node returned: %d\n", node);
433 if (node != NULL) {
434 if (node->getIntProperty(MTP_PROPERTY_OBJECT_FORMAT) == MTP_FORMAT_ASSOCIATION) {
435 local_parent_id = node->getMtpParentId();
436 }
437 MTPD("deleting handle: %d\n", handle);
438 i->second->deleteNode(handle);
439 MTPD("deleted\n");
440 goto end;
441 }
442 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400443 return -1;
444end:
445 if (local_parent_id) {
446 deleteTrees(local_parent_id);
447 }
448 return 0;
449}
450
451int MtpStorage::getObjectPropertyList(MtpObjectHandle handle, uint32_t format, uint32_t property, int groupCode, int depth, MtpDataPacket& packet) {
452 Node *n;
453 int local_mtpid = 0;
454 int local_mtpparentid = 0;
455 std::vector<int> propertyCodes;
456 std::vector<int> dataTypes;
457 std::vector<std::string> valueStrs;
458 std::vector<int> longValues;
459 int count = 0;
460 MTPD("MtpStorage::getObjectPropertyList handle: %d, format: %d, property: %lx\n", handle, format, property);
461 if (property == MTP_PROPERTY_OBJECT_FORMAT) {
462 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_OBJECT_FORMAT\n");
463 MTPD("mtpmap count: %d\n", mtpmap.size());
464 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
465 MTPD("root: %d\n", i->second->Root());
466 Node *node = i->second->findNode(handle, i->second->Root());
467 MTPD("index: %d\n", index);
468 MTPD("node: %d\n", node);
469 if (node != NULL) {
470 uint64_t longval = node->getIntProperty(MTP_PROPERTY_OBJECT_FORMAT);
471 local_mtpparentid = i->second->getMtpParentId(node);
472 MTPD("object format longValue: %llu\n", longval);
473 propertyCodes.push_back(MTP_PROPERTY_OBJECT_FORMAT);
474 longValues.push_back(node->getIntProperty(MTP_PROPERTY_OBJECT_FORMAT));
475 valueStrs.push_back("");
476 dataTypes.push_back(4);
477 count = 1;
478 local_mtpid = node->Mtpid();
479 goto endloop;
480 }
481 }
482 }
483 else if (property == MTP_PROPERTY_STORAGE_ID) {
484 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_STORAGE_ID\n");
485 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
486 MTPD("root: %d\n", i->second->Root());
487 Node *node = i->second->findNode(handle, i->second->Root());
488 if (node != NULL) {
489 propertyCodes.push_back(MTP_PROPERTY_STORAGE_ID);
490 longValues.push_back(getStorageID());
491 valueStrs.push_back("");
492 dataTypes.push_back(4);
493 count = 1;
494 local_mtpid = node->Mtpid();
495 goto endloop;
496 }
497 }
498 }
499 else if (property == MTP_PARENT_ROOT) {
500 MTPD("MtpStorage::getObjectPropertyList MTP_PARENT_ROOT\n");
501 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
502 MTPD("root: %d\n", i->second->Root());
503 Node* node = i->second->findNode(handle, i->second->Root());
504 if (node != NULL) {
505 local_mtpparentid = i->second->getMtpParentId(node);
506 MTPD("path: %s\n", i->second->getPath(node).c_str());
507 MTPD("mtpparentid: %d going to endloop\n", local_mtpparentid);
508 std::vector<Node::mtpProperty> mtpprop = node->getMtpProps();
509 count = mtpprop.size();
510 for (int i = 0; i < count; ++i) {
511 propertyCodes.push_back(mtpprop[i].property);
512 longValues.push_back(mtpprop[i].valueInt);
513 valueStrs.push_back(mtpprop[i].valueStr);
514 dataTypes.push_back(mtpprop[i].dataType);
515 }
516 local_mtpid = node->Mtpid();
517 goto endloop;
518 }
519 }
520 }
521 else if (property == MTP_PROPERTY_PROTECTION_STATUS) {
522 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_PROTECTION_STATUS\n");
523 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
524 MTPD("root: %d\n", i->second->Root());
525 Node *node = i->second->findNode(handle, i->second->Root());
526 if (node != NULL) {
527 propertyCodes.push_back(MTP_PROPERTY_PROTECTION_STATUS);
528 longValues.push_back(0);
529 valueStrs.push_back("");
530 dataTypes.push_back(8);
531 count = 1;
532 local_mtpid = node->Mtpid();
533 goto endloop;
534 }
535 }
536 }
537 else if (property == MTP_PROPERTY_OBJECT_SIZE) {
538 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_OBJECT_SIZE\n");
539 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
540 MTPD("root: %d\n", i->second->Root());
541 Node *node = i->second->findNode(handle, i->second->Root());
542 if (node != NULL) {
543 struct stat st;
544 uint64_t size;
545 lstat(node->getPath().c_str(), &st);
546 size = st.st_size;
547 propertyCodes.push_back(MTP_PROPERTY_OBJECT_SIZE);
548 longValues.push_back(size);
549 valueStrs.push_back("");
550 dataTypes.push_back(8);
551 count = 1;
552 local_mtpid = node->Mtpid();
553 goto endloop;
554 }
555 }
556 }
557 else {
Ethan Yonker5e083dc2014-09-03 14:46:41 -0500558 // Either the property is not supported or the handle is not on this storage
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400559 return -1;
560 }
561
562endloop:
563 MTPD("mtpparentid: %d\n", local_mtpparentid);
564 MTPD("count: %d\n", count);
565 packet.putUInt32(count);
566
567 if (count > 0) {
568 std::string stringValuesArray;
569 for (int i = 0; i < count; ++i) {
570 packet.putUInt32(local_mtpid);
571 packet.putUInt16(propertyCodes[i]);
572 MTPD("dataTypes: %d\n", dataTypes[i]);
573 packet.putUInt16(dataTypes[i]);
574 MTPD("propertyCode: %s\n", MtpDebug::getObjectPropCodeName(propertyCodes[i]));
575 MTPD("longValues: %d\n", longValues[i]);
576 switch (dataTypes[i]) {
577 case MTP_TYPE_INT8:
578 MTPD("MTP_TYPE_INT8\n");
579 packet.putInt8(longValues[i]);
580 break;
581 case MTP_TYPE_UINT8:
582 MTPD("MTP_TYPE_UINT8\n");
583 packet.putUInt8(longValues[i]);
584 break;
585 case MTP_TYPE_INT16:
586 MTPD("MTP_TYPE_INT16\n");
587 packet.putInt16(longValues[i]);
588 break;
589 case MTP_TYPE_UINT16:
590 MTPD("MTP_TYPE_UINT16\n");
591 packet.putUInt16(longValues[i]);
592 break;
593 case MTP_TYPE_INT32:
594 MTPD("MTP_TYPE_INT32\n");
595 packet.putInt32(longValues[i]);
596 break;
597 case MTP_TYPE_UINT32:
598 MTPD("MTP_TYPE_UINT32\n");
599 packet.putUInt32(longValues[i]);
600 break;
601 case MTP_TYPE_INT64:
602 MTPD("MTP_TYPE_INT64\n");
603 packet.putInt64(longValues[i]);
604 break;
605 case MTP_TYPE_UINT64:
606 MTPD("MTP_TYPE_UINT64\n");
607 packet.putUInt64(longValues[i]);
608 break;
609 case MTP_TYPE_INT128:
610 MTPD("MTP_TYPE_INT128\n");
611 packet.putInt128(longValues[i]);
612 break;
613 case MTP_TYPE_UINT128:
614 MTPD("MTP_TYPE_UINT128\n");
615 packet.putUInt128(longValues[i]);
616 break;
617 case MTP_TYPE_STR:
618 MTPD("MTP_TYPE_STR: %s\n", valueStrs[i].c_str());
619 packet.putString((const char*) valueStrs[i].c_str());
620 break;
621 default:
622 MTPE("bad or unsupported data type: %i in MyMtpDatabase::getObjectPropertyList", dataTypes[i]);
623 break;
624 }
625 }
626 }
627 return 0;
628}
629
630int MtpStorage::renameObject(MtpObjectHandle handle, std::string newName) {
631 int index;
632 MTPD("MtpStorage::renameObject, handle: %d, new name: '%s'\n", handle, newName.c_str());
633 if (handle == MTP_PARENT_ROOT) {
634 MTPE("parent == MTP_PARENT_ROOT, cannot rename root\n");
635 return -1;
636 } else {
637 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
638 MTPD("root: %d\n", i->second->Root());
639 Node* node = i->second->findNode(handle, i->second->Root());
640 if (node != NULL) {
641 std::string oldName = i->second->getPath(node);
642 std::string parentdir = oldName.substr(0, oldName.find_last_of('/'));
643 std::string newFullName = parentdir + "/" + newName;
644 MTPD("old: '%s', new: '%s'\n", oldName.c_str(), newFullName.c_str());
645 if (rename(oldName.c_str(), newFullName.c_str()) == 0) {
646 node->setPath(newFullName);
647 return 0;
648 } else {
649 MTPE("MtpStorage::renameObject failed, handle: %d, new name: '%s'\n", handle, newName.c_str());
650 return -1;
651 }
652 }
653 }
654 }
Ethan Yonker6f49e112014-09-03 21:42:49 -0500655 // handle not found on this storage
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400656 return -1;
657}
658
659void MtpStorage::createEmptyDir(const char* path) {
660 Node *node;
661 ++mtpparentid;
662 MtpStorageID storage = getStorageID();
663 MTPD("MtpStorage::createEmptyDir path: '%s', storage: %i, mtpparentid: %d\n", path, storage, mtpparentid);
664 mtpmap[mtpparentid] = new Tree();
665 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
666 node = i->second->findNodePath(path, i->second->Root());
667 if (node != NULL) {
668 mtpmap[mtpparentid]->setMtpParentId(mtpparentid, node);
669 }
670 }
671}
672
673int MtpStorage::getObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, uint64_t &longValue) {
674 Node *node;
675 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
676 node = i->second->findNode(handle, i->second->Root());
677 if (node != NULL) {
678 longValue = node->getIntProperty(property);
679 return 0;
680 }
681 }
Ethan Yonker5e083dc2014-09-03 14:46:41 -0500682 // handle not found on this storage
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400683 return -1;
684}
685pthread_t MtpStorage::inotify(void) {
686 pthread_t thread;
687 ThreadPtr inotifyptr = &MtpStorage::inotify_t;
688 PThreadPtr p = *(PThreadPtr*)&inotifyptr;
689 pthread_create(&thread, NULL, p, this);
690 return thread;
691}
692
693int MtpStorage::addInotifyDirs(std::string path) {
694 struct dirent *de;
695 DIR *d;
696 struct stat st;
697 std::string inotifypath;
698
699 d = opendir(path.c_str());
700 if (d == NULL) {
701 MTPE("MtpStorage::addInotifyDirs unable to open '%s'\n", path.c_str());
702 closedir(d);
703 return -1;
704 }
705
706 while ((de = readdir(d)) != NULL) {
707 if (de->d_type != DT_DIR || strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
708 continue;
709 inotifypath = path + "/" + de->d_name;
710 if (addInotifyDirs(inotifypath)) {
711 closedir(d);
712 return -1;
713 }
714 inotify_wd = inotify_add_watch(inotify_fd, inotifypath.c_str(), IN_CREATE | IN_DELETE);
715 inotifymap[inotify_wd] = inotifypath;
716 MTPD("added inotify dir: '%s'\n", inotifypath.c_str());
717 }
718 closedir(d);
719 return 0;
720}
721
722int MtpStorage::inotify_t(void) {
723 int len, i = 0;
724 int local_mtpparentid;
725 Node* node = NULL;
726 struct stat st;
727 #define EVENT_SIZE ( sizeof(struct inotify_event) )
728 #define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16) )
729 char buf[EVENT_BUF_LEN];
730 std::string item, parent = "";
731
732 MTPD("starting inotify thread\n");
733 inotify_fd = inotify_init();
734
735 if (inotify_fd < 0){
736 MTPE("Can't run inotify for mtp server\n");
737 }
738
739 inotify_wd = inotify_add_watch(inotify_fd, getPath(), WATCH_FLAGS);
740 inotifymap[inotify_wd] = getPath();
741 if (addInotifyDirs(getPath())) {
742 MTPE("MtpStorage::inotify_t failed to add watches to directories\n");
743 for (std::map<int, std::string>::iterator i = inotifymap.begin(); i != inotifymap.end(); i++) {
744 inotify_rm_watch(inotify_fd, i->first);
745 }
746 close(inotify_fd);
747 return -1;
748 }
749
750 while (true) {
751 i = 0;
752 len = read(inotify_fd, buf, EVENT_BUF_LEN);
753
754 if (len < 0) {
755 MTPE("inotify_t Can't read inotify events\n");
756 }
757
758 while (i < len) {
759 struct inotify_event *event = ( struct inotify_event * ) &buf[ i ];
760 if ( event->len ) {
761 if (inotifymap[event->wd].empty()) {
762 MTPE("Unable to locate inotify_wd: %i\n", event->wd);
763 goto end;
764 } else {
765 item = inotifymap[event->wd];
766 item = item + "/" + event->name;
767 MTPD("inotify_t item: '%s'\n", item.c_str());
768 if (event->mask & IN_CREATE || event->mask & IN_MOVED_TO) {
769 lockMutex(1);
770 if (event->mask & IN_ISDIR) {
771 MTPD("inotify_t create is dir\n");
772 } else {
773 MTPD("inotify_t create is file\n");
774 }
775 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
776 node = i->second->findNodePath(item, i->second->Root());
777 if (node != NULL)
778 break;
779 }
780 if (node == NULL) {
781 parent = item.substr(0, item.find_last_of('/'));
782 MTPD("parent: %s\n", parent.c_str());
783 if (parent == getPath()) {
784 local_mtpparentid = 1;
785 } else {
786 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
787 node = i->second->findNodePath(parent, i->second->Root());
788 MTPD("searching for node: %d\n", (int)node);
789 if (node != NULL) {
790 local_mtpparentid = i->second->getMtpParentId(node);
791 break;
792 }
793 }
794 if (node == NULL) {
795 MTPE("inotify_t unable to locate mtparentid\n");
796 goto end;
797 }
798 }
799 ++mtpid;
800 MTPD("mtpid: %d\n", mtpid);
801 MTPD("mtpparentid1: %d\n", local_mtpparentid);
802 node = mtpmap[local_mtpparentid]->addNode(mtpid, item);
803 mtpmap[local_mtpparentid]->setMtpParentId(local_mtpparentid, node);
804 node->addProperties(getStorageID(), getParentObject(parent));
805 if (event->mask & IN_ISDIR) {
806 createEmptyDir(item.c_str());
807 }
808 mServer->sendObjectAdded(mtpid);
809 } else {
810 MTPD("inotify_t item already exists.\n");
811 }
812 if (event->mask & IN_ISDIR) {
813 inotify_wd = inotify_add_watch(inotify_fd, item.c_str(), WATCH_FLAGS);
814 inotifymap[inotify_wd] = item;
815 MTPD("added inotify dir: '%s'\n", item.c_str());
816 MTPD("inotify_t scanning new dir\n");
817 readParentDirs(item);
818 std::string mtpParent;
819 while (!mtpParentList.empty()) {
820 mtpParent = mtpParentList.front();
821 mtpParentList.pop_front();
822 readParentDirs(mtpParent);
823 inotify_wd = inotify_add_watch(inotify_fd, mtpParent.c_str(), WATCH_FLAGS);
824 inotifymap[inotify_wd] = mtpParent;
825 MTPD("added inotify dir: '%s'\n", mtpParent.c_str());
826 }
827 }
828 } else if (event->mask & IN_DELETE || event->mask & IN_MOVED_FROM) {
829 lockMutex(1);
830 if (event->mask & IN_ISDIR) {
831 MTPD("inotify_t Directory %s deleted\n", event->name);
832 } else {
833 MTPD("inotify_t File %s deleted\n", event->name);
834 }
835 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
836 node = i->second->findNodePath(item, i->second->Root());
837 if (node != NULL)
838 break;
839 }
840 if (node != NULL && node->Mtpid() > 0) {
841 int local_id = node->Mtpid();
842 node = NULL;
843 deleteFile(local_id);
844 mServer->sendObjectRemoved(local_id);
845 } else {
846 MTPD("inotify_t already removed.\n");
847 }
848 if (event->mask & IN_ISDIR) {
849 std::string orig_item = item + "/";
850 size_t item_size = orig_item.size();
851 std::string path_check;
852 for (std::map<int, std::string>::iterator i = inotifymap.begin(); i != inotifymap.end(); i++) {
853 if ((i->second.size() > item_size && i->second.substr(0, item_size) == orig_item) || i->second == item) {
854 inotify_rm_watch(inotify_fd, i->first);
855 MTPD("inotify_t removing watch on '%s'\n", i->second.c_str());
856 inotifymap.erase(i->first);
857 }
858 }
859 }
860 } else if (event->mask & IN_MODIFY) {
861 MTPD("inotify_t item %s modified.\n", event->name);
862 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
863 node = i->second->findNodePath(item, i->second->Root());
864 if (node != NULL)
865 break;
866 }
867 if (node != NULL) {
868 uint64_t orig_size = node->getIntProperty(MTP_PROPERTY_OBJECT_SIZE);
869 struct stat st;
870 lstat(item.c_str(), &st);
871 uint64_t new_size = (uint64_t)st.st_size;
872 if (orig_size != new_size) {
873 MTPD("size changed from %llu to %llu on mtpid: %i\n", orig_size, new_size, node->Mtpid());
874 node->updateProperty(MTP_PROPERTY_OBJECT_SIZE, new_size, "", MTP_TYPE_UINT64);
875 mServer->sendObjectUpdated(node->Mtpid());
876 }
877 } else {
878 MTPE("inotify_t modified item not found\n");
879 }
880 }
881 }
882 }
883end:
884 unlockMutex(1);
885 i += EVENT_SIZE + event->len;
886 }
887 }
888
889 for (std::map<int, std::string>::iterator i = inotifymap.begin(); i != inotifymap.end(); i++) {
890 inotify_rm_watch(inotify_fd, i->first);
891 }
892 close(inotify_fd);
893 return 0;
894}
895
896int MtpStorage::getParentObject(std::string parent_path) {
897 Node* node;
898 if (parent_path == getPath()) {
899 MTPD("MtpStorage::getParentObject for: '%s' returning: 0 for root\n", parent_path.c_str());
900 return 0;
901 }
902 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
903 node = i->second->findNodePath(parent_path, i->second->Root());
904 if (node != NULL) {
905 MTPD("MtpStorage::getParentObject for: '%s' returning: %i\n", parent_path.c_str(), node->Mtpid());
906 return node->Mtpid();
907 }
908 }
909 MTPE("MtpStorage::getParentObject for: '%s' unable to locate node\n", parent_path.c_str());
910 return -1;
911}
912
913void MtpStorage::lockMutex(int thread_type) {
914 if (!use_mutex)
915 return; // mutex is disabled
916 if (thread_type) {
917 // inotify thread
918 pthread_mutex_lock(&inMutex);
919 while (pthread_mutex_trylock(&mtpMutex)) {
920 pthread_mutex_unlock(&inMutex);
921 usleep(32000);
922 pthread_mutex_lock(&inMutex);
923 }
924 } else {
925 // main mtp thread
926 pthread_mutex_lock(&mtpMutex);
927 while (pthread_mutex_trylock(&inMutex)) {
928 pthread_mutex_unlock(&mtpMutex);
929 usleep(13000);
930 pthread_mutex_lock(&mtpMutex);
931 }
932 }
933}
934
935void MtpStorage::unlockMutex(int thread_type) {
936 if (!use_mutex)
937 return; // mutex is disabled
938 pthread_mutex_unlock(&inMutex);
939 pthread_mutex_unlock(&mtpMutex);
940}