Hallo,
es geht um die While-Schleife von Zeile 453 bis 484. Nur wenn dirIterator != vfsIteratorStop
ist, soll die Schleife weiterlaufen. Sie läuft aber auch weiter, wenn beide Werte gleich sind, siehe Ausgabe unten. Auch der redundante Befehl if (dirIterator == vfsIteratorStop) break;
am Ende der Schleife ändert daran nichts.
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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | /* $Id: picsnvideos.c,v 1.8 2008/05/17 03:13:07 danbodoh Exp $ */ /******************************************************************************* * picsnvideos.c * * Copyright (C) 2008 by Dan Bodoh * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <utime.h> #include <pi-dlp.h> #include <pi-source.h> #include <pi-util.h> #include <gdbm.h> #include "libplugin.h" #define MYNAME "Pics&Videos" #define MYVERSION VERSION char *RootDirs[] = { "/DCIM", "/Photos & Videos", NULL }; char *rcsid = "$Id: picsnvideos.c,v 1.8 2008/05/17 03:13:07 danbodoh Exp $"; #define PCDIR "PalmPictures" #define DATABASE_FILE "picsnvideos-fetched.gdbm" #define UNFILED_ALBUM "Unfiled" #define LOGL1 JP_LOG_DEBUG #define LOGL2 JP_LOG_GUI #define LOGL3 JP_LOG_FATAL char *helpText = "%s %s JPilot plugin (c) 2008 by Dan Bodoh\n\ \n\ Fetches pictures and videos from the Pics&Videos\n\ application in the Palm to the directory '%s' in your.\n\ home directory.\n\ \n\ For more documentation, bug reports and new \n\ versions, see http://sourceforge.net/projects/picsnvideos."; struct PVAlbum { unsigned int volref; char root[vfsMAXFILENAME+1]; char albumName[vfsMAXFILENAME+1]; int isUnfiled; struct PVAlbum *next; }; struct PVAlbum *searchForAlbums(int, int *, int ); void fetchAlbum(int, GDBM_FILE, struct PVAlbum *); int vfsVolumeEnumerateIncludeHidden(int, int *, int *); void plugin_version(int *major_version, int *minor_version) { *major_version=0; *minor_version=99; } int plugin_get_name(char *name, int len) { strncpy(name, MYNAME, len); return 0; } int plugin_get_help_name(char *name, int len) { if (len > 6) { strcpy(name,"Help for "); strncat(name, MYNAME, len-10); } else { strncpy(name,"", len); } return 0; } int plugin_help(char **text, int *width, int *height) { *text = malloc(strlen(helpText)+strlen(MYNAME)+strlen(MYVERSION)+ +strlen(PCDIR) +20); if (*text==NULL) return 0; sprintf(*text,helpText,MYNAME,MYVERSION,PCDIR); *height = 0; *width = 0; return 0; } int plugin_startup(jp_startup_info *info) { jp_init(); return 0; } int plugin_sync(int sd) { int volrefs[16]; int volcount = 16; struct PVAlbum *albumList = NULL; char *gdbmfn; GDBM_FILE gdbmfh; jp_logf(LOGL2,"Fetching %s\n",MYNAME); jp_logf(LOGL1,"picsnvideos version %s (%s)\n",VERSION, rcsid); /* Get list of volumes on pilot. This function will find hidden * volumes, so that we also get the BUILTIN volume */ if (vfsVolumeEnumerateIncludeHidden(sd, &volcount, volrefs) < 0) { jp_logf(LOGL2,"Could not find any VFS volumes; no pictures fetched.\n", MYNAME); return -1; } /* Get list of albums on all the volumes */ albumList = searchForAlbums(sd, volrefs, volcount); jp_logf(LOGL1,"Album search finished\n"); if (albumList==NULL) { jp_logf(LOGL2, "Could not find any albums; no pictures fetched.\n"); return -1; } gdbmfn = malloc(1024); if (gdbmfn==NULL) { jp_logf(LOGL3,"Out of memory\n"); return -1; } gdbmfn[0]=0; jp_get_home_file_name(DATABASE_FILE, gdbmfn, 1023); gdbmfh = gdbm_open(gdbmfn, 0, GDBM_WRCREAT, 0600, NULL); if (gdbmfh==NULL) { jp_logf(LOGL3, "Failed to open database file '%s'\n",gdbmfn); } free(gdbmfn); /* Iterate over each album, and fetch the files in that album */ while (albumList) { jp_logf(LOGL1," Fetching '%s'\n", albumList->albumName); struct PVAlbum *tmp; fetchAlbum(sd, gdbmfh, albumList); tmp = albumList->next; free(albumList); albumList = tmp; } if (gdbmfh) { gdbm_close(gdbmfh); gdbmfh = NULL; } return 0; } /* * Return directory name on the PC where * the album should be stored. Returned string is of the form * "/home/danb/PalmPictures/Album/". Directories in * the path are created as needed. User should * free return value. Null is returned if out of memory. */ char *destinationDir(int sd, struct PVAlbum *album) { char *dst; int len; char *card; struct VFSInfo volInfo; char *home; /* Use $HOME, or current directory if it is not defined */ home = getenv("HOME"); if (home==NULL) { home = "./"; } /* Next level is indicator of which card */ if (dlp_VFSVolumeInfo(sd, album->volref, &volInfo) < 0) { jp_logf(LOGL3,"Error: Could not get volume info on volref %d\n", album->volref); return NULL; } card = malloc(16); if (card==NULL) return NULL; if (volInfo.mediaType == pi_mktag('T','F','F','S')) { strncpy(card, "Device", 16); } else if (volInfo.mediaType == pi_mktag('s','d','i','g')) { strncpy(card, "SDCard", 16); } else { sprintf(card,"card%d",volInfo.slotRefNum); } len = strlen(home) + strlen(PCDIR) + strlen(album->albumName) + strlen(card) + 10; dst = malloc(len); strcpy(dst, home); strcat(dst,"/"); strcat(dst,PCDIR); /* make PCDIR directory */ mkdir(dst, 0777); strcat(dst,"/"); strcat(dst,card); free(card); /* make card directory */ mkdir(dst, 0777); strcat(dst,"/"); strcat(dst,album->albumName); /* make album directory */ mkdir(dst, 0777); strcat(dst,"/"); return dst; } /* * Return a key for the picsandvideos-fetched database * Value must be free'd by caller */ char *fetchedDatabaseKey(struct PVAlbum *album, char *file, unsigned int size) { char *key = malloc(strlen(file) + 64); if (key==NULL) return NULL; sprintf(key,"%s:%d", file,size); return key; } void fetchFileIfNeeded(int sd, GDBM_FILE gdbmfh, struct PVAlbum *album, char *file, char *dstDir) { char *srcPath; FileRef fileRef; unsigned int filesize; int fetched = 0; datum key, val; const unsigned int buffersize = 65536; unsigned int readsize, writesize; pi_buffer_t *buffer; int errorDuringFetch = 0; srcPath = malloc(strlen(album->root)+strlen(album->albumName)+ strlen(file)+10); if (srcPath==NULL) { jp_logf(LOGL3,"Out of memory\n"); return; } if (album->isUnfiled) { sprintf(srcPath,"%s/%s",album->root,file); } else { sprintf(srcPath,"%s/%s/%s",album->root,album->albumName,file); } if (dlp_VFSFileOpen(sd, album->volref, srcPath, vfsModeRead, &fileRef)<=0) { jp_logf(LOGL2,"Could not open file '%s' on volume %d\n", srcPath, album->volref); free(srcPath); return; } if (dlp_VFSFileSize(sd, fileRef, (int *)(&filesize)) < 0) { jp_logf(LOGL2,"Could not get file size '%s' on volume %d\n", srcPath, album->volref); free(srcPath); return; } free(srcPath); key.dptr = fetchedDatabaseKey(album, file, (unsigned int)filesize); key.dsize = strlen(key.dptr); if (key.dptr==NULL) { jp_logf(LOGL3,"Out of memory\n"); return; } /* If file has not already been downloaded, fetch it */ if (! gdbm_exists(gdbmfh, key)) { char *dstfile; FILE *fp; dstfile = malloc(strlen(dstDir)+strlen(file)+10); if (dstfile==NULL) { jp_logf(LOGL3,"Out of memory\n"); return; } strcpy(dstfile,dstDir); strcat(dstfile,file); jp_logf(LOGL2," Fetching %s...\n",dstfile); fp = fopen(dstfile,"w"); if (fp==NULL) { jp_logf(LOGL3,"Cannot open %s for writing!\n",dstfile); free(dstfile); return; } /* This copy code is based on pilot-xfer.c by Kenneth Albanowski */ buffer = pi_buffer_new(buffersize); readsize = 0; while ((filesize > 0) && (readsize >=0) && ! errorDuringFetch) { int offset; pi_buffer_clear(buffer); readsize = dlp_VFSFileRead(sd, fileRef, buffer, ( filesize > buffersize ? buffersize : filesize)); if (readsize <= 0 ) { jp_logf(LOGL3,"File read error; aborting\n"); errorDuringFetch = 1; break; } filesize -= readsize; offset = 0; while (readsize > 0) { writesize = fwrite(buffer->data+offset, 1, readsize, fp); if (writesize < 0) { jp_logf(LOGL3,"File write error; aborting\n"); errorDuringFetch = 1; break; } readsize -= writesize; offset += writesize; } } fclose(fp); if (errorDuringFetch) { /* remove the partially created file */ unlink(dstfile); } else { int status; time_t date; fetched = 1; #ifdef HAVE_UTIME /* Get the date that the picture was created */ status = dlp_VFSFileGetDate(sd, fileRef,vfsFileDateCreated ,&date); /* And set the destination file mod time to that date */ if (status < 0) { jp_logf(LOGL2, "WARNING: Cannot get file date\n"); } else { struct utimbuf t; t.actime = date; t.modtime = date; if (utime(dstfile, &t)!=0) { jp_logf(LOGL2, "WARNING: Cannot set file date\n"); } } #endif // HAVE_UTIME } free(dstfile); } else { jp_logf(LOGL1," key '%s' exists, not copying file\n", key.dptr); } dlp_VFSFileClose(sd, fileRef); /* inform database that file has been fetched */ if (fetched) { int rv; val.dptr = ""; val.dsize = 1; rv = gdbm_store(gdbmfh, key, val, GDBM_REPLACE); } free(key.dptr); } /* * Return a list of albums on all the volumes in volrefs * */ struct PVAlbum *searchForAlbums(int sd, int *volrefs, int volcount) { FileRef dirRef; unsigned long dirIterator; int maxDirItems = 1024; struct VFSDirInfo *dirInfo; int i, volumeIndex; struct PVAlbum *albumList = NULL; int rootIndex = 0; struct PVAlbum *newAlbum; while (RootDirs[rootIndex] != NULL) { for (volumeIndex = 0; volumeIndex < volcount; volumeIndex++) { int volref = volrefs[volumeIndex]; if (dlp_VFSFileOpen(sd, volref, RootDirs[rootIndex], vfsModeRead, &dirRef)<=0) { jp_logf(LOGL1," Root %s does not exist on volume %d\n", RootDirs[rootIndex], volref); continue; } jp_logf(LOGL1," Opened root %s on volume %d\n", RootDirs[rootIndex], volref); dirInfo = malloc(maxDirItems * sizeof(struct VFSDirInfo)); if (dirInfo==NULL) { jp_logf(LOGL3,"Out of memory\n"); return NULL; } /* Add the unfiled album, which is simply the root dir. * Apparently the Treo 650 can store pics in the root dir, * as well as the album dirs */ newAlbum = malloc(sizeof(struct PVAlbum)); if (newAlbum==NULL) { jp_logf(LOGL3,"Out of memory\n"); return NULL; } newAlbum->next = albumList; albumList = newAlbum; newAlbum->albumName[0]=0; strncpy(newAlbum->albumName,UNFILED_ALBUM, vfsMAXFILENAME); strncpy(newAlbum->root,RootDirs[rootIndex], vfsMAXFILENAME); newAlbum->volref = volref; newAlbum->isUnfiled = 1; /* Iterate through the root directory looking for things * that might be albums */ dirIterator = vfsIteratorStart; jp_logf(LOGL1,"vfsIteratorStart '%d' vfsIteratorStop '%d'\n", vfsIteratorStart, vfsIteratorStop); while (dirIterator != vfsIteratorStop) { dlp_VFSDirEntryEnumerate(sd, dirRef, &dirIterator, &maxDirItems, dirInfo); jp_logf(LOGL1," iterator '%d' maxDirItems '%d'\n", dirIterator, maxDirItems); for (i=0; i<maxDirItems; i++) { jp_logf(LOGL1," Album '%d' '%s'\n", i, dirInfo[i].name); // treo 650 has #Thumbnail dir that is not an album if (strcmp(dirInfo[i].name,"#Thumbnail")==0) continue; if (dirInfo[i].attr & vfsFileAttrDirectory) { newAlbum = malloc(sizeof(struct PVAlbum)); if (newAlbum==NULL) { jp_logf(LOGL3,"Out of memory\n"); return NULL; } /* Add a new album to growing list */ newAlbum->next = albumList; albumList = newAlbum; strncpy(newAlbum->albumName,dirInfo[i].name, vfsMAXFILENAME); strncpy(newAlbum->root,RootDirs[rootIndex], vfsMAXFILENAME); newAlbum->volref = volref; newAlbum->isUnfiled = 0; jp_logf(LOGL1," Found album '%s'\n", newAlbum->albumName); } } printf(" iterator '%d' vfsIteratorStop '%d' maxDirItems '%d'\n", dirIterator, vfsIteratorStop, maxDirItems); if (dirIterator == vfsIteratorStop) break; } free(dirInfo); dlp_VFSFileClose(sd, dirRef); } ++rootIndex; } return albumList; } /* * Fetch the contents of one album * */ void fetchAlbum(int sd, GDBM_FILE gdbmfh, struct PVAlbum *album) { int maxDirItems = 1024; struct VFSDirInfo *dirInfo; char *srcAlbumDir; char *dstAlbumDir; unsigned long dirIterator; FileRef dirRef; jp_logf(LOGL2," Searching album '%s' on volume %d\n", album->albumName, album->volref); jp_logf(LOGL1," root=%s albumName=%s isUnfiled=%d\n", album->root, album->albumName, album->isUnfiled); srcAlbumDir = malloc(strlen(album->root)+strlen(album->albumName)+10); if (srcAlbumDir==NULL) { jp_logf(LOGL3,"Out of memory\n"); return; } /* Album is in /<root>/<albunName> */ strcpy(srcAlbumDir,album->root); /* Unfiled album is really just root dir; this happens on Treo 65O */ if (! album->isUnfiled) { strcat(srcAlbumDir,"/"); strcat(srcAlbumDir,album->albumName); } if (dlp_VFSFileOpen(sd, album->volref, srcAlbumDir, vfsModeRead, &dirRef)<=0) { jp_logf(LOGL2,"Could not open dir '%s' on volume %d\n", srcAlbumDir, album->volref); return; } dirInfo = malloc(maxDirItems * sizeof(struct VFSDirInfo)); if (dirInfo==NULL) { jp_logf(LOGL3,"Out of memory\n"); return; } dstAlbumDir = destinationDir(sd, album); if (dstAlbumDir==NULL) { jp_logf(LOGL3,"Out of memory\n"); return; } /* Iterate over all the files in the album dir, looking for * jpegs and 3gp's and 3g2's (videos) */ dirIterator = vfsIteratorStart; while (dirIterator != vfsIteratorStop) { int i; dlp_VFSDirEntryEnumerate(sd, dirRef, &dirIterator, &maxDirItems, dirInfo); for (i=0; i<maxDirItems; i++) { int fnlen = strlen(dirInfo[i].name); jp_logf(LOGL1," found file '%s' attribute %x\n", dirInfo[i].name, dirInfo[i].attr); if (fnlen < 5) continue; /* Grab only files with known extensions */ if ( /* .jpg = jpeg picture */ (strcmp(&(dirInfo[i].name[fnlen-4]), ".jpg") != 0) && /* .3gp is video (GSM phones) */ (strcmp(&(dirInfo[i].name[fnlen-4]), ".3gp") != 0) && /* .3g2 is video (CDMA phones) */ (strcmp(&(dirInfo[i].name[fnlen-4]), ".3g2") != 0) && /* .amr is audio caption (GSM phones) */ (strcmp(&(dirInfo[i].name[fnlen-4]), ".amr") != 0) && /* .qcp is audio caption (CDMA phones) */ (strcmp(&(dirInfo[i].name[fnlen-4]), ".qcp") != 0)) { continue; } /* must be a regular file, but ignore the 'archived' bit */ if ( dirInfo[i].attr & (vfsFileAttrHidden | vfsFileAttrSystem | vfsFileAttrVolumeLabel | vfsFileAttrDirectory | vfsFileAttrLink)) { continue; } fetchFileIfNeeded(sd, gdbmfh, album, dirInfo[i].name, dstAlbumDir); } } free(dirInfo); dlp_VFSFileClose(sd, dirRef); free(srcAlbumDir); free(dstAlbumDir); } /*********************************************************************** * * Function: vfsVolumeEnumerateIncludeHidden * * Summary: Drop-in replacement for dlp_VFSVolumeEnumerate(). * Attempts to include hidden volumes in the list. * Dan Bodoh, May 2, 2008 * * Parameters: sd --> Socket descriptor * volume_count <-> on input, size of volumes; on output * number of volumes on Palm * volumes <-- volume reference numbers * * Returns: Same as dlp_VFSVolumeEnumerate() * ***********************************************************************/ int vfsVolumeEnumerateIncludeHidden(int sd, int *numVols, int *volRefs) { int volenumResult; int result; int volRefsSize = *numVols; struct VFSInfo volInfo; int volRef1Found; int hiddenVolRef1Found; int i; volenumResult = dlp_VFSVolumeEnumerate(sd, numVols, volRefs); if (volenumResult <= 0) { *numVols = 0; } /* On the Centro, it appears that the first non-hidden * volRef is 2, and the hidden volRef is 1. Let's poke * around to see if there is really a volRef 1 that's * hidden from the dlp_VFSVolumeEnumerate() */ volRef1Found = 0; hiddenVolRef1Found = 0; for (i=0; i<*numVols; i++) { if (volRefs[i]==1) { volRef1Found = 1; break; } } if (volRef1Found) { return volenumResult; } else { result = dlp_VFSVolumeInfo(sd, 1, &volInfo); if (result > 0) { if (volInfo.attributes & vfsVolAttrHidden) { hiddenVolRef1Found = 1; } } } if (hiddenVolRef1Found) { ++ *numVols; if (volRefsSize >= *numVols) { volRefs[*numVols - 1] = 1; } if (volenumResult <= 0) return 4; /* fake dlp_VFSVolumeEnumerate() return val with 1 volume */ else return volenumResult; } return volenumResult; } |
Opened root /DCIM on volume 2 vfsIteratorStart '0' vfsIteratorStop '-1' iterator '-1' maxDirItems '1' Album '0' '137DSCIM' Found album '137DSCIM' iterator '-1' vfsIteratorStop '-1' maxDirItems '1' iterator '-1' maxDirItems '0' iterator '-1' vfsIteratorStop '-1' maxDirItems '0' iterator '-1' maxDirItems '0' iterator '-1' vfsIteratorStop '-1' maxDirItems '0' iterator '-1' maxDirItems '0' iterator '-1' vfsIteratorStop '-1' maxDirItems '0' iterator '-1' maxDirItems '0' [.....]
Irgend wie merkwürdig. Sind die beiden Werte etwa doch verschieden, und nur printf
stellt sie gleich dar?