blob: e20dab4021001088b6bcc4858d1cfd50e223f54b [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 }
215 MTPE("MtpStorage::getObjectInfo no object found, error!\n");
216 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 }
293 MTPE("MtpStorage::getObjectFilePath fauled to find handle: %i\n", handle);
294 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 }
443 MTPE("MtpStorage::deleteFile deleting handle: %d FAILED\n", handle);
444 return -1;
445end:
446 if (local_parent_id) {
447 deleteTrees(local_parent_id);
448 }
449 return 0;
450}
451
452int MtpStorage::getObjectPropertyList(MtpObjectHandle handle, uint32_t format, uint32_t property, int groupCode, int depth, MtpDataPacket& packet) {
453 Node *n;
454 int local_mtpid = 0;
455 int local_mtpparentid = 0;
456 std::vector<int> propertyCodes;
457 std::vector<int> dataTypes;
458 std::vector<std::string> valueStrs;
459 std::vector<int> longValues;
460 int count = 0;
461 MTPD("MtpStorage::getObjectPropertyList handle: %d, format: %d, property: %lx\n", handle, format, property);
462 if (property == MTP_PROPERTY_OBJECT_FORMAT) {
463 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_OBJECT_FORMAT\n");
464 MTPD("mtpmap count: %d\n", mtpmap.size());
465 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
466 MTPD("root: %d\n", i->second->Root());
467 Node *node = i->second->findNode(handle, i->second->Root());
468 MTPD("index: %d\n", index);
469 MTPD("node: %d\n", node);
470 if (node != NULL) {
471 uint64_t longval = node->getIntProperty(MTP_PROPERTY_OBJECT_FORMAT);
472 local_mtpparentid = i->second->getMtpParentId(node);
473 MTPD("object format longValue: %llu\n", longval);
474 propertyCodes.push_back(MTP_PROPERTY_OBJECT_FORMAT);
475 longValues.push_back(node->getIntProperty(MTP_PROPERTY_OBJECT_FORMAT));
476 valueStrs.push_back("");
477 dataTypes.push_back(4);
478 count = 1;
479 local_mtpid = node->Mtpid();
480 goto endloop;
481 }
482 }
483 }
484 else if (property == MTP_PROPERTY_STORAGE_ID) {
485 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_STORAGE_ID\n");
486 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
487 MTPD("root: %d\n", i->second->Root());
488 Node *node = i->second->findNode(handle, i->second->Root());
489 if (node != NULL) {
490 propertyCodes.push_back(MTP_PROPERTY_STORAGE_ID);
491 longValues.push_back(getStorageID());
492 valueStrs.push_back("");
493 dataTypes.push_back(4);
494 count = 1;
495 local_mtpid = node->Mtpid();
496 goto endloop;
497 }
498 }
499 }
500 else if (property == MTP_PARENT_ROOT) {
501 MTPD("MtpStorage::getObjectPropertyList MTP_PARENT_ROOT\n");
502 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
503 MTPD("root: %d\n", i->second->Root());
504 Node* node = i->second->findNode(handle, i->second->Root());
505 if (node != NULL) {
506 local_mtpparentid = i->second->getMtpParentId(node);
507 MTPD("path: %s\n", i->second->getPath(node).c_str());
508 MTPD("mtpparentid: %d going to endloop\n", local_mtpparentid);
509 std::vector<Node::mtpProperty> mtpprop = node->getMtpProps();
510 count = mtpprop.size();
511 for (int i = 0; i < count; ++i) {
512 propertyCodes.push_back(mtpprop[i].property);
513 longValues.push_back(mtpprop[i].valueInt);
514 valueStrs.push_back(mtpprop[i].valueStr);
515 dataTypes.push_back(mtpprop[i].dataType);
516 }
517 local_mtpid = node->Mtpid();
518 goto endloop;
519 }
520 }
521 }
522 else if (property == MTP_PROPERTY_PROTECTION_STATUS) {
523 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_PROTECTION_STATUS\n");
524 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
525 MTPD("root: %d\n", i->second->Root());
526 Node *node = i->second->findNode(handle, i->second->Root());
527 if (node != NULL) {
528 propertyCodes.push_back(MTP_PROPERTY_PROTECTION_STATUS);
529 longValues.push_back(0);
530 valueStrs.push_back("");
531 dataTypes.push_back(8);
532 count = 1;
533 local_mtpid = node->Mtpid();
534 goto endloop;
535 }
536 }
537 }
538 else if (property == MTP_PROPERTY_OBJECT_SIZE) {
539 MTPD("MtpStorage::getObjectPropertyList MTP_PROPERTY_OBJECT_SIZE\n");
540 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
541 MTPD("root: %d\n", i->second->Root());
542 Node *node = i->second->findNode(handle, i->second->Root());
543 if (node != NULL) {
544 struct stat st;
545 uint64_t size;
546 lstat(node->getPath().c_str(), &st);
547 size = st.st_size;
548 propertyCodes.push_back(MTP_PROPERTY_OBJECT_SIZE);
549 longValues.push_back(size);
550 valueStrs.push_back("");
551 dataTypes.push_back(8);
552 count = 1;
553 local_mtpid = node->Mtpid();
554 goto endloop;
555 }
556 }
557 }
558 else {
559 MTPE("MtpStorage::getObjectPropertyList unsupported property %x\n", property);
560 return -1;
561 }
562
563endloop:
564 MTPD("mtpparentid: %d\n", local_mtpparentid);
565 MTPD("count: %d\n", count);
566 packet.putUInt32(count);
567
568 if (count > 0) {
569 std::string stringValuesArray;
570 for (int i = 0; i < count; ++i) {
571 packet.putUInt32(local_mtpid);
572 packet.putUInt16(propertyCodes[i]);
573 MTPD("dataTypes: %d\n", dataTypes[i]);
574 packet.putUInt16(dataTypes[i]);
575 MTPD("propertyCode: %s\n", MtpDebug::getObjectPropCodeName(propertyCodes[i]));
576 MTPD("longValues: %d\n", longValues[i]);
577 switch (dataTypes[i]) {
578 case MTP_TYPE_INT8:
579 MTPD("MTP_TYPE_INT8\n");
580 packet.putInt8(longValues[i]);
581 break;
582 case MTP_TYPE_UINT8:
583 MTPD("MTP_TYPE_UINT8\n");
584 packet.putUInt8(longValues[i]);
585 break;
586 case MTP_TYPE_INT16:
587 MTPD("MTP_TYPE_INT16\n");
588 packet.putInt16(longValues[i]);
589 break;
590 case MTP_TYPE_UINT16:
591 MTPD("MTP_TYPE_UINT16\n");
592 packet.putUInt16(longValues[i]);
593 break;
594 case MTP_TYPE_INT32:
595 MTPD("MTP_TYPE_INT32\n");
596 packet.putInt32(longValues[i]);
597 break;
598 case MTP_TYPE_UINT32:
599 MTPD("MTP_TYPE_UINT32\n");
600 packet.putUInt32(longValues[i]);
601 break;
602 case MTP_TYPE_INT64:
603 MTPD("MTP_TYPE_INT64\n");
604 packet.putInt64(longValues[i]);
605 break;
606 case MTP_TYPE_UINT64:
607 MTPD("MTP_TYPE_UINT64\n");
608 packet.putUInt64(longValues[i]);
609 break;
610 case MTP_TYPE_INT128:
611 MTPD("MTP_TYPE_INT128\n");
612 packet.putInt128(longValues[i]);
613 break;
614 case MTP_TYPE_UINT128:
615 MTPD("MTP_TYPE_UINT128\n");
616 packet.putUInt128(longValues[i]);
617 break;
618 case MTP_TYPE_STR:
619 MTPD("MTP_TYPE_STR: %s\n", valueStrs[i].c_str());
620 packet.putString((const char*) valueStrs[i].c_str());
621 break;
622 default:
623 MTPE("bad or unsupported data type: %i in MyMtpDatabase::getObjectPropertyList", dataTypes[i]);
624 break;
625 }
626 }
627 }
628 return 0;
629}
630
631int MtpStorage::renameObject(MtpObjectHandle handle, std::string newName) {
632 int index;
633 MTPD("MtpStorage::renameObject, handle: %d, new name: '%s'\n", handle, newName.c_str());
634 if (handle == MTP_PARENT_ROOT) {
635 MTPE("parent == MTP_PARENT_ROOT, cannot rename root\n");
636 return -1;
637 } else {
638 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
639 MTPD("root: %d\n", i->second->Root());
640 Node* node = i->second->findNode(handle, i->second->Root());
641 if (node != NULL) {
642 std::string oldName = i->second->getPath(node);
643 std::string parentdir = oldName.substr(0, oldName.find_last_of('/'));
644 std::string newFullName = parentdir + "/" + newName;
645 MTPD("old: '%s', new: '%s'\n", oldName.c_str(), newFullName.c_str());
646 if (rename(oldName.c_str(), newFullName.c_str()) == 0) {
647 node->setPath(newFullName);
648 return 0;
649 } else {
650 MTPE("MtpStorage::renameObject failed, handle: %d, new name: '%s'\n", handle, newName.c_str());
651 return -1;
652 }
653 }
654 }
655 }
656 MTPE("MtpStorage::renameObject handle / node not found, error!\n");
657 return -1;
658}
659
660void MtpStorage::createEmptyDir(const char* path) {
661 Node *node;
662 ++mtpparentid;
663 MtpStorageID storage = getStorageID();
664 MTPD("MtpStorage::createEmptyDir path: '%s', storage: %i, mtpparentid: %d\n", path, storage, mtpparentid);
665 mtpmap[mtpparentid] = new Tree();
666 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
667 node = i->second->findNodePath(path, i->second->Root());
668 if (node != NULL) {
669 mtpmap[mtpparentid]->setMtpParentId(mtpparentid, node);
670 }
671 }
672}
673
674int MtpStorage::getObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, uint64_t &longValue) {
675 Node *node;
676 for (iter i = mtpmap.begin(); i != mtpmap.end(); i++) {
677 node = i->second->findNode(handle, i->second->Root());
678 if (node != NULL) {
679 longValue = node->getIntProperty(property);
680 return 0;
681 }
682 }
683 MTPE("MtpStorage::getObjectPropertyValue unable to locate handle: %i\n", handle);
684 return -1;
685}
686pthread_t MtpStorage::inotify(void) {
687 pthread_t thread;
688 ThreadPtr inotifyptr = &MtpStorage::inotify_t;
689 PThreadPtr p = *(PThreadPtr*)&inotifyptr;
690 pthread_create(&thread, NULL, p, this);
691 return thread;
692}
693
694int MtpStorage::addInotifyDirs(std::string path) {
695 struct dirent *de;
696 DIR *d;
697 struct stat st;
698 std::string inotifypath;
699
700 d = opendir(path.c_str());
701 if (d == NULL) {
702 MTPE("MtpStorage::addInotifyDirs unable to open '%s'\n", path.c_str());
703 closedir(d);
704 return -1;
705 }
706
707 while ((de = readdir(d)) != NULL) {
708 if (de->d_type != DT_DIR || strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
709 continue;
710 inotifypath = path + "/" + de->d_name;
711 if (addInotifyDirs(inotifypath)) {
712 closedir(d);
713 return -1;
714 }
715 inotify_wd = inotify_add_watch(inotify_fd, inotifypath.c_str(), IN_CREATE | IN_DELETE);
716 inotifymap[inotify_wd] = inotifypath;
717 MTPD("added inotify dir: '%s'\n", inotifypath.c_str());
718 }
719 closedir(d);
720 return 0;
721}
722
723int MtpStorage::inotify_t(void) {
724 int len, i = 0;
725 int local_mtpparentid;
726 Node* node = NULL;
727 struct stat st;
728 #define EVENT_SIZE ( sizeof(struct inotify_event) )
729 #define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16) )
730 char buf[EVENT_BUF_LEN];
731 std::string item, parent = "";
732
733 MTPD("starting inotify thread\n");
734 inotify_fd = inotify_init();
735
736 if (inotify_fd < 0){
737 MTPE("Can't run inotify for mtp server\n");
738 }
739
740 inotify_wd = inotify_add_watch(inotify_fd, getPath(), WATCH_FLAGS);
741 inotifymap[inotify_wd] = getPath();
742 if (addInotifyDirs(getPath())) {
743 MTPE("MtpStorage::inotify_t failed to add watches to directories\n");
744 for (std::map<int, std::string>::iterator i = inotifymap.begin(); i != inotifymap.end(); i++) {
745 inotify_rm_watch(inotify_fd, i->first);
746 }
747 close(inotify_fd);
748 return -1;
749 }
750
751 while (true) {
752 i = 0;
753 len = read(inotify_fd, buf, EVENT_BUF_LEN);
754
755 if (len < 0) {
756 MTPE("inotify_t Can't read inotify events\n");
757 }
758
759 while (i < len) {
760 struct inotify_event *event = ( struct inotify_event * ) &buf[ i ];
761 if ( event->len ) {
762 if (inotifymap[event->wd].empty()) {
763 MTPE("Unable to locate inotify_wd: %i\n", event->wd);
764 goto end;
765 } else {
766 item = inotifymap[event->wd];
767 item = item + "/" + event->name;
768 MTPD("inotify_t item: '%s'\n", item.c_str());
769 if (event->mask & IN_CREATE || event->mask & IN_MOVED_TO) {
770 lockMutex(1);
771 if (event->mask & IN_ISDIR) {
772 MTPD("inotify_t create is dir\n");
773 } else {
774 MTPD("inotify_t create is file\n");
775 }
776 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
777 node = i->second->findNodePath(item, i->second->Root());
778 if (node != NULL)
779 break;
780 }
781 if (node == NULL) {
782 parent = item.substr(0, item.find_last_of('/'));
783 MTPD("parent: %s\n", parent.c_str());
784 if (parent == getPath()) {
785 local_mtpparentid = 1;
786 } else {
787 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
788 node = i->second->findNodePath(parent, i->second->Root());
789 MTPD("searching for node: %d\n", (int)node);
790 if (node != NULL) {
791 local_mtpparentid = i->second->getMtpParentId(node);
792 break;
793 }
794 }
795 if (node == NULL) {
796 MTPE("inotify_t unable to locate mtparentid\n");
797 goto end;
798 }
799 }
800 ++mtpid;
801 MTPD("mtpid: %d\n", mtpid);
802 MTPD("mtpparentid1: %d\n", local_mtpparentid);
803 node = mtpmap[local_mtpparentid]->addNode(mtpid, item);
804 mtpmap[local_mtpparentid]->setMtpParentId(local_mtpparentid, node);
805 node->addProperties(getStorageID(), getParentObject(parent));
806 if (event->mask & IN_ISDIR) {
807 createEmptyDir(item.c_str());
808 }
809 mServer->sendObjectAdded(mtpid);
810 } else {
811 MTPD("inotify_t item already exists.\n");
812 }
813 if (event->mask & IN_ISDIR) {
814 inotify_wd = inotify_add_watch(inotify_fd, item.c_str(), WATCH_FLAGS);
815 inotifymap[inotify_wd] = item;
816 MTPD("added inotify dir: '%s'\n", item.c_str());
817 MTPD("inotify_t scanning new dir\n");
818 readParentDirs(item);
819 std::string mtpParent;
820 while (!mtpParentList.empty()) {
821 mtpParent = mtpParentList.front();
822 mtpParentList.pop_front();
823 readParentDirs(mtpParent);
824 inotify_wd = inotify_add_watch(inotify_fd, mtpParent.c_str(), WATCH_FLAGS);
825 inotifymap[inotify_wd] = mtpParent;
826 MTPD("added inotify dir: '%s'\n", mtpParent.c_str());
827 }
828 }
829 } else if (event->mask & IN_DELETE || event->mask & IN_MOVED_FROM) {
830 lockMutex(1);
831 if (event->mask & IN_ISDIR) {
832 MTPD("inotify_t Directory %s deleted\n", event->name);
833 } else {
834 MTPD("inotify_t File %s deleted\n", event->name);
835 }
836 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
837 node = i->second->findNodePath(item, i->second->Root());
838 if (node != NULL)
839 break;
840 }
841 if (node != NULL && node->Mtpid() > 0) {
842 int local_id = node->Mtpid();
843 node = NULL;
844 deleteFile(local_id);
845 mServer->sendObjectRemoved(local_id);
846 } else {
847 MTPD("inotify_t already removed.\n");
848 }
849 if (event->mask & IN_ISDIR) {
850 std::string orig_item = item + "/";
851 size_t item_size = orig_item.size();
852 std::string path_check;
853 for (std::map<int, std::string>::iterator i = inotifymap.begin(); i != inotifymap.end(); i++) {
854 if ((i->second.size() > item_size && i->second.substr(0, item_size) == orig_item) || i->second == item) {
855 inotify_rm_watch(inotify_fd, i->first);
856 MTPD("inotify_t removing watch on '%s'\n", i->second.c_str());
857 inotifymap.erase(i->first);
858 }
859 }
860 }
861 } else if (event->mask & IN_MODIFY) {
862 MTPD("inotify_t item %s modified.\n", event->name);
863 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
864 node = i->second->findNodePath(item, i->second->Root());
865 if (node != NULL)
866 break;
867 }
868 if (node != NULL) {
869 uint64_t orig_size = node->getIntProperty(MTP_PROPERTY_OBJECT_SIZE);
870 struct stat st;
871 lstat(item.c_str(), &st);
872 uint64_t new_size = (uint64_t)st.st_size;
873 if (orig_size != new_size) {
874 MTPD("size changed from %llu to %llu on mtpid: %i\n", orig_size, new_size, node->Mtpid());
875 node->updateProperty(MTP_PROPERTY_OBJECT_SIZE, new_size, "", MTP_TYPE_UINT64);
876 mServer->sendObjectUpdated(node->Mtpid());
877 }
878 } else {
879 MTPE("inotify_t modified item not found\n");
880 }
881 }
882 }
883 }
884end:
885 unlockMutex(1);
886 i += EVENT_SIZE + event->len;
887 }
888 }
889
890 for (std::map<int, std::string>::iterator i = inotifymap.begin(); i != inotifymap.end(); i++) {
891 inotify_rm_watch(inotify_fd, i->first);
892 }
893 close(inotify_fd);
894 return 0;
895}
896
897int MtpStorage::getParentObject(std::string parent_path) {
898 Node* node;
899 if (parent_path == getPath()) {
900 MTPD("MtpStorage::getParentObject for: '%s' returning: 0 for root\n", parent_path.c_str());
901 return 0;
902 }
903 for (iter i = mtpmap.begin(); i != mtpmap.end(); ++i) {
904 node = i->second->findNodePath(parent_path, i->second->Root());
905 if (node != NULL) {
906 MTPD("MtpStorage::getParentObject for: '%s' returning: %i\n", parent_path.c_str(), node->Mtpid());
907 return node->Mtpid();
908 }
909 }
910 MTPE("MtpStorage::getParentObject for: '%s' unable to locate node\n", parent_path.c_str());
911 return -1;
912}
913
914void MtpStorage::lockMutex(int thread_type) {
915 if (!use_mutex)
916 return; // mutex is disabled
917 if (thread_type) {
918 // inotify thread
919 pthread_mutex_lock(&inMutex);
920 while (pthread_mutex_trylock(&mtpMutex)) {
921 pthread_mutex_unlock(&inMutex);
922 usleep(32000);
923 pthread_mutex_lock(&inMutex);
924 }
925 } else {
926 // main mtp thread
927 pthread_mutex_lock(&mtpMutex);
928 while (pthread_mutex_trylock(&inMutex)) {
929 pthread_mutex_unlock(&mtpMutex);
930 usleep(13000);
931 pthread_mutex_lock(&mtpMutex);
932 }
933 }
934}
935
936void MtpStorage::unlockMutex(int thread_type) {
937 if (!use_mutex)
938 return; // mutex is disabled
939 pthread_mutex_unlock(&inMutex);
940 pthread_mutex_unlock(&mtpMutex);
941}