blob: 7c57dc6bca54e2ce8b61dcf4e41afce36d58c307 [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001/*
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 bigbiffaf32bb92018-12-18 18:39:53 -05008 * http://www.apache.org/licenses/LICENSE-2.0
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04009 *
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 bigbiffc7eee6f2014-09-02 18:59:01 -040017#include <vector>
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040018#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
that9e0593e2014-10-08 00:01:24 +020034Node::Node()
35 : handle(-1), parent(0), name("")
36{
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040037}
38
that9e0593e2014-10-08 00:01:24 +020039Node::Node(MtpObjectHandle handle, MtpObjectHandle parent, const std::string& name)
40 : handle(handle), parent(parent), name(name)
41{
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050042 MTPD("handle: %d\n", handle);
43 MTPD("parent: %d\n", parent);
44 MTPD("name: %s\n", name.c_str());
Dees Troy2e07c042014-09-04 15:00:57 +000045}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040046
that9e0593e2014-10-08 00:01:24 +020047void 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
54MtpObjectHandle Node::Mtpid() const { return handle; }
55MtpObjectHandle Node::getMtpParentId() const { return parent; }
56const std::string& Node::getName() const { return name; }
57
58uint64_t Node::getIntProperty(MtpPropertyCode property) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040059 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
that9e0593e2014-10-08 00:01:24 +020067const 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
77void 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 bigbiffc7eee6f2014-09-02 18:59:01 -040079 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
that9e0593e2014-10-08 00:01:24 +020087void Node::updateProperty(MtpPropertyCode property, uint64_t valueInt, std::string valueStr, MtpDataType dataType) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040088 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
99std::vector<Node::mtpProperty>& Node::getMtpProps() {
100 return mtpProp;
101}
102
that9e0593e2014-10-08 00:01:24 +0200103void Node::addProperties(const std::string& path, int storageID) {
104 MTPD("addProperties: handle: %u, filename: '%s'\n", handle, getName().c_str());
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400105 struct stat st;
106 int mFormat = 0;
thata1ad19f2014-11-08 01:18:44 +0100107 uint64_t puid = ((uint64_t)storageID << 32) + handle;
Ethan Yonker241a3ce2014-09-04 12:59:27 -0500108 off_t file_size = 0;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400109
Ethan Yonker241a3ce2014-09-04 12:59:27 -0500110 mFormat = MTP_FORMAT_UNDEFINED; // file
that9e0593e2014-10-08 00:01:24 +0200111 if (lstat(path.c_str(), &st) == 0) {
Ethan Yonker241a3ce2014-09-04 12:59:27 -0500112 file_size = st.st_size;
113 if (S_ISDIR(st.st_mode))
114 mFormat = MTP_FORMAT_ASSOCIATION; // folder
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400115 }
Ethan Yonker241a3ce2014-09-04 12:59:27 -0500116
that9e0593e2014-10-08 00:01:24 +0200117 // TODO: don't store properties with constant values at all, add them at query time instead
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400118 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 Yonker241a3ce2014-09-04 12:59:27 -0500121 addProperty(MTP_PROPERTY_OBJECT_SIZE, file_size, "", MTP_TYPE_UINT64);
that9e0593e2014-10-08 00:01:24 +0200122 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400125 addProperty(MTP_PROPERTY_PERSISTENT_UID, puid, "", MTP_TYPE_UINT128);
that9e0593e2014-10-08 00:01:24 +0200126 // 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...
thata1ad19f2014-11-08 01:18:44 +0100129 // however, Microsoft's own impl (Zune, etc.) does not support persistent UIDs either
that9e0593e2014-10-08 00:01:24 +0200130 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400133 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);
that9e0593e2014-10-08 00:01:24 +0200138 addProperty(MTP_PROPERTY_ORIGINAL_RELEASE_DATE, 2014, "", MTP_TYPE_UINT64); // TODO: extract year from st.st_mtime?
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400139 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}