/
update_directory_listing.c
543 lines (438 loc) · 15.8 KB
/
update_directory_listing.c
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include "ops-linux.h"
static gboolean WaitMediaStatus(void) {
// Another experimental function
unsigned int cmd;
char data[10];
return TRUE; //can't seem to get this to ensure media is available
//maybe it does something else
cmd=0xb600;
if(ControlMessageRead(cmd,data,10,TIMEOUT)) {
return TRUE;
}
return FALSE;
}
typedef struct cvs_dir_entry {
unsigned char unknown1[4]; // 0 if 1st byte 0x0f, no more files
char filename[12]; // 4 MS-DOS style, but with explicit '.'
unsigned char filetype; // 16 0x00==regular, 0x77==directory
unsigned char unknown2; // 17
unsigned char fileattr; // 18 0x20==regular, 0x10==directory
unsigned char unknown3; // 19
unsigned filesize; // 20 little-endian
unsigned short modtime; // 24 crazy DOS time
unsigned short moddate; // 26 crazy DOS date
// 28 (this is a 28-byte struct)
} cvs_dir_entry;
// this function assumes that you've already changed to the right partition
// and directory
gboolean GetAnyFileInfo(const char* filename, file_info *thisfileinfo) {
cvs_dir_entry data;
char tempstring[STRINGSIZE];
Log(DEBUGGING, "GetAnyFileInfo start");
memset(&data, 0, sizeof data);
strncpy(tempstring, filename, STRINGSIZE);
// first we set the filename
if(ControlMessageWrite(0xb101, (char*)tempstring, strlen(filename)+1, TIMEOUT)==FALSE) { //SetFileName
Log(ERROR, "failed to set filename");
return FALSE;
}
ControlMessageWrite(0xb901, (char *)&data, 0, TIMEOUT);
if(Read((char*)&data,28,TIMEOUT)<28) {
Log(ERROR, "failed to retrieve file information");
return FALSE;
}
Log(DEBUGGING, "success in Read in GetAnyFileInfo");
// filename may have trailing 0xff garbage
char *cp = memchr(data.filename, '\xff', 12);
if(!cp)
cp = memchr(data.filename, '\0', 12);
if(!cp)
cp = data.filename + 12;
size_t len = cp - data.filename;
memcpy(thisfileinfo->filename, data.filename, len);
data.filename[len] = '\0';
thisfileinfo->filesize = le32_to_cpu(data.filesize);
thisfileinfo->filetype = FIFILE;
if(data.fileattr==0x10)
thisfileinfo->filetype = FIDIR;
Log(DEBUGGING, "success in GetAnyFileInfo");
return TRUE;
}
gboolean GetFileInfo(file_info* thisfileinfo, gboolean isfirstfile) {
cvs_dir_entry data;
int dummy,command,s,t;
memset(&data, 0, sizeof data);
if(isfirstfile==TRUE)
command=0xbc00; //first file
else
if(isfirstfile==FALSE)
command=0xbd00; //next file
//sometimes the first-file read fails after a partition change
for(t=0;t<2;t++) {
if(ControlMessageWrite(command,(char*)&dummy,0,TIMEOUT)==FALSE) {
Log(ERROR, "failed at 0xbc (requesting first file info).");
return FALSE;
}
if(((s=Read((char*)&data,28,TIMEOUT)))<28) {
if(isfirstfile == FALSE)
break;
Log(ERROR, "failed to bulk read first file info...not retrying");
//Sleep(150);
continue;
}
break;
}
if(s!=28) {
Log(WARNING, "failed to bulk read file info");
return FALSE;
}
if(data.unknown1[0]==0x0f) { //we've already read the last file
Log(DEBUGGING, "Last file.");
return FALSE;
}
{
// filename can, oddly, be padded with 0xff
char *cp = memchr(data.filename, 0xff, sizeof data.filename);
if(cp)
*cp = '\0';
memcpy(thisfileinfo->filename, data.filename, sizeof data.filename);
thisfileinfo->filename[sizeof data.filename] = '\0';
}
thisfileinfo->filesize = le32_to_cpu(data.filesize);
switch(data.fileattr){
case 0x10:
thisfileinfo->filetype = FIDIR;
break;
default:
Log(WARNING, "unknown file type: 0x%02x attr 0x%02x", data.filetype, data.fileattr);
case 0x20:
thisfileinfo->filetype = FIFILE;
break;
}
return TRUE;
}
gboolean ChangePartition(unsigned int partition) {
int dummy=0x00;
unsigned int cmd;
if( (partition==1) || (partition>4)) {
Log(ERROR, "refusing to change to non-filesystem partition.");
return FALSE;
}
cmd=0xbf00 | partition;
ControlMessageWrite(cmd,(char*)&dummy,4,SHORT_TIMEOUT); // This cmd always fails!!
if(WaitMediaStatus()==FALSE)
return FALSE;
return TRUE;
}
static gboolean rTrim(char * c, const char e) {
//erase all chars of value e to the right of c
char* d;
if (strlen(c) == 0) return TRUE;
d = c;
while (*d++);
--d;
while (d != c) {
if (e == *d) {
*d = '\0';
} else {
return TRUE;
}
}
return TRUE;
}
gboolean ChangeDirectory(const char* d) {
Log(DEBUGGING, "ChangeDirectory \"%s\"", d);
char data[LIBUSB_PATH_MAX];
int c;
char directory_p[LIBUSB_PATH_MAX], *directory;
directory = directory_p;
strncpy(directory, d, LIBUSB_PATH_MAX - 1);
//the following breaks the CD into seperate directory paths...
//I found the complex path support in the camcorder a bit flakey.
if((strlen(directory) > 1) && (directory[0] == '/')) {
if(ChangeDirectory("/")==FALSE)
return FALSE;
//directory=directory.Mid(1,LIBUSB_PATH_MAX); //strip off leading /
directory++;
rTrim(directory, '/');
// directory.TrimRight("/"); //strip off trailing /
}
while( strlen(directory)>1 && directory[0]=='/' ) { //FIXME
char temp[LIBUSB_PATH_MAX];
strncpy(temp, directory, LIBUSB_PATH_MAX - 1);
if (directory[0]=='/')
directory[0] = '\0';
if(ChangeDirectory(temp)==FALSE) {
Log(ERROR, "failed to change directory to parent directory %s",temp);
return FALSE;
}
strncpy(temp, directory, LIBUSB_PATH_MAX - 1);
directory = temp + 1;
//ptemp[strlen(ptemp) - 1] = '\0';
rTrim(directory, '/');
// directory=directory.Mid(directory.Find('/')+1,LIBUSB_PATH_MAX);
// directory.TrimRight("/");
}
//We're at a single level directory at this point... lets validate it
if(strlen(directory) == 0)
return TRUE;
c=directory[0];
if(! ( ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z')) ||
((c>='0')&&(c<='9')) || (c=='_') || c=='/') ) {
Log(ERROR, "//not liking the first character of this directory!");
//the camcorder's CD command created directories if they're not
//there... best to be prudent and assume it's some kind of error.
return FALSE;
}
//validation is done... we can procede with the actual command stuff.
memset(&data,0x00,LIBUSB_PATH_MAX);
strcpy(data,directory);
if(ControlMessageWrite(0xb800,data, strlen(data)+1,TIMEOUT)==TRUE) {
return TRUE;
}
Log(ERROR, "change directory to %s failed", directory);
return FALSE;
}
static void AddFileDataAsChild(file_info* temp, file_info* addeditem) {
if (temp == NULL || addeditem == NULL)
return;
if (temp->number_of_children < MAX_NUMBER_OF_FILES_IN_DIRECTORY) {
temp->children[temp->number_of_children] = addeditem;
(temp->number_of_children)++;
}
}
static file_info* AddFileDataToList(file_info* temp) {
//note: returns malloc'd data
file_info* pointer = malloc(sizeof(file_info));
int count;
// Log("begin AddFileDataToList");
strcpy(pointer->filename, temp->filename);
strcpy(pointer->fullpath, temp->fullpath);
strcpy(pointer->dirpath, temp->dirpath);
pointer->filesize = temp->filesize;
pointer->partition = temp->partition;
pointer->filetype = temp->filetype;
pointer->number_of_children = temp->number_of_children;
for (count = 0; count < temp->number_of_children; ++count) {
pointer->children[count] = AddFileDataToList(temp->children[count]);
}
//Log("End AddFileDataToList");
return pointer;
}
static void FreeAllocatedFiles(file_info* temp) {
int count;
if (temp == NULL) return;
for (count = 0; count < temp->number_of_children; ++count) {
free(temp->children[count]);
}
temp->number_of_children = 0;
}
static file_info* AddToTreeStore(GtkTreeStore* treestore, GtkTreeIter* toplevel, GtkTreeIter* child, file_info* current, file_info* parent) {
file_info* addeditem;
GdkPixbuf* pixbuf = NULL;
gtk_tree_store_append(GTK_TREE_STORE(treestore), child, toplevel); //child is created here
addeditem = AddFileDataToList(current);
AddFileDataAsChild(parent, addeditem);
switch (addeditem->filetype) {
case FIFILE:
{
pixbuf = icon_binfile;
char *cp = strchr(addeditem->filename, '.');
if(!cp) break;
cp++;
if(!strcasecmp(cp,"avi")) pixbuf = icon_avifile;
else if(!strcasecmp(cp,"jpg")) pixbuf = icon_jpgfile;
else if(!strcasecmp(cp,"txt")) pixbuf = icon_txtfile;
else if(!strcasecmp(cp,"wav")) pixbuf = icon_wavfile;
else if(!strcasecmp(cp,"zbm")) pixbuf = icon_zbmfile;
else if(!strcasecmp(cp,"avi")) pixbuf = icon_avifile;
}
break;
case FIDIR:
pixbuf = icon_blankdir;
break;
case FIPART:
pixbuf = icon_blankdir;
break;
case FIROOT:
pixbuf = icon_blankdir;
break;
default:
break;
};
gtk_tree_store_set(GTK_TREE_STORE(treestore), child, COL_ICON, pixbuf, COL_FILENAME, current->filename, COL_POINTER, addeditem, -1);
return addeditem;
}
static void RecursiveListing(const char* parentpath, file_info* parent, GtkTreeIter* parent_place, int partition, int level, GtkTreeStore* treestore) {
gboolean firstitem;
file_info tData, pData;
int count;
// const char* parentpath = parentpath_cs.text;
int t;
GtkTreeIter child;
// file_info hitems[999];
char recursedir[STRINGSIZE];
char tempstring[STRINGSIZE];
file_info* f_i;
int total_getfileinfo_calls = 0;
firstitem = TRUE;
Log(DEBUGGING, "RecursiveListing: %s", parentpath);
if (level == 5) return; //recursive runaway!!! lookout!!!
//for (t=0; t < 1000; ++t) {
// hItem[t].number_of_children = 0; //only parameter that would really get you in trouble
// }
//NOTE: We take 2 passes at the directory, due to the way the
//camcorder directory listing works (doesn't lend itself to easy
//recursion.
for(t=0;t<1000;t++) { //don't bitch. 1000 items in one directory is a lot.
Log(DEBUGGING, "Getfileinfo?");
if(GetFileInfo(&tData, firstitem)==FALSE)
break; //we've passed the last item.
++total_getfileinfo_calls;
Log(DEBUGGING, "Getfileinfo.");
firstitem=FALSE;
if( (strcmp(tData.filename,".") == 0) ||
(strcmp(tData.filename,"..") == 0) ||
(tData.filename[0]==0x20) ) { //0x20 == space bar?
t--;
continue;
}
strcpy(pData.filename,tData.filename);
pData.filesize = tData.filesize;
pData.filetype = tData.filetype;
pData.number_of_children = 0;
strcpy(pData.dirpath, parentpath);
strcpy(pData.fullpath, parentpath);
if (strlen(tData.filename) + strlen(pData.fullpath) < STRINGSIZE - 1)
strcat(pData.fullpath, tData.filename);
pData.partition=partition;
Log(DEBUGGING, "pData.fullpath: %s", pData.fullpath);
f_i = AddToTreeStore(GTK_TREE_STORE(treestore), parent_place, &child, &pData, parent);
//from here on in f_i and pData should contain the same info (at different memory locations)
//also, AddToTreeStore stores data elsewhere so don't delete f_i
if(pData.filetype==FIDIR) {
Log(DEBUGGING, "if (pData.filetype == FIDIR)");
strncpy (recursedir, pData.fullpath, STRINGSIZE - 2);
strcat (recursedir, "/");
if (ChangeDirectory(pData.fullpath) == FALSE) {
Log(ERROR, "Couldn't recurse into %s", pData.fullpath);
return;
}
// RecursiveListing(recursedir, f_i);
//hItem[t] = m_directory_tree.InsertItem(pData->filename,0,0,parent);
//Log("enter recursion");
//RecursiveListing(NULL, NULL, NULL, 0, 0, NULL);
Log(DEBUGGING, "RecursiveListing recursion.");
RecursiveListing(recursedir, f_i, &child, partition, level + 1, GTK_TREE_STORE(treestore));
// strncpy(tempstring, f_i->dirpath, STRINGSIZE - 2); //really hoping dirpath is this dir
// strcat(tempstring, "/");
if (ChangeDirectory(pData.dirpath) == FALSE) {
Log(ERROR, "couldn't change back to current directory after recursion");
return;
}
if (firstitem == TRUE) firstitem = FALSE;
GetFileInfo(&pData, TRUE);
for (count = 1; count < total_getfileinfo_calls; ++count) {
GetFileInfo(&pData, FALSE);
}
//Log("end recursion");
} else {
//AddToTreeStore(treestore, parent_place, &child, &pData, parent);
Log(DEBUGGING, "Not a directory: %s", pData.fullpath);
//Log("that point");
//hItem[t] = m_directory_tree.InsertItem(pData->filename,2,2,parent);
}
//SetItemData(hItem[t],(DWORD)pData);;
}
//end = t;
//return; //debug... don't recurse
/*for(t=0;t<end;t++) {
//if(hItem[t]==0)
// return;
rData=(FILE_INFO *)m_directory_tree.GetItemData(hItem[t]);
if(rData->filetype==FIDIR)
{
CString recursedir=rData->fullpath + "/";
if(m_camcorder.ChangeDirectory(rData->fullpath)==false)
{
Log("Couldn't recurse into "+rData->fullpath);
return;
}
RecursiveListing(recursedir,hItem[t],partition, level+1);
}
}
*/
}
static GtkTreeModel* create_model(void) {
GtkTreeStore* treestore = gtk_tree_store_new(3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_POINTER);
GtkTreeIter rootlevel, toplevel;
int t;
file_info root_file_data, current_file_data, *addeditem = NULL;
char tempstring[STRINGSIZE];
// EnableControls(FALSE);
strcpy(root_file_data.filename, "/");
root_file_data.filesize = 0;
root_file_data.filetype = FIROOT;
strcpy(root_file_data.fullpath, "");
root_file_data.partition = 255;
root_file_data.number_of_children = 0;
AddToTreeStore(treestore, NULL, &rootlevel, &root_file_data, NULL);
/* gtk_tree_store_append(treestore, &rootlevel, NULL); //create blank entry
root_directory = AddFileDataToList(&root_file_data);
addeditem = root_directory;
gtk_tree_store_set(treestore, &rootlevel, COL_FILENAME, root_file_data.filename, COL_POINTER, addeditem, -1);*/
for (t=0; t<5; ++t) {
if (t == 1) continue; //skip the non-filesystem partition
snprintf(tempstring, STRINGSIZE-1, "p%d",t); //partition directory name
strcpy(current_file_data.filename, tempstring);
current_file_data.filesize = 0;
strcpy(current_file_data.fullpath, "/");
strcpy(current_file_data.dirpath, "/");
current_file_data.partition = t;
current_file_data.filetype = FIPART;
current_file_data.number_of_children = 0;
strcpy(tempstring, "add filename: ");
if (strlen(tempstring) + strlen(current_file_data.filename) < STRINGSIZE)
strcat(tempstring, current_file_data.filename);
// Log("add filename:");
Log(DEBUGGING, "current_file_data.filename: %s", current_file_data.filename);
AddToTreeStore(treestore, &rootlevel, &toplevel, ¤t_file_data, &root_file_data);
if (ChangePartition(t) == FALSE) {
EnableControls(TRUE);
return NULL;
}
if(ChangeDirectory("/")==FALSE) {
EnableControls(TRUE);
return NULL;
}
//RecursiveListing("/",addeditem,t);
// strcpy(tempstring, "/");
RecursiveListing("/", addeditem, &toplevel, t, 0, treestore);
//m_directory_tree.Expand(hItem[t],TVE_EXPAND);//hmm?
//TODO: map filename data to some data in memory
}
//m_directory_tree.Expand(rItem,TVE_EXPAND);
ChangePartition(0); //make sure we're back at the media partition!!!
ChangeDirectory("/DCIM");
//Log();
EnableControls(TRUE);
return GTK_TREE_MODEL(treestore);
}
gboolean update_directory_listing (GtkWidget *widget,
GdkEvent *event,
gpointer data) {
// HTREEITEM hItem[5],rItem;
GtkTreeModel* model;
if(CheckCameraOpen()==FALSE)
return FALSE;
EnableControls(FALSE);
model = create_model();
Log(DEBUGGING, "model created.");
FreeAllocatedFiles(root_directory);
root_directory = NULL;
gtk_tree_view_set_model(GTK_TREE_VIEW(m_directory_tree), GTK_TREE_MODEL(model));
gtk_tree_view_expand_all(GTK_TREE_VIEW(m_directory_tree));
Log(NOTICE, "directory tree created successfully");
EnableControls(TRUE);
return TRUE;
}