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 | * |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 17 | #include <vector> |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 18 | #include <sys/stat.h> |
| 19 | #include <sys/statfs.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | #include <dirent.h> |
| 23 | #include <limits.h> |
| 24 | #include <errno.h> |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | #include <libgen.h> |
| 28 | |
| 29 | #include "btree.hpp" |
| 30 | #include "mtp.h" |
| 31 | #include "MtpDebug.h" |
| 32 | |
| 33 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 34 | Node::Node() |
| 35 | : handle(-1), parent(0), name("") |
| 36 | { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 37 | } |
| 38 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 39 | Node::Node(MtpObjectHandle handle, MtpObjectHandle parent, const std::string& name) |
| 40 | : handle(handle), parent(parent), name(name) |
| 41 | { |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 42 | MTPD("handle: %d\n", handle); |
| 43 | MTPD("parent: %d\n", parent); |
| 44 | MTPD("name: %s\n", name.c_str()); |
Dees Troy | 2e07c04 | 2014-09-04 15:00:57 +0000 | [diff] [blame] | 45 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 46 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 47 | void Node::rename(const std::string& newName) { |
| 48 | name = newName; |
| 49 | updateProperty(MTP_PROPERTY_OBJECT_FILE_NAME, 0, name.c_str(), MTP_TYPE_STR); |
| 50 | updateProperty(MTP_PROPERTY_NAME, 0, name.c_str(), MTP_TYPE_STR); |
| 51 | updateProperty(MTP_PROPERTY_DISPLAY_NAME, 0, name.c_str(), MTP_TYPE_STR); |
| 52 | } |
| 53 | |
| 54 | MtpObjectHandle Node::Mtpid() const { return handle; } |
| 55 | MtpObjectHandle Node::getMtpParentId() const { return parent; } |
| 56 | const std::string& Node::getName() const { return name; } |
| 57 | |
| 58 | uint64_t Node::getIntProperty(MtpPropertyCode property) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 59 | for (unsigned index = 0; index < mtpProp.size(); ++index) { |
| 60 | if (mtpProp[index].property == property) |
| 61 | return mtpProp[index].valueInt; |
| 62 | } |
| 63 | MTPE("Node::getIntProperty failed to find property %x, returning -1\n", (unsigned)property); |
| 64 | return -1; |
| 65 | } |
| 66 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 67 | const Node::mtpProperty& Node::getProperty(MtpPropertyCode property) { |
| 68 | static const mtpProperty dummyProp; |
| 69 | for (size_t i = 0; i < mtpProp.size(); ++i) { |
| 70 | if (mtpProp[i].property == property) |
| 71 | return mtpProp[i]; |
| 72 | } |
| 73 | MTPE("Node::getProperty failed to find property %x, returning dummy property\n", (unsigned)property); |
| 74 | return dummyProp; |
| 75 | } |
| 76 | |
| 77 | void Node::addProperty(MtpPropertyCode property, uint64_t valueInt, std::string valueStr, MtpDataType dataType) { |
| 78 | // 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] | 79 | struct mtpProperty prop; |
| 80 | prop.property = property; |
| 81 | prop.valueInt = valueInt; |
| 82 | prop.valueStr = valueStr; |
| 83 | prop.dataType = dataType; |
| 84 | mtpProp.push_back(prop); |
| 85 | } |
| 86 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 87 | 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] | 88 | for (unsigned i = 0; i < mtpProp.size(); i++) { |
| 89 | if (mtpProp[i].property == property) { |
| 90 | mtpProp[i].valueInt = valueInt; |
| 91 | mtpProp[i].valueStr = valueStr; |
| 92 | mtpProp[i].dataType = dataType; |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | addProperty(property, valueInt, valueStr, dataType); |
| 97 | } |
| 98 | |
| 99 | std::vector<Node::mtpProperty>& Node::getMtpProps() { |
| 100 | return mtpProp; |
| 101 | } |
| 102 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 103 | void Node::addProperties(const std::string& path, int storageID) { |
| 104 | MTPD("addProperties: handle: %u, filename: '%s'\n", handle, getName().c_str()); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 105 | struct stat st; |
| 106 | int mFormat = 0; |
that | a1ad19f | 2014-11-08 01:18:44 +0100 | [diff] [blame] | 107 | uint64_t puid = ((uint64_t)storageID << 32) + handle; |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 108 | off_t file_size = 0; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 109 | |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 110 | mFormat = MTP_FORMAT_UNDEFINED; // file |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 111 | if (lstat(path.c_str(), &st) == 0) { |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 112 | file_size = st.st_size; |
| 113 | if (S_ISDIR(st.st_mode)) |
| 114 | mFormat = MTP_FORMAT_ASSOCIATION; // folder |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 115 | } |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 116 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 117 | // 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] | 118 | addProperty(MTP_PROPERTY_STORAGE_ID, storageID, "", MTP_TYPE_UINT32); |
| 119 | addProperty(MTP_PROPERTY_OBJECT_FORMAT, mFormat, "", MTP_TYPE_UINT16); |
| 120 | addProperty(MTP_PROPERTY_PROTECTION_STATUS, 0, "", MTP_TYPE_UINT16); |
Ethan Yonker | 241a3ce | 2014-09-04 12:59:27 -0500 | [diff] [blame] | 121 | addProperty(MTP_PROPERTY_OBJECT_SIZE, file_size, "", MTP_TYPE_UINT64); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 122 | addProperty(MTP_PROPERTY_OBJECT_FILE_NAME, 0, getName().c_str(), MTP_TYPE_STR); |
| 123 | addProperty(MTP_PROPERTY_DATE_MODIFIED, st.st_mtime, "", MTP_TYPE_UINT64); |
| 124 | addProperty(MTP_PROPERTY_PARENT_OBJECT, parent, "", MTP_TYPE_UINT32); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 125 | addProperty(MTP_PROPERTY_PERSISTENT_UID, puid, "", MTP_TYPE_UINT128); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 126 | // TODO: we can't really support persistent UIDs without a persistent DB. |
| 127 | // probably a combination of volume UUID + st_ino would come close. |
| 128 | // doesn't help for fs with no native inodes numbers like fat though... |
that | a1ad19f | 2014-11-08 01:18:44 +0100 | [diff] [blame] | 129 | // however, Microsoft's own impl (Zune, etc.) does not support persistent UIDs either |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 130 | addProperty(MTP_PROPERTY_NAME, 0, getName().c_str(), MTP_TYPE_STR); |
| 131 | addProperty(MTP_PROPERTY_DISPLAY_NAME, 0, getName().c_str(), MTP_TYPE_STR); |
| 132 | addProperty(MTP_PROPERTY_DATE_ADDED, st.st_mtime, "", MTP_TYPE_UINT64); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 133 | addProperty(MTP_PROPERTY_DESCRIPTION, 0, "", MTP_TYPE_STR); |
| 134 | addProperty(MTP_PROPERTY_ARTIST, 0, "", MTP_TYPE_STR); |
| 135 | addProperty(MTP_PROPERTY_ALBUM_NAME, 0, "", MTP_TYPE_STR); |
| 136 | addProperty(MTP_PROPERTY_ALBUM_ARTIST, 0, "", MTP_TYPE_STR); |
| 137 | addProperty(MTP_PROPERTY_TRACK, 0, "", MTP_TYPE_UINT16); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 138 | 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] | 139 | addProperty(MTP_PROPERTY_DURATION, 0, "", MTP_TYPE_UINT32); |
| 140 | addProperty(MTP_PROPERTY_GENRE, 0, "", MTP_TYPE_STR); |
| 141 | addProperty(MTP_PROPERTY_COMPOSER, 0, "", MTP_TYPE_STR); |
| 142 | addProperty(MTP_PROPERTY_ARTIST, 0, "", MTP_TYPE_STR); |
| 143 | addProperty(MTP_PROPERTY_ALBUM_NAME, 0, "", MTP_TYPE_STR); |
| 144 | addProperty(MTP_PROPERTY_DURATION, 0, "", MTP_TYPE_UINT32); |
| 145 | addProperty(MTP_PROPERTY_DESCRIPTION, 0, "", MTP_TYPE_STR); |
| 146 | } |