1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "MtpClient.h"
#include "MtpDevice.h"
#include "MtpObjectInfo.h"
#include "MtpFile.h"
#define PROMPT "mtp> "
using namespace android;
static MtpClient* sClient = NULL;
// current working directory information for interactive shell
static MtpFile* sCurrentDirectory = NULL;
static MtpFile* parse_path(char* path) {
return MtpFile::parsePath(sCurrentDirectory, path);
}
class MyClient : public MtpClient {
private:
virtual void deviceAdded(MtpDevice *device) {
}
virtual void deviceRemoved(MtpDevice *device) {
}
public:
};
static void init() {
sClient = new MyClient;
sClient->start();
MtpFile::init(sClient);
}
static int set_cwd(int argc, char* argv[]) {
if (argc != 1) {
fprintf(stderr, "cd should have one argument\n");
return -1;
}
if (!strcmp(argv[0], "/")) {
delete sCurrentDirectory;
sCurrentDirectory = NULL;
}
else {
MtpFile* file = parse_path(argv[0]);
if (file) {
delete sCurrentDirectory;
sCurrentDirectory = file;
} else {
fprintf(stderr, "could not find %s\n", argv[0]);
return -1;
}
}
return 0;
}
static void list_devices() {
// TODO - need to make sure the list will not change while iterating
MtpDeviceList& devices = sClient->getDeviceList();
for (int i = 0; i < devices.size(); i++) {
MtpDevice* device = devices[i];
MtpFile* file = new MtpFile(device);
file->print();
delete file;
}
}
static int list(int argc, char* argv[]) {
if (argc == 0) {
// list cwd
if (sCurrentDirectory) {
sCurrentDirectory->list();
} else {
list_devices();
}
}
for (int i = 0; i < argc; i++) {
char* path = argv[i];
if (!strcmp(path, "/")) {
list_devices();
} else {
MtpFile* file = parse_path(path);
if (!file) {
fprintf(stderr, "could not find %s\n", path);
return -1;
}
file->list();
}
}
return 0;
}
static int get_file(int argc, char* argv[]) {
int ret = -1;
int srcFD = -1;
int destFD = -1;
MtpFile* srcFile = NULL;
MtpObjectInfo* info = NULL;
char* dest;
if (argc < 1) {
fprintf(stderr, "not enough arguments\n");
return -1;
} else if (argc > 2) {
fprintf(stderr, "too many arguments\n");
return -1;
}
// find source object
char* src = argv[0];
srcFile = parse_path(src);
if (!srcFile) {
fprintf(stderr, "could not find %s\n", src);
return -1;
}
info = srcFile->getObjectInfo();
if (!info) {
fprintf(stderr, "could not find object info for %s\n", src);
goto fail;
}
if (info->mFormat == MTP_FORMAT_ASSOCIATION) {
fprintf(stderr, "copying directories not implemented yet\n");
goto fail;
}
dest = (argc > 1 ? argv[1] : info->mName);
if (srcFile->getDevice()->readObject(info->mHandle, dest))
ret = 0;
fail:
delete srcFile;
delete info;
return ret;
}
static int put_file(int argc, char* argv[]) {
int ret = -1;
int srcFD = -1;
MtpFile* destFile = NULL;
MtpObjectInfo* srcInfo = NULL;
MtpObjectInfo* destInfo = NULL;
MtpObjectHandle handle;
struct stat statbuf;
const char* lastSlash;
if (argc < 1) {
fprintf(stderr, "not enough arguments\n");
return -1;
} else if (argc > 2) {
fprintf(stderr, "too many arguments\n");
return -1;
}
const char* src = argv[0];
srcFD = open(src, O_RDONLY);
if (srcFD < 0) {
fprintf(stderr, "could not open %s\n", src);
goto fail;
}
if (argc == 2) {
char* dest = argv[1];
destFile = parse_path(dest);
if (!destFile) {
fprintf(stderr, "could not find %s\n", dest);
goto fail;
}
} else {
if (!sCurrentDirectory) {
fprintf(stderr, "current working directory not set\n");
goto fail;
}
destFile = new MtpFile(sCurrentDirectory);
}
destInfo = destFile->getObjectInfo();
if (!destInfo) {
fprintf(stderr, "could not find object info destination directory\n");
goto fail;
}
if (destInfo->mFormat != MTP_FORMAT_ASSOCIATION) {
fprintf(stderr, "destination not a directory\n");
goto fail;
}
if (fstat(srcFD, &statbuf))
goto fail;
srcInfo = new MtpObjectInfo(0);
srcInfo->mStorageID = destInfo->mStorageID;
srcInfo->mFormat = MTP_FORMAT_EXIF_JPEG; // FIXME
srcInfo->mCompressedSize = statbuf.st_size;
srcInfo->mParent = destInfo->mHandle;
lastSlash = strrchr(src, '/');
srcInfo->mName = strdup(lastSlash ? lastSlash + 1 : src);
srcInfo->mDateModified = statbuf.st_mtime;
handle = destFile->getDevice()->sendObjectInfo(srcInfo);
if (handle <= 0) {
printf("sendObjectInfo returned %04X\n", handle);
goto fail;
}
if (destFile->getDevice()->sendObject(srcInfo, srcFD))
ret = 0;
fail:
delete destFile;
delete srcInfo;
delete destInfo;
if (srcFD >= 0)
close(srcFD);
printf("returning %d\n", ret);
return ret;
}
typedef int (* command_func)(int argc, char* argv[]);
struct command_table_entry {
const char* name;
command_func func;
};
const command_table_entry command_list[] = {
{ "cd", set_cwd },
{ "ls", list },
{ "get", get_file },
{ "put", put_file },
{ NULL, NULL },
};
static int do_command(int argc, char* argv[]) {
const command_table_entry* command = command_list;
const char* name = *argv++;
argc--;
while (command->name) {
if (!strcmp(command->name, name))
return command->func(argc, argv);
else
command++;
}
fprintf(stderr, "unknown command %s\n", name);
return -1;
}
static int shell() {
int argc;
int result = 0;
#define MAX_ARGS 100
char* argv[MAX_ARGS];
#if HAVE_READLINE
using_history();
#endif
while (1) {
#if HAVE_READLINE
char* line = readline(PROMPT);
if (!line) {
printf("\n");
exit(0);
}
#else
char buffer[1000];
printf("%s", PROMPT);
char* line = NULL;
size_t length = 0;
buffer[0] = 0;
fgets(buffer, sizeof(buffer), stdin);
int count = strlen(buffer);
if (count > 0 && buffer[0] == (char)EOF) {
printf("\n");
exit(0);
}
if (count > 0 && line[count - 1] == '\n')
line[count - 1] == 0;
#endif
char* tok = strtok(line, " \t\n\r");
if (!tok)
continue;
if (!strcmp(tok, "quit") || !strcmp(tok, "exit")) {
exit(0);
}
#if HAVE_READLINE
add_history(line);
#endif
argc = 0;
while (tok) {
if (argc + 1 == MAX_ARGS) {
fprintf(stderr, "too many arguments\n");
result = -1;
goto bottom_of_loop;
}
argv[argc++] = strdup(tok);
tok = strtok(NULL, " \t\n\r");
}
result = do_command(argc, argv);
bottom_of_loop:
for (int i = 0; i < argc; i++)
free(argv[i]);
free(line);
}
return result;
}
int main(int argc, char* argv[]) {
init();
if (argc == 1)
return shell();
else
return do_command(argc - 1, argv + 1);
}
|