blob: 6fcf1199763e65e71df7b53ac3a86da65760465f [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
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
Jerry Zhange5aa05d2017-10-13 12:14:42 -070017#include <algorithm>
Jerry Zhange242f122017-10-16 14:54:08 -070018#include <android-base/logging.h>
Jerry Zhang487be612016-10-24 12:10:41 -070019#include <android-base/properties.h>
20#include <chrono>
Jerry Zhang487be612016-10-24 12:10:41 -070021#include <dirent.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <inttypes.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040025#include <stdio.h>
26#include <stdlib.h>
27#include <sys/types.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040028#include <sys/stat.h>
Mike Lockwoodd3211492010-09-13 17:15:58 -040029#include <sys/stat.h>
caozhiyuan854cb172017-04-26 16:52:30 +080030#include <sys/time.h>
Mike Lockwoodc42aa122010-06-14 17:58:08 -070031
Mike Lockwooda881b442010-09-23 22:32:05 -040032#define LOG_TAG "MtpServer"
33
Mike Lockwood16864ba2010-05-11 17:16:59 -040034#include "MtpDebug.h"
Jerry Zhange5aa05d2017-10-13 12:14:42 -070035#include "IMtpDatabase.h"
Jerry Zhang63dac452017-12-06 15:19:36 -080036#include "MtpDescriptors.h"
Jerry Zhangdf69dd32017-05-03 17:17:49 -070037#include "MtpDevHandle.h"
38#include "MtpFfsCompatHandle.h"
39#include "MtpFfsHandle.h"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070040#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040041#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040042#include "MtpServer.h"
43#include "MtpStorage.h"
44#include "MtpStringBuffer.h"
nolyn.luef9bef52018-07-05 16:24:13 +080045#include "android-base/strings.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040046
Mike Lockwood7850ef92010-05-14 10:10:36 -040047namespace android {
James Weie783e4b2019-05-28 17:18:21 +080048static const int SN_EVENT_LOG_ID = 0x534e4554;
Mike Lockwood7850ef92010-05-14 10:10:36 -040049
Mike Lockwood16864ba2010-05-11 17:16:59 -040050static const MtpOperationCode kSupportedOperationCodes[] = {
51 MTP_OPERATION_GET_DEVICE_INFO,
52 MTP_OPERATION_OPEN_SESSION,
53 MTP_OPERATION_CLOSE_SESSION,
54 MTP_OPERATION_GET_STORAGE_IDS,
55 MTP_OPERATION_GET_STORAGE_INFO,
56 MTP_OPERATION_GET_NUM_OBJECTS,
57 MTP_OPERATION_GET_OBJECT_HANDLES,
58 MTP_OPERATION_GET_OBJECT_INFO,
59 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070060 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040061 MTP_OPERATION_DELETE_OBJECT,
62 MTP_OPERATION_SEND_OBJECT_INFO,
63 MTP_OPERATION_SEND_OBJECT,
64// MTP_OPERATION_INITIATE_CAPTURE,
65// MTP_OPERATION_FORMAT_STORE,
Jerry Zhang6dafecc2017-02-23 16:39:30 -080066 MTP_OPERATION_RESET_DEVICE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040067// MTP_OPERATION_SELF_TEST,
68// MTP_OPERATION_SET_OBJECT_PROTECTION,
69// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040070 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040071 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
72 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
73 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040074// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
Jerry Zhang708b3e02017-09-26 17:53:39 -070075 MTP_OPERATION_MOVE_OBJECT,
76 MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050077 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040078// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
79 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040080 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040081 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
82 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040083 MTP_OPERATION_GET_OBJECT_PROP_LIST,
84// MTP_OPERATION_SET_OBJECT_PROP_LIST,
85// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
86// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Jeff Sharkeyb8d374b2019-12-16 14:11:11 -070087// MTP_OPERATION_GET_OBJECT_REFERENCES,
88// MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040089// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070090 // Android extension for direct file IO
91 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
92 MTP_OPERATION_SEND_PARTIAL_OBJECT,
93 MTP_OPERATION_TRUNCATE_OBJECT,
94 MTP_OPERATION_BEGIN_EDIT_OBJECT,
95 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040096};
97
Mike Lockwood873871f2010-07-12 18:54:16 -040098static const MtpEventCode kSupportedEventCodes[] = {
99 MTP_EVENT_OBJECT_ADDED,
100 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -0500101 MTP_EVENT_STORE_ADDED,
102 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800103 MTP_EVENT_DEVICE_PROP_CHANGED,
James55138432018-07-02 18:07:30 +0800104 MTP_EVENT_OBJECT_INFO_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -0400105};
106
Jerry Zhang63dac452017-12-06 15:19:36 -0800107MtpServer::MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700108 const char *deviceInfoManufacturer,
109 const char *deviceInfoModel,
110 const char *deviceInfoDeviceVersion,
111 const char *deviceInfoSerialNumber)
Jerry Zhang487be612016-10-24 12:10:41 -0700112 : mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400113 mPtp(ptp),
Alex Klyubin792298f2016-12-21 11:20:22 -0800114 mDeviceInfoManufacturer(deviceInfoManufacturer),
115 mDeviceInfoModel(deviceInfoModel),
116 mDeviceInfoDeviceVersion(deviceInfoDeviceVersion),
117 mDeviceInfoSerialNumber(deviceInfoSerialNumber),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400118 mSessionID(0),
119 mSessionOpen(false),
120 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400121 mSendObjectFormat(0),
caozhiyuan854cb172017-04-26 16:52:30 +0800122 mSendObjectFileSize(0),
123 mSendObjectModifiedTime(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400124{
Jerry Zhang63dac452017-12-06 15:19:36 -0800125 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
126 if (ffs_ok) {
127 bool aio_compat = android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false);
128 mHandle = aio_compat ? new MtpFfsCompatHandle(controlFd) : new MtpFfsHandle(controlFd);
129 } else {
130 mHandle = new MtpDevHandle();
131 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400132}
133
134MtpServer::~MtpServer() {
135}
136
Mike Lockwooda8494402011-02-18 09:07:14 -0500137void MtpServer::addStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700138 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500139
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700140 mStorages.push_back(storage);
Mike Lockwooda8494402011-02-18 09:07:14 -0500141 sendStoreAdded(storage->getStorageID());
142}
143
144void MtpServer::removeStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700145 std::lock_guard<std::mutex> lg(mMutex);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700146 auto iter = std::find(mStorages.begin(), mStorages.end(), storage);
147 if (iter != mStorages.end()) {
148 sendStoreRemoved(storage->getStorageID());
149 mStorages.erase(iter);
Mike Lockwooda8494402011-02-18 09:07:14 -0500150 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400151}
152
153MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800154 if (id == 0)
155 return mStorages[0];
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700156 for (MtpStorage *storage : mStorages) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400157 if (storage->getStorageID() == id)
158 return storage;
159 }
Jerry Zhang487be612016-10-24 12:10:41 -0700160 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400161}
162
Mike Lockwooda8494402011-02-18 09:07:14 -0500163bool MtpServer::hasStorage(MtpStorageID id) {
164 if (id == 0 || id == 0xFFFFFFFF)
165 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700166 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500167}
168
Mike Lockwood16864ba2010-05-11 17:16:59 -0400169void MtpServer::run() {
Jerry Zhang63dac452017-12-06 15:19:36 -0800170 if (mHandle->start(mPtp)) {
Jerry Zhang487be612016-10-24 12:10:41 -0700171 ALOGE("Failed to start usb driver!");
Jerry Zhang63dac452017-12-06 15:19:36 -0800172 mHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700173 return;
174 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400175
176 while (1) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800177 int ret = mRequest.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400178 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700179 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400180 if (errno == ECANCELED) {
181 // return to top of loop and wait for next command
182 continue;
183 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400184 break;
185 }
186 MtpOperationCode operation = mRequest.getOperationCode();
187 MtpTransactionID transaction = mRequest.getTransactionID();
188
Steve Block3856b092011-10-20 11:56:00 +0100189 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400190 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400191 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400192 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
193 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
194 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400195 if (dataIn) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800196 int ret = mData.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400197 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000198 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400199 if (errno == ECANCELED) {
200 // return to top of loop and wait for next command
201 continue;
202 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400203 break;
204 }
Steve Block3856b092011-10-20 11:56:00 +0100205 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400206 } else {
207 mData.reset();
208 }
209
Mike Lockwood916076c2010-06-04 09:49:21 -0400210 if (handleRequest()) {
211 if (!dataIn && mData.hasData()) {
212 mData.setOperationCode(operation);
213 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100214 ALOGV("sending data:");
Jerry Zhang63dac452017-12-06 15:19:36 -0800215 ret = mData.write(mHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400216 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000217 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400218 if (errno == ECANCELED) {
219 // return to top of loop and wait for next command
220 continue;
221 }
222 break;
223 }
224 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400225
Mike Lockwood916076c2010-06-04 09:49:21 -0400226 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang63dac452017-12-06 15:19:36 -0800228 ret = mResponse.write(mHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800229 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400230 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000231 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800232 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400233 // return to top of loop and wait for next command
234 continue;
235 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400236 break;
237 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400238 } else {
Steve Block3856b092011-10-20 11:56:00 +0100239 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400240 }
241 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400242
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700243 // commit any open edits
244 int count = mObjectEditList.size();
245 for (int i = 0; i < count; i++) {
246 ObjectEdit* edit = mObjectEditList[i];
247 commitEdit(edit);
248 delete edit;
249 }
250 mObjectEditList.clear();
251
Jerry Zhang63dac452017-12-06 15:19:36 -0800252 mHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400253}
254
Mike Lockwood873871f2010-07-12 18:54:16 -0400255void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500257 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400258}
259
260void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500262 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
263}
264
James55138432018-07-02 18:07:30 +0800265void MtpServer::sendObjectInfoChanged(MtpObjectHandle handle) {
266 ALOGV("sendObjectInfoChanged %d\n", handle);
267 sendEvent(MTP_EVENT_OBJECT_INFO_CHANGED, handle);
268}
269
Mike Lockwooda8494402011-02-18 09:07:14 -0500270void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500272 sendEvent(MTP_EVENT_STORE_ADDED, id);
273}
274
275void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100276 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500277 sendEvent(MTP_EVENT_STORE_REMOVED, id);
278}
279
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800280void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
281 ALOGV("sendDevicePropertyChanged %d\n", property);
282 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
283}
284
Mike Lockwooda8494402011-02-18 09:07:14 -0500285void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400286 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500287 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400288 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500289 mEvent.setParameter(1, param1);
Jerry Zhang63dac452017-12-06 15:19:36 -0800290 if (mEvent.write(mHandle))
Jerry Zhang487be612016-10-24 12:10:41 -0700291 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400292 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400293}
294
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700295void MtpServer::addEditObject(MtpObjectHandle handle, MtpStringBuffer& path,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700296 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700297 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700298 mObjectEditList.push_back(edit);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700299}
300
301MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
302 int count = mObjectEditList.size();
303 for (int i = 0; i < count; i++) {
304 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700305 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700306 }
Jerry Zhang487be612016-10-24 12:10:41 -0700307 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700308}
309
310void MtpServer::removeEditObject(MtpObjectHandle handle) {
311 int count = mObjectEditList.size();
312 for (int i = 0; i < count; i++) {
313 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700314 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700315 delete edit;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700316 mObjectEditList.erase(mObjectEditList.begin() + i);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700317 return;
318 }
319 }
Steve Block29357bc2012-01-06 19:20:56 +0000320 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700321}
322
323void MtpServer::commitEdit(ObjectEdit* edit) {
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700324 mDatabase->rescanFile((const char *)edit->mPath, edit->mHandle, edit->mFormat);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700325}
326
327
Mike Lockwood916076c2010-06-04 09:49:21 -0400328bool MtpServer::handleRequest() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700329 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500330
Mike Lockwood16864ba2010-05-11 17:16:59 -0400331 MtpOperationCode operation = mRequest.getOperationCode();
332 MtpResponseCode response;
333
334 mResponse.reset();
335
336 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400337 mSendObjectHandle = kInvalidObjectHandle;
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700338 mSendObjectFormat = 0;
339 mSendObjectModifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400340 }
341
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700342 int containertype = mRequest.getContainerType();
343 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
344 ALOGE("wrong container type %d", containertype);
345 return false;
346 }
347
348 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
349
Mike Lockwood16864ba2010-05-11 17:16:59 -0400350 switch (operation) {
351 case MTP_OPERATION_GET_DEVICE_INFO:
352 response = doGetDeviceInfo();
353 break;
354 case MTP_OPERATION_OPEN_SESSION:
355 response = doOpenSession();
356 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800357 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400358 case MTP_OPERATION_CLOSE_SESSION:
359 response = doCloseSession();
360 break;
361 case MTP_OPERATION_GET_STORAGE_IDS:
362 response = doGetStorageIDs();
363 break;
364 case MTP_OPERATION_GET_STORAGE_INFO:
365 response = doGetStorageInfo();
366 break;
367 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
368 response = doGetObjectPropsSupported();
369 break;
370 case MTP_OPERATION_GET_OBJECT_HANDLES:
371 response = doGetObjectHandles();
372 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400373 case MTP_OPERATION_GET_NUM_OBJECTS:
374 response = doGetNumObjects();
375 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400376 case MTP_OPERATION_GET_OBJECT_REFERENCES:
377 response = doGetObjectReferences();
378 break;
379 case MTP_OPERATION_SET_OBJECT_REFERENCES:
380 response = doSetObjectReferences();
381 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400382 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
383 response = doGetObjectPropValue();
384 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400385 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
386 response = doSetObjectPropValue();
387 break;
388 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
389 response = doGetDevicePropValue();
390 break;
391 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
392 response = doSetDevicePropValue();
393 break;
394 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
395 response = doResetDevicePropValue();
396 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400397 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
398 response = doGetObjectPropList();
399 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400400 case MTP_OPERATION_GET_OBJECT_INFO:
401 response = doGetObjectInfo();
402 break;
403 case MTP_OPERATION_GET_OBJECT:
404 response = doGetObject();
405 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700406 case MTP_OPERATION_GET_THUMB:
407 response = doGetThumb();
408 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500409 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700410 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
411 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500412 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400413 case MTP_OPERATION_SEND_OBJECT_INFO:
414 response = doSendObjectInfo();
415 break;
416 case MTP_OPERATION_SEND_OBJECT:
417 response = doSendObject();
418 break;
419 case MTP_OPERATION_DELETE_OBJECT:
420 response = doDeleteObject();
421 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700422 case MTP_OPERATION_COPY_OBJECT:
423 response = doCopyObject();
424 break;
425 case MTP_OPERATION_MOVE_OBJECT:
426 response = doMoveObject();
427 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400428 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400429 response = doGetObjectPropDesc();
430 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400431 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
432 response = doGetDevicePropDesc();
433 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700434 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
435 response = doSendPartialObject();
436 break;
437 case MTP_OPERATION_TRUNCATE_OBJECT:
438 response = doTruncateObject();
439 break;
440 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
441 response = doBeginEditObject();
442 break;
443 case MTP_OPERATION_END_EDIT_OBJECT:
444 response = doEndEditObject();
445 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400446 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700447 ALOGE("got unsupported command %s (%x)",
448 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400449 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
450 break;
451 }
452
James Weiba389222018-09-27 19:22:55 +0800453 if (response != MTP_RESPONSE_OK)
454 ALOGW("[MTP] got response 0x%X in command %s (%x)", response,
455 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood916076c2010-06-04 09:49:21 -0400456 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
457 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400458 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400459 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400460}
461
462MtpResponseCode MtpServer::doGetDeviceInfo() {
463 MtpStringBuffer string;
464
Mike Lockwood782aef12010-08-10 07:37:50 -0400465 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
466 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
467 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
468
Mike Lockwood16864ba2010-05-11 17:16:59 -0400469 // fill in device info
470 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400471 if (mPtp) {
472 mData.putUInt32(0);
473 } else {
474 // MTP Vendor Extension ID
475 mData.putUInt32(6);
476 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400477 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400478 if (mPtp) {
479 // no extensions
480 string.set("");
481 } else {
482 // MTP extensions
483 string.set("microsoft.com: 1.0; android.com: 1.0;");
484 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400485 mData.putString(string); // MTP Extensions
486 mData.putUInt16(0); //Functional Mode
487 mData.putAUInt16(kSupportedOperationCodes,
488 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400489 mData.putAUInt16(kSupportedEventCodes,
490 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400491 mData.putAUInt16(deviceProperties); // Device Properties Supported
492 mData.putAUInt16(captureFormats); // Capture Formats
493 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500494
Alex Klyubin792298f2016-12-21 11:20:22 -0800495 mData.putString(mDeviceInfoManufacturer); // Manufacturer
496 mData.putString(mDeviceInfoModel); // Model
497 mData.putString(mDeviceInfoDeviceVersion); // Device Version
498 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400499
Mike Lockwood782aef12010-08-10 07:37:50 -0400500 delete playbackFormats;
501 delete captureFormats;
502 delete deviceProperties;
503
Mike Lockwood16864ba2010-05-11 17:16:59 -0400504 return MTP_RESPONSE_OK;
505}
506
507MtpResponseCode MtpServer::doOpenSession() {
508 if (mSessionOpen) {
509 mResponse.setParameter(1, mSessionID);
510 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
511 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800512 if (mRequest.getParameterCount() < 1)
513 return MTP_RESPONSE_INVALID_PARAMETER;
514
Mike Lockwood16864ba2010-05-11 17:16:59 -0400515 mSessionID = mRequest.getParameter(1);
516 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400517
Mike Lockwood16864ba2010-05-11 17:16:59 -0400518 return MTP_RESPONSE_OK;
519}
520
521MtpResponseCode MtpServer::doCloseSession() {
522 if (!mSessionOpen)
523 return MTP_RESPONSE_SESSION_NOT_OPEN;
524 mSessionID = 0;
525 mSessionOpen = false;
526 return MTP_RESPONSE_OK;
527}
528
529MtpResponseCode MtpServer::doGetStorageIDs() {
530 if (!mSessionOpen)
531 return MTP_RESPONSE_SESSION_NOT_OPEN;
532
533 int count = mStorages.size();
534 mData.putUInt32(count);
535 for (int i = 0; i < count; i++)
536 mData.putUInt32(mStorages[i]->getStorageID());
537
538 return MTP_RESPONSE_OK;
539}
540
541MtpResponseCode MtpServer::doGetStorageInfo() {
542 MtpStringBuffer string;
543
544 if (!mSessionOpen)
545 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800546 if (mRequest.getParameterCount() < 1)
547 return MTP_RESPONSE_INVALID_PARAMETER;
548
Mike Lockwood16864ba2010-05-11 17:16:59 -0400549 MtpStorageID id = mRequest.getParameter(1);
550 MtpStorage* storage = getStorage(id);
551 if (!storage)
552 return MTP_RESPONSE_INVALID_STORAGE_ID;
553
554 mData.putUInt16(storage->getType());
555 mData.putUInt16(storage->getFileSystemType());
556 mData.putUInt16(storage->getAccessCapability());
557 mData.putUInt64(storage->getMaxCapacity());
558 mData.putUInt64(storage->getFreeSpace());
559 mData.putUInt32(1024*1024*1024); // Free Space in Objects
560 string.set(storage->getDescription());
561 mData.putString(string);
562 mData.putEmptyString(); // Volume Identifier
563
564 return MTP_RESPONSE_OK;
565}
566
567MtpResponseCode MtpServer::doGetObjectPropsSupported() {
568 if (!mSessionOpen)
569 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800570 if (mRequest.getParameterCount() < 1)
571 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400572 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800573 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400574 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400575 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400576 return MTP_RESPONSE_OK;
577}
578
579MtpResponseCode MtpServer::doGetObjectHandles() {
580 if (!mSessionOpen)
581 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800582 if (mRequest.getParameterCount() < 3)
583 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400584 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400585 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400586 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400587 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500588
589 if (!hasStorage(storageID))
590 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400591
592 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700593 if (handles == NULL)
594 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400595 mData.putAUInt32(handles);
596 delete handles;
597 return MTP_RESPONSE_OK;
598}
599
Mike Lockwood343af4e2010-08-02 10:52:20 -0400600MtpResponseCode MtpServer::doGetNumObjects() {
601 if (!mSessionOpen)
602 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800603 if (mRequest.getParameterCount() < 3)
604 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400605 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
606 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
607 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400608 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500609 if (!hasStorage(storageID))
610 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400611
612 int count = mDatabase->getNumObjects(storageID, format, parent);
613 if (count >= 0) {
614 mResponse.setParameter(1, count);
615 return MTP_RESPONSE_OK;
616 } else {
617 mResponse.setParameter(1, 0);
618 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
619 }
620}
621
Mike Lockwood438344f2010-08-03 15:30:09 -0400622MtpResponseCode MtpServer::doGetObjectReferences() {
623 if (!mSessionOpen)
624 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500625 if (!hasStorage())
626 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800627 if (mRequest.getParameterCount() < 1)
628 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500629 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400630
631 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400632 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400633 if (handles) {
634 mData.putAUInt32(handles);
635 delete handles;
636 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400637 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400638 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400639 return MTP_RESPONSE_OK;
640}
641
642MtpResponseCode MtpServer::doSetObjectReferences() {
643 if (!mSessionOpen)
644 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500645 if (!hasStorage())
646 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800647 if (mRequest.getParameterCount() < 1)
648 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400649 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500650
Mike Lockwood438344f2010-08-03 15:30:09 -0400651 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800652 if (!references)
653 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400654 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
655 delete references;
656 return result;
657}
658
Mike Lockwood16864ba2010-05-11 17:16:59 -0400659MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500660 if (!hasStorage())
661 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800662 if (mRequest.getParameterCount() < 2)
663 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400664 MtpObjectHandle handle = mRequest.getParameter(1);
665 MtpObjectProperty property = mRequest.getParameter(2);
James Weiba389222018-09-27 19:22:55 +0800666 ALOGV("GetObjectPropValue %d %s (0x%04X)\n", handle,
667 MtpDebug::getObjectPropCodeName(property), property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400668
Mike Lockwood8277cec2010-08-10 15:20:35 -0400669 return mDatabase->getObjectPropertyValue(handle, property, mData);
670}
671
672MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500673 if (!hasStorage())
674 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800675 if (mRequest.getParameterCount() < 2)
676 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400677 MtpObjectHandle handle = mRequest.getParameter(1);
678 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400680 MtpDebug::getObjectPropCodeName(property));
681
682 return mDatabase->setObjectPropertyValue(handle, property, mData);
683}
684
685MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800686 if (mRequest.getParameterCount() < 1)
687 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400688 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100689 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400690 MtpDebug::getDevicePropCodeName(property));
691
692 return mDatabase->getDevicePropertyValue(property, mData);
693}
694
695MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800696 if (mRequest.getParameterCount() < 1)
697 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400698 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400700 MtpDebug::getDevicePropCodeName(property));
701
702 return mDatabase->setDevicePropertyValue(property, mData);
703}
704
705MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800706 if (mRequest.getParameterCount() < 1)
707 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400708 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400710 MtpDebug::getDevicePropCodeName(property));
711
712 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400713}
714
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400715MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500716 if (!hasStorage())
717 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800718 if (mRequest.getParameterCount() < 5)
719 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400720
721 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500722 // use uint32_t so we can support 0xFFFFFFFF
723 uint32_t format = mRequest.getParameter(2);
724 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400725 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500726 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100727 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400728 handle, MtpDebug::getFormatCodeName(format),
729 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
730
731 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
732}
733
Mike Lockwood16864ba2010-05-11 17:16:59 -0400734MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500735 if (!hasStorage())
736 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800737 if (mRequest.getParameterCount() < 1)
738 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400739 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700740 MtpObjectInfo info(handle);
741 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
742 if (result == MTP_RESPONSE_OK) {
743 char date[20];
744
745 mData.putUInt32(info.mStorageID);
746 mData.putUInt16(info.mFormat);
747 mData.putUInt16(info.mProtectionStatus);
748
749 // if object is being edited the database size may be out of date
750 uint32_t size = info.mCompressedSize;
751 ObjectEdit* edit = getEditObject(handle);
752 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700753 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700754 mData.putUInt32(size);
755
756 mData.putUInt16(info.mThumbFormat);
757 mData.putUInt32(info.mThumbCompressedSize);
758 mData.putUInt32(info.mThumbPixWidth);
759 mData.putUInt32(info.mThumbPixHeight);
760 mData.putUInt32(info.mImagePixWidth);
761 mData.putUInt32(info.mImagePixHeight);
762 mData.putUInt32(info.mImagePixDepth);
763 mData.putUInt32(info.mParent);
764 mData.putUInt16(info.mAssociationType);
765 mData.putUInt32(info.mAssociationDesc);
766 mData.putUInt32(info.mSequenceNumber);
767 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700768 formatDateTime(info.mDateCreated, date, sizeof(date));
769 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700770 formatDateTime(info.mDateModified, date, sizeof(date));
771 mData.putString(date); // date modified
772 mData.putEmptyString(); // keywords
773 }
774 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400775}
776
777MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500778 if (!hasStorage())
779 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800780 if (mRequest.getParameterCount() < 1)
781 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400782 MtpObjectHandle handle = mRequest.getParameter(1);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700783 MtpStringBuffer pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400784 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800785 MtpObjectFormat format;
786 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400787 if (result != MTP_RESPONSE_OK)
788 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400789
Jerry Zhang487be612016-10-24 12:10:41 -0700790 auto start = std::chrono::steady_clock::now();
791
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400792 const char* filePath = (const char *)pathBuf;
Zime3b5ba02020-11-13 20:09:06 +0000793 mtp_file_range mfr;
794 struct stat sstat;
795 uint64_t finalsize;
Manish Singhc0c6b512021-02-04 04:54:23 +0000796 bool transcode = android::base::GetBoolProperty("sys.fuse.transcode_mtp", false);
Zimc43e61a2021-06-18 10:52:54 +0100797 bool filePathAccess = true;
Manish Singhfd5500a2021-01-19 21:35:21 +0000798 ALOGD("Mtp transcode = %d", transcode);
Zimc43e61a2021-06-18 10:52:54 +0100799
800 // For performance reasons, only attempt a ContentResolver open when transcode is required.
801 // This is fine as long as we don't transcode by default on the device. If we suddenly
802 // transcode by default, we'll need to ensure that MTP doesn't transcode by default and we
803 // might need to make a binder call to avoid transcoding or come up with a better strategy.
804 if (transcode) {
805 mfr.fd = mDatabase->openFilePath(filePath, true);
Manish Singhfd5500a2021-01-19 21:35:21 +0000806 fstat(mfr.fd, &sstat);
807 finalsize = sstat.st_size;
808 fileLength = finalsize;
Zimc43e61a2021-06-18 10:52:54 +0100809 if (mfr.fd < 0) {
810 ALOGW("Mtp open via IMtpDatabase failed for %s. Falling back to the original",
811 filePath);
812 filePathAccess = true;
813 } else {
814 filePathAccess = false;
815 }
816 }
Zime3b5ba02020-11-13 20:09:06 +0000817
Zimc43e61a2021-06-18 10:52:54 +0100818 if (filePathAccess) {
Zime3b5ba02020-11-13 20:09:06 +0000819 mfr.fd = open(filePath, O_RDONLY);
820 if (mfr.fd < 0) {
821 return MTP_RESPONSE_GENERAL_ERROR;
822 }
823 fstat(mfr.fd, &sstat);
824 finalsize = sstat.st_size;
825 }
826
Mike Lockwood16864ba2010-05-11 17:16:59 -0400827 mfr.offset = 0;
828 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400829 mfr.command = mRequest.getOperationCode();
830 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400831
832 // then transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800833 int ret = mHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800834 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700835 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800836 if (errno == ECANCELED) {
837 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
838 } else {
839 result = MTP_RESPONSE_GENERAL_ERROR;
840 }
841 } else {
842 result = MTP_RESPONSE_OK;
843 }
844
Jerry Zhang487be612016-10-24 12:10:41 -0700845 auto end = std::chrono::steady_clock::now();
846 std::chrono::duration<double> diff = end - start;
Jerry Zhang487be612016-10-24 12:10:41 -0700847 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
848 diff.count(), finalsize, ((double) finalsize) / diff.count());
Jerry Zhangdc148de2018-05-14 12:07:16 -0700849 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800850 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400851}
852
Mike Lockwood64000782011-04-24 18:40:17 -0700853MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800854 if (mRequest.getParameterCount() < 1)
855 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700856 MtpObjectHandle handle = mRequest.getParameter(1);
857 size_t thumbSize;
858 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
859 if (thumb) {
860 // send data
861 mData.setOperationCode(mRequest.getOperationCode());
862 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang63dac452017-12-06 15:19:36 -0800863 mData.writeData(mHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700864 free(thumb);
865 return MTP_RESPONSE_OK;
866 } else {
867 return MTP_RESPONSE_GENERAL_ERROR;
868 }
869}
870
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700871MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500872 if (!hasStorage())
873 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500874 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700875 uint64_t offset;
876 uint32_t length;
877 offset = mRequest.getParameter(2);
878 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800879 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
880 if (mRequest.getParameterCount() < 4)
881 return MTP_RESPONSE_INVALID_PARAMETER;
882
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700883 // android extension with 64 bit offset
884 uint64_t offset2 = mRequest.getParameter(3);
885 offset = offset | (offset2 << 32);
886 length = mRequest.getParameter(4);
887 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800888 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
889 if (mRequest.getParameterCount() < 3)
890 return MTP_RESPONSE_INVALID_PARAMETER;
891
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700892 // standard GetPartialObject
893 length = mRequest.getParameter(3);
894 }
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700895 MtpStringBuffer pathBuf;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500896 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800897 MtpObjectFormat format;
898 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500899 if (result != MTP_RESPONSE_OK)
900 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700901 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500902 length = fileLength - offset;
903
904 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700905 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500906 mtp_file_range mfr;
907 mfr.fd = open(filePath, O_RDONLY);
908 if (mfr.fd < 0) {
909 return MTP_RESPONSE_GENERAL_ERROR;
910 }
911 mfr.offset = offset;
912 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400913 mfr.command = mRequest.getOperationCode();
914 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500915 mResponse.setParameter(1, length);
916
Mike Lockwoodef441d92011-07-14 21:00:02 -0400917 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800918 int ret = mHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800920 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500921 if (ret < 0) {
922 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800923 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500924 else
tao.pei07a9e542015-07-17 17:18:41 +0800925 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500926 }
Jerry Zhangdc148de2018-05-14 12:07:16 -0700927 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800928 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500929}
930
Mike Lockwood16864ba2010-05-11 17:16:59 -0400931MtpResponseCode MtpServer::doSendObjectInfo() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700932 MtpStringBuffer path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800933 uint16_t temp16;
934 uint32_t temp32;
935
936 if (mRequest.getParameterCount() < 2)
937 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400938 MtpStorageID storageID = mRequest.getParameter(1);
939 MtpStorage* storage = getStorage(storageID);
940 MtpObjectHandle parent = mRequest.getParameter(2);
941 if (!storage)
942 return MTP_RESPONSE_INVALID_STORAGE_ID;
943
944 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400945 if (parent == MTP_PARENT_ROOT) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700946 path.set(storage->getPath());
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400947 parent = 0;
948 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800949 int64_t length;
950 MtpObjectFormat format;
951 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400952 if (result != MTP_RESPONSE_OK)
953 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800954 if (format != MTP_FORMAT_ASSOCIATION)
955 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400956 }
957
958 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800959 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
960 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
961 MtpObjectFormat format = temp16;
962 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
963 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
964 mSendObjectFileSize = temp32;
965 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
966 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
967 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
968 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
969 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
970 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
971 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
972 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
973 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800974 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800975 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400976 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800977 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700978 if (name.isEmpty()) {
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700979 ALOGE("empty name");
980 return MTP_RESPONSE_INVALID_PARAMETER;
981 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800982 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
983 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400984 // keywords follow
985
nolyn.luef9bef52018-07-05 16:24:13 +0800986 int type = storage->getType();
987 if (type == MTP_STORAGE_REMOVABLE_RAM) {
988 std::string str = android::base::Trim((const char*)name);
989 name.set(str.c_str());
990 }
James Weiba389222018-09-27 19:22:55 +0800991 ALOGV("name: %s format: 0x%04X (%s)\n", (const char*)name, format,
992 MtpDebug::getFormatCodeName(format));
Mike Lockwoodfceef462010-05-14 15:35:17 -0400993 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400994 if (!parseDateTime(modified, modifiedTime))
995 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400996
James Weie783e4b2019-05-28 17:18:21 +0800997 if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0) ||
James Wei0931b102020-02-12 23:20:06 +0800998 (strchr(name, '/') != NULL)) {
James Weie783e4b2019-05-28 17:18:21 +0800999 char errMsg[80];
1000
James Wei68ccf5c2019-06-25 18:58:37 +08001001 snprintf(errMsg, sizeof(errMsg), "Invalid name: %s", (const char *) name);
James Weie783e4b2019-05-28 17:18:21 +08001002 ALOGE("%s (b/130656917)", errMsg);
1003 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "130656917", -1, errMsg,
1004 strlen(errMsg));
1005
1006 return MTP_RESPONSE_INVALID_PARAMETER;
1007 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001008 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001009 path.append("/");
James Wei0931b102020-02-12 23:20:06 +08001010 path.append(name);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001011
Mike Lockwood20c3be02010-12-12 12:17:43 -08001012 // check space first
1013 if (mSendObjectFileSize > storage->getFreeSpace())
1014 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -04001015 uint64_t maxFileSize = storage->getMaxFileSize();
1016 // check storage max file size
1017 if (maxFileSize != 0) {
1018 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
1019 // is >= 0xFFFFFFFF
1020 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
1021 return MTP_RESPONSE_OBJECT_TOO_LARGE;
1022 }
Mike Lockwood20c3be02010-12-12 12:17:43 -08001023
James Weiba389222018-09-27 19:22:55 +08001024 ALOGV("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001025 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, format,
1026 parent, storageID);
James Weiba389222018-09-27 19:22:55 +08001027 ALOGD("handle: %d, parent: %d, storageID: %08X", handle, parent, storageID);
Mike Lockwoodfceef462010-05-14 15:35:17 -04001028 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -04001029 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -04001030 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001031
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001032 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -07001033 int ret = makeFolder((const char *)path);
1034 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -04001035 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -08001036
1037 // SendObject does not get sent for directories, so call endSendObject here instead
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001038 mDatabase->endSendObject(handle, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001039 }
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001040 mSendObjectFilePath = path;
1041 // save the handle for the SendObject call, which should follow
1042 mSendObjectHandle = handle;
1043 mSendObjectFormat = format;
1044 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001045
1046 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001047 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001048 mResponse.setParameter(3, handle);
1049
1050 return MTP_RESPONSE_OK;
1051}
1052
Jerry Zhang708b3e02017-09-26 17:53:39 -07001053MtpResponseCode MtpServer::doMoveObject() {
1054 if (!hasStorage())
1055 return MTP_RESPONSE_GENERAL_ERROR;
1056 if (mRequest.getParameterCount() < 3)
1057 return MTP_RESPONSE_INVALID_PARAMETER;
1058 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1059 MtpStorageID storageID = mRequest.getParameter(2);
1060 MtpStorage* storage = getStorage(storageID);
1061 MtpObjectHandle parent = mRequest.getParameter(3);
1062 if (!storage)
1063 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001064 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001065 MtpResponseCode result;
1066
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001067 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001068 int64_t fileLength;
1069 MtpObjectFormat format;
1070 MtpObjectInfo info(objectHandle);
1071 result = mDatabase->getObjectInfo(objectHandle, info);
1072 if (result != MTP_RESPONSE_OK)
1073 return result;
1074 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1075 if (result != MTP_RESPONSE_OK)
1076 return result;
1077
1078 // special case the root
1079 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001080 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001081 } else {
1082 int64_t parentLength;
1083 MtpObjectFormat parentFormat;
1084 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1085 if (result != MTP_RESPONSE_OK)
1086 return result;
1087 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1088 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1089 }
1090
1091 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001092 path.append("/");
1093 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001094
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001095 result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
1096 if (result != MTP_RESPONSE_OK)
1097 return result;
1098
Jerry Zhang708b3e02017-09-26 17:53:39 -07001099 if (info.mStorageID == storageID) {
1100 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001101 if (renameTo(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001102 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001103 result = MTP_RESPONSE_GENERAL_ERROR;
1104 }
1105 } else {
1106 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001107 if (format == MTP_FORMAT_ASSOCIATION) {
1108 int ret = makeFolder((const char *)path);
1109 ret += copyRecursive(fromPath, path);
1110 if (ret) {
1111 result = MTP_RESPONSE_GENERAL_ERROR;
1112 } else {
1113 deletePath(fromPath);
1114 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001115 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001116 if (copyFile(fromPath, path)) {
1117 result = MTP_RESPONSE_GENERAL_ERROR;
1118 } else {
1119 deletePath(fromPath);
1120 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001121 }
1122 }
1123
1124 // If the move failed, undo the database change
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001125 mDatabase->endMoveObject(info.mParent, parent, info.mStorageID, storageID, objectHandle,
1126 result == MTP_RESPONSE_OK);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001127
1128 return result;
1129}
1130
1131MtpResponseCode MtpServer::doCopyObject() {
1132 if (!hasStorage())
1133 return MTP_RESPONSE_GENERAL_ERROR;
1134 MtpResponseCode result = MTP_RESPONSE_OK;
1135 if (mRequest.getParameterCount() < 3)
1136 return MTP_RESPONSE_INVALID_PARAMETER;
1137 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1138 MtpStorageID storageID = mRequest.getParameter(2);
1139 MtpStorage* storage = getStorage(storageID);
1140 MtpObjectHandle parent = mRequest.getParameter(3);
1141 if (!storage)
1142 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001143 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001144
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001145 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001146 int64_t fileLength;
1147 MtpObjectFormat format;
1148 MtpObjectInfo info(objectHandle);
1149 result = mDatabase->getObjectInfo(objectHandle, info);
1150 if (result != MTP_RESPONSE_OK)
1151 return result;
1152 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1153 if (result != MTP_RESPONSE_OK)
1154 return result;
1155
1156 // special case the root
1157 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001158 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001159 } else {
1160 int64_t parentLength;
1161 MtpObjectFormat parentFormat;
1162 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1163 if (result != MTP_RESPONSE_OK)
1164 return result;
1165 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1166 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1167 }
1168
1169 // check space first
1170 if ((uint64_t) fileLength > storage->getFreeSpace())
1171 return MTP_RESPONSE_STORAGE_FULL;
1172
1173 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001174 path.append("/");
1175 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001176
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001177 MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001178 if (handle == kInvalidObjectHandle) {
1179 return MTP_RESPONSE_GENERAL_ERROR;
1180 }
1181
1182 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001183 if (format == MTP_FORMAT_ASSOCIATION) {
1184 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001185 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001186 if (ret) {
1187 result = MTP_RESPONSE_GENERAL_ERROR;
1188 }
1189 } else {
1190 if (copyFile(fromPath, path)) {
1191 result = MTP_RESPONSE_GENERAL_ERROR;
1192 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001193 }
1194
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001195 mDatabase->endCopyObject(handle, result);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001196 mResponse.setParameter(1, handle);
1197 return result;
1198}
1199
Mike Lockwood16864ba2010-05-11 17:16:59 -04001200MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001201 if (!hasStorage())
1202 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001203 MtpResponseCode result = MTP_RESPONSE_OK;
1204 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001205 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001206 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001207 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001208
Jerry Zhang487be612016-10-24 12:10:41 -07001209 auto start = std::chrono::steady_clock::now();
1210
Mike Lockwood16864ba2010-05-11 17:16:59 -04001211 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001212 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001213 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1214 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001215 }
1216
Mike Lockwoodef441d92011-07-14 21:00:02 -04001217 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001218 ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001219 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1220 result = MTP_RESPONSE_GENERAL_ERROR;
1221 goto done;
1222 }
1223 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001224
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001225 if (mSendObjectFormat == MTP_FORMAT_ASSOCIATION) {
1226 if (initialData != 0)
1227 ALOGE("Expected folder size to be 0!");
1228 mSendObjectHandle = kInvalidObjectHandle;
1229 mSendObjectFormat = 0;
1230 mSendObjectModifiedTime = 0;
1231 return result;
1232 }
1233
Mike Lockwood16864ba2010-05-11 17:16:59 -04001234 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001235 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001236 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001237 result = MTP_RESPONSE_GENERAL_ERROR;
1238 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001239 }
Jerry Zhange242f122017-10-16 14:54:08 -07001240 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001241 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001242 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001243 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001244 umask(mask);
1245
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001246 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001247 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001248 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001249
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001250 if (ret < 0) {
1251 ALOGE("failed to write initial data");
1252 result = MTP_RESPONSE_GENERAL_ERROR;
1253 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001254 mfr.offset = initialData;
1255 if (mSendObjectFileSize == 0xFFFFFFFF) {
1256 // tell driver to read until it receives a short packet
1257 mfr.length = 0xFFFFFFFF;
1258 } else {
1259 mfr.length = mSendObjectFileSize - initialData;
1260 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001261
Jerry Zhang54107562017-05-15 11:54:19 -07001262 mfr.command = 0;
1263 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001264
Jerry Zhang54107562017-05-15 11:54:19 -07001265 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001266 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001267 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1268 if ((ret < 0) && (errno == ECANCELED)) {
1269 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001270 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001271 }
caozhiyuan854cb172017-04-26 16:52:30 +08001272
1273 if (mSendObjectModifiedTime) {
1274 struct timespec newTime[2];
1275 newTime[0].tv_nsec = UTIME_NOW;
1276 newTime[1].tv_sec = mSendObjectModifiedTime;
1277 newTime[1].tv_nsec = 0;
1278 if (futimens(mfr.fd, newTime) < 0) {
1279 ALOGW("changing modified time failed, %s", strerror(errno));
1280 }
1281 }
1282
Jerry Zhang487be612016-10-24 12:10:41 -07001283 fstat(mfr.fd, &sstat);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001284 closeObjFd(mfr.fd, mSendObjectFilePath);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001285
Mike Lockwood916076c2010-06-04 09:49:21 -04001286 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001287 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001288 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001289 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001290 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001291 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001292 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001293 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001294
1295done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001296 // reset so we don't attempt to send the data back
1297 mData.reset();
1298
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001299 mDatabase->endSendObject(mSendObjectHandle, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001300 mSendObjectHandle = kInvalidObjectHandle;
1301 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001302 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001303
1304 auto end = std::chrono::steady_clock::now();
1305 std::chrono::duration<double> diff = end - start;
1306 uint64_t finalsize = sstat.st_size;
1307 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1308 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001309 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001310}
1311
1312MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001313 if (!hasStorage())
1314 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001315 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001316 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001317 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001318 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001319 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1320 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001321
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001322 MtpStringBuffer filePath;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001323 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001324 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001325 if (result != MTP_RESPONSE_OK)
1326 return result;
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001327
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001328 // Don't delete the actual files unless the database deletion is allowed
1329 result = mDatabase->beginDeleteObject(handle);
1330 if (result != MTP_RESPONSE_OK)
1331 return result;
1332
1333 bool success = deletePath((const char *)filePath);
1334
1335 mDatabase->endDeleteObject(handle, success);
1336 return success ? result : MTP_RESPONSE_PARTIAL_DELETION;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001337}
1338
1339MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001340 if (mRequest.getParameterCount() < 2)
1341 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001342 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001343 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001344 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001345 MtpDebug::getFormatCodeName(format));
1346 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001347 if (!property)
1348 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001349 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001350 delete property;
1351 return MTP_RESPONSE_OK;
1352}
1353
1354MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001355 if (mRequest.getParameterCount() < 1)
1356 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001357 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001358 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001359 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1360 if (!property)
1361 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1362 property->write(mData);
1363 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001364 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001365}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001366
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001367MtpResponseCode MtpServer::doSendPartialObject() {
1368 if (!hasStorage())
1369 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001370 if (mRequest.getParameterCount() < 4)
1371 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001372 MtpObjectHandle handle = mRequest.getParameter(1);
1373 uint64_t offset = mRequest.getParameter(2);
1374 uint64_t offset2 = mRequest.getParameter(3);
1375 offset = offset | (offset2 << 32);
1376 uint32_t length = mRequest.getParameter(4);
1377
1378 ObjectEdit* edit = getEditObject(handle);
1379 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001380 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001381 return MTP_RESPONSE_GENERAL_ERROR;
1382 }
1383
1384 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001385 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001386 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1387 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001388 return MTP_RESPONSE_GENERAL_ERROR;
1389 }
1390
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001391 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001392 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001393
Mike Lockwoodef441d92011-07-14 21:00:02 -04001394 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001395 int ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001396 if (ret < MTP_CONTAINER_HEADER_SIZE)
1397 return MTP_RESPONSE_GENERAL_ERROR;
1398 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1399
1400 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001401 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001402 offset += initialData;
1403 length -= initialData;
1404 }
1405
tao.pei07a9e542015-07-17 17:18:41 +08001406 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001407 if (ret < 0) {
1408 ALOGE("failed to write initial data");
1409 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001410 mtp_file_range mfr;
1411 mfr.fd = edit->mFD;
1412 mfr.offset = offset;
1413 mfr.length = length;
1414 mfr.command = 0;
1415 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001416
Jerry Zhang54107562017-05-15 11:54:19 -07001417 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001418 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001419 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1420 if ((ret < 0) && (errno == ECANCELED)) {
1421 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001422 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001423 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001424 if (ret < 0) {
1425 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001426 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001427 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1428 else
1429 return MTP_RESPONSE_GENERAL_ERROR;
1430 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001431
1432 // reset so we don't attempt to send this back
1433 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001434 mResponse.setParameter(1, length);
1435 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001436 if (end > edit->mSize) {
1437 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001438 }
1439 return MTP_RESPONSE_OK;
1440}
1441
1442MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001443 if (mRequest.getParameterCount() < 3)
1444 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001445 MtpObjectHandle handle = mRequest.getParameter(1);
1446 ObjectEdit* edit = getEditObject(handle);
1447 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001448 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001449 return MTP_RESPONSE_GENERAL_ERROR;
1450 }
1451
1452 uint64_t offset = mRequest.getParameter(2);
1453 uint64_t offset2 = mRequest.getParameter(3);
1454 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001455 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001456 return MTP_RESPONSE_GENERAL_ERROR;
1457 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001458 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001459 return MTP_RESPONSE_OK;
1460 }
1461}
1462
1463MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001464 if (mRequest.getParameterCount() < 1)
1465 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001466 MtpObjectHandle handle = mRequest.getParameter(1);
1467 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001468 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001469 return MTP_RESPONSE_GENERAL_ERROR;
1470 }
1471
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001472 MtpStringBuffer path;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001473 int64_t fileLength;
1474 MtpObjectFormat format;
1475 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1476 if (result != MTP_RESPONSE_OK)
1477 return result;
1478
1479 int fd = open((const char *)path, O_RDWR | O_EXCL);
1480 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001481 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001482 return MTP_RESPONSE_GENERAL_ERROR;
1483 }
1484
1485 addEditObject(handle, path, fileLength, format, fd);
1486 return MTP_RESPONSE_OK;
1487}
1488
1489MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001490 if (mRequest.getParameterCount() < 1)
1491 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001492 MtpObjectHandle handle = mRequest.getParameter(1);
1493 ObjectEdit* edit = getEditObject(handle);
1494 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001495 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001496 return MTP_RESPONSE_GENERAL_ERROR;
1497 }
1498
1499 commitEdit(edit);
1500 removeEditObject(handle);
1501 return MTP_RESPONSE_OK;
1502}
1503
Mike Lockwood7850ef92010-05-14 10:10:36 -04001504} // namespace android