bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++ |
| 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 | |
| 17 | #include <iostream> |
| 18 | #include <vector> |
| 19 | #include <sstream> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/statfs.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | #include <dirent.h> |
| 25 | #include <limits.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | #include <libgen.h> |
| 30 | |
| 31 | #include "btree.hpp" |
| 32 | #include "mtp.h" |
| 33 | #include "MtpDebug.h" |
| 34 | |
| 35 | |
| 36 | Node::Node() { |
| 37 | mtpid= -1; |
| 38 | path = ""; |
| 39 | left = NULL; |
| 40 | right = NULL; |
| 41 | parent = NULL; |
| 42 | mtpparentid = 0; |
| 43 | } |
| 44 | |
| 45 | void Node::setMtpid(int aMtpid) { mtpid = aMtpid; } |
| 46 | void Node::setPath(std::string aPath) { path = aPath; } |
Dees Troy | 2e07c04 | 2014-09-04 15:00:57 +0000 | [diff] [blame] | 47 | void Node::rename(std::string aPath) { |
| 48 | path = aPath; |
| 49 | updateProperty(MTP_PROPERTY_OBJECT_FILE_NAME, 0, basename(aPath.c_str()), MTP_TYPE_STR); |
| 50 | updateProperty(MTP_PROPERTY_NAME, 0, basename(aPath.c_str()), MTP_TYPE_STR); |
| 51 | updateProperty(MTP_PROPERTY_DISPLAY_NAME, 0, basename(aPath.c_str()), MTP_TYPE_STR); |
| 52 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 53 | void Node::setLeft(Node* aLeft) { left = aLeft; } |
| 54 | void Node::setRight(Node* aRight) { right = aRight; } |
| 55 | void Node::setParent(Node* aParent) { parent = aParent; } |
| 56 | void Node::setMtpParentId(int id) { |
| 57 | mtpparentid = id; |
| 58 | MTPD("setting mtpparentid: %i on mtpid: %i\n", mtpparentid, mtpid); |
| 59 | } |
| 60 | int Node::Mtpid() { return mtpid; } |
| 61 | int Node::getMtpParentId() { return mtpparentid; } |
| 62 | std::string Node::getPath() { return path; } |
| 63 | Node* Node::Left() { return left; } |
| 64 | Node* Node::Right() { return right; } |
| 65 | Node* Node::Parent() { return parent; } |
| 66 | |
| 67 | uint64_t Node::getIntProperty(uint64_t property) { |
| 68 | for (unsigned index = 0; index < mtpProp.size(); ++index) { |
| 69 | if (mtpProp[index].property == property) |
| 70 | return mtpProp[index].valueInt; |
| 71 | } |
| 72 | MTPE("Node::getIntProperty failed to find property %x, returning -1\n", (unsigned)property); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | void Node::addProperty(uint64_t property, uint64_t valueInt, std::string valueStr, int dataType) { |
| 77 | MTPD("adding str property: %lld, valueInt: %lld, valueStr: %s, dataType: %d\n", property, valueInt, valueStr.c_str(), dataType); |
| 78 | struct mtpProperty prop; |
| 79 | prop.property = property; |
| 80 | prop.valueInt = valueInt; |
| 81 | prop.valueStr = valueStr; |
| 82 | prop.dataType = dataType; |
| 83 | mtpProp.push_back(prop); |
| 84 | } |
| 85 | |
| 86 | void Node::updateProperty(uint64_t property, uint64_t valueInt, std::string valueStr, int dataType) { |
| 87 | for (unsigned i = 0; i < mtpProp.size(); i++) { |
| 88 | if (mtpProp[i].property == property) { |
| 89 | mtpProp[i].valueInt = valueInt; |
| 90 | mtpProp[i].valueStr = valueStr; |
| 91 | mtpProp[i].dataType = dataType; |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | addProperty(property, valueInt, valueStr, dataType); |
| 96 | } |
| 97 | |
| 98 | std::vector<Node::mtpProperty>& Node::getMtpProps() { |
| 99 | return mtpProp; |
| 100 | } |
| 101 | |
| 102 | void Node::addProperties(int storageID, int parent_object) { |
| 103 | struct stat st; |
| 104 | int mFormat = 0; |
| 105 | uint64_t puid; |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 106 | off_t file_size = 0; |
| 107 | std::string mtimeStr = "00101T000000"; |
| 108 | std::string atimeStr = "00101T000000"; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 109 | |
| 110 | std::string mtpidStr = static_cast<std::ostringstream*>( &(std::ostringstream() << mtpid) )->str(); |
| 111 | std::string storageIDStr = static_cast<std::ostringstream*>( &(std::ostringstream() << storageID) )->str(); |
| 112 | std::string puidStr = storageIDStr + mtpidStr; |
| 113 | if ( ! (std::istringstream(puidStr) >> puid) ) puid = 0; |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 114 | mFormat = MTP_FORMAT_UNDEFINED; // file |
| 115 | if (lstat(getPath().c_str(), &st) == 0) { |
| 116 | file_size = st.st_size; |
| 117 | if (S_ISDIR(st.st_mode)) |
| 118 | mFormat = MTP_FORMAT_ASSOCIATION; // folder |
| 119 | mtimeStr = static_cast<std::ostringstream*>( &(std::ostringstream() << st.st_mtime) )->str(); |
| 120 | atimeStr = static_cast<std::ostringstream*>( &(std::ostringstream() << st.st_atime) )->str(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 121 | } |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 122 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 123 | addProperty(MTP_PROPERTY_STORAGE_ID, storageID, "", MTP_TYPE_UINT32); |
| 124 | addProperty(MTP_PROPERTY_OBJECT_FORMAT, mFormat, "", MTP_TYPE_UINT16); |
| 125 | addProperty(MTP_PROPERTY_PROTECTION_STATUS, 0, "", MTP_TYPE_UINT16); |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 126 | addProperty(MTP_PROPERTY_OBJECT_SIZE, file_size, "", MTP_TYPE_UINT64); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 127 | addProperty(MTP_PROPERTY_OBJECT_FILE_NAME, 0, basename(getPath().c_str()), MTP_TYPE_STR); |
| 128 | MTPD("mtpid: %i, filename: '%s', parent object: %i\n", mtpid, basename(getPath().c_str()), parent_object); |
| 129 | addProperty(MTP_PROPERTY_DATE_MODIFIED, 0, mtimeStr, MTP_TYPE_STR); |
| 130 | addProperty(MTP_PROPERTY_PARENT_OBJECT, parent_object, "", MTP_TYPE_UINT32); |
| 131 | addProperty(MTP_PROPERTY_PERSISTENT_UID, puid, "", MTP_TYPE_UINT128); |
| 132 | addProperty(MTP_PROPERTY_NAME, 0, basename(getPath().c_str()), MTP_TYPE_STR); |
| 133 | addProperty(MTP_PROPERTY_DISPLAY_NAME, 0, basename(getPath().c_str()), MTP_TYPE_STR); |
| 134 | addProperty(MTP_PROPERTY_DATE_ADDED, 0, atimeStr, MTP_TYPE_STR); |
| 135 | addProperty(MTP_PROPERTY_DESCRIPTION, 0, "", MTP_TYPE_STR); |
| 136 | addProperty(MTP_PROPERTY_ARTIST, 0, "", MTP_TYPE_STR); |
| 137 | addProperty(MTP_PROPERTY_ALBUM_NAME, 0, "", MTP_TYPE_STR); |
| 138 | addProperty(MTP_PROPERTY_ALBUM_ARTIST, 0, "", MTP_TYPE_STR); |
| 139 | addProperty(MTP_PROPERTY_TRACK, 0, "", MTP_TYPE_UINT16); |
| 140 | addProperty(MTP_PROPERTY_ORIGINAL_RELEASE_DATE, 0, "00101T000000", MTP_TYPE_STR); |
| 141 | addProperty(MTP_PROPERTY_DURATION, 0, "", MTP_TYPE_UINT32); |
| 142 | addProperty(MTP_PROPERTY_GENRE, 0, "", MTP_TYPE_STR); |
| 143 | addProperty(MTP_PROPERTY_COMPOSER, 0, "", MTP_TYPE_STR); |
| 144 | addProperty(MTP_PROPERTY_ARTIST, 0, "", MTP_TYPE_STR); |
| 145 | addProperty(MTP_PROPERTY_ALBUM_NAME, 0, "", MTP_TYPE_STR); |
| 146 | addProperty(MTP_PROPERTY_DURATION, 0, "", MTP_TYPE_UINT32); |
| 147 | addProperty(MTP_PROPERTY_DESCRIPTION, 0, "", MTP_TYPE_STR); |
| 148 | } |