blob: dd69496160709db29c0cad281b274d4ec4de9997 [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"
Mike Lockwood16864ba2010-05-11 17:16:59 -040045
Mike Lockwood7850ef92010-05-14 10:10:36 -040046namespace android {
James Wei6111b2b2019-05-28 17:18:21 +080047static const int SN_EVENT_LOG_ID = 0x534e4554;
Mike Lockwood7850ef92010-05-14 10:10:36 -040048
Mike Lockwood16864ba2010-05-11 17:16:59 -040049static const MtpOperationCode kSupportedOperationCodes[] = {
50 MTP_OPERATION_GET_DEVICE_INFO,
51 MTP_OPERATION_OPEN_SESSION,
52 MTP_OPERATION_CLOSE_SESSION,
53 MTP_OPERATION_GET_STORAGE_IDS,
54 MTP_OPERATION_GET_STORAGE_INFO,
55 MTP_OPERATION_GET_NUM_OBJECTS,
56 MTP_OPERATION_GET_OBJECT_HANDLES,
57 MTP_OPERATION_GET_OBJECT_INFO,
58 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070059 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040060 MTP_OPERATION_DELETE_OBJECT,
61 MTP_OPERATION_SEND_OBJECT_INFO,
62 MTP_OPERATION_SEND_OBJECT,
63// MTP_OPERATION_INITIATE_CAPTURE,
64// MTP_OPERATION_FORMAT_STORE,
Jerry Zhang6dafecc2017-02-23 16:39:30 -080065 MTP_OPERATION_RESET_DEVICE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040066// MTP_OPERATION_SELF_TEST,
67// MTP_OPERATION_SET_OBJECT_PROTECTION,
68// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040069 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040070 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
71 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
72 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040073// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
Jerry Zhang708b3e02017-09-26 17:53:39 -070074 MTP_OPERATION_MOVE_OBJECT,
75 MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050076 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040077// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
78 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040079 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040080 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
81 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040082 MTP_OPERATION_GET_OBJECT_PROP_LIST,
83// MTP_OPERATION_SET_OBJECT_PROP_LIST,
84// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
85// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040086 MTP_OPERATION_GET_OBJECT_REFERENCES,
87 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040088// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070089 // Android extension for direct file IO
90 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
91 MTP_OPERATION_SEND_PARTIAL_OBJECT,
92 MTP_OPERATION_TRUNCATE_OBJECT,
93 MTP_OPERATION_BEGIN_EDIT_OBJECT,
94 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040095};
96
Mike Lockwood873871f2010-07-12 18:54:16 -040097static const MtpEventCode kSupportedEventCodes[] = {
98 MTP_EVENT_OBJECT_ADDED,
99 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -0500100 MTP_EVENT_STORE_ADDED,
101 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800102 MTP_EVENT_DEVICE_PROP_CHANGED,
James55138432018-07-02 18:07:30 +0800103 MTP_EVENT_OBJECT_INFO_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -0400104};
105
Jerry Zhang63dac452017-12-06 15:19:36 -0800106MtpServer::MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700107 const char *deviceInfoManufacturer,
108 const char *deviceInfoModel,
109 const char *deviceInfoDeviceVersion,
110 const char *deviceInfoSerialNumber)
Jerry Zhang487be612016-10-24 12:10:41 -0700111 : mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400112 mPtp(ptp),
Alex Klyubin792298f2016-12-21 11:20:22 -0800113 mDeviceInfoManufacturer(deviceInfoManufacturer),
114 mDeviceInfoModel(deviceInfoModel),
115 mDeviceInfoDeviceVersion(deviceInfoDeviceVersion),
116 mDeviceInfoSerialNumber(deviceInfoSerialNumber),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400117 mSessionID(0),
118 mSessionOpen(false),
119 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400120 mSendObjectFormat(0),
caozhiyuan854cb172017-04-26 16:52:30 +0800121 mSendObjectFileSize(0),
122 mSendObjectModifiedTime(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400123{
Jerry Zhang63dac452017-12-06 15:19:36 -0800124 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
125 if (ffs_ok) {
126 bool aio_compat = android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false);
127 mHandle = aio_compat ? new MtpFfsCompatHandle(controlFd) : new MtpFfsHandle(controlFd);
128 } else {
129 mHandle = new MtpDevHandle();
130 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400131}
132
133MtpServer::~MtpServer() {
134}
135
Mike Lockwooda8494402011-02-18 09:07:14 -0500136void MtpServer::addStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700137 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500138
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700139 mStorages.push_back(storage);
Mike Lockwooda8494402011-02-18 09:07:14 -0500140 sendStoreAdded(storage->getStorageID());
141}
142
143void MtpServer::removeStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700144 std::lock_guard<std::mutex> lg(mMutex);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700145 auto iter = std::find(mStorages.begin(), mStorages.end(), storage);
146 if (iter != mStorages.end()) {
147 sendStoreRemoved(storage->getStorageID());
148 mStorages.erase(iter);
Mike Lockwooda8494402011-02-18 09:07:14 -0500149 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400150}
151
152MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800153 if (id == 0)
154 return mStorages[0];
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700155 for (MtpStorage *storage : mStorages) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400156 if (storage->getStorageID() == id)
157 return storage;
158 }
Jerry Zhang487be612016-10-24 12:10:41 -0700159 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400160}
161
Mike Lockwooda8494402011-02-18 09:07:14 -0500162bool MtpServer::hasStorage(MtpStorageID id) {
163 if (id == 0 || id == 0xFFFFFFFF)
164 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700165 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500166}
167
Mike Lockwood16864ba2010-05-11 17:16:59 -0400168void MtpServer::run() {
Jerry Zhang63dac452017-12-06 15:19:36 -0800169 if (mHandle->start(mPtp)) {
Jerry Zhang487be612016-10-24 12:10:41 -0700170 ALOGE("Failed to start usb driver!");
Jerry Zhang63dac452017-12-06 15:19:36 -0800171 mHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700172 return;
173 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400174
175 while (1) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800176 int ret = mRequest.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400177 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700178 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400179 if (errno == ECANCELED) {
180 // return to top of loop and wait for next command
181 continue;
182 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400183 break;
184 }
185 MtpOperationCode operation = mRequest.getOperationCode();
186 MtpTransactionID transaction = mRequest.getTransactionID();
187
Steve Block3856b092011-10-20 11:56:00 +0100188 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400189 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400190 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400191 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
192 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
193 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400194 if (dataIn) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800195 int ret = mData.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400196 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000197 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400198 if (errno == ECANCELED) {
199 // return to top of loop and wait for next command
200 continue;
201 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400202 break;
203 }
Steve Block3856b092011-10-20 11:56:00 +0100204 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400205 } else {
206 mData.reset();
207 }
208
Mike Lockwood916076c2010-06-04 09:49:21 -0400209 if (handleRequest()) {
210 if (!dataIn && mData.hasData()) {
211 mData.setOperationCode(operation);
212 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100213 ALOGV("sending data:");
Jerry Zhang63dac452017-12-06 15:19:36 -0800214 ret = mData.write(mHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400215 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000216 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400217 if (errno == ECANCELED) {
218 // return to top of loop and wait for next command
219 continue;
220 }
221 break;
222 }
223 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400224
Mike Lockwood916076c2010-06-04 09:49:21 -0400225 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100226 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang63dac452017-12-06 15:19:36 -0800227 ret = mResponse.write(mHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800228 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400229 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000230 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800231 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400232 // return to top of loop and wait for next command
233 continue;
234 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400235 break;
236 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400237 } else {
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400239 }
240 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400241
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700242 // commit any open edits
243 int count = mObjectEditList.size();
244 for (int i = 0; i < count; i++) {
245 ObjectEdit* edit = mObjectEditList[i];
246 commitEdit(edit);
247 delete edit;
248 }
249 mObjectEditList.clear();
250
Jerry Zhang63dac452017-12-06 15:19:36 -0800251 mHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400252}
253
Mike Lockwood873871f2010-07-12 18:54:16 -0400254void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100255 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500256 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400257}
258
259void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100260 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500261 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
262}
263
James55138432018-07-02 18:07:30 +0800264void MtpServer::sendObjectInfoChanged(MtpObjectHandle handle) {
265 ALOGV("sendObjectInfoChanged %d\n", handle);
266 sendEvent(MTP_EVENT_OBJECT_INFO_CHANGED, handle);
267}
268
Mike Lockwooda8494402011-02-18 09:07:14 -0500269void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100270 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500271 sendEvent(MTP_EVENT_STORE_ADDED, id);
272}
273
274void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500276 sendEvent(MTP_EVENT_STORE_REMOVED, id);
277}
278
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800279void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
280 ALOGV("sendDevicePropertyChanged %d\n", property);
281 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
282}
283
Mike Lockwooda8494402011-02-18 09:07:14 -0500284void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400285 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500286 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400287 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500288 mEvent.setParameter(1, param1);
Jerry Zhang63dac452017-12-06 15:19:36 -0800289 if (mEvent.write(mHandle))
Jerry Zhang487be612016-10-24 12:10:41 -0700290 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400291 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400292}
293
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700294void MtpServer::addEditObject(MtpObjectHandle handle, MtpStringBuffer& path,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700295 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700296 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700297 mObjectEditList.push_back(edit);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700298}
299
300MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
301 int count = mObjectEditList.size();
302 for (int i = 0; i < count; i++) {
303 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700304 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700305 }
Jerry Zhang487be612016-10-24 12:10:41 -0700306 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700307}
308
309void MtpServer::removeEditObject(MtpObjectHandle handle) {
310 int count = mObjectEditList.size();
311 for (int i = 0; i < count; i++) {
312 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700313 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700314 delete edit;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700315 mObjectEditList.erase(mObjectEditList.begin() + i);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700316 return;
317 }
318 }
Steve Block29357bc2012-01-06 19:20:56 +0000319 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700320}
321
322void MtpServer::commitEdit(ObjectEdit* edit) {
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700323 mDatabase->rescanFile((const char *)edit->mPath, edit->mHandle, edit->mFormat);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700324}
325
326
Mike Lockwood916076c2010-06-04 09:49:21 -0400327bool MtpServer::handleRequest() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700328 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500329
Mike Lockwood16864ba2010-05-11 17:16:59 -0400330 MtpOperationCode operation = mRequest.getOperationCode();
331 MtpResponseCode response;
332
333 mResponse.reset();
334
335 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400336 mSendObjectHandle = kInvalidObjectHandle;
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700337 mSendObjectFormat = 0;
338 mSendObjectModifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400339 }
340
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700341 int containertype = mRequest.getContainerType();
342 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
343 ALOGE("wrong container type %d", containertype);
344 return false;
345 }
346
347 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
348
Mike Lockwood16864ba2010-05-11 17:16:59 -0400349 switch (operation) {
350 case MTP_OPERATION_GET_DEVICE_INFO:
351 response = doGetDeviceInfo();
352 break;
353 case MTP_OPERATION_OPEN_SESSION:
354 response = doOpenSession();
355 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800356 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400357 case MTP_OPERATION_CLOSE_SESSION:
358 response = doCloseSession();
359 break;
360 case MTP_OPERATION_GET_STORAGE_IDS:
361 response = doGetStorageIDs();
362 break;
363 case MTP_OPERATION_GET_STORAGE_INFO:
364 response = doGetStorageInfo();
365 break;
366 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
367 response = doGetObjectPropsSupported();
368 break;
369 case MTP_OPERATION_GET_OBJECT_HANDLES:
370 response = doGetObjectHandles();
371 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400372 case MTP_OPERATION_GET_NUM_OBJECTS:
373 response = doGetNumObjects();
374 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400375 case MTP_OPERATION_GET_OBJECT_REFERENCES:
376 response = doGetObjectReferences();
377 break;
378 case MTP_OPERATION_SET_OBJECT_REFERENCES:
379 response = doSetObjectReferences();
380 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400381 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
382 response = doGetObjectPropValue();
383 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400384 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
385 response = doSetObjectPropValue();
386 break;
387 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
388 response = doGetDevicePropValue();
389 break;
390 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
391 response = doSetDevicePropValue();
392 break;
393 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
394 response = doResetDevicePropValue();
395 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400396 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
397 response = doGetObjectPropList();
398 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400399 case MTP_OPERATION_GET_OBJECT_INFO:
400 response = doGetObjectInfo();
401 break;
402 case MTP_OPERATION_GET_OBJECT:
403 response = doGetObject();
404 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700405 case MTP_OPERATION_GET_THUMB:
406 response = doGetThumb();
407 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500408 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700409 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
410 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500411 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400412 case MTP_OPERATION_SEND_OBJECT_INFO:
413 response = doSendObjectInfo();
414 break;
415 case MTP_OPERATION_SEND_OBJECT:
416 response = doSendObject();
417 break;
418 case MTP_OPERATION_DELETE_OBJECT:
419 response = doDeleteObject();
420 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700421 case MTP_OPERATION_COPY_OBJECT:
422 response = doCopyObject();
423 break;
424 case MTP_OPERATION_MOVE_OBJECT:
425 response = doMoveObject();
426 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400427 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400428 response = doGetObjectPropDesc();
429 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400430 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
431 response = doGetDevicePropDesc();
432 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700433 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
434 response = doSendPartialObject();
435 break;
436 case MTP_OPERATION_TRUNCATE_OBJECT:
437 response = doTruncateObject();
438 break;
439 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
440 response = doBeginEditObject();
441 break;
442 case MTP_OPERATION_END_EDIT_OBJECT:
443 response = doEndEditObject();
444 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400445 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700446 ALOGE("got unsupported command %s (%x)",
447 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400448 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
449 break;
450 }
451
James Weiba389222018-09-27 19:22:55 +0800452 if (response != MTP_RESPONSE_OK)
453 ALOGW("[MTP] got response 0x%X in command %s (%x)", response,
454 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood916076c2010-06-04 09:49:21 -0400455 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
456 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400457 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400458 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400459}
460
461MtpResponseCode MtpServer::doGetDeviceInfo() {
462 MtpStringBuffer string;
463
Mike Lockwood782aef12010-08-10 07:37:50 -0400464 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
465 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
466 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
467
Mike Lockwood16864ba2010-05-11 17:16:59 -0400468 // fill in device info
469 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400470 if (mPtp) {
471 mData.putUInt32(0);
472 } else {
473 // MTP Vendor Extension ID
474 mData.putUInt32(6);
475 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400476 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400477 if (mPtp) {
478 // no extensions
479 string.set("");
480 } else {
481 // MTP extensions
482 string.set("microsoft.com: 1.0; android.com: 1.0;");
483 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400484 mData.putString(string); // MTP Extensions
485 mData.putUInt16(0); //Functional Mode
486 mData.putAUInt16(kSupportedOperationCodes,
487 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400488 mData.putAUInt16(kSupportedEventCodes,
489 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400490 mData.putAUInt16(deviceProperties); // Device Properties Supported
491 mData.putAUInt16(captureFormats); // Capture Formats
492 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500493
Alex Klyubin792298f2016-12-21 11:20:22 -0800494 mData.putString(mDeviceInfoManufacturer); // Manufacturer
495 mData.putString(mDeviceInfoModel); // Model
496 mData.putString(mDeviceInfoDeviceVersion); // Device Version
497 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400498
Mike Lockwood782aef12010-08-10 07:37:50 -0400499 delete playbackFormats;
500 delete captureFormats;
501 delete deviceProperties;
502
Mike Lockwood16864ba2010-05-11 17:16:59 -0400503 return MTP_RESPONSE_OK;
504}
505
506MtpResponseCode MtpServer::doOpenSession() {
507 if (mSessionOpen) {
508 mResponse.setParameter(1, mSessionID);
509 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
510 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800511 if (mRequest.getParameterCount() < 1)
512 return MTP_RESPONSE_INVALID_PARAMETER;
513
Mike Lockwood16864ba2010-05-11 17:16:59 -0400514 mSessionID = mRequest.getParameter(1);
515 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400516
Mike Lockwood16864ba2010-05-11 17:16:59 -0400517 return MTP_RESPONSE_OK;
518}
519
520MtpResponseCode MtpServer::doCloseSession() {
521 if (!mSessionOpen)
522 return MTP_RESPONSE_SESSION_NOT_OPEN;
523 mSessionID = 0;
524 mSessionOpen = false;
525 return MTP_RESPONSE_OK;
526}
527
528MtpResponseCode MtpServer::doGetStorageIDs() {
529 if (!mSessionOpen)
530 return MTP_RESPONSE_SESSION_NOT_OPEN;
531
532 int count = mStorages.size();
533 mData.putUInt32(count);
534 for (int i = 0; i < count; i++)
535 mData.putUInt32(mStorages[i]->getStorageID());
536
537 return MTP_RESPONSE_OK;
538}
539
540MtpResponseCode MtpServer::doGetStorageInfo() {
541 MtpStringBuffer string;
542
543 if (!mSessionOpen)
544 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800545 if (mRequest.getParameterCount() < 1)
546 return MTP_RESPONSE_INVALID_PARAMETER;
547
Mike Lockwood16864ba2010-05-11 17:16:59 -0400548 MtpStorageID id = mRequest.getParameter(1);
549 MtpStorage* storage = getStorage(id);
550 if (!storage)
551 return MTP_RESPONSE_INVALID_STORAGE_ID;
552
553 mData.putUInt16(storage->getType());
554 mData.putUInt16(storage->getFileSystemType());
555 mData.putUInt16(storage->getAccessCapability());
556 mData.putUInt64(storage->getMaxCapacity());
557 mData.putUInt64(storage->getFreeSpace());
558 mData.putUInt32(1024*1024*1024); // Free Space in Objects
559 string.set(storage->getDescription());
560 mData.putString(string);
561 mData.putEmptyString(); // Volume Identifier
562
563 return MTP_RESPONSE_OK;
564}
565
566MtpResponseCode MtpServer::doGetObjectPropsSupported() {
567 if (!mSessionOpen)
568 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800569 if (mRequest.getParameterCount() < 1)
570 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400571 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800572 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400573 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400574 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400575 return MTP_RESPONSE_OK;
576}
577
578MtpResponseCode MtpServer::doGetObjectHandles() {
579 if (!mSessionOpen)
580 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800581 if (mRequest.getParameterCount() < 3)
582 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400583 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400584 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400585 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400586 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500587
588 if (!hasStorage(storageID))
589 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400590
591 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700592 if (handles == NULL)
593 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400594 mData.putAUInt32(handles);
595 delete handles;
596 return MTP_RESPONSE_OK;
597}
598
Mike Lockwood343af4e2010-08-02 10:52:20 -0400599MtpResponseCode MtpServer::doGetNumObjects() {
600 if (!mSessionOpen)
601 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800602 if (mRequest.getParameterCount() < 3)
603 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400604 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
605 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
606 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400607 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500608 if (!hasStorage(storageID))
609 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400610
611 int count = mDatabase->getNumObjects(storageID, format, parent);
612 if (count >= 0) {
613 mResponse.setParameter(1, count);
614 return MTP_RESPONSE_OK;
615 } else {
616 mResponse.setParameter(1, 0);
617 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
618 }
619}
620
Mike Lockwood438344f2010-08-03 15:30:09 -0400621MtpResponseCode MtpServer::doGetObjectReferences() {
622 if (!mSessionOpen)
623 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500624 if (!hasStorage())
625 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800626 if (mRequest.getParameterCount() < 1)
627 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500628 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400629
630 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400631 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400632 if (handles) {
633 mData.putAUInt32(handles);
634 delete handles;
635 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400636 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400637 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400638 return MTP_RESPONSE_OK;
639}
640
641MtpResponseCode MtpServer::doSetObjectReferences() {
642 if (!mSessionOpen)
643 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500644 if (!hasStorage())
645 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800646 if (mRequest.getParameterCount() < 1)
647 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400648 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500649
Mike Lockwood438344f2010-08-03 15:30:09 -0400650 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800651 if (!references)
652 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400653 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
654 delete references;
655 return result;
656}
657
Mike Lockwood16864ba2010-05-11 17:16:59 -0400658MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500659 if (!hasStorage())
660 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800661 if (mRequest.getParameterCount() < 2)
662 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400663 MtpObjectHandle handle = mRequest.getParameter(1);
664 MtpObjectProperty property = mRequest.getParameter(2);
James Weiba389222018-09-27 19:22:55 +0800665 ALOGV("GetObjectPropValue %d %s (0x%04X)\n", handle,
666 MtpDebug::getObjectPropCodeName(property), property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400667
Mike Lockwood8277cec2010-08-10 15:20:35 -0400668 return mDatabase->getObjectPropertyValue(handle, property, mData);
669}
670
671MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500672 if (!hasStorage())
673 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800674 if (mRequest.getParameterCount() < 2)
675 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400676 MtpObjectHandle handle = mRequest.getParameter(1);
677 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400679 MtpDebug::getObjectPropCodeName(property));
680
681 return mDatabase->setObjectPropertyValue(handle, property, mData);
682}
683
684MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800685 if (mRequest.getParameterCount() < 1)
686 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400687 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400689 MtpDebug::getDevicePropCodeName(property));
690
691 return mDatabase->getDevicePropertyValue(property, mData);
692}
693
694MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800695 if (mRequest.getParameterCount() < 1)
696 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400697 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400699 MtpDebug::getDevicePropCodeName(property));
700
701 return mDatabase->setDevicePropertyValue(property, mData);
702}
703
704MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800705 if (mRequest.getParameterCount() < 1)
706 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400707 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400709 MtpDebug::getDevicePropCodeName(property));
710
711 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400712}
713
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400714MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500715 if (!hasStorage())
716 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800717 if (mRequest.getParameterCount() < 5)
718 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400719
720 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500721 // use uint32_t so we can support 0xFFFFFFFF
722 uint32_t format = mRequest.getParameter(2);
723 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400724 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500725 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100726 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400727 handle, MtpDebug::getFormatCodeName(format),
728 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
729
730 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
731}
732
Mike Lockwood16864ba2010-05-11 17:16:59 -0400733MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500734 if (!hasStorage())
735 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800736 if (mRequest.getParameterCount() < 1)
737 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400738 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700739 MtpObjectInfo info(handle);
740 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
741 if (result == MTP_RESPONSE_OK) {
742 char date[20];
743
744 mData.putUInt32(info.mStorageID);
745 mData.putUInt16(info.mFormat);
746 mData.putUInt16(info.mProtectionStatus);
747
748 // if object is being edited the database size may be out of date
749 uint32_t size = info.mCompressedSize;
750 ObjectEdit* edit = getEditObject(handle);
751 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700752 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700753 mData.putUInt32(size);
754
755 mData.putUInt16(info.mThumbFormat);
756 mData.putUInt32(info.mThumbCompressedSize);
757 mData.putUInt32(info.mThumbPixWidth);
758 mData.putUInt32(info.mThumbPixHeight);
759 mData.putUInt32(info.mImagePixWidth);
760 mData.putUInt32(info.mImagePixHeight);
761 mData.putUInt32(info.mImagePixDepth);
762 mData.putUInt32(info.mParent);
763 mData.putUInt16(info.mAssociationType);
764 mData.putUInt32(info.mAssociationDesc);
765 mData.putUInt32(info.mSequenceNumber);
766 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700767 formatDateTime(info.mDateCreated, date, sizeof(date));
768 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700769 formatDateTime(info.mDateModified, date, sizeof(date));
770 mData.putString(date); // date modified
771 mData.putEmptyString(); // keywords
772 }
773 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400774}
775
776MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500777 if (!hasStorage())
778 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800779 if (mRequest.getParameterCount() < 1)
780 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400781 MtpObjectHandle handle = mRequest.getParameter(1);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700782 MtpStringBuffer pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400783 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800784 MtpObjectFormat format;
785 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400786 if (result != MTP_RESPONSE_OK)
787 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400788
Jerry Zhang487be612016-10-24 12:10:41 -0700789 auto start = std::chrono::steady_clock::now();
790
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400791 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400792 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400793 mfr.fd = open(filePath, O_RDONLY);
794 if (mfr.fd < 0) {
795 return MTP_RESPONSE_GENERAL_ERROR;
796 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400797 mfr.offset = 0;
798 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400799 mfr.command = mRequest.getOperationCode();
800 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400801
802 // then transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800803 int ret = mHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800804 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700805 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800806 if (errno == ECANCELED) {
807 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
808 } else {
809 result = MTP_RESPONSE_GENERAL_ERROR;
810 }
811 } else {
812 result = MTP_RESPONSE_OK;
813 }
814
Jerry Zhang487be612016-10-24 12:10:41 -0700815 auto end = std::chrono::steady_clock::now();
816 std::chrono::duration<double> diff = end - start;
817 struct stat sstat;
818 fstat(mfr.fd, &sstat);
819 uint64_t finalsize = sstat.st_size;
820 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
821 diff.count(), finalsize, ((double) finalsize) / diff.count());
Jerry Zhangdc148de2018-05-14 12:07:16 -0700822 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800823 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400824}
825
Mike Lockwood64000782011-04-24 18:40:17 -0700826MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800827 if (mRequest.getParameterCount() < 1)
828 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700829 MtpObjectHandle handle = mRequest.getParameter(1);
830 size_t thumbSize;
831 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
832 if (thumb) {
833 // send data
834 mData.setOperationCode(mRequest.getOperationCode());
835 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang63dac452017-12-06 15:19:36 -0800836 mData.writeData(mHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700837 free(thumb);
838 return MTP_RESPONSE_OK;
839 } else {
840 return MTP_RESPONSE_GENERAL_ERROR;
841 }
842}
843
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700844MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500845 if (!hasStorage())
846 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500847 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700848 uint64_t offset;
849 uint32_t length;
850 offset = mRequest.getParameter(2);
851 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800852 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
853 if (mRequest.getParameterCount() < 4)
854 return MTP_RESPONSE_INVALID_PARAMETER;
855
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700856 // android extension with 64 bit offset
857 uint64_t offset2 = mRequest.getParameter(3);
858 offset = offset | (offset2 << 32);
859 length = mRequest.getParameter(4);
860 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800861 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
862 if (mRequest.getParameterCount() < 3)
863 return MTP_RESPONSE_INVALID_PARAMETER;
864
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700865 // standard GetPartialObject
866 length = mRequest.getParameter(3);
867 }
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700868 MtpStringBuffer pathBuf;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500869 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800870 MtpObjectFormat format;
871 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500872 if (result != MTP_RESPONSE_OK)
873 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700874 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500875 length = fileLength - offset;
876
877 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700878 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500879 mtp_file_range mfr;
880 mfr.fd = open(filePath, O_RDONLY);
881 if (mfr.fd < 0) {
882 return MTP_RESPONSE_GENERAL_ERROR;
883 }
884 mfr.offset = offset;
885 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400886 mfr.command = mRequest.getOperationCode();
887 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500888 mResponse.setParameter(1, length);
889
Mike Lockwoodef441d92011-07-14 21:00:02 -0400890 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800891 int ret = mHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100892 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800893 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500894 if (ret < 0) {
895 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800896 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500897 else
tao.pei07a9e542015-07-17 17:18:41 +0800898 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500899 }
Jerry Zhangdc148de2018-05-14 12:07:16 -0700900 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800901 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500902}
903
Mike Lockwood16864ba2010-05-11 17:16:59 -0400904MtpResponseCode MtpServer::doSendObjectInfo() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700905 MtpStringBuffer path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800906 uint16_t temp16;
907 uint32_t temp32;
908
909 if (mRequest.getParameterCount() < 2)
910 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400911 MtpStorageID storageID = mRequest.getParameter(1);
912 MtpStorage* storage = getStorage(storageID);
913 MtpObjectHandle parent = mRequest.getParameter(2);
914 if (!storage)
915 return MTP_RESPONSE_INVALID_STORAGE_ID;
916
917 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400918 if (parent == MTP_PARENT_ROOT) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700919 path.set(storage->getPath());
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400920 parent = 0;
921 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800922 int64_t length;
923 MtpObjectFormat format;
924 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400925 if (result != MTP_RESPONSE_OK)
926 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800927 if (format != MTP_FORMAT_ASSOCIATION)
928 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400929 }
930
931 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800932 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
933 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
934 MtpObjectFormat format = temp16;
935 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
936 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
937 mSendObjectFileSize = temp32;
938 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
939 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
940 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
941 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
942 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
943 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
944 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
945 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
946 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800947 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800948 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400949 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800950 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700951 if (name.isEmpty()) {
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700952 ALOGE("empty name");
953 return MTP_RESPONSE_INVALID_PARAMETER;
954 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800955 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
956 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400957 // keywords follow
958
James Weiba389222018-09-27 19:22:55 +0800959 ALOGV("name: %s format: 0x%04X (%s)\n", (const char*)name, format,
960 MtpDebug::getFormatCodeName(format));
Mike Lockwoodfceef462010-05-14 15:35:17 -0400961 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400962 if (!parseDateTime(modified, modifiedTime))
963 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400964
James Wei6111b2b2019-05-28 17:18:21 +0800965 if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0) ||
966 (strchr(name, '/') != NULL)) {
967 char errMsg[80];
968
969 snprintf(errMsg, sizeof(errMsg), "Invalid name: %s", (const char *) name);
970 ALOGE("%s (b/130656917)", errMsg);
971 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "130656917", -1, errMsg,
972 strlen(errMsg));
973
974 return MTP_RESPONSE_INVALID_PARAMETER;
975 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400976 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700977 path.append("/");
978 path.append(name);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400979
Mike Lockwood20c3be02010-12-12 12:17:43 -0800980 // check space first
981 if (mSendObjectFileSize > storage->getFreeSpace())
982 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400983 uint64_t maxFileSize = storage->getMaxFileSize();
984 // check storage max file size
985 if (maxFileSize != 0) {
986 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
987 // is >= 0xFFFFFFFF
988 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
989 return MTP_RESPONSE_OBJECT_TOO_LARGE;
990 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800991
James Weiba389222018-09-27 19:22:55 +0800992 ALOGV("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700993 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, format,
994 parent, storageID);
James Weiba389222018-09-27 19:22:55 +0800995 ALOGD("handle: %d, parent: %d, storageID: %08X", handle, parent, storageID);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400996 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400997 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400998 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400999
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001000 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -07001001 int ret = makeFolder((const char *)path);
1002 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -04001003 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -08001004
1005 // SendObject does not get sent for directories, so call endSendObject here instead
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001006 mDatabase->endSendObject(handle, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001007 }
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001008 mSendObjectFilePath = path;
1009 // save the handle for the SendObject call, which should follow
1010 mSendObjectHandle = handle;
1011 mSendObjectFormat = format;
1012 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001013
1014 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001015 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001016 mResponse.setParameter(3, handle);
1017
1018 return MTP_RESPONSE_OK;
1019}
1020
Jerry Zhang708b3e02017-09-26 17:53:39 -07001021MtpResponseCode MtpServer::doMoveObject() {
1022 if (!hasStorage())
1023 return MTP_RESPONSE_GENERAL_ERROR;
1024 if (mRequest.getParameterCount() < 3)
1025 return MTP_RESPONSE_INVALID_PARAMETER;
1026 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1027 MtpStorageID storageID = mRequest.getParameter(2);
1028 MtpStorage* storage = getStorage(storageID);
1029 MtpObjectHandle parent = mRequest.getParameter(3);
1030 if (!storage)
1031 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001032 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001033 MtpResponseCode result;
1034
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001035 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001036 int64_t fileLength;
1037 MtpObjectFormat format;
1038 MtpObjectInfo info(objectHandle);
1039 result = mDatabase->getObjectInfo(objectHandle, info);
1040 if (result != MTP_RESPONSE_OK)
1041 return result;
1042 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1043 if (result != MTP_RESPONSE_OK)
1044 return result;
1045
1046 // special case the root
1047 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001048 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001049 } else {
1050 int64_t parentLength;
1051 MtpObjectFormat parentFormat;
1052 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1053 if (result != MTP_RESPONSE_OK)
1054 return result;
1055 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1056 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1057 }
1058
1059 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001060 path.append("/");
1061 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001062
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001063 result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
1064 if (result != MTP_RESPONSE_OK)
1065 return result;
1066
Jerry Zhang708b3e02017-09-26 17:53:39 -07001067 if (info.mStorageID == storageID) {
1068 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001069 if (renameTo(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001070 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001071 result = MTP_RESPONSE_GENERAL_ERROR;
1072 }
1073 } else {
1074 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001075 if (format == MTP_FORMAT_ASSOCIATION) {
1076 int ret = makeFolder((const char *)path);
1077 ret += copyRecursive(fromPath, path);
1078 if (ret) {
1079 result = MTP_RESPONSE_GENERAL_ERROR;
1080 } else {
1081 deletePath(fromPath);
1082 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001083 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001084 if (copyFile(fromPath, path)) {
1085 result = MTP_RESPONSE_GENERAL_ERROR;
1086 } else {
1087 deletePath(fromPath);
1088 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001089 }
1090 }
1091
1092 // If the move failed, undo the database change
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001093 mDatabase->endMoveObject(info.mParent, parent, info.mStorageID, storageID, objectHandle,
1094 result == MTP_RESPONSE_OK);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001095
1096 return result;
1097}
1098
1099MtpResponseCode MtpServer::doCopyObject() {
1100 if (!hasStorage())
1101 return MTP_RESPONSE_GENERAL_ERROR;
1102 MtpResponseCode result = MTP_RESPONSE_OK;
1103 if (mRequest.getParameterCount() < 3)
1104 return MTP_RESPONSE_INVALID_PARAMETER;
1105 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1106 MtpStorageID storageID = mRequest.getParameter(2);
1107 MtpStorage* storage = getStorage(storageID);
1108 MtpObjectHandle parent = mRequest.getParameter(3);
1109 if (!storage)
1110 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001111 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001112
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001113 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001114 int64_t fileLength;
1115 MtpObjectFormat format;
1116 MtpObjectInfo info(objectHandle);
1117 result = mDatabase->getObjectInfo(objectHandle, info);
1118 if (result != MTP_RESPONSE_OK)
1119 return result;
1120 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1121 if (result != MTP_RESPONSE_OK)
1122 return result;
1123
1124 // special case the root
1125 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001126 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001127 } else {
1128 int64_t parentLength;
1129 MtpObjectFormat parentFormat;
1130 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1131 if (result != MTP_RESPONSE_OK)
1132 return result;
1133 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1134 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1135 }
1136
1137 // check space first
1138 if ((uint64_t) fileLength > storage->getFreeSpace())
1139 return MTP_RESPONSE_STORAGE_FULL;
1140
1141 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001142 path.append("/");
1143 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001144
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001145 MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001146 if (handle == kInvalidObjectHandle) {
1147 return MTP_RESPONSE_GENERAL_ERROR;
1148 }
1149
1150 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001151 if (format == MTP_FORMAT_ASSOCIATION) {
1152 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001153 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001154 if (ret) {
1155 result = MTP_RESPONSE_GENERAL_ERROR;
1156 }
1157 } else {
1158 if (copyFile(fromPath, path)) {
1159 result = MTP_RESPONSE_GENERAL_ERROR;
1160 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001161 }
1162
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001163 mDatabase->endCopyObject(handle, result);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001164 mResponse.setParameter(1, handle);
1165 return result;
1166}
1167
Mike Lockwood16864ba2010-05-11 17:16:59 -04001168MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001169 if (!hasStorage())
1170 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001171 MtpResponseCode result = MTP_RESPONSE_OK;
1172 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001173 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001174 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001175 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001176
Jerry Zhang487be612016-10-24 12:10:41 -07001177 auto start = std::chrono::steady_clock::now();
1178
Mike Lockwood16864ba2010-05-11 17:16:59 -04001179 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001180 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001181 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1182 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001183 }
1184
Mike Lockwoodef441d92011-07-14 21:00:02 -04001185 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001186 ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001187 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1188 result = MTP_RESPONSE_GENERAL_ERROR;
1189 goto done;
1190 }
1191 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001192
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001193 if (mSendObjectFormat == MTP_FORMAT_ASSOCIATION) {
1194 if (initialData != 0)
1195 ALOGE("Expected folder size to be 0!");
1196 mSendObjectHandle = kInvalidObjectHandle;
1197 mSendObjectFormat = 0;
1198 mSendObjectModifiedTime = 0;
1199 return result;
1200 }
1201
Mike Lockwood16864ba2010-05-11 17:16:59 -04001202 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001203 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001204 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001205 result = MTP_RESPONSE_GENERAL_ERROR;
1206 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001207 }
Jerry Zhange242f122017-10-16 14:54:08 -07001208 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001209 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001210 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001211 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001212 umask(mask);
1213
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001214 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001215 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001216 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001217
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001218 if (ret < 0) {
1219 ALOGE("failed to write initial data");
1220 result = MTP_RESPONSE_GENERAL_ERROR;
1221 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001222 mfr.offset = initialData;
1223 if (mSendObjectFileSize == 0xFFFFFFFF) {
1224 // tell driver to read until it receives a short packet
1225 mfr.length = 0xFFFFFFFF;
1226 } else {
1227 mfr.length = mSendObjectFileSize - initialData;
1228 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001229
Jerry Zhang54107562017-05-15 11:54:19 -07001230 mfr.command = 0;
1231 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001232
Jerry Zhang54107562017-05-15 11:54:19 -07001233 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001234 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001235 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1236 if ((ret < 0) && (errno == ECANCELED)) {
1237 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001238 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001239 }
caozhiyuan854cb172017-04-26 16:52:30 +08001240
1241 if (mSendObjectModifiedTime) {
1242 struct timespec newTime[2];
1243 newTime[0].tv_nsec = UTIME_NOW;
1244 newTime[1].tv_sec = mSendObjectModifiedTime;
1245 newTime[1].tv_nsec = 0;
1246 if (futimens(mfr.fd, newTime) < 0) {
1247 ALOGW("changing modified time failed, %s", strerror(errno));
1248 }
1249 }
1250
Jerry Zhang487be612016-10-24 12:10:41 -07001251 fstat(mfr.fd, &sstat);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001252 closeObjFd(mfr.fd, mSendObjectFilePath);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001253
Mike Lockwood916076c2010-06-04 09:49:21 -04001254 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001255 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001256 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001257 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001258 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001259 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001260 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001261 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001262
1263done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001264 // reset so we don't attempt to send the data back
1265 mData.reset();
1266
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001267 mDatabase->endSendObject(mSendObjectHandle, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001268 mSendObjectHandle = kInvalidObjectHandle;
1269 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001270 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001271
1272 auto end = std::chrono::steady_clock::now();
1273 std::chrono::duration<double> diff = end - start;
1274 uint64_t finalsize = sstat.st_size;
1275 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1276 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001277 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001278}
1279
1280MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001281 if (!hasStorage())
1282 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001283 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001284 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001285 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001286 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001287 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1288 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001289
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001290 MtpStringBuffer filePath;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001291 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001292 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001293 if (result != MTP_RESPONSE_OK)
1294 return result;
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001295
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001296 // Don't delete the actual files unless the database deletion is allowed
1297 result = mDatabase->beginDeleteObject(handle);
1298 if (result != MTP_RESPONSE_OK)
1299 return result;
1300
1301 bool success = deletePath((const char *)filePath);
1302
1303 mDatabase->endDeleteObject(handle, success);
1304 return success ? result : MTP_RESPONSE_PARTIAL_DELETION;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001305}
1306
1307MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001308 if (mRequest.getParameterCount() < 2)
1309 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001310 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001311 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001312 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001313 MtpDebug::getFormatCodeName(format));
1314 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001315 if (!property)
1316 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001317 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001318 delete property;
1319 return MTP_RESPONSE_OK;
1320}
1321
1322MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001323 if (mRequest.getParameterCount() < 1)
1324 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001325 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001326 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001327 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1328 if (!property)
1329 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1330 property->write(mData);
1331 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001332 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001333}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001334
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001335MtpResponseCode MtpServer::doSendPartialObject() {
1336 if (!hasStorage())
1337 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001338 if (mRequest.getParameterCount() < 4)
1339 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001340 MtpObjectHandle handle = mRequest.getParameter(1);
1341 uint64_t offset = mRequest.getParameter(2);
1342 uint64_t offset2 = mRequest.getParameter(3);
1343 offset = offset | (offset2 << 32);
1344 uint32_t length = mRequest.getParameter(4);
1345
1346 ObjectEdit* edit = getEditObject(handle);
1347 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001348 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001349 return MTP_RESPONSE_GENERAL_ERROR;
1350 }
1351
1352 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001353 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001354 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1355 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001356 return MTP_RESPONSE_GENERAL_ERROR;
1357 }
1358
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001359 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001360 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001361
Mike Lockwoodef441d92011-07-14 21:00:02 -04001362 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001363 int ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001364 if (ret < MTP_CONTAINER_HEADER_SIZE)
1365 return MTP_RESPONSE_GENERAL_ERROR;
1366 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1367
1368 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001369 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001370 offset += initialData;
1371 length -= initialData;
1372 }
1373
tao.pei07a9e542015-07-17 17:18:41 +08001374 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001375 if (ret < 0) {
1376 ALOGE("failed to write initial data");
1377 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001378 mtp_file_range mfr;
1379 mfr.fd = edit->mFD;
1380 mfr.offset = offset;
1381 mfr.length = length;
1382 mfr.command = 0;
1383 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001384
Jerry Zhang54107562017-05-15 11:54:19 -07001385 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001386 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001387 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1388 if ((ret < 0) && (errno == ECANCELED)) {
1389 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001390 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001391 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001392 if (ret < 0) {
1393 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001394 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001395 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1396 else
1397 return MTP_RESPONSE_GENERAL_ERROR;
1398 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001399
1400 // reset so we don't attempt to send this back
1401 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001402 mResponse.setParameter(1, length);
1403 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001404 if (end > edit->mSize) {
1405 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001406 }
1407 return MTP_RESPONSE_OK;
1408}
1409
1410MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001411 if (mRequest.getParameterCount() < 3)
1412 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001413 MtpObjectHandle handle = mRequest.getParameter(1);
1414 ObjectEdit* edit = getEditObject(handle);
1415 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001416 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001417 return MTP_RESPONSE_GENERAL_ERROR;
1418 }
1419
1420 uint64_t offset = mRequest.getParameter(2);
1421 uint64_t offset2 = mRequest.getParameter(3);
1422 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001423 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001424 return MTP_RESPONSE_GENERAL_ERROR;
1425 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001426 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001427 return MTP_RESPONSE_OK;
1428 }
1429}
1430
1431MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001432 if (mRequest.getParameterCount() < 1)
1433 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001434 MtpObjectHandle handle = mRequest.getParameter(1);
1435 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001436 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001437 return MTP_RESPONSE_GENERAL_ERROR;
1438 }
1439
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001440 MtpStringBuffer path;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001441 int64_t fileLength;
1442 MtpObjectFormat format;
1443 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1444 if (result != MTP_RESPONSE_OK)
1445 return result;
1446
1447 int fd = open((const char *)path, O_RDWR | O_EXCL);
1448 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001449 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001450 return MTP_RESPONSE_GENERAL_ERROR;
1451 }
1452
1453 addEditObject(handle, path, fileLength, format, fd);
1454 return MTP_RESPONSE_OK;
1455}
1456
1457MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001458 if (mRequest.getParameterCount() < 1)
1459 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001460 MtpObjectHandle handle = mRequest.getParameter(1);
1461 ObjectEdit* edit = getEditObject(handle);
1462 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001463 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001464 return MTP_RESPONSE_GENERAL_ERROR;
1465 }
1466
1467 commitEdit(edit);
1468 removeEditObject(handle);
1469 return MTP_RESPONSE_OK;
1470}
1471
Mike Lockwood7850ef92010-05-14 10:10:36 -04001472} // namespace android