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 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 36 | Node::Node() |
| 37 | : handle(-1), parent(0), name("") |
| 38 | { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 39 | } |
| 40 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 41 | Node::Node(MtpObjectHandle handle, MtpObjectHandle parent, const std::string& name) |
| 42 | : handle(handle), parent(parent), name(name) |
| 43 | { |
Dees Troy | 2e07c04 | 2014-09-04 15:00:57 +0000 | [diff] [blame] | 44 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 45 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 46 | void Node::rename(const std::string& newName) { |
| 47 | name = newName; |
| 48 | updateProperty(MTP_PROPERTY_OBJECT_FILE_NAME, 0, name.c_str(), MTP_TYPE_STR); |
| 49 | updateProperty(MTP_PROPERTY_NAME, 0, name.c_str(), MTP_TYPE_STR); |
| 50 | updateProperty(MTP_PROPERTY_DISPLAY_NAME, 0, name.c_str(), MTP_TYPE_STR); |
| 51 | } |
| 52 | |
| 53 | MtpObjectHandle Node::Mtpid() const { return handle; } |
| 54 | MtpObjectHandle Node::getMtpParentId() const { return parent; } |
| 55 | const std::string& Node::getName() const { return name; } |
| 56 | |
| 57 | uint64_t Node::getIntProperty(MtpPropertyCode property) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 58 | for (unsigned index = 0; index < mtpProp.size(); ++index) { |
| 59 | if (mtpProp[index].property == property) |
| 60 | return mtpProp[index].valueInt; |
| 61 | } |
| 62 | MTPE("Node::getIntProperty failed to find property %x, returning -1\n", (unsigned)property); |
| 63 | return -1; |
| 64 | } |
| 65 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 66 | const Node::mtpProperty& Node::getProperty(MtpPropertyCode property) { |
| 67 | static const mtpProperty dummyProp; |
| 68 | for (size_t i = 0; i < mtpProp.size(); ++i) { |
| 69 | if (mtpProp[i].property == property) |
| 70 | return mtpProp[i]; |
| 71 | } |
| 72 | MTPE("Node::getProperty failed to find property %x, returning dummy property\n", (unsigned)property); |
| 73 | return dummyProp; |
| 74 | } |
| 75 | |
| 76 | void Node::addProperty(MtpPropertyCode property, uint64_t valueInt, std::string valueStr, MtpDataType dataType) { |
| 77 | // MTPD("adding property: %lld, valueInt: %lld, valueStr: %s, dataType: %d\n", property, valueInt, valueStr.c_str(), dataType); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 86 | void Node::updateProperty(MtpPropertyCode property, uint64_t valueInt, std::string valueStr, MtpDataType dataType) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 102 | void Node::addProperties(const std::string& path, int storageID) { |
| 103 | MTPD("addProperties: handle: %u, filename: '%s'\n", handle, getName().c_str()); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 104 | struct stat st; |
| 105 | int mFormat = 0; |
| 106 | uint64_t puid; |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 107 | off_t file_size = 0; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 108 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 109 | std::string mtpidStr = static_cast<std::ostringstream*>( &(std::ostringstream() << handle) )->str(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 110 | std::string storageIDStr = static_cast<std::ostringstream*>( &(std::ostringstream() << storageID) )->str(); |
| 111 | std::string puidStr = storageIDStr + mtpidStr; |
| 112 | if ( ! (std::istringstream(puidStr) >> puid) ) puid = 0; |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 113 | mFormat = MTP_FORMAT_UNDEFINED; // file |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 114 | if (lstat(path.c_str(), &st) == 0) { |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 115 | file_size = st.st_size; |
| 116 | if (S_ISDIR(st.st_mode)) |
| 117 | mFormat = MTP_FORMAT_ASSOCIATION; // folder |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 118 | } |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 119 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 120 | // TODO: don't store properties with constant values at all, add them at query time instead |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 121 | addProperty(MTP_PROPERTY_STORAGE_ID, storageID, "", MTP_TYPE_UINT32); |
| 122 | addProperty(MTP_PROPERTY_OBJECT_FORMAT, mFormat, "", MTP_TYPE_UINT16); |
| 123 | addProperty(MTP_PROPERTY_PROTECTION_STATUS, 0, "", MTP_TYPE_UINT16); |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 124 | addProperty(MTP_PROPERTY_OBJECT_SIZE, file_size, "", MTP_TYPE_UINT64); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 125 | addProperty(MTP_PROPERTY_OBJECT_FILE_NAME, 0, getName().c_str(), MTP_TYPE_STR); |
| 126 | addProperty(MTP_PROPERTY_DATE_MODIFIED, st.st_mtime, "", MTP_TYPE_UINT64); |
| 127 | addProperty(MTP_PROPERTY_PARENT_OBJECT, parent, "", MTP_TYPE_UINT32); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 128 | addProperty(MTP_PROPERTY_PERSISTENT_UID, puid, "", MTP_TYPE_UINT128); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 129 | // TODO: we can't really support persistent UIDs without a persistent DB. |
| 130 | // probably a combination of volume UUID + st_ino would come close. |
| 131 | // doesn't help for fs with no native inodes numbers like fat though... |
| 132 | addProperty(MTP_PROPERTY_NAME, 0, getName().c_str(), MTP_TYPE_STR); |
| 133 | addProperty(MTP_PROPERTY_DISPLAY_NAME, 0, getName().c_str(), MTP_TYPE_STR); |
| 134 | addProperty(MTP_PROPERTY_DATE_ADDED, st.st_mtime, "", MTP_TYPE_UINT64); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 140 | addProperty(MTP_PROPERTY_ORIGINAL_RELEASE_DATE, 2014, "", MTP_TYPE_UINT64); // TODO: extract year from st.st_mtime? |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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 | } |