blob: 80fe51abceff41bc0216093104fa914103f43356 [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() {
Ashish Kumar Guptae34b0da2024-05-08 12:28:19 +0000135 if (mHandle) {
136 delete mHandle;
137 mHandle = NULL;
138 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400139}
140
Mike Lockwooda8494402011-02-18 09:07:14 -0500141void MtpServer::addStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700142 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500143
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700144 mStorages.push_back(storage);
Mike Lockwooda8494402011-02-18 09:07:14 -0500145 sendStoreAdded(storage->getStorageID());
146}
147
148void MtpServer::removeStorage(MtpStorage* storage) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700149 std::lock_guard<std::mutex> lg(mMutex);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700150 auto iter = std::find(mStorages.begin(), mStorages.end(), storage);
151 if (iter != mStorages.end()) {
152 sendStoreRemoved(storage->getStorageID());
153 mStorages.erase(iter);
Mike Lockwooda8494402011-02-18 09:07:14 -0500154 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400155}
156
157MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800158 if (id == 0)
159 return mStorages[0];
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700160 for (MtpStorage *storage : mStorages) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400161 if (storage->getStorageID() == id)
162 return storage;
163 }
Jerry Zhang487be612016-10-24 12:10:41 -0700164 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400165}
166
Mike Lockwooda8494402011-02-18 09:07:14 -0500167bool MtpServer::hasStorage(MtpStorageID id) {
168 if (id == 0 || id == 0xFFFFFFFF)
169 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700170 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500171}
172
Mike Lockwood16864ba2010-05-11 17:16:59 -0400173void MtpServer::run() {
Jerry Zhang63dac452017-12-06 15:19:36 -0800174 if (mHandle->start(mPtp)) {
Jerry Zhang487be612016-10-24 12:10:41 -0700175 ALOGE("Failed to start usb driver!");
Jerry Zhang63dac452017-12-06 15:19:36 -0800176 mHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700177 return;
178 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400179
180 while (1) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800181 int ret = mRequest.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400182 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700183 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400184 if (errno == ECANCELED) {
185 // return to top of loop and wait for next command
186 continue;
187 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400188 break;
189 }
190 MtpOperationCode operation = mRequest.getOperationCode();
191 MtpTransactionID transaction = mRequest.getTransactionID();
192
Steve Block3856b092011-10-20 11:56:00 +0100193 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400194 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400195 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400196 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
197 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
198 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400199 if (dataIn) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800200 int ret = mData.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400201 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000202 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400203 if (errno == ECANCELED) {
204 // return to top of loop and wait for next command
205 continue;
206 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400207 break;
208 }
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400210 } else {
211 mData.reset();
212 }
213
Mike Lockwood916076c2010-06-04 09:49:21 -0400214 if (handleRequest()) {
215 if (!dataIn && mData.hasData()) {
216 mData.setOperationCode(operation);
217 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("sending data:");
Jerry Zhang63dac452017-12-06 15:19:36 -0800219 ret = mData.write(mHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400220 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000221 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400222 if (errno == ECANCELED) {
223 // return to top of loop and wait for next command
224 continue;
225 }
226 break;
227 }
228 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400229
Mike Lockwood916076c2010-06-04 09:49:21 -0400230 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100231 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang63dac452017-12-06 15:19:36 -0800232 ret = mResponse.write(mHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800233 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400234 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000235 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800236 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400237 // return to top of loop and wait for next command
238 continue;
239 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400240 break;
241 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400242 } else {
Steve Block3856b092011-10-20 11:56:00 +0100243 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400244 }
245 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400246
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700247 // commit any open edits
248 int count = mObjectEditList.size();
249 for (int i = 0; i < count; i++) {
250 ObjectEdit* edit = mObjectEditList[i];
251 commitEdit(edit);
252 delete edit;
253 }
254 mObjectEditList.clear();
255
Jerry Zhang63dac452017-12-06 15:19:36 -0800256 mHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400257}
258
Mike Lockwood873871f2010-07-12 18:54:16 -0400259void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100260 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500261 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400262}
263
264void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500266 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
267}
268
James55138432018-07-02 18:07:30 +0800269void MtpServer::sendObjectInfoChanged(MtpObjectHandle handle) {
270 ALOGV("sendObjectInfoChanged %d\n", handle);
271 sendEvent(MTP_EVENT_OBJECT_INFO_CHANGED, handle);
272}
273
Mike Lockwooda8494402011-02-18 09:07:14 -0500274void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500276 sendEvent(MTP_EVENT_STORE_ADDED, id);
277}
278
279void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100280 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500281 sendEvent(MTP_EVENT_STORE_REMOVED, id);
282}
283
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800284void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
285 ALOGV("sendDevicePropertyChanged %d\n", property);
286 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
287}
288
Mike Lockwooda8494402011-02-18 09:07:14 -0500289void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400290 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500291 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400292 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500293 mEvent.setParameter(1, param1);
Jerry Zhang63dac452017-12-06 15:19:36 -0800294 if (mEvent.write(mHandle))
Jerry Zhang487be612016-10-24 12:10:41 -0700295 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400296 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400297}
298
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700299void MtpServer::addEditObject(MtpObjectHandle handle, MtpStringBuffer& path,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700300 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700301 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700302 mObjectEditList.push_back(edit);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700303}
304
305MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
306 int count = mObjectEditList.size();
307 for (int i = 0; i < count; i++) {
308 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700309 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700310 }
Jerry Zhang487be612016-10-24 12:10:41 -0700311 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700312}
313
314void MtpServer::removeEditObject(MtpObjectHandle handle) {
315 int count = mObjectEditList.size();
316 for (int i = 0; i < count; i++) {
317 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700318 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700319 delete edit;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700320 mObjectEditList.erase(mObjectEditList.begin() + i);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700321 return;
322 }
323 }
Steve Block29357bc2012-01-06 19:20:56 +0000324 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700325}
326
327void MtpServer::commitEdit(ObjectEdit* edit) {
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700328 mDatabase->rescanFile((const char *)edit->mPath, edit->mHandle, edit->mFormat);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700329}
330
331
Mike Lockwood916076c2010-06-04 09:49:21 -0400332bool MtpServer::handleRequest() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700333 std::lock_guard<std::mutex> lg(mMutex);
Mike Lockwooda8494402011-02-18 09:07:14 -0500334
Mike Lockwood16864ba2010-05-11 17:16:59 -0400335 MtpOperationCode operation = mRequest.getOperationCode();
336 MtpResponseCode response;
337
338 mResponse.reset();
339
340 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400341 mSendObjectHandle = kInvalidObjectHandle;
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700342 mSendObjectFormat = 0;
343 mSendObjectModifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400344 }
345
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700346 int containertype = mRequest.getContainerType();
347 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
348 ALOGE("wrong container type %d", containertype);
349 return false;
350 }
351
352 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
353
Mike Lockwood16864ba2010-05-11 17:16:59 -0400354 switch (operation) {
355 case MTP_OPERATION_GET_DEVICE_INFO:
356 response = doGetDeviceInfo();
357 break;
358 case MTP_OPERATION_OPEN_SESSION:
359 response = doOpenSession();
360 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800361 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400362 case MTP_OPERATION_CLOSE_SESSION:
363 response = doCloseSession();
364 break;
365 case MTP_OPERATION_GET_STORAGE_IDS:
366 response = doGetStorageIDs();
367 break;
368 case MTP_OPERATION_GET_STORAGE_INFO:
369 response = doGetStorageInfo();
370 break;
371 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
372 response = doGetObjectPropsSupported();
373 break;
374 case MTP_OPERATION_GET_OBJECT_HANDLES:
375 response = doGetObjectHandles();
376 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400377 case MTP_OPERATION_GET_NUM_OBJECTS:
378 response = doGetNumObjects();
379 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400380 case MTP_OPERATION_GET_OBJECT_REFERENCES:
381 response = doGetObjectReferences();
382 break;
383 case MTP_OPERATION_SET_OBJECT_REFERENCES:
384 response = doSetObjectReferences();
385 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400386 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
387 response = doGetObjectPropValue();
388 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400389 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
390 response = doSetObjectPropValue();
391 break;
392 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
393 response = doGetDevicePropValue();
394 break;
395 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
396 response = doSetDevicePropValue();
397 break;
398 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
399 response = doResetDevicePropValue();
400 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400401 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
402 response = doGetObjectPropList();
403 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400404 case MTP_OPERATION_GET_OBJECT_INFO:
405 response = doGetObjectInfo();
406 break;
407 case MTP_OPERATION_GET_OBJECT:
408 response = doGetObject();
409 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700410 case MTP_OPERATION_GET_THUMB:
411 response = doGetThumb();
412 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500413 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700414 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
415 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500416 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400417 case MTP_OPERATION_SEND_OBJECT_INFO:
418 response = doSendObjectInfo();
419 break;
420 case MTP_OPERATION_SEND_OBJECT:
421 response = doSendObject();
422 break;
423 case MTP_OPERATION_DELETE_OBJECT:
424 response = doDeleteObject();
425 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700426 case MTP_OPERATION_COPY_OBJECT:
427 response = doCopyObject();
428 break;
429 case MTP_OPERATION_MOVE_OBJECT:
430 response = doMoveObject();
431 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400432 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400433 response = doGetObjectPropDesc();
434 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400435 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
436 response = doGetDevicePropDesc();
437 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700438 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
439 response = doSendPartialObject();
440 break;
441 case MTP_OPERATION_TRUNCATE_OBJECT:
442 response = doTruncateObject();
443 break;
444 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
445 response = doBeginEditObject();
446 break;
447 case MTP_OPERATION_END_EDIT_OBJECT:
448 response = doEndEditObject();
449 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400450 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700451 ALOGE("got unsupported command %s (%x)",
452 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400453 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
454 break;
455 }
456
James Weiba389222018-09-27 19:22:55 +0800457 if (response != MTP_RESPONSE_OK)
458 ALOGW("[MTP] got response 0x%X in command %s (%x)", response,
459 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood916076c2010-06-04 09:49:21 -0400460 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
461 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400462 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400463 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400464}
465
466MtpResponseCode MtpServer::doGetDeviceInfo() {
467 MtpStringBuffer string;
468
Mike Lockwood782aef12010-08-10 07:37:50 -0400469 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
470 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
471 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
472
Mike Lockwood16864ba2010-05-11 17:16:59 -0400473 // fill in device info
474 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400475 if (mPtp) {
476 mData.putUInt32(0);
477 } else {
478 // MTP Vendor Extension ID
479 mData.putUInt32(6);
480 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400481 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400482 if (mPtp) {
483 // no extensions
484 string.set("");
485 } else {
486 // MTP extensions
487 string.set("microsoft.com: 1.0; android.com: 1.0;");
488 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400489 mData.putString(string); // MTP Extensions
490 mData.putUInt16(0); //Functional Mode
491 mData.putAUInt16(kSupportedOperationCodes,
492 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400493 mData.putAUInt16(kSupportedEventCodes,
494 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400495 mData.putAUInt16(deviceProperties); // Device Properties Supported
496 mData.putAUInt16(captureFormats); // Capture Formats
497 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500498
Alex Klyubin792298f2016-12-21 11:20:22 -0800499 mData.putString(mDeviceInfoManufacturer); // Manufacturer
500 mData.putString(mDeviceInfoModel); // Model
501 mData.putString(mDeviceInfoDeviceVersion); // Device Version
502 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400503
Mike Lockwood782aef12010-08-10 07:37:50 -0400504 delete playbackFormats;
505 delete captureFormats;
506 delete deviceProperties;
507
Mike Lockwood16864ba2010-05-11 17:16:59 -0400508 return MTP_RESPONSE_OK;
509}
510
511MtpResponseCode MtpServer::doOpenSession() {
512 if (mSessionOpen) {
513 mResponse.setParameter(1, mSessionID);
514 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
515 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800516 if (mRequest.getParameterCount() < 1)
517 return MTP_RESPONSE_INVALID_PARAMETER;
518
Mike Lockwood16864ba2010-05-11 17:16:59 -0400519 mSessionID = mRequest.getParameter(1);
520 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400521
Mike Lockwood16864ba2010-05-11 17:16:59 -0400522 return MTP_RESPONSE_OK;
523}
524
525MtpResponseCode MtpServer::doCloseSession() {
526 if (!mSessionOpen)
527 return MTP_RESPONSE_SESSION_NOT_OPEN;
528 mSessionID = 0;
529 mSessionOpen = false;
530 return MTP_RESPONSE_OK;
531}
532
533MtpResponseCode MtpServer::doGetStorageIDs() {
534 if (!mSessionOpen)
535 return MTP_RESPONSE_SESSION_NOT_OPEN;
536
537 int count = mStorages.size();
538 mData.putUInt32(count);
539 for (int i = 0; i < count; i++)
540 mData.putUInt32(mStorages[i]->getStorageID());
541
542 return MTP_RESPONSE_OK;
543}
544
545MtpResponseCode MtpServer::doGetStorageInfo() {
546 MtpStringBuffer string;
547
548 if (!mSessionOpen)
549 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800550 if (mRequest.getParameterCount() < 1)
551 return MTP_RESPONSE_INVALID_PARAMETER;
552
Mike Lockwood16864ba2010-05-11 17:16:59 -0400553 MtpStorageID id = mRequest.getParameter(1);
554 MtpStorage* storage = getStorage(id);
555 if (!storage)
556 return MTP_RESPONSE_INVALID_STORAGE_ID;
557
558 mData.putUInt16(storage->getType());
559 mData.putUInt16(storage->getFileSystemType());
560 mData.putUInt16(storage->getAccessCapability());
561 mData.putUInt64(storage->getMaxCapacity());
562 mData.putUInt64(storage->getFreeSpace());
563 mData.putUInt32(1024*1024*1024); // Free Space in Objects
564 string.set(storage->getDescription());
565 mData.putString(string);
566 mData.putEmptyString(); // Volume Identifier
567
568 return MTP_RESPONSE_OK;
569}
570
571MtpResponseCode MtpServer::doGetObjectPropsSupported() {
572 if (!mSessionOpen)
573 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800574 if (mRequest.getParameterCount() < 1)
575 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400576 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800577 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400578 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400579 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400580 return MTP_RESPONSE_OK;
581}
582
583MtpResponseCode MtpServer::doGetObjectHandles() {
584 if (!mSessionOpen)
585 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800586 if (mRequest.getParameterCount() < 3)
587 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400588 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400589 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400590 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400591 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500592
593 if (!hasStorage(storageID))
594 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400595
596 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700597 if (handles == NULL)
598 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400599 mData.putAUInt32(handles);
600 delete handles;
601 return MTP_RESPONSE_OK;
602}
603
Mike Lockwood343af4e2010-08-02 10:52:20 -0400604MtpResponseCode MtpServer::doGetNumObjects() {
605 if (!mSessionOpen)
606 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800607 if (mRequest.getParameterCount() < 3)
608 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400609 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
610 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
611 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400612 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500613 if (!hasStorage(storageID))
614 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400615
616 int count = mDatabase->getNumObjects(storageID, format, parent);
617 if (count >= 0) {
618 mResponse.setParameter(1, count);
619 return MTP_RESPONSE_OK;
620 } else {
621 mResponse.setParameter(1, 0);
622 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
623 }
624}
625
Mike Lockwood438344f2010-08-03 15:30:09 -0400626MtpResponseCode MtpServer::doGetObjectReferences() {
627 if (!mSessionOpen)
628 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500629 if (!hasStorage())
630 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800631 if (mRequest.getParameterCount() < 1)
632 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500633 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400634
635 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400636 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400637 if (handles) {
638 mData.putAUInt32(handles);
639 delete handles;
640 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400641 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400642 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400643 return MTP_RESPONSE_OK;
644}
645
646MtpResponseCode MtpServer::doSetObjectReferences() {
647 if (!mSessionOpen)
648 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500649 if (!hasStorage())
650 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800651 if (mRequest.getParameterCount() < 1)
652 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400653 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500654
Mike Lockwood438344f2010-08-03 15:30:09 -0400655 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800656 if (!references)
657 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400658 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
659 delete references;
660 return result;
661}
662
Mike Lockwood16864ba2010-05-11 17:16:59 -0400663MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500664 if (!hasStorage())
665 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800666 if (mRequest.getParameterCount() < 2)
667 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400668 MtpObjectHandle handle = mRequest.getParameter(1);
669 MtpObjectProperty property = mRequest.getParameter(2);
James Weiba389222018-09-27 19:22:55 +0800670 ALOGV("GetObjectPropValue %d %s (0x%04X)\n", handle,
671 MtpDebug::getObjectPropCodeName(property), property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400672
Mike Lockwood8277cec2010-08-10 15:20:35 -0400673 return mDatabase->getObjectPropertyValue(handle, property, mData);
674}
675
676MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500677 if (!hasStorage())
678 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800679 if (mRequest.getParameterCount() < 2)
680 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400681 MtpObjectHandle handle = mRequest.getParameter(1);
682 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100683 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400684 MtpDebug::getObjectPropCodeName(property));
685
686 return mDatabase->setObjectPropertyValue(handle, property, mData);
687}
688
689MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800690 if (mRequest.getParameterCount() < 1)
691 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400692 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400694 MtpDebug::getDevicePropCodeName(property));
695
696 return mDatabase->getDevicePropertyValue(property, mData);
697}
698
699MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800700 if (mRequest.getParameterCount() < 1)
701 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400702 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400704 MtpDebug::getDevicePropCodeName(property));
705
706 return mDatabase->setDevicePropertyValue(property, mData);
707}
708
709MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800710 if (mRequest.getParameterCount() < 1)
711 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400712 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100713 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400714 MtpDebug::getDevicePropCodeName(property));
715
716 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400717}
718
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400719MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500720 if (!hasStorage())
721 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800722 if (mRequest.getParameterCount() < 5)
723 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400724
725 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500726 // use uint32_t so we can support 0xFFFFFFFF
727 uint32_t format = mRequest.getParameter(2);
728 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400729 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500730 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100731 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400732 handle, MtpDebug::getFormatCodeName(format),
733 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
734
735 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
736}
737
Mike Lockwood16864ba2010-05-11 17:16:59 -0400738MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500739 if (!hasStorage())
740 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800741 if (mRequest.getParameterCount() < 1)
742 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400743 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700744 MtpObjectInfo info(handle);
745 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
746 if (result == MTP_RESPONSE_OK) {
747 char date[20];
748
749 mData.putUInt32(info.mStorageID);
750 mData.putUInt16(info.mFormat);
751 mData.putUInt16(info.mProtectionStatus);
752
753 // if object is being edited the database size may be out of date
754 uint32_t size = info.mCompressedSize;
755 ObjectEdit* edit = getEditObject(handle);
756 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700757 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700758 mData.putUInt32(size);
759
760 mData.putUInt16(info.mThumbFormat);
761 mData.putUInt32(info.mThumbCompressedSize);
762 mData.putUInt32(info.mThumbPixWidth);
763 mData.putUInt32(info.mThumbPixHeight);
764 mData.putUInt32(info.mImagePixWidth);
765 mData.putUInt32(info.mImagePixHeight);
766 mData.putUInt32(info.mImagePixDepth);
767 mData.putUInt32(info.mParent);
768 mData.putUInt16(info.mAssociationType);
769 mData.putUInt32(info.mAssociationDesc);
770 mData.putUInt32(info.mSequenceNumber);
771 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700772 formatDateTime(info.mDateCreated, date, sizeof(date));
773 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700774 formatDateTime(info.mDateModified, date, sizeof(date));
775 mData.putString(date); // date modified
776 mData.putEmptyString(); // keywords
777 }
778 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400779}
780
781MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500782 if (!hasStorage())
783 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800784 if (mRequest.getParameterCount() < 1)
785 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400786 MtpObjectHandle handle = mRequest.getParameter(1);
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700787 MtpStringBuffer pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400788 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800789 MtpObjectFormat format;
790 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400791 if (result != MTP_RESPONSE_OK)
792 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400793
Jerry Zhang487be612016-10-24 12:10:41 -0700794 auto start = std::chrono::steady_clock::now();
795
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400796 const char* filePath = (const char *)pathBuf;
Zime3b5ba02020-11-13 20:09:06 +0000797 mtp_file_range mfr;
798 struct stat sstat;
799 uint64_t finalsize;
Manish Singhc0c6b512021-02-04 04:54:23 +0000800 bool transcode = android::base::GetBoolProperty("sys.fuse.transcode_mtp", false);
Zimc43e61a2021-06-18 10:52:54 +0100801 bool filePathAccess = true;
Manish Singhfd5500a2021-01-19 21:35:21 +0000802 ALOGD("Mtp transcode = %d", transcode);
Zimc43e61a2021-06-18 10:52:54 +0100803
804 // For performance reasons, only attempt a ContentResolver open when transcode is required.
805 // This is fine as long as we don't transcode by default on the device. If we suddenly
806 // transcode by default, we'll need to ensure that MTP doesn't transcode by default and we
807 // might need to make a binder call to avoid transcoding or come up with a better strategy.
808 if (transcode) {
809 mfr.fd = mDatabase->openFilePath(filePath, true);
Manish Singhfd5500a2021-01-19 21:35:21 +0000810 fstat(mfr.fd, &sstat);
811 finalsize = sstat.st_size;
812 fileLength = finalsize;
Zimc43e61a2021-06-18 10:52:54 +0100813 if (mfr.fd < 0) {
814 ALOGW("Mtp open via IMtpDatabase failed for %s. Falling back to the original",
815 filePath);
816 filePathAccess = true;
817 } else {
818 filePathAccess = false;
819 }
820 }
Zime3b5ba02020-11-13 20:09:06 +0000821
Zimc43e61a2021-06-18 10:52:54 +0100822 if (filePathAccess) {
Zime3b5ba02020-11-13 20:09:06 +0000823 mfr.fd = open(filePath, O_RDONLY);
824 if (mfr.fd < 0) {
825 return MTP_RESPONSE_GENERAL_ERROR;
826 }
827 fstat(mfr.fd, &sstat);
828 finalsize = sstat.st_size;
829 }
830
Mike Lockwood16864ba2010-05-11 17:16:59 -0400831 mfr.offset = 0;
832 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400833 mfr.command = mRequest.getOperationCode();
834 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400835
836 // then transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800837 int ret = mHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800838 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700839 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800840 if (errno == ECANCELED) {
841 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
842 } else {
843 result = MTP_RESPONSE_GENERAL_ERROR;
844 }
845 } else {
846 result = MTP_RESPONSE_OK;
847 }
848
Jerry Zhang487be612016-10-24 12:10:41 -0700849 auto end = std::chrono::steady_clock::now();
850 std::chrono::duration<double> diff = end - start;
Jerry Zhang487be612016-10-24 12:10:41 -0700851 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
852 diff.count(), finalsize, ((double) finalsize) / diff.count());
Jerry Zhangdc148de2018-05-14 12:07:16 -0700853 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800854 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400855}
856
Mike Lockwood64000782011-04-24 18:40:17 -0700857MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800858 if (mRequest.getParameterCount() < 1)
859 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700860 MtpObjectHandle handle = mRequest.getParameter(1);
861 size_t thumbSize;
862 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
863 if (thumb) {
864 // send data
865 mData.setOperationCode(mRequest.getOperationCode());
866 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang63dac452017-12-06 15:19:36 -0800867 mData.writeData(mHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700868 free(thumb);
869 return MTP_RESPONSE_OK;
870 } else {
871 return MTP_RESPONSE_GENERAL_ERROR;
872 }
873}
874
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700875MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500876 if (!hasStorage())
877 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500878 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700879 uint64_t offset;
880 uint32_t length;
881 offset = mRequest.getParameter(2);
882 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800883 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
884 if (mRequest.getParameterCount() < 4)
885 return MTP_RESPONSE_INVALID_PARAMETER;
886
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700887 // android extension with 64 bit offset
888 uint64_t offset2 = mRequest.getParameter(3);
889 offset = offset | (offset2 << 32);
890 length = mRequest.getParameter(4);
891 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800892 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
893 if (mRequest.getParameterCount() < 3)
894 return MTP_RESPONSE_INVALID_PARAMETER;
895
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700896 // standard GetPartialObject
897 length = mRequest.getParameter(3);
898 }
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700899 MtpStringBuffer pathBuf;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500900 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800901 MtpObjectFormat format;
902 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500903 if (result != MTP_RESPONSE_OK)
904 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700905 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500906 length = fileLength - offset;
907
908 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700909 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500910 mtp_file_range mfr;
911 mfr.fd = open(filePath, O_RDONLY);
912 if (mfr.fd < 0) {
913 return MTP_RESPONSE_GENERAL_ERROR;
914 }
915 mfr.offset = offset;
916 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400917 mfr.command = mRequest.getOperationCode();
918 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500919 mResponse.setParameter(1, length);
920
Mike Lockwoodef441d92011-07-14 21:00:02 -0400921 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800922 int ret = mHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800924 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500925 if (ret < 0) {
926 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800927 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500928 else
tao.pei07a9e542015-07-17 17:18:41 +0800929 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500930 }
Jerry Zhangdc148de2018-05-14 12:07:16 -0700931 closeObjFd(mfr.fd, filePath);
tao.pei07a9e542015-07-17 17:18:41 +0800932 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500933}
934
Mike Lockwood16864ba2010-05-11 17:16:59 -0400935MtpResponseCode MtpServer::doSendObjectInfo() {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700936 MtpStringBuffer path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800937 uint16_t temp16;
938 uint32_t temp32;
939
940 if (mRequest.getParameterCount() < 2)
941 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400942 MtpStorageID storageID = mRequest.getParameter(1);
943 MtpStorage* storage = getStorage(storageID);
944 MtpObjectHandle parent = mRequest.getParameter(2);
945 if (!storage)
946 return MTP_RESPONSE_INVALID_STORAGE_ID;
947
948 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400949 if (parent == MTP_PARENT_ROOT) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700950 path.set(storage->getPath());
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400951 parent = 0;
952 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800953 int64_t length;
954 MtpObjectFormat format;
955 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400956 if (result != MTP_RESPONSE_OK)
957 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800958 if (format != MTP_FORMAT_ASSOCIATION)
959 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400960 }
961
962 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800963 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
964 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
965 MtpObjectFormat format = temp16;
966 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
967 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
968 mSendObjectFileSize = temp32;
969 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
970 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
971 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
972 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
973 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
974 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
975 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
976 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
977 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800978 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800979 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400980 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800981 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Jerry Zhangbc1d4b42018-03-27 15:25:03 -0700982 if (name.isEmpty()) {
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700983 ALOGE("empty name");
984 return MTP_RESPONSE_INVALID_PARAMETER;
985 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800986 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
987 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400988 // keywords follow
989
nolyn.luef9bef52018-07-05 16:24:13 +0800990 int type = storage->getType();
991 if (type == MTP_STORAGE_REMOVABLE_RAM) {
T.J. Mercier4e8c5162022-07-23 18:05:08 +0000992 std::string str = android::base::Trim(name);
nolyn.luef9bef52018-07-05 16:24:13 +0800993 name.set(str.c_str());
994 }
James Weiba389222018-09-27 19:22:55 +0800995 ALOGV("name: %s format: 0x%04X (%s)\n", (const char*)name, format,
996 MtpDebug::getFormatCodeName(format));
Mike Lockwoodfceef462010-05-14 15:35:17 -0400997 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400998 if (!parseDateTime(modified, modifiedTime))
999 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001000
James Weie783e4b2019-05-28 17:18:21 +08001001 if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0) ||
James Wei0931b102020-02-12 23:20:06 +08001002 (strchr(name, '/') != NULL)) {
James Weie783e4b2019-05-28 17:18:21 +08001003 char errMsg[80];
1004
James Wei68ccf5c2019-06-25 18:58:37 +08001005 snprintf(errMsg, sizeof(errMsg), "Invalid name: %s", (const char *) name);
James Weie783e4b2019-05-28 17:18:21 +08001006 ALOGE("%s (b/130656917)", errMsg);
1007 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "130656917", -1, errMsg,
1008 strlen(errMsg));
1009
1010 return MTP_RESPONSE_INVALID_PARAMETER;
1011 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001012 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001013 path.append("/");
James Wei0931b102020-02-12 23:20:06 +08001014 path.append(name);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001015
Mike Lockwood20c3be02010-12-12 12:17:43 -08001016 // check space first
1017 if (mSendObjectFileSize > storage->getFreeSpace())
1018 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -04001019 uint64_t maxFileSize = storage->getMaxFileSize();
1020 // check storage max file size
1021 if (maxFileSize != 0) {
1022 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
1023 // is >= 0xFFFFFFFF
1024 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
1025 return MTP_RESPONSE_OBJECT_TOO_LARGE;
1026 }
Mike Lockwood20c3be02010-12-12 12:17:43 -08001027
James Weiba389222018-09-27 19:22:55 +08001028 ALOGV("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001029 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, format,
1030 parent, storageID);
James Weiba389222018-09-27 19:22:55 +08001031 ALOGD("handle: %d, parent: %d, storageID: %08X", handle, parent, storageID);
Mike Lockwoodfceef462010-05-14 15:35:17 -04001032 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -04001033 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -04001034 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001035
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001036 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -07001037 int ret = makeFolder((const char *)path);
1038 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -04001039 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -08001040
1041 // SendObject does not get sent for directories, so call endSendObject here instead
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001042 mDatabase->endSendObject(handle, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001043 }
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001044 mSendObjectFilePath = path;
1045 // save the handle for the SendObject call, which should follow
1046 mSendObjectHandle = handle;
1047 mSendObjectFormat = format;
1048 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001049
1050 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001051 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001052 mResponse.setParameter(3, handle);
1053
1054 return MTP_RESPONSE_OK;
1055}
1056
Jerry Zhang708b3e02017-09-26 17:53:39 -07001057MtpResponseCode MtpServer::doMoveObject() {
1058 if (!hasStorage())
1059 return MTP_RESPONSE_GENERAL_ERROR;
1060 if (mRequest.getParameterCount() < 3)
1061 return MTP_RESPONSE_INVALID_PARAMETER;
1062 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1063 MtpStorageID storageID = mRequest.getParameter(2);
1064 MtpStorage* storage = getStorage(storageID);
1065 MtpObjectHandle parent = mRequest.getParameter(3);
1066 if (!storage)
1067 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001068 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001069 MtpResponseCode result;
1070
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001071 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001072 int64_t fileLength;
1073 MtpObjectFormat format;
1074 MtpObjectInfo info(objectHandle);
1075 result = mDatabase->getObjectInfo(objectHandle, info);
1076 if (result != MTP_RESPONSE_OK)
1077 return result;
1078 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1079 if (result != MTP_RESPONSE_OK)
1080 return result;
1081
1082 // special case the root
1083 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001084 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001085 } else {
1086 int64_t parentLength;
1087 MtpObjectFormat parentFormat;
1088 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1089 if (result != MTP_RESPONSE_OK)
1090 return result;
1091 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1092 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1093 }
1094
1095 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001096 path.append("/");
1097 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001098
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001099 result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
1100 if (result != MTP_RESPONSE_OK)
1101 return result;
1102
Jerry Zhang708b3e02017-09-26 17:53:39 -07001103 if (info.mStorageID == storageID) {
1104 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001105 if (renameTo(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001106 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001107 result = MTP_RESPONSE_GENERAL_ERROR;
1108 }
1109 } else {
1110 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001111 if (format == MTP_FORMAT_ASSOCIATION) {
1112 int ret = makeFolder((const char *)path);
1113 ret += copyRecursive(fromPath, path);
1114 if (ret) {
1115 result = MTP_RESPONSE_GENERAL_ERROR;
1116 } else {
1117 deletePath(fromPath);
1118 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001119 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001120 if (copyFile(fromPath, path)) {
1121 result = MTP_RESPONSE_GENERAL_ERROR;
1122 } else {
1123 deletePath(fromPath);
1124 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001125 }
1126 }
1127
1128 // If the move failed, undo the database change
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001129 mDatabase->endMoveObject(info.mParent, parent, info.mStorageID, storageID, objectHandle,
1130 result == MTP_RESPONSE_OK);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001131
1132 return result;
1133}
1134
1135MtpResponseCode MtpServer::doCopyObject() {
1136 if (!hasStorage())
1137 return MTP_RESPONSE_GENERAL_ERROR;
1138 MtpResponseCode result = MTP_RESPONSE_OK;
1139 if (mRequest.getParameterCount() < 3)
1140 return MTP_RESPONSE_INVALID_PARAMETER;
1141 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1142 MtpStorageID storageID = mRequest.getParameter(2);
1143 MtpStorage* storage = getStorage(storageID);
1144 MtpObjectHandle parent = mRequest.getParameter(3);
1145 if (!storage)
1146 return MTP_RESPONSE_INVALID_STORAGE_ID;
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001147 MtpStringBuffer path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001148
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001149 MtpStringBuffer fromPath;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001150 int64_t fileLength;
1151 MtpObjectFormat format;
1152 MtpObjectInfo info(objectHandle);
1153 result = mDatabase->getObjectInfo(objectHandle, info);
1154 if (result != MTP_RESPONSE_OK)
1155 return result;
1156 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1157 if (result != MTP_RESPONSE_OK)
1158 return result;
1159
1160 // special case the root
1161 if (parent == 0) {
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001162 path.set(storage->getPath());
Jerry Zhang708b3e02017-09-26 17:53:39 -07001163 } else {
1164 int64_t parentLength;
1165 MtpObjectFormat parentFormat;
1166 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1167 if (result != MTP_RESPONSE_OK)
1168 return result;
1169 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1170 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1171 }
1172
1173 // check space first
1174 if ((uint64_t) fileLength > storage->getFreeSpace())
1175 return MTP_RESPONSE_STORAGE_FULL;
1176
1177 if (path[path.size() - 1] != '/')
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001178 path.append("/");
1179 path.append(info.mName);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001180
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001181 MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001182 if (handle == kInvalidObjectHandle) {
1183 return MTP_RESPONSE_GENERAL_ERROR;
1184 }
1185
1186 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001187 if (format == MTP_FORMAT_ASSOCIATION) {
1188 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001189 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001190 if (ret) {
1191 result = MTP_RESPONSE_GENERAL_ERROR;
1192 }
1193 } else {
1194 if (copyFile(fromPath, path)) {
1195 result = MTP_RESPONSE_GENERAL_ERROR;
1196 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001197 }
1198
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001199 mDatabase->endCopyObject(handle, result);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001200 mResponse.setParameter(1, handle);
1201 return result;
1202}
1203
Mike Lockwood16864ba2010-05-11 17:16:59 -04001204MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001205 if (!hasStorage())
1206 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001207 MtpResponseCode result = MTP_RESPONSE_OK;
1208 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001209 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001210 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001211 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001212
Jerry Zhang487be612016-10-24 12:10:41 -07001213 auto start = std::chrono::steady_clock::now();
1214
Mike Lockwood16864ba2010-05-11 17:16:59 -04001215 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001216 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001217 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1218 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001219 }
1220
Mike Lockwoodef441d92011-07-14 21:00:02 -04001221 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001222 ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001223 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1224 result = MTP_RESPONSE_GENERAL_ERROR;
1225 goto done;
1226 }
1227 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001228
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001229 if (mSendObjectFormat == MTP_FORMAT_ASSOCIATION) {
1230 if (initialData != 0)
1231 ALOGE("Expected folder size to be 0!");
1232 mSendObjectHandle = kInvalidObjectHandle;
1233 mSendObjectFormat = 0;
1234 mSendObjectModifiedTime = 0;
1235 return result;
1236 }
1237
Mike Lockwood16864ba2010-05-11 17:16:59 -04001238 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001239 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001240 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001241 result = MTP_RESPONSE_GENERAL_ERROR;
1242 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001243 }
Jerry Zhange242f122017-10-16 14:54:08 -07001244 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001245 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001246 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001247 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001248 umask(mask);
1249
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001250 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001251 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001252 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001253
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001254 if (ret < 0) {
1255 ALOGE("failed to write initial data");
1256 result = MTP_RESPONSE_GENERAL_ERROR;
1257 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001258 mfr.offset = initialData;
1259 if (mSendObjectFileSize == 0xFFFFFFFF) {
1260 // tell driver to read until it receives a short packet
1261 mfr.length = 0xFFFFFFFF;
1262 } else {
1263 mfr.length = mSendObjectFileSize - initialData;
1264 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001265
Jerry Zhang54107562017-05-15 11:54:19 -07001266 mfr.command = 0;
1267 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001268
Jerry Zhang54107562017-05-15 11:54:19 -07001269 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001270 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001271 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1272 if ((ret < 0) && (errno == ECANCELED)) {
1273 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001274 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001275 }
caozhiyuan854cb172017-04-26 16:52:30 +08001276
1277 if (mSendObjectModifiedTime) {
1278 struct timespec newTime[2];
1279 newTime[0].tv_nsec = UTIME_NOW;
1280 newTime[1].tv_sec = mSendObjectModifiedTime;
1281 newTime[1].tv_nsec = 0;
1282 if (futimens(mfr.fd, newTime) < 0) {
1283 ALOGW("changing modified time failed, %s", strerror(errno));
1284 }
1285 }
1286
Jerry Zhang487be612016-10-24 12:10:41 -07001287 fstat(mfr.fd, &sstat);
Jerry Zhangdc148de2018-05-14 12:07:16 -07001288 closeObjFd(mfr.fd, mSendObjectFilePath);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001289
Mike Lockwood916076c2010-06-04 09:49:21 -04001290 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001291 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001292 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001293 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001294 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001295 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001296 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001297 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001298
1299done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001300 // reset so we don't attempt to send the data back
1301 mData.reset();
1302
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001303 mDatabase->endSendObject(mSendObjectHandle, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001304 mSendObjectHandle = kInvalidObjectHandle;
1305 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001306 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001307
1308 auto end = std::chrono::steady_clock::now();
1309 std::chrono::duration<double> diff = end - start;
1310 uint64_t finalsize = sstat.st_size;
1311 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1312 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001313 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001314}
1315
1316MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001317 if (!hasStorage())
1318 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001319 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001320 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001321 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001322 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001323 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1324 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001325
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001326 MtpStringBuffer filePath;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001327 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001328 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001329 if (result != MTP_RESPONSE_OK)
1330 return result;
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001331
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001332 // Don't delete the actual files unless the database deletion is allowed
1333 result = mDatabase->beginDeleteObject(handle);
1334 if (result != MTP_RESPONSE_OK)
1335 return result;
1336
1337 bool success = deletePath((const char *)filePath);
1338
1339 mDatabase->endDeleteObject(handle, success);
1340 return success ? result : MTP_RESPONSE_PARTIAL_DELETION;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001341}
1342
1343MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001344 if (mRequest.getParameterCount() < 2)
1345 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001346 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001347 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001348 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001349 MtpDebug::getFormatCodeName(format));
1350 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001351 if (!property)
1352 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001353 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001354 delete property;
1355 return MTP_RESPONSE_OK;
1356}
1357
1358MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001359 if (mRequest.getParameterCount() < 1)
1360 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001361 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001362 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001363 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1364 if (!property)
1365 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1366 property->write(mData);
1367 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001368 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001369}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001370
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001371MtpResponseCode MtpServer::doSendPartialObject() {
1372 if (!hasStorage())
1373 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001374 if (mRequest.getParameterCount() < 4)
1375 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001376 MtpObjectHandle handle = mRequest.getParameter(1);
1377 uint64_t offset = mRequest.getParameter(2);
1378 uint64_t offset2 = mRequest.getParameter(3);
1379 offset = offset | (offset2 << 32);
1380 uint32_t length = mRequest.getParameter(4);
1381
1382 ObjectEdit* edit = getEditObject(handle);
1383 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001384 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001385 return MTP_RESPONSE_GENERAL_ERROR;
1386 }
1387
1388 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001389 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001390 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1391 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001392 return MTP_RESPONSE_GENERAL_ERROR;
1393 }
1394
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001395 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001396 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001397
Mike Lockwoodef441d92011-07-14 21:00:02 -04001398 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001399 int ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001400 if (ret < MTP_CONTAINER_HEADER_SIZE)
1401 return MTP_RESPONSE_GENERAL_ERROR;
1402 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1403
1404 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001405 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001406 offset += initialData;
1407 length -= initialData;
1408 }
1409
tao.pei07a9e542015-07-17 17:18:41 +08001410 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001411 if (ret < 0) {
1412 ALOGE("failed to write initial data");
1413 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001414 mtp_file_range mfr;
1415 mfr.fd = edit->mFD;
1416 mfr.offset = offset;
1417 mfr.length = length;
1418 mfr.command = 0;
1419 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001420
Jerry Zhang54107562017-05-15 11:54:19 -07001421 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001422 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001423 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1424 if ((ret < 0) && (errno == ECANCELED)) {
1425 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001426 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001427 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001428 if (ret < 0) {
1429 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001430 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001431 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1432 else
1433 return MTP_RESPONSE_GENERAL_ERROR;
1434 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001435
1436 // reset so we don't attempt to send this back
1437 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001438 mResponse.setParameter(1, length);
1439 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001440 if (end > edit->mSize) {
1441 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001442 }
1443 return MTP_RESPONSE_OK;
1444}
1445
1446MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001447 if (mRequest.getParameterCount() < 3)
1448 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001449 MtpObjectHandle handle = mRequest.getParameter(1);
1450 ObjectEdit* edit = getEditObject(handle);
1451 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001452 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001453 return MTP_RESPONSE_GENERAL_ERROR;
1454 }
1455
1456 uint64_t offset = mRequest.getParameter(2);
1457 uint64_t offset2 = mRequest.getParameter(3);
1458 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001459 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001460 return MTP_RESPONSE_GENERAL_ERROR;
1461 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001462 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001463 return MTP_RESPONSE_OK;
1464 }
1465}
1466
1467MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001468 if (mRequest.getParameterCount() < 1)
1469 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001470 MtpObjectHandle handle = mRequest.getParameter(1);
1471 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001472 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001473 return MTP_RESPONSE_GENERAL_ERROR;
1474 }
1475
Jerry Zhangbc1d4b42018-03-27 15:25:03 -07001476 MtpStringBuffer path;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001477 int64_t fileLength;
1478 MtpObjectFormat format;
1479 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1480 if (result != MTP_RESPONSE_OK)
1481 return result;
1482
1483 int fd = open((const char *)path, O_RDWR | O_EXCL);
1484 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001485 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001486 return MTP_RESPONSE_GENERAL_ERROR;
1487 }
1488
1489 addEditObject(handle, path, fileLength, format, fd);
1490 return MTP_RESPONSE_OK;
1491}
1492
1493MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001494 if (mRequest.getParameterCount() < 1)
1495 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001496 MtpObjectHandle handle = mRequest.getParameter(1);
1497 ObjectEdit* edit = getEditObject(handle);
1498 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001499 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001500 return MTP_RESPONSE_GENERAL_ERROR;
1501 }
1502
1503 commitEdit(edit);
1504 removeEditObject(handle);
1505 return MTP_RESPONSE_OK;
1506}
1507
Mike Lockwood7850ef92010-05-14 10:10:36 -04001508} // namespace android