blob: 1bca1d96f4773cbe3a3cb5c3d0fe347a5088f74a [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 *
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
that9e0593e2014-10-08 00:01:24 +020036Node::Node()
37 : handle(-1), parent(0), name("")
38{
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040039}
40
that9e0593e2014-10-08 00:01:24 +020041Node::Node(MtpObjectHandle handle, MtpObjectHandle parent, const std::string& name)
42 : handle(handle), parent(parent), name(name)
43{
Dees Troy2e07c042014-09-04 15:00:57 +000044}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040045
that9e0593e2014-10-08 00:01:24 +020046void 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
53MtpObjectHandle Node::Mtpid() const { return handle; }
54MtpObjectHandle Node::getMtpParentId() const { return parent; }
55const std::string& Node::getName() const { return name; }
56
57uint64_t Node::getIntProperty(MtpPropertyCode property) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040058 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
that9e0593e2014-10-08 00:01:24 +020066const 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
76void 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 bigbiffc7eee6f2014-09-02 18:59:01 -040078 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
that9e0593e2014-10-08 00:01:24 +020086void Node::updateProperty(MtpPropertyCode property, uint64_t valueInt, std::string valueStr, MtpDataType dataType) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040087 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
98std::vector<Node::mtpProperty>& Node::getMtpProps() {
99 return mtpProp;
100}
101
that9e0593e2014-10-08 00:01:24 +0200102void Node::addProperties(const std::string& path, int storageID) {
103 MTPD("addProperties: handle: %u, filename: '%s'\n", handle, getName().c_str());
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400104 struct stat st;
105 int mFormat = 0;
106 uint64_t puid;
Ethan Yonker241a3ce2014-09-04 12:59:27 -0500107 off_t file_size = 0;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400108
that9e0593e2014-10-08 00:01:24 +0200109 std::string mtpidStr = static_cast<std::ostringstream*>( &(std::ostringstream() << handle) )->str();
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400110 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 Yonker241a3ce2014-09-04 12:59:27 -0500113 mFormat = MTP_FORMAT_UNDEFINED; // file
that9e0593e2014-10-08 00:01:24 +0200114 if (lstat(path.c_str(), &st) == 0) {
Ethan Yonker241a3ce2014-09-04 12:59:27 -0500115 file_size = st.st_size;
116 if (S_ISDIR(st.st_mode))
117 mFormat = MTP_FORMAT_ASSOCIATION; // folder
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400118 }
Ethan Yonker241a3ce2014-09-04 12:59:27 -0500119
that9e0593e2014-10-08 00:01:24 +0200120 // TODO: don't store properties with constant values at all, add them at query time instead
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400121 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 Yonker241a3ce2014-09-04 12:59:27 -0500124 addProperty(MTP_PROPERTY_OBJECT_SIZE, file_size, "", MTP_TYPE_UINT64);
that9e0593e2014-10-08 00:01:24 +0200125 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400128 addProperty(MTP_PROPERTY_PERSISTENT_UID, puid, "", MTP_TYPE_UINT128);
that9e0593e2014-10-08 00:01:24 +0200129 // 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 bigbiffc7eee6f2014-09-02 18:59:01 -0400135 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);
that9e0593e2014-10-08 00:01:24 +0200140 addProperty(MTP_PROPERTY_ORIGINAL_RELEASE_DATE, 2014, "", MTP_TYPE_UINT64); // TODO: extract year from st.st_mtime?
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400141 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}