Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <errno.h> |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <dirent.h> |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 26 | |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
| 28 | |
Mike Lockwood | 9f67924 | 2010-09-23 22:32:05 -0400 | [diff] [blame] | 29 | #define LOG_TAG "MtpServer" |
| 30 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 31 | #include "MtpDebug.h" |
Mike Lockwood | c5c7853 | 2010-07-09 10:45:22 -0400 | [diff] [blame] | 32 | #include "MtpDatabase.h" |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 33 | #include "MtpObjectInfo.h" |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 34 | #include "MtpProperty.h" |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 35 | #include "MtpServer.h" |
| 36 | #include "MtpStorage.h" |
| 37 | #include "MtpStringBuffer.h" |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 38 | |
Mike Lockwood | 9c7fdf5 | 2010-07-15 13:36:52 -0400 | [diff] [blame] | 39 | #include <linux/usb/f_mtp.h> |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 40 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 43 | static const MtpOperationCode kSupportedOperationCodes[] = { |
| 44 | MTP_OPERATION_GET_DEVICE_INFO, |
| 45 | MTP_OPERATION_OPEN_SESSION, |
| 46 | MTP_OPERATION_CLOSE_SESSION, |
| 47 | MTP_OPERATION_GET_STORAGE_IDS, |
| 48 | MTP_OPERATION_GET_STORAGE_INFO, |
| 49 | MTP_OPERATION_GET_NUM_OBJECTS, |
| 50 | MTP_OPERATION_GET_OBJECT_HANDLES, |
| 51 | MTP_OPERATION_GET_OBJECT_INFO, |
| 52 | MTP_OPERATION_GET_OBJECT, |
Mike Lockwood | c89f222 | 2011-04-24 18:40:17 -0700 | [diff] [blame] | 53 | MTP_OPERATION_GET_THUMB, |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 54 | MTP_OPERATION_DELETE_OBJECT, |
| 55 | MTP_OPERATION_SEND_OBJECT_INFO, |
| 56 | MTP_OPERATION_SEND_OBJECT, |
| 57 | // MTP_OPERATION_INITIATE_CAPTURE, |
| 58 | // MTP_OPERATION_FORMAT_STORE, |
| 59 | // MTP_OPERATION_RESET_DEVICE, |
| 60 | // MTP_OPERATION_SELF_TEST, |
| 61 | // MTP_OPERATION_SET_OBJECT_PROTECTION, |
| 62 | // MTP_OPERATION_POWER_DOWN, |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame] | 63 | MTP_OPERATION_GET_DEVICE_PROP_DESC, |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 64 | MTP_OPERATION_GET_DEVICE_PROP_VALUE, |
| 65 | MTP_OPERATION_SET_DEVICE_PROP_VALUE, |
| 66 | MTP_OPERATION_RESET_DEVICE_PROP_VALUE, |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 67 | // MTP_OPERATION_TERMINATE_OPEN_CAPTURE, |
| 68 | // MTP_OPERATION_MOVE_OBJECT, |
| 69 | // MTP_OPERATION_COPY_OBJECT, |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 70 | MTP_OPERATION_GET_PARTIAL_OBJECT, |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 71 | // MTP_OPERATION_INITIATE_OPEN_CAPTURE, |
| 72 | MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED, |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 73 | MTP_OPERATION_GET_OBJECT_PROP_DESC, |
Mike Lockwood | d3bfecb | 2010-09-23 23:04:28 -0400 | [diff] [blame] | 74 | MTP_OPERATION_GET_OBJECT_PROP_VALUE, |
| 75 | MTP_OPERATION_SET_OBJECT_PROP_VALUE, |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 76 | MTP_OPERATION_GET_OBJECT_PROP_LIST, |
| 77 | // MTP_OPERATION_SET_OBJECT_PROP_LIST, |
| 78 | // MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC, |
| 79 | // MTP_OPERATION_SEND_OBJECT_PROP_LIST, |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 80 | MTP_OPERATION_GET_OBJECT_REFERENCES, |
| 81 | MTP_OPERATION_SET_OBJECT_REFERENCES, |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 82 | // MTP_OPERATION_SKIP, |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 83 | // Android extension for direct file IO |
| 84 | MTP_OPERATION_GET_PARTIAL_OBJECT_64, |
| 85 | MTP_OPERATION_SEND_PARTIAL_OBJECT, |
| 86 | MTP_OPERATION_TRUNCATE_OBJECT, |
| 87 | MTP_OPERATION_BEGIN_EDIT_OBJECT, |
| 88 | MTP_OPERATION_END_EDIT_OBJECT, |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 91 | static const MtpEventCode kSupportedEventCodes[] = { |
| 92 | MTP_EVENT_OBJECT_ADDED, |
| 93 | MTP_EVENT_OBJECT_REMOVED, |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 94 | MTP_EVENT_STORE_ADDED, |
| 95 | MTP_EVENT_STORE_REMOVED, |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame] | 98 | MtpServer::MtpServer(int fd, MtpDatabase* database, bool ptp, |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 99 | int fileGroup, int filePerm, int directoryPerm) |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 100 | : mFD(fd), |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 101 | mDatabase(database), |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame] | 102 | mPtp(ptp), |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 103 | mFileGroup(fileGroup), |
| 104 | mFilePermission(filePerm), |
| 105 | mDirectoryPermission(directoryPerm), |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 106 | mSessionID(0), |
| 107 | mSessionOpen(false), |
| 108 | mSendObjectHandle(kInvalidObjectHandle), |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 109 | mSendObjectFormat(0), |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 110 | mSendObjectFileSize(0) |
| 111 | { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | MtpServer::~MtpServer() { |
| 115 | } |
| 116 | |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 117 | void MtpServer::addStorage(MtpStorage* storage) { |
| 118 | Mutex::Autolock autoLock(mMutex); |
| 119 | |
| 120 | mStorages.push(storage); |
| 121 | sendStoreAdded(storage->getStorageID()); |
| 122 | } |
| 123 | |
| 124 | void MtpServer::removeStorage(MtpStorage* storage) { |
| 125 | Mutex::Autolock autoLock(mMutex); |
| 126 | |
| 127 | for (int i = 0; i < mStorages.size(); i++) { |
| 128 | if (mStorages[i] == storage) { |
| 129 | mStorages.removeAt(i); |
| 130 | sendStoreRemoved(storage->getStorageID()); |
| 131 | break; |
| 132 | } |
| 133 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | MtpStorage* MtpServer::getStorage(MtpStorageID id) { |
Mike Lockwood | 365e03e | 2010-12-08 16:08:01 -0800 | [diff] [blame] | 137 | if (id == 0) |
| 138 | return mStorages[0]; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 139 | for (int i = 0; i < mStorages.size(); i++) { |
Mike Lockwood | 365e03e | 2010-12-08 16:08:01 -0800 | [diff] [blame] | 140 | MtpStorage* storage = mStorages[i]; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 141 | if (storage->getStorageID() == id) |
| 142 | return storage; |
| 143 | } |
| 144 | return NULL; |
| 145 | } |
| 146 | |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 147 | bool MtpServer::hasStorage(MtpStorageID id) { |
| 148 | if (id == 0 || id == 0xFFFFFFFF) |
| 149 | return mStorages.size() > 0; |
| 150 | return (getStorage(id) != NULL); |
| 151 | } |
| 152 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 153 | void MtpServer::run() { |
| 154 | int fd = mFD; |
| 155 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 156 | ALOGV("MtpServer::run fd: %d\n", fd); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 157 | |
| 158 | while (1) { |
| 159 | int ret = mRequest.read(fd); |
| 160 | if (ret < 0) { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 161 | ALOGV("request read returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 162 | if (errno == ECANCELED) { |
| 163 | // return to top of loop and wait for next command |
| 164 | continue; |
| 165 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | MtpOperationCode operation = mRequest.getOperationCode(); |
| 169 | MtpTransactionID transaction = mRequest.getTransactionID(); |
| 170 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 171 | ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation)); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 172 | mRequest.dump(); |
| 173 | |
| 174 | // FIXME need to generalize this |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 175 | bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 176 | || operation == MTP_OPERATION_SET_OBJECT_REFERENCES |
| 177 | || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE |
| 178 | || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 179 | if (dataIn) { |
| 180 | int ret = mData.read(fd); |
| 181 | if (ret < 0) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 182 | ALOGE("data read returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 183 | if (errno == ECANCELED) { |
| 184 | // return to top of loop and wait for next command |
| 185 | continue; |
| 186 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 187 | break; |
| 188 | } |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 189 | ALOGV("received data:"); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 190 | mData.dump(); |
| 191 | } else { |
| 192 | mData.reset(); |
| 193 | } |
| 194 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 195 | if (handleRequest()) { |
| 196 | if (!dataIn && mData.hasData()) { |
| 197 | mData.setOperationCode(operation); |
| 198 | mData.setTransactionID(transaction); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 199 | ALOGV("sending data:"); |
Mike Lockwood | db77431 | 2010-10-11 17:31:44 -0400 | [diff] [blame] | 200 | mData.dump(); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 201 | ret = mData.write(fd); |
| 202 | if (ret < 0) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 203 | ALOGE("request write returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 204 | if (errno == ECANCELED) { |
| 205 | // return to top of loop and wait for next command |
| 206 | continue; |
| 207 | } |
| 208 | break; |
| 209 | } |
| 210 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 211 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 212 | mResponse.setTransactionID(transaction); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 213 | ALOGV("sending response %04X", mResponse.getResponseCode()); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 214 | ret = mResponse.write(fd); |
Mike Lockwood | db77431 | 2010-10-11 17:31:44 -0400 | [diff] [blame] | 215 | mResponse.dump(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 216 | if (ret < 0) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 217 | ALOGE("request write returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 218 | if (errno == ECANCELED) { |
| 219 | // return to top of loop and wait for next command |
| 220 | continue; |
| 221 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 222 | break; |
| 223 | } |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 224 | } else { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 225 | ALOGV("skipping response\n"); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 226 | } |
| 227 | } |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 228 | |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 229 | // commit any open edits |
| 230 | int count = mObjectEditList.size(); |
| 231 | for (int i = 0; i < count; i++) { |
| 232 | ObjectEdit* edit = mObjectEditList[i]; |
| 233 | commitEdit(edit); |
| 234 | delete edit; |
| 235 | } |
| 236 | mObjectEditList.clear(); |
| 237 | |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 238 | if (mSessionOpen) |
| 239 | mDatabase->sessionEnded(); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 240 | close(fd); |
| 241 | mFD = -1; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 244 | void MtpServer::sendObjectAdded(MtpObjectHandle handle) { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 245 | ALOGV("sendObjectAdded %d\n", handle); |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 246 | sendEvent(MTP_EVENT_OBJECT_ADDED, handle); |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void MtpServer::sendObjectRemoved(MtpObjectHandle handle) { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 250 | ALOGV("sendObjectRemoved %d\n", handle); |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 251 | sendEvent(MTP_EVENT_OBJECT_REMOVED, handle); |
| 252 | } |
| 253 | |
| 254 | void MtpServer::sendStoreAdded(MtpStorageID id) { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 255 | ALOGV("sendStoreAdded %08X\n", id); |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 256 | sendEvent(MTP_EVENT_STORE_ADDED, id); |
| 257 | } |
| 258 | |
| 259 | void MtpServer::sendStoreRemoved(MtpStorageID id) { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 260 | ALOGV("sendStoreRemoved %08X\n", id); |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 261 | sendEvent(MTP_EVENT_STORE_REMOVED, id); |
| 262 | } |
| 263 | |
| 264 | void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) { |
Mike Lockwood | dc453d4 | 2010-07-19 14:29:58 -0400 | [diff] [blame] | 265 | if (mSessionOpen) { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 266 | mEvent.setEventCode(code); |
Mike Lockwood | dc453d4 | 2010-07-19 14:29:58 -0400 | [diff] [blame] | 267 | mEvent.setTransactionID(mRequest.getTransactionID()); |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 268 | mEvent.setParameter(1, param1); |
Mike Lockwood | dc453d4 | 2010-07-19 14:29:58 -0400 | [diff] [blame] | 269 | int ret = mEvent.write(mFD); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 270 | ALOGV("mEvent.write returned %d\n", ret); |
Mike Lockwood | dc453d4 | 2010-07-19 14:29:58 -0400 | [diff] [blame] | 271 | } |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 274 | void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path, |
| 275 | uint64_t size, MtpObjectFormat format, int fd) { |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 276 | ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 277 | mObjectEditList.add(edit); |
| 278 | } |
| 279 | |
| 280 | MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) { |
| 281 | int count = mObjectEditList.size(); |
| 282 | for (int i = 0; i < count; i++) { |
| 283 | ObjectEdit* edit = mObjectEditList[i]; |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 284 | if (edit->mHandle == handle) return edit; |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 285 | } |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | void MtpServer::removeEditObject(MtpObjectHandle handle) { |
| 290 | int count = mObjectEditList.size(); |
| 291 | for (int i = 0; i < count; i++) { |
| 292 | ObjectEdit* edit = mObjectEditList[i]; |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 293 | if (edit->mHandle == handle) { |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 294 | delete edit; |
| 295 | mObjectEditList.removeAt(i); |
| 296 | return; |
| 297 | } |
| 298 | } |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 299 | ALOGE("ObjectEdit not found in removeEditObject"); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void MtpServer::commitEdit(ObjectEdit* edit) { |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 303 | mDatabase->endSendObject((const char *)edit->mPath, edit->mHandle, edit->mFormat, true); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 307 | bool MtpServer::handleRequest() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 308 | Mutex::Autolock autoLock(mMutex); |
| 309 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 310 | MtpOperationCode operation = mRequest.getOperationCode(); |
| 311 | MtpResponseCode response; |
| 312 | |
| 313 | mResponse.reset(); |
| 314 | |
| 315 | if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) { |
| 316 | // FIXME - need to delete mSendObjectHandle from the database |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 317 | ALOGE("expected SendObject after SendObjectInfo"); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 318 | mSendObjectHandle = kInvalidObjectHandle; |
| 319 | } |
| 320 | |
| 321 | switch (operation) { |
| 322 | case MTP_OPERATION_GET_DEVICE_INFO: |
| 323 | response = doGetDeviceInfo(); |
| 324 | break; |
| 325 | case MTP_OPERATION_OPEN_SESSION: |
| 326 | response = doOpenSession(); |
| 327 | break; |
| 328 | case MTP_OPERATION_CLOSE_SESSION: |
| 329 | response = doCloseSession(); |
| 330 | break; |
| 331 | case MTP_OPERATION_GET_STORAGE_IDS: |
| 332 | response = doGetStorageIDs(); |
| 333 | break; |
| 334 | case MTP_OPERATION_GET_STORAGE_INFO: |
| 335 | response = doGetStorageInfo(); |
| 336 | break; |
| 337 | case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED: |
| 338 | response = doGetObjectPropsSupported(); |
| 339 | break; |
| 340 | case MTP_OPERATION_GET_OBJECT_HANDLES: |
| 341 | response = doGetObjectHandles(); |
| 342 | break; |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 343 | case MTP_OPERATION_GET_NUM_OBJECTS: |
| 344 | response = doGetNumObjects(); |
| 345 | break; |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 346 | case MTP_OPERATION_GET_OBJECT_REFERENCES: |
| 347 | response = doGetObjectReferences(); |
| 348 | break; |
| 349 | case MTP_OPERATION_SET_OBJECT_REFERENCES: |
| 350 | response = doSetObjectReferences(); |
| 351 | break; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 352 | case MTP_OPERATION_GET_OBJECT_PROP_VALUE: |
| 353 | response = doGetObjectPropValue(); |
| 354 | break; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 355 | case MTP_OPERATION_SET_OBJECT_PROP_VALUE: |
| 356 | response = doSetObjectPropValue(); |
| 357 | break; |
| 358 | case MTP_OPERATION_GET_DEVICE_PROP_VALUE: |
| 359 | response = doGetDevicePropValue(); |
| 360 | break; |
| 361 | case MTP_OPERATION_SET_DEVICE_PROP_VALUE: |
| 362 | response = doSetDevicePropValue(); |
| 363 | break; |
| 364 | case MTP_OPERATION_RESET_DEVICE_PROP_VALUE: |
| 365 | response = doResetDevicePropValue(); |
| 366 | break; |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 367 | case MTP_OPERATION_GET_OBJECT_PROP_LIST: |
| 368 | response = doGetObjectPropList(); |
| 369 | break; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 370 | case MTP_OPERATION_GET_OBJECT_INFO: |
| 371 | response = doGetObjectInfo(); |
| 372 | break; |
| 373 | case MTP_OPERATION_GET_OBJECT: |
| 374 | response = doGetObject(); |
| 375 | break; |
Mike Lockwood | c89f222 | 2011-04-24 18:40:17 -0700 | [diff] [blame] | 376 | case MTP_OPERATION_GET_THUMB: |
| 377 | response = doGetThumb(); |
| 378 | break; |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 379 | case MTP_OPERATION_GET_PARTIAL_OBJECT: |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 380 | case MTP_OPERATION_GET_PARTIAL_OBJECT_64: |
| 381 | response = doGetPartialObject(operation); |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 382 | break; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 383 | case MTP_OPERATION_SEND_OBJECT_INFO: |
| 384 | response = doSendObjectInfo(); |
| 385 | break; |
| 386 | case MTP_OPERATION_SEND_OBJECT: |
| 387 | response = doSendObject(); |
| 388 | break; |
| 389 | case MTP_OPERATION_DELETE_OBJECT: |
| 390 | response = doDeleteObject(); |
| 391 | break; |
| 392 | case MTP_OPERATION_GET_OBJECT_PROP_DESC: |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 393 | response = doGetObjectPropDesc(); |
| 394 | break; |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame] | 395 | case MTP_OPERATION_GET_DEVICE_PROP_DESC: |
| 396 | response = doGetDevicePropDesc(); |
| 397 | break; |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 398 | case MTP_OPERATION_SEND_PARTIAL_OBJECT: |
| 399 | response = doSendPartialObject(); |
| 400 | break; |
| 401 | case MTP_OPERATION_TRUNCATE_OBJECT: |
| 402 | response = doTruncateObject(); |
| 403 | break; |
| 404 | case MTP_OPERATION_BEGIN_EDIT_OBJECT: |
| 405 | response = doBeginEditObject(); |
| 406 | break; |
| 407 | case MTP_OPERATION_END_EDIT_OBJECT: |
| 408 | response = doEndEditObject(); |
| 409 | break; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 410 | default: |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 411 | ALOGE("got unsupported command %s", MtpDebug::getOperationCodeName(operation)); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 412 | response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED; |
| 413 | break; |
| 414 | } |
| 415 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 416 | if (response == MTP_RESPONSE_TRANSACTION_CANCELLED) |
| 417 | return false; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 418 | mResponse.setResponseCode(response); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 419 | return true; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | MtpResponseCode MtpServer::doGetDeviceInfo() { |
| 423 | MtpStringBuffer string; |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 424 | char prop_value[PROPERTY_VALUE_MAX]; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 425 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 426 | MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats(); |
| 427 | MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats(); |
| 428 | MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties(); |
| 429 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 430 | // fill in device info |
| 431 | mData.putUInt16(MTP_STANDARD_VERSION); |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame] | 432 | if (mPtp) { |
| 433 | mData.putUInt32(0); |
| 434 | } else { |
| 435 | // MTP Vendor Extension ID |
| 436 | mData.putUInt32(6); |
| 437 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 438 | mData.putUInt16(MTP_STANDARD_VERSION); |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame] | 439 | if (mPtp) { |
| 440 | // no extensions |
| 441 | string.set(""); |
| 442 | } else { |
| 443 | // MTP extensions |
| 444 | string.set("microsoft.com: 1.0; android.com: 1.0;"); |
| 445 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 446 | mData.putString(string); // MTP Extensions |
| 447 | mData.putUInt16(0); //Functional Mode |
| 448 | mData.putAUInt16(kSupportedOperationCodes, |
| 449 | sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 450 | mData.putAUInt16(kSupportedEventCodes, |
| 451 | sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 452 | mData.putAUInt16(deviceProperties); // Device Properties Supported |
| 453 | mData.putAUInt16(captureFormats); // Capture Formats |
| 454 | mData.putAUInt16(playbackFormats); // Playback Formats |
Mike Lockwood | 4639532 | 2011-01-31 16:44:44 -0500 | [diff] [blame] | 455 | |
| 456 | property_get("ro.product.manufacturer", prop_value, "unknown manufacturer"); |
| 457 | string.set(prop_value); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 458 | mData.putString(string); // Manufacturer |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 459 | |
| 460 | property_get("ro.product.model", prop_value, "MTP Device"); |
| 461 | string.set(prop_value); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 462 | mData.putString(string); // Model |
| 463 | string.set("1.0"); |
| 464 | mData.putString(string); // Device Version |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 465 | |
| 466 | property_get("ro.serialno", prop_value, "????????"); |
| 467 | string.set(prop_value); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 468 | mData.putString(string); // Serial Number |
| 469 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 470 | delete playbackFormats; |
| 471 | delete captureFormats; |
| 472 | delete deviceProperties; |
| 473 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 474 | return MTP_RESPONSE_OK; |
| 475 | } |
| 476 | |
| 477 | MtpResponseCode MtpServer::doOpenSession() { |
| 478 | if (mSessionOpen) { |
| 479 | mResponse.setParameter(1, mSessionID); |
| 480 | return MTP_RESPONSE_SESSION_ALREADY_OPEN; |
| 481 | } |
| 482 | mSessionID = mRequest.getParameter(1); |
| 483 | mSessionOpen = true; |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 484 | |
| 485 | mDatabase->sessionStarted(); |
| 486 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 487 | return MTP_RESPONSE_OK; |
| 488 | } |
| 489 | |
| 490 | MtpResponseCode MtpServer::doCloseSession() { |
| 491 | if (!mSessionOpen) |
| 492 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 493 | mSessionID = 0; |
| 494 | mSessionOpen = false; |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 495 | mDatabase->sessionEnded(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 496 | return MTP_RESPONSE_OK; |
| 497 | } |
| 498 | |
| 499 | MtpResponseCode MtpServer::doGetStorageIDs() { |
| 500 | if (!mSessionOpen) |
| 501 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 502 | |
| 503 | int count = mStorages.size(); |
| 504 | mData.putUInt32(count); |
| 505 | for (int i = 0; i < count; i++) |
| 506 | mData.putUInt32(mStorages[i]->getStorageID()); |
| 507 | |
| 508 | return MTP_RESPONSE_OK; |
| 509 | } |
| 510 | |
| 511 | MtpResponseCode MtpServer::doGetStorageInfo() { |
| 512 | MtpStringBuffer string; |
| 513 | |
| 514 | if (!mSessionOpen) |
| 515 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 516 | MtpStorageID id = mRequest.getParameter(1); |
| 517 | MtpStorage* storage = getStorage(id); |
| 518 | if (!storage) |
| 519 | return MTP_RESPONSE_INVALID_STORAGE_ID; |
| 520 | |
| 521 | mData.putUInt16(storage->getType()); |
| 522 | mData.putUInt16(storage->getFileSystemType()); |
| 523 | mData.putUInt16(storage->getAccessCapability()); |
| 524 | mData.putUInt64(storage->getMaxCapacity()); |
| 525 | mData.putUInt64(storage->getFreeSpace()); |
| 526 | mData.putUInt32(1024*1024*1024); // Free Space in Objects |
| 527 | string.set(storage->getDescription()); |
| 528 | mData.putString(string); |
| 529 | mData.putEmptyString(); // Volume Identifier |
| 530 | |
| 531 | return MTP_RESPONSE_OK; |
| 532 | } |
| 533 | |
| 534 | MtpResponseCode MtpServer::doGetObjectPropsSupported() { |
| 535 | if (!mSessionOpen) |
| 536 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 537 | MtpObjectFormat format = mRequest.getParameter(1); |
Mike Lockwood | 4cb956c | 2010-12-07 10:51:20 -0800 | [diff] [blame] | 538 | MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format); |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 539 | mData.putAUInt16(properties); |
Mike Lockwood | 33ea5a4 | 2010-08-10 15:11:32 -0400 | [diff] [blame] | 540 | delete properties; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 541 | return MTP_RESPONSE_OK; |
| 542 | } |
| 543 | |
| 544 | MtpResponseCode MtpServer::doGetObjectHandles() { |
| 545 | if (!mSessionOpen) |
| 546 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 547 | MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage |
Mike Lockwood | 3743365 | 2010-05-19 15:12:14 -0400 | [diff] [blame] | 548 | MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 549 | MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent |
Mike Lockwood | 6acc90f | 2011-06-17 13:44:24 -0400 | [diff] [blame] | 550 | // 0x00000000 for all objects |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 551 | |
| 552 | if (!hasStorage(storageID)) |
| 553 | return MTP_RESPONSE_INVALID_STORAGE_ID; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 554 | |
| 555 | MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent); |
| 556 | mData.putAUInt32(handles); |
| 557 | delete handles; |
| 558 | return MTP_RESPONSE_OK; |
| 559 | } |
| 560 | |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 561 | MtpResponseCode MtpServer::doGetNumObjects() { |
| 562 | if (!mSessionOpen) |
| 563 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 564 | MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage |
| 565 | MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats |
| 566 | MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent |
Mike Lockwood | 6acc90f | 2011-06-17 13:44:24 -0400 | [diff] [blame] | 567 | // 0x00000000 for all objects |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 568 | if (!hasStorage(storageID)) |
| 569 | return MTP_RESPONSE_INVALID_STORAGE_ID; |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 570 | |
| 571 | int count = mDatabase->getNumObjects(storageID, format, parent); |
| 572 | if (count >= 0) { |
| 573 | mResponse.setParameter(1, count); |
| 574 | return MTP_RESPONSE_OK; |
| 575 | } else { |
| 576 | mResponse.setParameter(1, 0); |
| 577 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
| 578 | } |
| 579 | } |
| 580 | |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 581 | MtpResponseCode MtpServer::doGetObjectReferences() { |
| 582 | if (!mSessionOpen) |
| 583 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 584 | if (!hasStorage()) |
| 585 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
| 586 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 587 | |
| 588 | // FIXME - check for invalid object handle |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 589 | MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 590 | if (handles) { |
| 591 | mData.putAUInt32(handles); |
| 592 | delete handles; |
| 593 | } else { |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 594 | mData.putEmptyArray(); |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 595 | } |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 596 | return MTP_RESPONSE_OK; |
| 597 | } |
| 598 | |
| 599 | MtpResponseCode MtpServer::doSetObjectReferences() { |
| 600 | if (!mSessionOpen) |
| 601 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 602 | if (!hasStorage()) |
| 603 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 604 | MtpStorageID handle = mRequest.getParameter(1); |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 605 | |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 606 | MtpObjectHandleList* references = mData.getAUInt32(); |
| 607 | MtpResponseCode result = mDatabase->setObjectReferences(handle, references); |
| 608 | delete references; |
| 609 | return result; |
| 610 | } |
| 611 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 612 | MtpResponseCode MtpServer::doGetObjectPropValue() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 613 | if (!hasStorage()) |
| 614 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 615 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 616 | MtpObjectProperty property = mRequest.getParameter(2); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 617 | ALOGV("GetObjectPropValue %d %s\n", handle, |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 618 | MtpDebug::getObjectPropCodeName(property)); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 619 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 620 | return mDatabase->getObjectPropertyValue(handle, property, mData); |
| 621 | } |
| 622 | |
| 623 | MtpResponseCode MtpServer::doSetObjectPropValue() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 624 | if (!hasStorage()) |
| 625 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 626 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 627 | MtpObjectProperty property = mRequest.getParameter(2); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 628 | ALOGV("SetObjectPropValue %d %s\n", handle, |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 629 | MtpDebug::getObjectPropCodeName(property)); |
| 630 | |
| 631 | return mDatabase->setObjectPropertyValue(handle, property, mData); |
| 632 | } |
| 633 | |
| 634 | MtpResponseCode MtpServer::doGetDevicePropValue() { |
| 635 | MtpDeviceProperty property = mRequest.getParameter(1); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 636 | ALOGV("GetDevicePropValue %s\n", |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 637 | MtpDebug::getDevicePropCodeName(property)); |
| 638 | |
| 639 | return mDatabase->getDevicePropertyValue(property, mData); |
| 640 | } |
| 641 | |
| 642 | MtpResponseCode MtpServer::doSetDevicePropValue() { |
| 643 | MtpDeviceProperty property = mRequest.getParameter(1); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 644 | ALOGV("SetDevicePropValue %s\n", |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 645 | MtpDebug::getDevicePropCodeName(property)); |
| 646 | |
| 647 | return mDatabase->setDevicePropertyValue(property, mData); |
| 648 | } |
| 649 | |
| 650 | MtpResponseCode MtpServer::doResetDevicePropValue() { |
| 651 | MtpDeviceProperty property = mRequest.getParameter(1); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 652 | ALOGV("ResetDevicePropValue %s\n", |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 653 | MtpDebug::getDevicePropCodeName(property)); |
| 654 | |
| 655 | return mDatabase->resetDeviceProperty(property); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 658 | MtpResponseCode MtpServer::doGetObjectPropList() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 659 | if (!hasStorage()) |
| 660 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 661 | |
| 662 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | 7d7fb63 | 2010-12-01 18:46:23 -0500 | [diff] [blame] | 663 | // use uint32_t so we can support 0xFFFFFFFF |
| 664 | uint32_t format = mRequest.getParameter(2); |
| 665 | uint32_t property = mRequest.getParameter(3); |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 666 | int groupCode = mRequest.getParameter(4); |
Mike Lockwood | e3634c3 | 2010-11-23 18:45:25 -0500 | [diff] [blame] | 667 | int depth = mRequest.getParameter(5); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 668 | ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n", |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 669 | handle, MtpDebug::getFormatCodeName(format), |
| 670 | MtpDebug::getObjectPropCodeName(property), groupCode, depth); |
| 671 | |
| 672 | return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData); |
| 673 | } |
| 674 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 675 | MtpResponseCode MtpServer::doGetObjectInfo() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 676 | if (!hasStorage()) |
| 677 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 678 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 679 | MtpObjectInfo info(handle); |
| 680 | MtpResponseCode result = mDatabase->getObjectInfo(handle, info); |
| 681 | if (result == MTP_RESPONSE_OK) { |
| 682 | char date[20]; |
| 683 | |
| 684 | mData.putUInt32(info.mStorageID); |
| 685 | mData.putUInt16(info.mFormat); |
| 686 | mData.putUInt16(info.mProtectionStatus); |
| 687 | |
| 688 | // if object is being edited the database size may be out of date |
| 689 | uint32_t size = info.mCompressedSize; |
| 690 | ObjectEdit* edit = getEditObject(handle); |
| 691 | if (edit) |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 692 | size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 693 | mData.putUInt32(size); |
| 694 | |
| 695 | mData.putUInt16(info.mThumbFormat); |
| 696 | mData.putUInt32(info.mThumbCompressedSize); |
| 697 | mData.putUInt32(info.mThumbPixWidth); |
| 698 | mData.putUInt32(info.mThumbPixHeight); |
| 699 | mData.putUInt32(info.mImagePixWidth); |
| 700 | mData.putUInt32(info.mImagePixHeight); |
| 701 | mData.putUInt32(info.mImagePixDepth); |
| 702 | mData.putUInt32(info.mParent); |
| 703 | mData.putUInt16(info.mAssociationType); |
| 704 | mData.putUInt32(info.mAssociationDesc); |
| 705 | mData.putUInt32(info.mSequenceNumber); |
| 706 | mData.putString(info.mName); |
| 707 | mData.putEmptyString(); // date created |
| 708 | formatDateTime(info.mDateModified, date, sizeof(date)); |
| 709 | mData.putString(date); // date modified |
| 710 | mData.putEmptyString(); // keywords |
| 711 | } |
| 712 | return result; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | MtpResponseCode MtpServer::doGetObject() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 716 | if (!hasStorage()) |
| 717 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 718 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 719 | MtpString pathBuf; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 720 | int64_t fileLength; |
Mike Lockwood | 365e03e | 2010-12-08 16:08:01 -0800 | [diff] [blame] | 721 | MtpObjectFormat format; |
| 722 | int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 723 | if (result != MTP_RESPONSE_OK) |
| 724 | return result; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 725 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 726 | const char* filePath = (const char *)pathBuf; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 727 | mtp_file_range mfr; |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 728 | mfr.fd = open(filePath, O_RDONLY); |
| 729 | if (mfr.fd < 0) { |
| 730 | return MTP_RESPONSE_GENERAL_ERROR; |
| 731 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 732 | mfr.offset = 0; |
| 733 | mfr.length = fileLength; |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 734 | mfr.command = mRequest.getOperationCode(); |
| 735 | mfr.transaction_id = mRequest.getTransactionID(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 736 | |
| 737 | // then transfer the file |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 738 | int ret = ioctl(mFD, MTP_SEND_FILE_WITH_HEADER, (unsigned long)&mfr); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 739 | ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret); |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 740 | close(mfr.fd); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 741 | if (ret < 0) { |
| 742 | if (errno == ECANCELED) |
| 743 | return MTP_RESPONSE_TRANSACTION_CANCELLED; |
| 744 | else |
| 745 | return MTP_RESPONSE_GENERAL_ERROR; |
| 746 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 747 | return MTP_RESPONSE_OK; |
| 748 | } |
| 749 | |
Mike Lockwood | c89f222 | 2011-04-24 18:40:17 -0700 | [diff] [blame] | 750 | MtpResponseCode MtpServer::doGetThumb() { |
| 751 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 752 | size_t thumbSize; |
| 753 | void* thumb = mDatabase->getThumbnail(handle, thumbSize); |
| 754 | if (thumb) { |
| 755 | // send data |
| 756 | mData.setOperationCode(mRequest.getOperationCode()); |
| 757 | mData.setTransactionID(mRequest.getTransactionID()); |
| 758 | mData.writeData(mFD, thumb, thumbSize); |
| 759 | free(thumb); |
| 760 | return MTP_RESPONSE_OK; |
| 761 | } else { |
| 762 | return MTP_RESPONSE_GENERAL_ERROR; |
| 763 | } |
| 764 | } |
| 765 | |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 766 | MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 767 | if (!hasStorage()) |
| 768 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 769 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 770 | uint64_t offset; |
| 771 | uint32_t length; |
| 772 | offset = mRequest.getParameter(2); |
| 773 | if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) { |
| 774 | // android extension with 64 bit offset |
| 775 | uint64_t offset2 = mRequest.getParameter(3); |
| 776 | offset = offset | (offset2 << 32); |
| 777 | length = mRequest.getParameter(4); |
| 778 | } else { |
| 779 | // standard GetPartialObject |
| 780 | length = mRequest.getParameter(3); |
| 781 | } |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 782 | MtpString pathBuf; |
| 783 | int64_t fileLength; |
Mike Lockwood | 365e03e | 2010-12-08 16:08:01 -0800 | [diff] [blame] | 784 | MtpObjectFormat format; |
| 785 | int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format); |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 786 | if (result != MTP_RESPONSE_OK) |
| 787 | return result; |
| 788 | if (offset + length > fileLength) |
| 789 | length = fileLength - offset; |
| 790 | |
| 791 | const char* filePath = (const char *)pathBuf; |
| 792 | mtp_file_range mfr; |
| 793 | mfr.fd = open(filePath, O_RDONLY); |
| 794 | if (mfr.fd < 0) { |
| 795 | return MTP_RESPONSE_GENERAL_ERROR; |
| 796 | } |
| 797 | mfr.offset = offset; |
| 798 | mfr.length = length; |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 799 | mfr.command = mRequest.getOperationCode(); |
| 800 | mfr.transaction_id = mRequest.getTransactionID(); |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 801 | mResponse.setParameter(1, length); |
| 802 | |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 803 | // transfer the file |
| 804 | int ret = ioctl(mFD, MTP_SEND_FILE_WITH_HEADER, (unsigned long)&mfr); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 805 | ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret); |
Mike Lockwood | 44e82dd | 2010-11-23 09:08:01 -0500 | [diff] [blame] | 806 | close(mfr.fd); |
| 807 | if (ret < 0) { |
| 808 | if (errno == ECANCELED) |
| 809 | return MTP_RESPONSE_TRANSACTION_CANCELLED; |
| 810 | else |
| 811 | return MTP_RESPONSE_GENERAL_ERROR; |
| 812 | } |
| 813 | return MTP_RESPONSE_OK; |
| 814 | } |
| 815 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 816 | MtpResponseCode MtpServer::doSendObjectInfo() { |
| 817 | MtpString path; |
| 818 | MtpStorageID storageID = mRequest.getParameter(1); |
| 819 | MtpStorage* storage = getStorage(storageID); |
| 820 | MtpObjectHandle parent = mRequest.getParameter(2); |
| 821 | if (!storage) |
| 822 | return MTP_RESPONSE_INVALID_STORAGE_ID; |
| 823 | |
| 824 | // special case the root |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 825 | if (parent == MTP_PARENT_ROOT) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 826 | path = storage->getPath(); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 827 | parent = 0; |
| 828 | } else { |
Mike Lockwood | 365e03e | 2010-12-08 16:08:01 -0800 | [diff] [blame] | 829 | int64_t length; |
| 830 | MtpObjectFormat format; |
| 831 | int result = mDatabase->getObjectFilePath(parent, path, length, format); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 832 | if (result != MTP_RESPONSE_OK) |
| 833 | return result; |
Mike Lockwood | 365e03e | 2010-12-08 16:08:01 -0800 | [diff] [blame] | 834 | if (format != MTP_FORMAT_ASSOCIATION) |
| 835 | return MTP_RESPONSE_INVALID_PARENT_OBJECT; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // read only the fields we need |
| 839 | mData.getUInt32(); // storage ID |
| 840 | MtpObjectFormat format = mData.getUInt16(); |
| 841 | mData.getUInt16(); // protection status |
| 842 | mSendObjectFileSize = mData.getUInt32(); |
| 843 | mData.getUInt16(); // thumb format |
| 844 | mData.getUInt32(); // thumb compressed size |
| 845 | mData.getUInt32(); // thumb pix width |
| 846 | mData.getUInt32(); // thumb pix height |
| 847 | mData.getUInt32(); // image pix width |
| 848 | mData.getUInt32(); // image pix height |
| 849 | mData.getUInt32(); // image bit depth |
| 850 | mData.getUInt32(); // parent |
| 851 | uint16_t associationType = mData.getUInt16(); |
| 852 | uint32_t associationDesc = mData.getUInt32(); // association desc |
| 853 | mData.getUInt32(); // sequence number |
| 854 | MtpStringBuffer name, created, modified; |
| 855 | mData.getString(name); // file name |
| 856 | mData.getString(created); // date created |
| 857 | mData.getString(modified); // date modified |
| 858 | // keywords follow |
| 859 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 860 | ALOGV("name: %s format: %04X\n", (const char *)name, format); |
Mike Lockwood | d078267 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 861 | time_t modifiedTime; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 862 | if (!parseDateTime(modified, modifiedTime)) |
| 863 | modifiedTime = 0; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 864 | |
| 865 | if (path[path.size() - 1] != '/') |
| 866 | path += "/"; |
| 867 | path += (const char *)name; |
| 868 | |
Mike Lockwood | 7f36b19 | 2010-12-12 12:17:43 -0800 | [diff] [blame] | 869 | // check space first |
| 870 | if (mSendObjectFileSize > storage->getFreeSpace()) |
| 871 | return MTP_RESPONSE_STORAGE_FULL; |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 872 | uint64_t maxFileSize = storage->getMaxFileSize(); |
| 873 | // check storage max file size |
| 874 | if (maxFileSize != 0) { |
| 875 | // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size |
| 876 | // is >= 0xFFFFFFFF |
| 877 | if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF) |
| 878 | return MTP_RESPONSE_OBJECT_TOO_LARGE; |
| 879 | } |
Mike Lockwood | 7f36b19 | 2010-12-12 12:17:43 -0800 | [diff] [blame] | 880 | |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 881 | ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID); |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 882 | MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, |
| 883 | format, parent, storageID, mSendObjectFileSize, modifiedTime); |
Mike Lockwood | d078267 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 884 | if (handle == kInvalidObjectHandle) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 885 | return MTP_RESPONSE_GENERAL_ERROR; |
Mike Lockwood | d078267 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 886 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 887 | |
| 888 | if (format == MTP_FORMAT_ASSOCIATION) { |
| 889 | mode_t mask = umask(0); |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 890 | int ret = mkdir((const char *)path, mDirectoryPermission); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 891 | umask(mask); |
| 892 | if (ret && ret != -EEXIST) |
| 893 | return MTP_RESPONSE_GENERAL_ERROR; |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 894 | chown((const char *)path, getuid(), mFileGroup); |
Mike Lockwood | 7a0bd17 | 2011-01-18 11:06:19 -0800 | [diff] [blame] | 895 | |
| 896 | // SendObject does not get sent for directories, so call endSendObject here instead |
| 897 | mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 898 | } else { |
| 899 | mSendObjectFilePath = path; |
| 900 | // save the handle for the SendObject call, which should follow |
| 901 | mSendObjectHandle = handle; |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 902 | mSendObjectFormat = format; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | mResponse.setParameter(1, storageID); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 906 | mResponse.setParameter(2, parent); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 907 | mResponse.setParameter(3, handle); |
| 908 | |
| 909 | return MTP_RESPONSE_OK; |
| 910 | } |
| 911 | |
| 912 | MtpResponseCode MtpServer::doSendObject() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 913 | if (!hasStorage()) |
| 914 | return MTP_RESPONSE_GENERAL_ERROR; |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 915 | MtpResponseCode result = MTP_RESPONSE_OK; |
| 916 | mode_t mask; |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 917 | int ret, initialData; |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 918 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 919 | if (mSendObjectHandle == kInvalidObjectHandle) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 920 | ALOGE("Expected SendObjectInfo before SendObject"); |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 921 | result = MTP_RESPONSE_NO_VALID_OBJECT_INFO; |
| 922 | goto done; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 923 | } |
| 924 | |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 925 | // read the header, and possibly some data |
| 926 | ret = mData.read(mFD); |
| 927 | if (ret < MTP_CONTAINER_HEADER_SIZE) { |
| 928 | result = MTP_RESPONSE_GENERAL_ERROR; |
| 929 | goto done; |
| 930 | } |
| 931 | initialData = ret - MTP_CONTAINER_HEADER_SIZE; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 932 | |
| 933 | mtp_file_range mfr; |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 934 | mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC); |
| 935 | if (mfr.fd < 0) { |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 936 | result = MTP_RESPONSE_GENERAL_ERROR; |
| 937 | goto done; |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 938 | } |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 939 | fchown(mfr.fd, getuid(), mFileGroup); |
| 940 | // set permissions |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 941 | mask = umask(0); |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 942 | fchmod(mfr.fd, mFilePermission); |
| 943 | umask(mask); |
| 944 | |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 945 | if (initialData > 0) |
| 946 | ret = write(mfr.fd, mData.getData(), initialData); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 947 | |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 948 | if (mSendObjectFileSize - initialData > 0) { |
| 949 | mfr.offset = initialData; |
Mike Lockwood | 8334038 | 2011-10-13 11:38:20 -0400 | [diff] [blame] | 950 | if (mSendObjectFileSize == 0xFFFFFFFF) { |
| 951 | // tell driver to read until it receives a short packet |
| 952 | mfr.length = 0xFFFFFFFF; |
| 953 | } else { |
| 954 | mfr.length = mSendObjectFileSize - initialData; |
| 955 | } |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 956 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 957 | ALOGV("receiving %s\n", (const char *)mSendObjectFilePath); |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 958 | // transfer the file |
| 959 | ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 960 | ALOGV("MTP_RECEIVE_FILE returned %d\n", ret); |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 961 | } |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 962 | close(mfr.fd); |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 963 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 964 | if (ret < 0) { |
| 965 | unlink(mSendObjectFilePath); |
| 966 | if (errno == ECANCELED) |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 967 | result = MTP_RESPONSE_TRANSACTION_CANCELLED; |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 968 | else |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 969 | result = MTP_RESPONSE_GENERAL_ERROR; |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 970 | } |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 971 | |
| 972 | done: |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 973 | // reset so we don't attempt to send the data back |
| 974 | mData.reset(); |
| 975 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 976 | mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat, |
Mike Lockwood | 7a0bd17 | 2011-01-18 11:06:19 -0800 | [diff] [blame] | 977 | result == MTP_RESPONSE_OK); |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 978 | mSendObjectHandle = kInvalidObjectHandle; |
| 979 | mSendObjectFormat = 0; |
| 980 | return result; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 981 | } |
| 982 | |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 983 | static void deleteRecursive(const char* path) { |
| 984 | char pathbuf[PATH_MAX]; |
| 985 | int pathLength = strlen(path); |
| 986 | if (pathLength >= sizeof(pathbuf) - 1) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 987 | ALOGE("path too long: %s\n", path); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 988 | } |
| 989 | strcpy(pathbuf, path); |
| 990 | if (pathbuf[pathLength - 1] != '/') { |
| 991 | pathbuf[pathLength++] = '/'; |
| 992 | } |
| 993 | char* fileSpot = pathbuf + pathLength; |
| 994 | int pathRemaining = sizeof(pathbuf) - pathLength - 1; |
| 995 | |
| 996 | DIR* dir = opendir(path); |
| 997 | if (!dir) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 998 | ALOGE("opendir %s failed: %s", path, strerror(errno)); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | struct dirent* entry; |
| 1003 | while ((entry = readdir(dir))) { |
| 1004 | const char* name = entry->d_name; |
| 1005 | |
| 1006 | // ignore "." and ".." |
| 1007 | if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
| 1011 | int nameLength = strlen(name); |
| 1012 | if (nameLength > pathRemaining) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1013 | ALOGE("path %s/%s too long\n", path, name); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 1014 | continue; |
| 1015 | } |
| 1016 | strcpy(fileSpot, name); |
| 1017 | |
| 1018 | int type = entry->d_type; |
| 1019 | if (entry->d_type == DT_DIR) { |
| 1020 | deleteRecursive(pathbuf); |
| 1021 | rmdir(pathbuf); |
| 1022 | } else { |
| 1023 | unlink(pathbuf); |
| 1024 | } |
| 1025 | } |
Mike Lockwood | a108b2f | 2010-11-11 11:22:32 -0500 | [diff] [blame] | 1026 | closedir(dir); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | static void deletePath(const char* path) { |
| 1030 | struct stat statbuf; |
| 1031 | if (stat(path, &statbuf) == 0) { |
| 1032 | if (S_ISDIR(statbuf.st_mode)) { |
| 1033 | deleteRecursive(path); |
| 1034 | rmdir(path); |
| 1035 | } else { |
| 1036 | unlink(path); |
| 1037 | } |
| 1038 | } else { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1039 | ALOGE("deletePath stat failed for %s: %s", path, strerror(errno)); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1043 | MtpResponseCode MtpServer::doDeleteObject() { |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 1044 | if (!hasStorage()) |
| 1045 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1046 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 1047 | MtpObjectFormat format = mRequest.getParameter(2); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1048 | // FIXME - support deleting all objects if handle is 0xFFFFFFFF |
| 1049 | // FIXME - implement deleting objects by format |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1050 | |
| 1051 | MtpString filePath; |
| 1052 | int64_t fileLength; |
Mike Lockwood | 365e03e | 2010-12-08 16:08:01 -0800 | [diff] [blame] | 1053 | int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 1054 | if (result == MTP_RESPONSE_OK) { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1055 | ALOGV("deleting %s", (const char *)filePath); |
Mike Lockwood | 7944c1b | 2011-12-01 16:58:41 -0500 | [diff] [blame] | 1056 | result = mDatabase->deleteFile(handle); |
| 1057 | // Don't delete the actual files unless the database deletion is allowed |
| 1058 | if (result == MTP_RESPONSE_OK) { |
| 1059 | deletePath((const char *)filePath); |
| 1060 | } |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 1061 | } |
Mike Lockwood | 7944c1b | 2011-12-01 16:58:41 -0500 | [diff] [blame] | 1062 | |
| 1063 | return result; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | MtpResponseCode MtpServer::doGetObjectPropDesc() { |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 1067 | MtpObjectProperty propCode = mRequest.getParameter(1); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1068 | MtpObjectFormat format = mRequest.getParameter(2); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1069 | ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode), |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 1070 | MtpDebug::getFormatCodeName(format)); |
| 1071 | MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format); |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 1072 | if (!property) |
| 1073 | return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED; |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 1074 | property->write(mData); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 1075 | delete property; |
| 1076 | return MTP_RESPONSE_OK; |
| 1077 | } |
| 1078 | |
| 1079 | MtpResponseCode MtpServer::doGetDevicePropDesc() { |
| 1080 | MtpDeviceProperty propCode = mRequest.getParameter(1); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1081 | ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode)); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 1082 | MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode); |
| 1083 | if (!property) |
| 1084 | return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED; |
| 1085 | property->write(mData); |
| 1086 | delete property; |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 1087 | return MTP_RESPONSE_OK; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1088 | } |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 1089 | |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1090 | MtpResponseCode MtpServer::doSendPartialObject() { |
| 1091 | if (!hasStorage()) |
| 1092 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
| 1093 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 1094 | uint64_t offset = mRequest.getParameter(2); |
| 1095 | uint64_t offset2 = mRequest.getParameter(3); |
| 1096 | offset = offset | (offset2 << 32); |
| 1097 | uint32_t length = mRequest.getParameter(4); |
| 1098 | |
| 1099 | ObjectEdit* edit = getEditObject(handle); |
| 1100 | if (!edit) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1101 | ALOGE("object not open for edit in doSendPartialObject"); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1102 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1103 | } |
| 1104 | |
| 1105 | // can't start writing past the end of the file |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 1106 | if (offset > edit->mSize) { |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1107 | ALOGD("writing past end of object, offset: %lld, edit->mSize: %lld", offset, edit->mSize); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1108 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1109 | } |
| 1110 | |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 1111 | const char* filePath = (const char *)edit->mPath; |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1112 | ALOGV("receiving partial %s %lld %lld\n", filePath, offset, length); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1113 | |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 1114 | // read the header, and possibly some data |
| 1115 | int ret = mData.read(mFD); |
| 1116 | if (ret < MTP_CONTAINER_HEADER_SIZE) |
| 1117 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1118 | int initialData = ret - MTP_CONTAINER_HEADER_SIZE; |
| 1119 | |
| 1120 | if (initialData > 0) { |
| 1121 | ret = write(edit->mFD, mData.getData(), initialData); |
| 1122 | offset += initialData; |
| 1123 | length -= initialData; |
| 1124 | } |
| 1125 | |
| 1126 | if (length > 0) { |
| 1127 | mtp_file_range mfr; |
| 1128 | mfr.fd = edit->mFD; |
| 1129 | mfr.offset = offset; |
| 1130 | mfr.length = length; |
| 1131 | |
| 1132 | // transfer the file |
| 1133 | ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr); |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1134 | ALOGV("MTP_RECEIVE_FILE returned %d", ret); |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 1135 | } |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1136 | if (ret < 0) { |
| 1137 | mResponse.setParameter(1, 0); |
| 1138 | if (errno == ECANCELED) |
| 1139 | return MTP_RESPONSE_TRANSACTION_CANCELLED; |
| 1140 | else |
| 1141 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1142 | } |
Mike Lockwood | de6d73a | 2011-07-14 21:00:02 -0400 | [diff] [blame] | 1143 | |
| 1144 | // reset so we don't attempt to send this back |
| 1145 | mData.reset(); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1146 | mResponse.setParameter(1, length); |
| 1147 | uint64_t end = offset + length; |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 1148 | if (end > edit->mSize) { |
| 1149 | edit->mSize = end; |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1150 | } |
| 1151 | return MTP_RESPONSE_OK; |
| 1152 | } |
| 1153 | |
| 1154 | MtpResponseCode MtpServer::doTruncateObject() { |
| 1155 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 1156 | ObjectEdit* edit = getEditObject(handle); |
| 1157 | if (!edit) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1158 | ALOGE("object not open for edit in doTruncateObject"); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1159 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1160 | } |
| 1161 | |
| 1162 | uint64_t offset = mRequest.getParameter(2); |
| 1163 | uint64_t offset2 = mRequest.getParameter(3); |
| 1164 | offset |= (offset2 << 32); |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 1165 | if (ftruncate(edit->mFD, offset) != 0) { |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1166 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1167 | } else { |
Mike Lockwood | 1003199 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 1168 | edit->mSize = offset; |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1169 | return MTP_RESPONSE_OK; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | MtpResponseCode MtpServer::doBeginEditObject() { |
| 1174 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 1175 | if (getEditObject(handle)) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1176 | ALOGE("object already open for edit in doBeginEditObject"); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1177 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1178 | } |
| 1179 | |
| 1180 | MtpString path; |
| 1181 | int64_t fileLength; |
| 1182 | MtpObjectFormat format; |
| 1183 | int result = mDatabase->getObjectFilePath(handle, path, fileLength, format); |
| 1184 | if (result != MTP_RESPONSE_OK) |
| 1185 | return result; |
| 1186 | |
| 1187 | int fd = open((const char *)path, O_RDWR | O_EXCL); |
| 1188 | if (fd < 0) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1189 | ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1190 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1191 | } |
| 1192 | |
| 1193 | addEditObject(handle, path, fileLength, format, fd); |
| 1194 | return MTP_RESPONSE_OK; |
| 1195 | } |
| 1196 | |
| 1197 | MtpResponseCode MtpServer::doEndEditObject() { |
| 1198 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 1199 | ObjectEdit* edit = getEditObject(handle); |
| 1200 | if (!edit) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1201 | ALOGE("object not open for edit in doEndEditObject"); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 1202 | return MTP_RESPONSE_GENERAL_ERROR; |
| 1203 | } |
| 1204 | |
| 1205 | commitEdit(edit); |
| 1206 | removeEditObject(handle); |
| 1207 | return MTP_RESPONSE_OK; |
| 1208 | } |
| 1209 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 1210 | } // namespace android |