blob: a29193922b05e4837a42ddc00e4523740311da36 [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 {
48
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
nolyn.luef9bef52018-07-05 16:24:13 +0800959 int type = storage->getType();
960 if (type == MTP_STORAGE_REMOVABLE_RAM) {
961 std::string str = android::base::Trim((const char*)name);
962 name.set(str.c_str());
963 }
James Weiba389222018-09-27 19:22:55 +0800964 ALOGV("name: %s format: 0x%04X (%s)\n", (const char*)name, format,
965 MtpDebug::getFormatCodeName(format));
Mike Lockwoodfceef462010-05-14 15:35:17 -0400966 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400967 if (!parseDateTime(modified, modifiedTime))
968 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400969
970 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700971 path.append("/");
972 path.append(name);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400973
Mike Lockwood20c3be02010-12-12 12:17:43 -0800974 // check space first
975 if (mSendObjectFileSize > storage->getFreeSpace())
976 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400977 uint64_t maxFileSize = storage->getMaxFileSize();
978 // check storage max file size
979 if (maxFileSize != 0) {
980 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
981 // is >= 0xFFFFFFFF
982 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
983 return MTP_RESPONSE_OBJECT_TOO_LARGE;
984 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800985
James Weiba389222018-09-27 19:22:55 +0800986 ALOGV("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700987 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, format,
988 parent, storageID);
James Weiba389222018-09-27 19:22:55 +0800989 ALOGD("handle: %d, parent: %d, storageID: %08X", handle, parent, storageID);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400990 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400991 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400992 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400993
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700994 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -0700995 int ret = makeFolder((const char *)path);
996 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400997 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -0800998
999 // SendObject does not get sent for directories, so call endSendObject here instead
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001000 mDatabase->endSendObject(handle, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001001 }
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001002 mSendObjectFilePath = path;
1003 // save the handle for the SendObject call, which should follow
1004 mSendObjectHandle = handle;
1005 mSendObjectFormat = format;
1006 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001007
1008 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001009 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001010 mResponse.setParameter(3, handle);
1011
1012 return MTP_RESPONSE_OK;
1013}
1014
Jerry Zhang708b3e02017-09-26 17:53:39 -07001015MtpResponseCode MtpServer::doMoveObject() {
1016 if (!hasStorage())
1017 return MTP_RESPONSE_GENERAL_ERROR;
1018 if (mRequest.getParameterCount() < 3)
1019 return MTP_RESPONSE_INVALID_PARAMETER;
1020 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1021 MtpStorageID storageID = mRequest.getParameter(2);
1022 MtpStorage* storage = getStorage(storageID);
1023 MtpObjectHandle parent = mRequest.getParameter(3);
1024 if (!storage)
1025 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001026 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001027 MtpResponseCode result;
1028
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001029 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001030 int64_t fileLength;
1031 MtpObjectFormat format;
1032 MtpObjectInfo info(objectHandle);
1033 result = mDatabase->getObjectInfo(objectHandle, info);
1034 if (result != MTP_RESPONSE_OK)
1035 return result;
1036 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1037 if (result != MTP_RESPONSE_OK)
1038 return result;
1039
1040 // special case the root
1041 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001042 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001043 } else {
1044 int64_t parentLength;
1045 MtpObjectFormat parentFormat;
1046 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1047 if (result != MTP_RESPONSE_OK)
1048 return result;
1049 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1050 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1051 }
1052
1053 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001054 path.append("/");
1055 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001056
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001057 result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
1058 if (result != MTP_RESPONSE_OK)
1059 return result;
1060
Jerry Zhang708b3e02017-09-26 17:53:39 -07001061 if (info.mStorageID == storageID) {
1062 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001063 if (renameTo(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001064 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001065 result = MTP_RESPONSE_GENERAL_ERROR;
1066 }
1067 } else {
1068 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001069 if (format == MTP_FORMAT_ASSOCIATION) {
1070 int ret = makeFolder((const char *)path);
1071 ret += copyRecursive(fromPath, path);
1072 if (ret) {
1073 result = MTP_RESPONSE_GENERAL_ERROR;
1074 } else {
1075 deletePath(fromPath);
1076 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001077 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001078 if (copyFile(fromPath, path)) {
1079 result = MTP_RESPONSE_GENERAL_ERROR;
1080 } else {
1081 deletePath(fromPath);
1082 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001083 }
1084 }
1085
1086 // If the move failed, undo the database change
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001087 mDatabase->endMoveObject(info.mParent, parent, info.mStorageID, storageID, objectHandle,
1088 result == MTP_RESPONSE_OK);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001089
1090 return result;
1091}
1092
1093MtpResponseCode MtpServer::doCopyObject() {
1094 if (!hasStorage())
1095 return MTP_RESPONSE_GENERAL_ERROR;
1096 MtpResponseCode result = MTP_RESPONSE_OK;
1097 if (mRequest.getParameterCount() < 3)
1098 return MTP_RESPONSE_INVALID_PARAMETER;
1099 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1100 MtpStorageID storageID = mRequest.getParameter(2);
1101 MtpStorage* storage = getStorage(storageID);
1102 MtpObjectHandle parent = mRequest.getParameter(3);
1103 if (!storage)
1104 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001105 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001106
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001107 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001108 int64_t fileLength;
1109 MtpObjectFormat format;
1110 MtpObjectInfo info(objectHandle);
1111 result = mDatabase->getObjectInfo(objectHandle, info);
1112 if (result != MTP_RESPONSE_OK)
1113 return result;
1114 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1115 if (result != MTP_RESPONSE_OK)
1116 return result;
1117
1118 // special case the root
1119 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001120 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001121 } else {
1122 int64_t parentLength;
1123 MtpObjectFormat parentFormat;
1124 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1125 if (result != MTP_RESPONSE_OK)
1126 return result;
1127 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1128 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1129 }
1130
1131 // check space first
1132 if ((uint64_t) fileLength > storage->getFreeSpace())
1133 return MTP_RESPONSE_STORAGE_FULL;
1134
1135 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001136 path.append("/");
1137 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001138
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001139 MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001140 if (handle == kInvalidObjectHandle) {
1141 return MTP_RESPONSE_GENERAL_ERROR;
1142 }
1143
1144 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001145 if (format == MTP_FORMAT_ASSOCIATION) {
1146 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001147 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001148 if (ret) {
1149 result = MTP_RESPONSE_GENERAL_ERROR;
1150 }
1151 } else {
1152 if (copyFile(fromPath, path)) {
1153 result = MTP_RESPONSE_GENERAL_ERROR;
1154 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001155 }
1156
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001157 mDatabase->endCopyObject(handle, result);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001158 mResponse.setParameter(1, handle);
1159 return result;
1160}
1161
Mike Lockwood16864ba2010-05-11 17:16:59 -04001162MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001163 if (!hasStorage())
1164 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001165 MtpResponseCode result = MTP_RESPONSE_OK;
1166 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001167 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001168 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001169 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001170
Jerry Zhang487be612016-10-24 12:10:41 -07001171 auto start = std::chrono::steady_clock::now();
1172
Mike Lockwood16864ba2010-05-11 17:16:59 -04001173 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001174 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001175 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1176 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001177 }
1178
Mike Lockwoodef441d92011-07-14 21:00:02 -04001179 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001180 ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001181 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1182 result = MTP_RESPONSE_GENERAL_ERROR;
1183 goto done;
1184 }
1185 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001186
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001187 if (mSendObjectFormat == MTP_FORMAT_ASSOCIATION) {
1188 if (initialData != 0)
1189 ALOGE("Expected folder size to be 0!");
1190 mSendObjectHandle = kInvalidObjectHandle;
1191 mSendObjectFormat = 0;
1192 mSendObjectModifiedTime = 0;
1193 return result;
1194 }
1195
Mike Lockwood16864ba2010-05-11 17:16:59 -04001196 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001197 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001198 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001199 result = MTP_RESPONSE_GENERAL_ERROR;
1200 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001201 }
Jerry Zhange242f122017-10-16 14:54:08 -07001202 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001203 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001204 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001205 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001206 umask(mask);
1207
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001208 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001209 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001210 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001211
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001212 if (ret < 0) {
1213 ALOGE("failed to write initial data");
1214 result = MTP_RESPONSE_GENERAL_ERROR;
1215 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001216 mfr.offset = initialData;
1217 if (mSendObjectFileSize == 0xFFFFFFFF) {
1218 // tell driver to read until it receives a short packet
1219 mfr.length = 0xFFFFFFFF;
1220 } else {
1221 mfr.length = mSendObjectFileSize - initialData;
1222 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001223
Jerry Zhang54107562017-05-15 11:54:19 -07001224 mfr.command = 0;
1225 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001226
Jerry Zhang54107562017-05-15 11:54:19 -07001227 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001228 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001229 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1230 if ((ret < 0) && (errno == ECANCELED)) {
1231 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001232 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001233 }
caozhiyuan854cb172017-04-26 16:52:30 +08001234
1235 if (mSendObjectModifiedTime) {
1236 struct timespec newTime[2];
1237 newTime[0].tv_nsec = UTIME_NOW;
1238 newTime[1].tv_sec = mSendObjectModifiedTime;
1239 newTime[1].tv_nsec = 0;
1240 if (futimens(mfr.fd, newTime) < 0) {
1241 ALOGW("changing modified time failed, %s", strerror(errno));
1242 }
1243 }
1244
Jerry Zhang487be612016-10-24 12:10:41 -07001245 fstat(mfr.fd, &sstat);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001246 closeObjFd(mfr.fd, mSendObjectFilePath);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001247
Mike Lockwood916076c2010-06-04 09:49:21 -04001248 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001249 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001250 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001251 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001252 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001253 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001254 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001255 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001256
1257done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001258 // reset so we don't attempt to send the data back
1259 mData.reset();
1260
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001261 mDatabase->endSendObject(mSendObjectHandle, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001262 mSendObjectHandle = kInvalidObjectHandle;
1263 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001264 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001265
1266 auto end = std::chrono::steady_clock::now();
1267 std::chrono::duration<double> diff = end - start;
1268 uint64_t finalsize = sstat.st_size;
1269 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1270 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001271 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001272}
1273
1274MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001275 if (!hasStorage())
1276 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001277 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001278 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001279 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001280 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001281 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1282 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001283
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001284 MtpStringBuffer filePath;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001285 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001286 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001287 if (result != MTP_RESPONSE_OK)
1288 return result;
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001289
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001290 // Don't delete the actual files unless the database deletion is allowed
1291 result = mDatabase->beginDeleteObject(handle);
1292 if (result != MTP_RESPONSE_OK)
1293 return result;
1294
1295 bool success = deletePath((const char *)filePath);
1296
1297 mDatabase->endDeleteObject(handle, success);
1298 return success ? result : MTP_RESPONSE_PARTIAL_DELETION;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001299}
1300
1301MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001302 if (mRequest.getParameterCount() < 2)
1303 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001304 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001305 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001306 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001307 MtpDebug::getFormatCodeName(format));
1308 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001309 if (!property)
1310 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001311 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001312 delete property;
1313 return MTP_RESPONSE_OK;
1314}
1315
1316MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001317 if (mRequest.getParameterCount() < 1)
1318 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001319 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001321 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1322 if (!property)
1323 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1324 property->write(mData);
1325 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001326 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001327}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001328
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001329MtpResponseCode MtpServer::doSendPartialObject() {
1330 if (!hasStorage())
1331 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001332 if (mRequest.getParameterCount() < 4)
1333 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001334 MtpObjectHandle handle = mRequest.getParameter(1);
1335 uint64_t offset = mRequest.getParameter(2);
1336 uint64_t offset2 = mRequest.getParameter(3);
1337 offset = offset | (offset2 << 32);
1338 uint32_t length = mRequest.getParameter(4);
1339
1340 ObjectEdit* edit = getEditObject(handle);
1341 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001342 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001343 return MTP_RESPONSE_GENERAL_ERROR;
1344 }
1345
1346 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001347 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001348 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1349 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001350 return MTP_RESPONSE_GENERAL_ERROR;
1351 }
1352
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001353 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001354 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001355
Mike Lockwoodef441d92011-07-14 21:00:02 -04001356 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001357 int ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001358 if (ret < MTP_CONTAINER_HEADER_SIZE)
1359 return MTP_RESPONSE_GENERAL_ERROR;
1360 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1361
1362 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001363 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001364 offset += initialData;
1365 length -= initialData;
1366 }
1367
tao.pei07a9e542015-07-17 17:18:41 +08001368 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001369 if (ret < 0) {
1370 ALOGE("failed to write initial data");
1371 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001372 mtp_file_range mfr;
1373 mfr.fd = edit->mFD;
1374 mfr.offset = offset;
1375 mfr.length = length;
1376 mfr.command = 0;
1377 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001378
Jerry Zhang54107562017-05-15 11:54:19 -07001379 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001380 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001381 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1382 if ((ret < 0) && (errno == ECANCELED)) {
1383 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001384 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001385 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001386 if (ret < 0) {
1387 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001388 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001389 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1390 else
1391 return MTP_RESPONSE_GENERAL_ERROR;
1392 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001393
1394 // reset so we don't attempt to send this back
1395 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001396 mResponse.setParameter(1, length);
1397 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001398 if (end > edit->mSize) {
1399 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001400 }
1401 return MTP_RESPONSE_OK;
1402}
1403
1404MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001405 if (mRequest.getParameterCount() < 3)
1406 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001407 MtpObjectHandle handle = mRequest.getParameter(1);
1408 ObjectEdit* edit = getEditObject(handle);
1409 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001410 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001411 return MTP_RESPONSE_GENERAL_ERROR;
1412 }
1413
1414 uint64_t offset = mRequest.getParameter(2);
1415 uint64_t offset2 = mRequest.getParameter(3);
1416 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001417 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001418 return MTP_RESPONSE_GENERAL_ERROR;
1419 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001420 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001421 return MTP_RESPONSE_OK;
1422 }
1423}
1424
1425MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001426 if (mRequest.getParameterCount() < 1)
1427 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001428 MtpObjectHandle handle = mRequest.getParameter(1);
1429 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001430 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001431 return MTP_RESPONSE_GENERAL_ERROR;
1432 }
1433
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001434 MtpStringBuffer path;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001435 int64_t fileLength;
1436 MtpObjectFormat format;
1437 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1438 if (result != MTP_RESPONSE_OK)
1439 return result;
1440
1441 int fd = open((const char *)path, O_RDWR | O_EXCL);
1442 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001443 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001444 return MTP_RESPONSE_GENERAL_ERROR;
1445 }
1446
1447 addEditObject(handle, path, fileLength, format, fd);
1448 return MTP_RESPONSE_OK;
1449}
1450
1451MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001452 if (mRequest.getParameterCount() < 1)
1453 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001454 MtpObjectHandle handle = mRequest.getParameter(1);
1455 ObjectEdit* edit = getEditObject(handle);
1456 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001457 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001458 return MTP_RESPONSE_GENERAL_ERROR;
1459 }
1460
1461 commitEdit(edit);
1462 removeEditObject(handle);
1463 return MTP_RESPONSE_OK;
1464}
1465
Mike Lockwood7850ef92010-05-14 10:10:36 -04001466} // namespace android