blob: 8677b90480891e57769f8225d74691e26eecf7cc [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jerry Zhange5aa05d2017-10-13 12:14:42 -070017#include <algorithm>
Jerry Zhange242f122017-10-16 14:54:08 -070018#include <android-base/logging.h>
Jerry Zhang487be612016-10-24 12:10:41 -070019#include <android-base/properties.h>
20#include <chrono>
Jerry Zhang487be612016-10-24 12:10:41 -070021#include <dirent.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <inttypes.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040025#include <stdio.h>
26#include <stdlib.h>
27#include <sys/types.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040028#include <sys/stat.h>
Mike Lockwoodd3211492010-09-13 17:15:58 -040029#include <sys/stat.h>
caozhiyuan854cb172017-04-26 16:52:30 +080030#include <sys/time.h>
Mike Lockwoodc42aa122010-06-14 17:58:08 -070031
Mike Lockwooda881b442010-09-23 22:32:05 -040032#define LOG_TAG "MtpServer"
33
Mike Lockwood16864ba2010-05-11 17:16:59 -040034#include "MtpDebug.h"
Jerry Zhange5aa05d2017-10-13 12:14:42 -070035#include "IMtpDatabase.h"
Jerry Zhang63dac452017-12-06 15:19:36 -080036#include "MtpDescriptors.h"
Jerry Zhangdf69dd32017-05-03 17:17:49 -070037#include "MtpDevHandle.h"
38#include "MtpFfsCompatHandle.h"
39#include "MtpFfsHandle.h"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070040#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040041#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040042#include "MtpServer.h"
43#include "MtpStorage.h"
44#include "MtpStringBuffer.h"
nolyn.luef9bef52018-07-05 16:24:13 +080045#include "android-base/strings.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040046
Mike Lockwood7850ef92010-05-14 10:10:36 -040047namespace android {
James Weie783e4b2019-05-28 17:18:21 +080048static const int SN_EVENT_LOG_ID = 0x534e4554;
Mike Lockwood7850ef92010-05-14 10:10:36 -040049
Mike Lockwood16864ba2010-05-11 17:16:59 -040050static const MtpOperationCode kSupportedOperationCodes[] = {
51 MTP_OPERATION_GET_DEVICE_INFO,
52 MTP_OPERATION_OPEN_SESSION,
53 MTP_OPERATION_CLOSE_SESSION,
54 MTP_OPERATION_GET_STORAGE_IDS,
55 MTP_OPERATION_GET_STORAGE_INFO,
56 MTP_OPERATION_GET_NUM_OBJECTS,
57 MTP_OPERATION_GET_OBJECT_HANDLES,
58 MTP_OPERATION_GET_OBJECT_INFO,
59 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070060 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040061 MTP_OPERATION_DELETE_OBJECT,
62 MTP_OPERATION_SEND_OBJECT_INFO,
63 MTP_OPERATION_SEND_OBJECT,
64// MTP_OPERATION_INITIATE_CAPTURE,
65// MTP_OPERATION_FORMAT_STORE,
Jerry Zhang6dafecc2017-02-23 16:39:30 -080066 MTP_OPERATION_RESET_DEVICE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040067// MTP_OPERATION_SELF_TEST,
68// MTP_OPERATION_SET_OBJECT_PROTECTION,
69// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040070 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040071 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
72 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
73 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040074// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
Jerry Zhang708b3e02017-09-26 17:53:39 -070075 MTP_OPERATION_MOVE_OBJECT,
76 MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050077 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040078// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
79 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040080 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040081 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
82 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040083 MTP_OPERATION_GET_OBJECT_PROP_LIST,
84// MTP_OPERATION_SET_OBJECT_PROP_LIST,
85// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
86// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Jeff Sharkeyb8d374b2019-12-16 14:11:11 -070087// MTP_OPERATION_GET_OBJECT_REFERENCES,
88// MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040089// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070090 // Android extension for direct file IO
91 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
92 MTP_OPERATION_SEND_PARTIAL_OBJECT,
93 MTP_OPERATION_TRUNCATE_OBJECT,
94 MTP_OPERATION_BEGIN_EDIT_OBJECT,
95 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040096};
97
Mike Lockwood873871f2010-07-12 18:54:16 -040098static const MtpEventCode kSupportedEventCodes[] = {
99 MTP_EVENT_OBJECT_ADDED,
100 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -0500101 MTP_EVENT_STORE_ADDED,
102 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800103 MTP_EVENT_DEVICE_PROP_CHANGED,
James55138432018-07-02 18:07:30 +0800104 MTP_EVENT_OBJECT_INFO_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -0400105};
106
Jerry Zhang63dac452017-12-06 15:19:36 -0800107MtpServer::MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700108 const char *deviceInfoManufacturer,
109 const char *deviceInfoModel,
110 const char *deviceInfoDeviceVersion,
111 const char *deviceInfoSerialNumber)
Jerry Zhang487be612016-10-24 12:10:41 -0700112 : mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400113 mPtp(ptp),
Alex Klyubin792298f2016-12-21 11:20:22 -0800114 mDeviceInfoManufacturer(deviceInfoManufacturer),
115 mDeviceInfoModel(deviceInfoModel),
116 mDeviceInfoDeviceVersion(deviceInfoDeviceVersion),
117 mDeviceInfoSerialNumber(deviceInfoSerialNumber),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400118 mSessionID(0),
119 mSessionOpen(false),
120 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400121 mSendObjectFormat(0),
caozhiyuan854cb172017-04-26 16:52:30 +0800122 mSendObjectFileSize(0),
123 mSendObjectModifiedTime(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400124{
Jerry Zhang63dac452017-12-06 15:19:36 -0800125 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
126 if (ffs_ok) {
127 bool aio_compat = android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false);
128 mHandle = aio_compat ? new MtpFfsCompatHandle(controlFd) : new MtpFfsHandle(controlFd);
129 } else {
130 mHandle = new MtpDevHandle();
131 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400132}
133
134MtpServer::~MtpServer() {
135}
136
Mike Lockwooda8494402011-02-18 09:07:14 -0500137void MtpServer::addStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700138 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500139
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700140 mStorages.push_back(storage);
Mike Lockwooda8494402011-02-18 09:07:14 -0500141 sendStoreAdded(storage->getStorageID());
142}
143
144void MtpServer::removeStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700145 std::lock_guard<std::mutex> lg(mMutex);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700146 auto iter = std::find(mStorages.begin(), mStorages.end(), storage);
147 if (iter != mStorages.end()) {
148 sendStoreRemoved(storage->getStorageID());
149 mStorages.erase(iter);
Mike Lockwooda8494402011-02-18 09:07:14 -0500150 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400151}
152
153MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800154 if (id == 0)
155 return mStorages[0];
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700156 for (MtpStorage *storage : mStorages) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400157 if (storage->getStorageID() == id)
158 return storage;
159 }
Jerry Zhang487be612016-10-24 12:10:41 -0700160 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400161}
162
Mike Lockwooda8494402011-02-18 09:07:14 -0500163bool MtpServer::hasStorage(MtpStorageID id) {
164 if (id == 0 || id == 0xFFFFFFFF)
165 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700166 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500167}
168
Mike Lockwood16864ba2010-05-11 17:16:59 -0400169void MtpServer::run() {
Jerry Zhang63dac452017-12-06 15:19:36 -0800170 if (mHandle->start(mPtp)) {
Jerry Zhang487be612016-10-24 12:10:41 -0700171 ALOGE("Failed to start usb driver!");
Jerry Zhang63dac452017-12-06 15:19:36 -0800172 mHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700173 return;
174 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400175
176 while (1) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800177 int ret = mRequest.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400178 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700179 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400180 if (errno == ECANCELED) {
181 // return to top of loop and wait for next command
182 continue;
183 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400184 break;
185 }
186 MtpOperationCode operation = mRequest.getOperationCode();
187 MtpTransactionID transaction = mRequest.getTransactionID();
188
Steve Block3856b092011-10-20 11:56:00 +0100189 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400190 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400191 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400192 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
193 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
194 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400195 if (dataIn) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800196 int ret = mData.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400197 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000198 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400199 if (errno == ECANCELED) {
200 // return to top of loop and wait for next command
201 continue;
202 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400203 break;
204 }
Steve Block3856b092011-10-20 11:56:00 +0100205 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400206 } else {
207 mData.reset();
208 }
209
Mike Lockwood916076c2010-06-04 09:49:21 -0400210 if (handleRequest()) {
211 if (!dataIn && mData.hasData()) {
212 mData.setOperationCode(operation);
213 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100214 ALOGV("sending data:");
Jerry Zhang63dac452017-12-06 15:19:36 -0800215 ret = mData.write(mHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400216 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000217 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400218 if (errno == ECANCELED) {
219 // return to top of loop and wait for next command
220 continue;
221 }
222 break;
223 }
224 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400225
Mike Lockwood916076c2010-06-04 09:49:21 -0400226 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang63dac452017-12-06 15:19:36 -0800228 ret = mResponse.write(mHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800229 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400230 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000231 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800232 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400233 // return to top of loop and wait for next command
234 continue;
235 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400236 break;
237 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400238 } else {
Steve Block3856b092011-10-20 11:56:00 +0100239 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400240 }
241 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400242
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700243 // commit any open edits
244 int count = mObjectEditList.size();
245 for (int i = 0; i < count; i++) {
246 ObjectEdit* edit = mObjectEditList[i];
247 commitEdit(edit);
248 delete edit;
249 }
250 mObjectEditList.clear();
251
Jerry Zhang63dac452017-12-06 15:19:36 -0800252 mHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400253}
254
Mike Lockwood873871f2010-07-12 18:54:16 -0400255void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500257 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400258}
259
260void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500262 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
263}
264
James55138432018-07-02 18:07:30 +0800265void MtpServer::sendObjectInfoChanged(MtpObjectHandle handle) {
266 ALOGV("sendObjectInfoChanged %d\n", handle);
267 sendEvent(MTP_EVENT_OBJECT_INFO_CHANGED, handle);
268}
269
Mike Lockwooda8494402011-02-18 09:07:14 -0500270void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500272 sendEvent(MTP_EVENT_STORE_ADDED, id);
273}
274
275void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100276 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500277 sendEvent(MTP_EVENT_STORE_REMOVED, id);
278}
279
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800280void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
281 ALOGV("sendDevicePropertyChanged %d\n", property);
282 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
283}
284
Mike Lockwooda8494402011-02-18 09:07:14 -0500285void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400286 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500287 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400288 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500289 mEvent.setParameter(1, param1);
Jerry Zhang63dac452017-12-06 15:19:36 -0800290 if (mEvent.write(mHandle))
Jerry Zhang487be612016-10-24 12:10:41 -0700291 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400292 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400293}
294
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700295void MtpServer::addEditObject(MtpObjectHandle handle, MtpStringBuffer& path,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700296 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700297 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700298 mObjectEditList.push_back(edit);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700299}
300
301MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
302 int count = mObjectEditList.size();
303 for (int i = 0; i < count; i++) {
304 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700305 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700306 }
Jerry Zhang487be612016-10-24 12:10:41 -0700307 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700308}
309
310void MtpServer::removeEditObject(MtpObjectHandle handle) {
311 int count = mObjectEditList.size();
312 for (int i = 0; i < count; i++) {
313 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700314 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700315 delete edit;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700316 mObjectEditList.erase(mObjectEditList.begin() + i);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700317 return;
318 }
319 }
Steve Block29357bc2012-01-06 19:20:56 +0000320 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700321}
322
323void MtpServer::commitEdit(ObjectEdit* edit) {
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700324 mDatabase->rescanFile((const char *)edit->mPath, edit->mHandle, edit->mFormat);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700325}
326
327
Mike Lockwood916076c2010-06-04 09:49:21 -0400328bool MtpServer::handleRequest() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700329 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500330
Mike Lockwood16864ba2010-05-11 17:16:59 -0400331 MtpOperationCode operation = mRequest.getOperationCode();
332 MtpResponseCode response;
333
334 mResponse.reset();
335
336 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400337 mSendObjectHandle = kInvalidObjectHandle;
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700338 mSendObjectFormat = 0;
339 mSendObjectModifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400340 }
341
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700342 int containertype = mRequest.getContainerType();
343 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
344 ALOGE("wrong container type %d", containertype);
345 return false;
346 }
347
348 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
349
Mike Lockwood16864ba2010-05-11 17:16:59 -0400350 switch (operation) {
351 case MTP_OPERATION_GET_DEVICE_INFO:
352 response = doGetDeviceInfo();
353 break;
354 case MTP_OPERATION_OPEN_SESSION:
355 response = doOpenSession();
356 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800357 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400358 case MTP_OPERATION_CLOSE_SESSION:
359 response = doCloseSession();
360 break;
361 case MTP_OPERATION_GET_STORAGE_IDS:
362 response = doGetStorageIDs();
363 break;
364 case MTP_OPERATION_GET_STORAGE_INFO:
365 response = doGetStorageInfo();
366 break;
367 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
368 response = doGetObjectPropsSupported();
369 break;
370 case MTP_OPERATION_GET_OBJECT_HANDLES:
371 response = doGetObjectHandles();
372 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400373 case MTP_OPERATION_GET_NUM_OBJECTS:
374 response = doGetNumObjects();
375 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400376 case MTP_OPERATION_GET_OBJECT_REFERENCES:
377 response = doGetObjectReferences();
378 break;
379 case MTP_OPERATION_SET_OBJECT_REFERENCES:
380 response = doSetObjectReferences();
381 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400382 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
383 response = doGetObjectPropValue();
384 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400385 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
386 response = doSetObjectPropValue();
387 break;
388 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
389 response = doGetDevicePropValue();
390 break;
391 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
392 response = doSetDevicePropValue();
393 break;
394 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
395 response = doResetDevicePropValue();
396 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400397 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
398 response = doGetObjectPropList();
399 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400400 case MTP_OPERATION_GET_OBJECT_INFO:
401 response = doGetObjectInfo();
402 break;
403 case MTP_OPERATION_GET_OBJECT:
404 response = doGetObject();
405 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700406 case MTP_OPERATION_GET_THUMB:
407 response = doGetThumb();
408 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500409 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700410 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
411 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500412 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400413 case MTP_OPERATION_SEND_OBJECT_INFO:
414 response = doSendObjectInfo();
415 break;
416 case MTP_OPERATION_SEND_OBJECT:
417 response = doSendObject();
418 break;
419 case MTP_OPERATION_DELETE_OBJECT:
420 response = doDeleteObject();
421 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700422 case MTP_OPERATION_COPY_OBJECT:
423 response = doCopyObject();
424 break;
425 case MTP_OPERATION_MOVE_OBJECT:
426 response = doMoveObject();
427 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400428 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400429 response = doGetObjectPropDesc();
430 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400431 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
432 response = doGetDevicePropDesc();
433 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700434 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
435 response = doSendPartialObject();
436 break;
437 case MTP_OPERATION_TRUNCATE_OBJECT:
438 response = doTruncateObject();
439 break;
440 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
441 response = doBeginEditObject();
442 break;
443 case MTP_OPERATION_END_EDIT_OBJECT:
444 response = doEndEditObject();
445 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400446 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700447 ALOGE("got unsupported command %s (%x)",
448 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400449 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
450 break;
451 }
452
James Weiba389222018-09-27 19:22:55 +0800453 if (response != MTP_RESPONSE_OK)
454 ALOGW("[MTP] got response 0x%X in command %s (%x)", response,
455 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood916076c2010-06-04 09:49:21 -0400456 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
457 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400458 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400459 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400460}
461
462MtpResponseCode MtpServer::doGetDeviceInfo() {
463 MtpStringBuffer string;
464
Mike Lockwood782aef12010-08-10 07:37:50 -0400465 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
466 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
467 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
468
Mike Lockwood16864ba2010-05-11 17:16:59 -0400469 // fill in device info
470 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400471 if (mPtp) {
472 mData.putUInt32(0);
473 } else {
474 // MTP Vendor Extension ID
475 mData.putUInt32(6);
476 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400477 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400478 if (mPtp) {
479 // no extensions
480 string.set("");
481 } else {
482 // MTP extensions
483 string.set("microsoft.com: 1.0; android.com: 1.0;");
484 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400485 mData.putString(string); // MTP Extensions
486 mData.putUInt16(0); //Functional Mode
487 mData.putAUInt16(kSupportedOperationCodes,
488 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400489 mData.putAUInt16(kSupportedEventCodes,
490 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400491 mData.putAUInt16(deviceProperties); // Device Properties Supported
492 mData.putAUInt16(captureFormats); // Capture Formats
493 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500494
Alex Klyubin792298f2016-12-21 11:20:22 -0800495 mData.putString(mDeviceInfoManufacturer); // Manufacturer
496 mData.putString(mDeviceInfoModel); // Model
497 mData.putString(mDeviceInfoDeviceVersion); // Device Version
498 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400499
Mike Lockwood782aef12010-08-10 07:37:50 -0400500 delete playbackFormats;
501 delete captureFormats;
502 delete deviceProperties;
503
Mike Lockwood16864ba2010-05-11 17:16:59 -0400504 return MTP_RESPONSE_OK;
505}
506
507MtpResponseCode MtpServer::doOpenSession() {
508 if (mSessionOpen) {
509 mResponse.setParameter(1, mSessionID);
510 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
511 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800512 if (mRequest.getParameterCount() < 1)
513 return MTP_RESPONSE_INVALID_PARAMETER;
514
Mike Lockwood16864ba2010-05-11 17:16:59 -0400515 mSessionID = mRequest.getParameter(1);
516 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400517
Mike Lockwood16864ba2010-05-11 17:16:59 -0400518 return MTP_RESPONSE_OK;
519}
520
521MtpResponseCode MtpServer::doCloseSession() {
522 if (!mSessionOpen)
523 return MTP_RESPONSE_SESSION_NOT_OPEN;
524 mSessionID = 0;
525 mSessionOpen = false;
526 return MTP_RESPONSE_OK;
527}
528
529MtpResponseCode MtpServer::doGetStorageIDs() {
530 if (!mSessionOpen)
531 return MTP_RESPONSE_SESSION_NOT_OPEN;
532
533 int count = mStorages.size();
534 mData.putUInt32(count);
535 for (int i = 0; i < count; i++)
536 mData.putUInt32(mStorages[i]->getStorageID());
537
538 return MTP_RESPONSE_OK;
539}
540
541MtpResponseCode MtpServer::doGetStorageInfo() {
542 MtpStringBuffer string;
543
544 if (!mSessionOpen)
545 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800546 if (mRequest.getParameterCount() < 1)
547 return MTP_RESPONSE_INVALID_PARAMETER;
548
Mike Lockwood16864ba2010-05-11 17:16:59 -0400549 MtpStorageID id = mRequest.getParameter(1);
550 MtpStorage* storage = getStorage(id);
551 if (!storage)
552 return MTP_RESPONSE_INVALID_STORAGE_ID;
553
554 mData.putUInt16(storage->getType());
555 mData.putUInt16(storage->getFileSystemType());
556 mData.putUInt16(storage->getAccessCapability());
557 mData.putUInt64(storage->getMaxCapacity());
558 mData.putUInt64(storage->getFreeSpace());
559 mData.putUInt32(1024*1024*1024); // Free Space in Objects
560 string.set(storage->getDescription());
561 mData.putString(string);
562 mData.putEmptyString(); // Volume Identifier
563
564 return MTP_RESPONSE_OK;
565}
566
567MtpResponseCode MtpServer::doGetObjectPropsSupported() {
568 if (!mSessionOpen)
569 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800570 if (mRequest.getParameterCount() < 1)
571 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400572 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800573 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400574 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400575 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400576 return MTP_RESPONSE_OK;
577}
578
579MtpResponseCode MtpServer::doGetObjectHandles() {
580 if (!mSessionOpen)
581 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800582 if (mRequest.getParameterCount() < 3)
583 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400584 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400585 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400586 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400587 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500588
589 if (!hasStorage(storageID))
590 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400591
592 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700593 if (handles == NULL)
594 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400595 mData.putAUInt32(handles);
596 delete handles;
597 return MTP_RESPONSE_OK;
598}
599
Mike Lockwood343af4e2010-08-02 10:52:20 -0400600MtpResponseCode MtpServer::doGetNumObjects() {
601 if (!mSessionOpen)
602 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800603 if (mRequest.getParameterCount() < 3)
604 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400605 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
606 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
607 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400608 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500609 if (!hasStorage(storageID))
610 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400611
612 int count = mDatabase->getNumObjects(storageID, format, parent);
613 if (count >= 0) {
614 mResponse.setParameter(1, count);
615 return MTP_RESPONSE_OK;
616 } else {
617 mResponse.setParameter(1, 0);
618 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
619 }
620}
621
Mike Lockwood438344f2010-08-03 15:30:09 -0400622MtpResponseCode MtpServer::doGetObjectReferences() {
623 if (!mSessionOpen)
624 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500625 if (!hasStorage())
626 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800627 if (mRequest.getParameterCount() < 1)
628 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500629 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400630
631 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400632 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400633 if (handles) {
634 mData.putAUInt32(handles);
635 delete handles;
636 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400637 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400638 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400639 return MTP_RESPONSE_OK;
640}
641
642MtpResponseCode MtpServer::doSetObjectReferences() {
643 if (!mSessionOpen)
644 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500645 if (!hasStorage())
646 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800647 if (mRequest.getParameterCount() < 1)
648 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400649 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500650
Mike Lockwood438344f2010-08-03 15:30:09 -0400651 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800652 if (!references)
653 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400654 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
655 delete references;
656 return result;
657}
658
Mike Lockwood16864ba2010-05-11 17:16:59 -0400659MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500660 if (!hasStorage())
661 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800662 if (mRequest.getParameterCount() < 2)
663 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400664 MtpObjectHandle handle = mRequest.getParameter(1);
665 MtpObjectProperty property = mRequest.getParameter(2);
James Weiba389222018-09-27 19:22:55 +0800666 ALOGV("GetObjectPropValue %d %s (0x%04X)\n", handle,
667 MtpDebug::getObjectPropCodeName(property), property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400668
Mike Lockwood8277cec2010-08-10 15:20:35 -0400669 return mDatabase->getObjectPropertyValue(handle, property, mData);
670}
671
672MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500673 if (!hasStorage())
674 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800675 if (mRequest.getParameterCount() < 2)
676 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400677 MtpObjectHandle handle = mRequest.getParameter(1);
678 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400680 MtpDebug::getObjectPropCodeName(property));
681
682 return mDatabase->setObjectPropertyValue(handle, property, mData);
683}
684
685MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800686 if (mRequest.getParameterCount() < 1)
687 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400688 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100689 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400690 MtpDebug::getDevicePropCodeName(property));
691
692 return mDatabase->getDevicePropertyValue(property, mData);
693}
694
695MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800696 if (mRequest.getParameterCount() < 1)
697 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400698 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400700 MtpDebug::getDevicePropCodeName(property));
701
702 return mDatabase->setDevicePropertyValue(property, mData);
703}
704
705MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800706 if (mRequest.getParameterCount() < 1)
707 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400708 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400710 MtpDebug::getDevicePropCodeName(property));
711
712 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400713}
714
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400715MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500716 if (!hasStorage())
717 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800718 if (mRequest.getParameterCount() < 5)
719 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400720
721 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500722 // use uint32_t so we can support 0xFFFFFFFF
723 uint32_t format = mRequest.getParameter(2);
724 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400725 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500726 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100727 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400728 handle, MtpDebug::getFormatCodeName(format),
729 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
730
731 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
732}
733
Mike Lockwood16864ba2010-05-11 17:16:59 -0400734MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500735 if (!hasStorage())
736 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800737 if (mRequest.getParameterCount() < 1)
738 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400739 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700740 MtpObjectInfo info(handle);
741 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
742 if (result == MTP_RESPONSE_OK) {
743 char date[20];
744
745 mData.putUInt32(info.mStorageID);
746 mData.putUInt16(info.mFormat);
747 mData.putUInt16(info.mProtectionStatus);
748
749 // if object is being edited the database size may be out of date
750 uint32_t size = info.mCompressedSize;
751 ObjectEdit* edit = getEditObject(handle);
752 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700753 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700754 mData.putUInt32(size);
755
756 mData.putUInt16(info.mThumbFormat);
757 mData.putUInt32(info.mThumbCompressedSize);
758 mData.putUInt32(info.mThumbPixWidth);
759 mData.putUInt32(info.mThumbPixHeight);
760 mData.putUInt32(info.mImagePixWidth);
761 mData.putUInt32(info.mImagePixHeight);
762 mData.putUInt32(info.mImagePixDepth);
763 mData.putUInt32(info.mParent);
764 mData.putUInt16(info.mAssociationType);
765 mData.putUInt32(info.mAssociationDesc);
766 mData.putUInt32(info.mSequenceNumber);
767 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700768 formatDateTime(info.mDateCreated, date, sizeof(date));
769 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700770 formatDateTime(info.mDateModified, date, sizeof(date));
771 mData.putString(date); // date modified
772 mData.putEmptyString(); // keywords
773 }
774 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400775}
776
777MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500778 if (!hasStorage())
779 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800780 if (mRequest.getParameterCount() < 1)
781 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400782 MtpObjectHandle handle = mRequest.getParameter(1);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700783 MtpStringBuffer pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400784 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800785 MtpObjectFormat format;
786 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400787 if (result != MTP_RESPONSE_OK)
788 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400789
Jerry Zhang487be612016-10-24 12:10:41 -0700790 auto start = std::chrono::steady_clock::now();
791
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400792 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400793 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400794 mfr.fd = open(filePath, O_RDONLY);
795 if (mfr.fd < 0) {
796 return MTP_RESPONSE_GENERAL_ERROR;
797 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400798 mfr.offset = 0;
799 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400800 mfr.command = mRequest.getOperationCode();
801 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400802
803 // then transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800804 int ret = mHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800805 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700806 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800807 if (errno == ECANCELED) {
808 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
809 } else {
810 result = MTP_RESPONSE_GENERAL_ERROR;
811 }
812 } else {
813 result = MTP_RESPONSE_OK;
814 }
815
Jerry Zhang487be612016-10-24 12:10:41 -0700816 auto end = std::chrono::steady_clock::now();
817 std::chrono::duration<double> diff = end - start;
818 struct stat sstat;
819 fstat(mfr.fd, &sstat);
820 uint64_t finalsize = sstat.st_size;
821 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
822 diff.count(), finalsize, ((double) finalsize) / diff.count());
Jerry Zhangdc148de2018-05-14 12:07:16 -0700823 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800824 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400825}
826
Mike Lockwood64000782011-04-24 18:40:17 -0700827MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800828 if (mRequest.getParameterCount() < 1)
829 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700830 MtpObjectHandle handle = mRequest.getParameter(1);
831 size_t thumbSize;
832 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
833 if (thumb) {
834 // send data
835 mData.setOperationCode(mRequest.getOperationCode());
836 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang63dac452017-12-06 15:19:36 -0800837 mData.writeData(mHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700838 free(thumb);
839 return MTP_RESPONSE_OK;
840 } else {
841 return MTP_RESPONSE_GENERAL_ERROR;
842 }
843}
844
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700845MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500846 if (!hasStorage())
847 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500848 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700849 uint64_t offset;
850 uint32_t length;
851 offset = mRequest.getParameter(2);
852 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800853 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
854 if (mRequest.getParameterCount() < 4)
855 return MTP_RESPONSE_INVALID_PARAMETER;
856
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700857 // android extension with 64 bit offset
858 uint64_t offset2 = mRequest.getParameter(3);
859 offset = offset | (offset2 << 32);
860 length = mRequest.getParameter(4);
861 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800862 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
863 if (mRequest.getParameterCount() < 3)
864 return MTP_RESPONSE_INVALID_PARAMETER;
865
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700866 // standard GetPartialObject
867 length = mRequest.getParameter(3);
868 }
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700869 MtpStringBuffer pathBuf;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500870 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800871 MtpObjectFormat format;
872 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500873 if (result != MTP_RESPONSE_OK)
874 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700875 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500876 length = fileLength - offset;
877
878 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700879 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500880 mtp_file_range mfr;
881 mfr.fd = open(filePath, O_RDONLY);
882 if (mfr.fd < 0) {
883 return MTP_RESPONSE_GENERAL_ERROR;
884 }
885 mfr.offset = offset;
886 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400887 mfr.command = mRequest.getOperationCode();
888 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500889 mResponse.setParameter(1, length);
890
Mike Lockwoodef441d92011-07-14 21:00:02 -0400891 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800892 int ret = mHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100893 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800894 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500895 if (ret < 0) {
896 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800897 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500898 else
tao.pei07a9e542015-07-17 17:18:41 +0800899 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500900 }
Jerry Zhangdc148de2018-05-14 12:07:16 -0700901 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800902 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500903}
904
Mike Lockwood16864ba2010-05-11 17:16:59 -0400905MtpResponseCode MtpServer::doSendObjectInfo() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700906 MtpStringBuffer path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800907 uint16_t temp16;
908 uint32_t temp32;
909
910 if (mRequest.getParameterCount() < 2)
911 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400912 MtpStorageID storageID = mRequest.getParameter(1);
913 MtpStorage* storage = getStorage(storageID);
914 MtpObjectHandle parent = mRequest.getParameter(2);
915 if (!storage)
916 return MTP_RESPONSE_INVALID_STORAGE_ID;
917
918 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400919 if (parent == MTP_PARENT_ROOT) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700920 path.set(storage->getPath());
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400921 parent = 0;
922 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800923 int64_t length;
924 MtpObjectFormat format;
925 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400926 if (result != MTP_RESPONSE_OK)
927 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800928 if (format != MTP_FORMAT_ASSOCIATION)
929 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400930 }
931
932 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800933 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
934 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
935 MtpObjectFormat format = temp16;
936 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
937 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
938 mSendObjectFileSize = temp32;
939 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
940 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
941 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
942 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
943 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
944 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
945 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
946 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
947 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800948 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800949 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400950 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800951 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700952 if (name.isEmpty()) {
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700953 ALOGE("empty name");
954 return MTP_RESPONSE_INVALID_PARAMETER;
955 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800956 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
957 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400958 // keywords follow
959
nolyn.luef9bef52018-07-05 16:24:13 +0800960 int type = storage->getType();
961 if (type == MTP_STORAGE_REMOVABLE_RAM) {
962 std::string str = android::base::Trim((const char*)name);
963 name.set(str.c_str());
964 }
James Weiba389222018-09-27 19:22:55 +0800965 ALOGV("name: %s format: 0x%04X (%s)\n", (const char*)name, format,
966 MtpDebug::getFormatCodeName(format));
Mike Lockwoodfceef462010-05-14 15:35:17 -0400967 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400968 if (!parseDateTime(modified, modifiedTime))
969 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400970
James Weie783e4b2019-05-28 17:18:21 +0800971 if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0) ||
James Wei0931b102020-02-12 23:20:06 +0800972 (strchr(name, '/') != NULL)) {
James Weie783e4b2019-05-28 17:18:21 +0800973 char errMsg[80];
974
James Wei68ccf5c2019-06-25 18:58:37 +0800975 snprintf(errMsg, sizeof(errMsg), "Invalid name: %s", (const char *) name);
James Weie783e4b2019-05-28 17:18:21 +0800976 ALOGE("%s (b/130656917)", errMsg);
977 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "130656917", -1, errMsg,
978 strlen(errMsg));
979
980 return MTP_RESPONSE_INVALID_PARAMETER;
981 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400982 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700983 path.append("/");
James Wei0931b102020-02-12 23:20:06 +0800984 path.append(name);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400985
Mike Lockwood20c3be02010-12-12 12:17:43 -0800986 // check space first
987 if (mSendObjectFileSize > storage->getFreeSpace())
988 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400989 uint64_t maxFileSize = storage->getMaxFileSize();
990 // check storage max file size
991 if (maxFileSize != 0) {
992 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
993 // is >= 0xFFFFFFFF
994 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
995 return MTP_RESPONSE_OBJECT_TOO_LARGE;
996 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800997
James Weiba389222018-09-27 19:22:55 +0800998 ALOGV("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700999 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, format,
1000 parent, storageID);
James Weiba389222018-09-27 19:22:55 +08001001 ALOGD("handle: %d, parent: %d, storageID: %08X", handle, parent, storageID);
Mike Lockwoodfceef462010-05-14 15:35:17 -04001002 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -04001003 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -04001004 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001005
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001006 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -07001007 int ret = makeFolder((const char *)path);
1008 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -04001009 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -08001010
1011 // SendObject does not get sent for directories, so call endSendObject here instead
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001012 mDatabase->endSendObject(handle, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001013 }
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001014 mSendObjectFilePath = path;
1015 // save the handle for the SendObject call, which should follow
1016 mSendObjectHandle = handle;
1017 mSendObjectFormat = format;
1018 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001019
1020 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001021 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001022 mResponse.setParameter(3, handle);
1023
1024 return MTP_RESPONSE_OK;
1025}
1026
Jerry Zhang708b3e02017-09-26 17:53:39 -07001027MtpResponseCode MtpServer::doMoveObject() {
1028 if (!hasStorage())
1029 return MTP_RESPONSE_GENERAL_ERROR;
1030 if (mRequest.getParameterCount() < 3)
1031 return MTP_RESPONSE_INVALID_PARAMETER;
1032 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1033 MtpStorageID storageID = mRequest.getParameter(2);
1034 MtpStorage* storage = getStorage(storageID);
1035 MtpObjectHandle parent = mRequest.getParameter(3);
1036 if (!storage)
1037 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001038 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001039 MtpResponseCode result;
1040
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001041 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001042 int64_t fileLength;
1043 MtpObjectFormat format;
1044 MtpObjectInfo info(objectHandle);
1045 result = mDatabase->getObjectInfo(objectHandle, info);
1046 if (result != MTP_RESPONSE_OK)
1047 return result;
1048 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1049 if (result != MTP_RESPONSE_OK)
1050 return result;
1051
1052 // special case the root
1053 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001054 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001055 } else {
1056 int64_t parentLength;
1057 MtpObjectFormat parentFormat;
1058 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1059 if (result != MTP_RESPONSE_OK)
1060 return result;
1061 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1062 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1063 }
1064
1065 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001066 path.append("/");
1067 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001068
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001069 result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
1070 if (result != MTP_RESPONSE_OK)
1071 return result;
1072
Jerry Zhang708b3e02017-09-26 17:53:39 -07001073 if (info.mStorageID == storageID) {
1074 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001075 if (renameTo(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001076 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001077 result = MTP_RESPONSE_GENERAL_ERROR;
1078 }
1079 } else {
1080 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001081 if (format == MTP_FORMAT_ASSOCIATION) {
1082 int ret = makeFolder((const char *)path);
1083 ret += copyRecursive(fromPath, path);
1084 if (ret) {
1085 result = MTP_RESPONSE_GENERAL_ERROR;
1086 } else {
1087 deletePath(fromPath);
1088 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001089 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001090 if (copyFile(fromPath, path)) {
1091 result = MTP_RESPONSE_GENERAL_ERROR;
1092 } else {
1093 deletePath(fromPath);
1094 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001095 }
1096 }
1097
1098 // If the move failed, undo the database change
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001099 mDatabase->endMoveObject(info.mParent, parent, info.mStorageID, storageID, objectHandle,
1100 result == MTP_RESPONSE_OK);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001101
1102 return result;
1103}
1104
1105MtpResponseCode MtpServer::doCopyObject() {
1106 if (!hasStorage())
1107 return MTP_RESPONSE_GENERAL_ERROR;
1108 MtpResponseCode result = MTP_RESPONSE_OK;
1109 if (mRequest.getParameterCount() < 3)
1110 return MTP_RESPONSE_INVALID_PARAMETER;
1111 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1112 MtpStorageID storageID = mRequest.getParameter(2);
1113 MtpStorage* storage = getStorage(storageID);
1114 MtpObjectHandle parent = mRequest.getParameter(3);
1115 if (!storage)
1116 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001117 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001118
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001119 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001120 int64_t fileLength;
1121 MtpObjectFormat format;
1122 MtpObjectInfo info(objectHandle);
1123 result = mDatabase->getObjectInfo(objectHandle, info);
1124 if (result != MTP_RESPONSE_OK)
1125 return result;
1126 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1127 if (result != MTP_RESPONSE_OK)
1128 return result;
1129
1130 // special case the root
1131 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001132 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001133 } else {
1134 int64_t parentLength;
1135 MtpObjectFormat parentFormat;
1136 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1137 if (result != MTP_RESPONSE_OK)
1138 return result;
1139 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1140 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1141 }
1142
1143 // check space first
1144 if ((uint64_t) fileLength > storage->getFreeSpace())
1145 return MTP_RESPONSE_STORAGE_FULL;
1146
1147 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001148 path.append("/");
1149 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001150
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001151 MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001152 if (handle == kInvalidObjectHandle) {
1153 return MTP_RESPONSE_GENERAL_ERROR;
1154 }
1155
1156 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001157 if (format == MTP_FORMAT_ASSOCIATION) {
1158 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001159 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001160 if (ret) {
1161 result = MTP_RESPONSE_GENERAL_ERROR;
1162 }
1163 } else {
1164 if (copyFile(fromPath, path)) {
1165 result = MTP_RESPONSE_GENERAL_ERROR;
1166 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001167 }
1168
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001169 mDatabase->endCopyObject(handle, result);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001170 mResponse.setParameter(1, handle);
1171 return result;
1172}
1173
Mike Lockwood16864ba2010-05-11 17:16:59 -04001174MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001175 if (!hasStorage())
1176 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001177 MtpResponseCode result = MTP_RESPONSE_OK;
1178 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001179 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001180 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001181 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001182
Jerry Zhang487be612016-10-24 12:10:41 -07001183 auto start = std::chrono::steady_clock::now();
1184
Mike Lockwood16864ba2010-05-11 17:16:59 -04001185 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001186 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001187 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1188 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001189 }
1190
Mike Lockwoodef441d92011-07-14 21:00:02 -04001191 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001192 ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001193 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1194 result = MTP_RESPONSE_GENERAL_ERROR;
1195 goto done;
1196 }
1197 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001198
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001199 if (mSendObjectFormat == MTP_FORMAT_ASSOCIATION) {
1200 if (initialData != 0)
1201 ALOGE("Expected folder size to be 0!");
1202 mSendObjectHandle = kInvalidObjectHandle;
1203 mSendObjectFormat = 0;
1204 mSendObjectModifiedTime = 0;
1205 return result;
1206 }
1207
Mike Lockwood16864ba2010-05-11 17:16:59 -04001208 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001209 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001210 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001211 result = MTP_RESPONSE_GENERAL_ERROR;
1212 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001213 }
Jerry Zhange242f122017-10-16 14:54:08 -07001214 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001215 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001216 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001217 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001218 umask(mask);
1219
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001220 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001221 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001222 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001223
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001224 if (ret < 0) {
1225 ALOGE("failed to write initial data");
1226 result = MTP_RESPONSE_GENERAL_ERROR;
1227 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001228 mfr.offset = initialData;
1229 if (mSendObjectFileSize == 0xFFFFFFFF) {
1230 // tell driver to read until it receives a short packet
1231 mfr.length = 0xFFFFFFFF;
1232 } else {
1233 mfr.length = mSendObjectFileSize - initialData;
1234 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001235
Jerry Zhang54107562017-05-15 11:54:19 -07001236 mfr.command = 0;
1237 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001238
Jerry Zhang54107562017-05-15 11:54:19 -07001239 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001240 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001241 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1242 if ((ret < 0) && (errno == ECANCELED)) {
1243 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001244 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001245 }
caozhiyuan854cb172017-04-26 16:52:30 +08001246
1247 if (mSendObjectModifiedTime) {
1248 struct timespec newTime[2];
1249 newTime[0].tv_nsec = UTIME_NOW;
1250 newTime[1].tv_sec = mSendObjectModifiedTime;
1251 newTime[1].tv_nsec = 0;
1252 if (futimens(mfr.fd, newTime) < 0) {
1253 ALOGW("changing modified time failed, %s", strerror(errno));
1254 }
1255 }
1256
Jerry Zhang487be612016-10-24 12:10:41 -07001257 fstat(mfr.fd, &sstat);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001258 closeObjFd(mfr.fd, mSendObjectFilePath);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001259
Mike Lockwood916076c2010-06-04 09:49:21 -04001260 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001261 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001262 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001263 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001264 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001265 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001266 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001267 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001268
1269done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001270 // reset so we don't attempt to send the data back
1271 mData.reset();
1272
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001273 mDatabase->endSendObject(mSendObjectHandle, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001274 mSendObjectHandle = kInvalidObjectHandle;
1275 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001276 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001277
1278 auto end = std::chrono::steady_clock::now();
1279 std::chrono::duration<double> diff = end - start;
1280 uint64_t finalsize = sstat.st_size;
1281 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1282 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001283 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001284}
1285
1286MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001287 if (!hasStorage())
1288 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001289 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001290 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001291 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001292 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001293 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1294 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001295
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001296 MtpStringBuffer filePath;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001297 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001298 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001299 if (result != MTP_RESPONSE_OK)
1300 return result;
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001301
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001302 // Don't delete the actual files unless the database deletion is allowed
1303 result = mDatabase->beginDeleteObject(handle);
1304 if (result != MTP_RESPONSE_OK)
1305 return result;
1306
1307 bool success = deletePath((const char *)filePath);
1308
1309 mDatabase->endDeleteObject(handle, success);
1310 return success ? result : MTP_RESPONSE_PARTIAL_DELETION;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001311}
1312
1313MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001314 if (mRequest.getParameterCount() < 2)
1315 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001316 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001317 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001318 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001319 MtpDebug::getFormatCodeName(format));
1320 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001321 if (!property)
1322 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001323 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001324 delete property;
1325 return MTP_RESPONSE_OK;
1326}
1327
1328MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001329 if (mRequest.getParameterCount() < 1)
1330 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001331 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001332 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001333 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1334 if (!property)
1335 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1336 property->write(mData);
1337 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001338 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001339}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001340
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001341MtpResponseCode MtpServer::doSendPartialObject() {
1342 if (!hasStorage())
1343 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001344 if (mRequest.getParameterCount() < 4)
1345 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001346 MtpObjectHandle handle = mRequest.getParameter(1);
1347 uint64_t offset = mRequest.getParameter(2);
1348 uint64_t offset2 = mRequest.getParameter(3);
1349 offset = offset | (offset2 << 32);
1350 uint32_t length = mRequest.getParameter(4);
1351
1352 ObjectEdit* edit = getEditObject(handle);
1353 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001354 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001355 return MTP_RESPONSE_GENERAL_ERROR;
1356 }
1357
1358 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001359 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001360 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1361 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001362 return MTP_RESPONSE_GENERAL_ERROR;
1363 }
1364
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001365 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001366 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001367
Mike Lockwoodef441d92011-07-14 21:00:02 -04001368 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001369 int ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001370 if (ret < MTP_CONTAINER_HEADER_SIZE)
1371 return MTP_RESPONSE_GENERAL_ERROR;
1372 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1373
1374 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001375 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001376 offset += initialData;
1377 length -= initialData;
1378 }
1379
tao.pei07a9e542015-07-17 17:18:41 +08001380 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001381 if (ret < 0) {
1382 ALOGE("failed to write initial data");
1383 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001384 mtp_file_range mfr;
1385 mfr.fd = edit->mFD;
1386 mfr.offset = offset;
1387 mfr.length = length;
1388 mfr.command = 0;
1389 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001390
Jerry Zhang54107562017-05-15 11:54:19 -07001391 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001392 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001393 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1394 if ((ret < 0) && (errno == ECANCELED)) {
1395 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001396 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001397 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001398 if (ret < 0) {
1399 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001400 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001401 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1402 else
1403 return MTP_RESPONSE_GENERAL_ERROR;
1404 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001405
1406 // reset so we don't attempt to send this back
1407 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001408 mResponse.setParameter(1, length);
1409 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001410 if (end > edit->mSize) {
1411 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001412 }
1413 return MTP_RESPONSE_OK;
1414}
1415
1416MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001417 if (mRequest.getParameterCount() < 3)
1418 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001419 MtpObjectHandle handle = mRequest.getParameter(1);
1420 ObjectEdit* edit = getEditObject(handle);
1421 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001422 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001423 return MTP_RESPONSE_GENERAL_ERROR;
1424 }
1425
1426 uint64_t offset = mRequest.getParameter(2);
1427 uint64_t offset2 = mRequest.getParameter(3);
1428 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001429 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001430 return MTP_RESPONSE_GENERAL_ERROR;
1431 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001432 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001433 return MTP_RESPONSE_OK;
1434 }
1435}
1436
1437MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001438 if (mRequest.getParameterCount() < 1)
1439 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001440 MtpObjectHandle handle = mRequest.getParameter(1);
1441 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001442 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001443 return MTP_RESPONSE_GENERAL_ERROR;
1444 }
1445
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001446 MtpStringBuffer path;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001447 int64_t fileLength;
1448 MtpObjectFormat format;
1449 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1450 if (result != MTP_RESPONSE_OK)
1451 return result;
1452
1453 int fd = open((const char *)path, O_RDWR | O_EXCL);
1454 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001455 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001456 return MTP_RESPONSE_GENERAL_ERROR;
1457 }
1458
1459 addEditObject(handle, path, fileLength, format, fd);
1460 return MTP_RESPONSE_OK;
1461}
1462
1463MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001464 if (mRequest.getParameterCount() < 1)
1465 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001466 MtpObjectHandle handle = mRequest.getParameter(1);
1467 ObjectEdit* edit = getEditObject(handle);
1468 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001469 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001470 return MTP_RESPONSE_GENERAL_ERROR;
1471 }
1472
1473 commitEdit(edit);
1474 removeEditObject(handle);
1475 return MTP_RESPONSE_OK;
1476}
1477
Mike Lockwood7850ef92010-05-14 10:10:36 -04001478} // namespace android