forked from danbodoh/picsnvideos-jpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.c
1111 lines (1038 loc) · 53.1 KB
/
media.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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* media.c
*
* Copyright (C) 2008 by Dan Bodoh
* Copyright (C) 2022 by Ulf Zibis <[email protected]>
*
* 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/param.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 "libplugin.h"
//#include "i18n.h"
#define MYNAME PACKAGE_NAME
#define PCDIR MYNAME
#define PREFS_VERSION 3
#define ADDITIONAL_FILES "/#AdditionalFiles"
#define L_DEBUG JP_LOG_DEBUG
#define L_INFO JP_LOG_WARN // JP_LOG_INFO unfortunately doesn't show up in GUI, so use JP_LOG_WARN.
#define L_WARN JP_LOG_WARN
#define L_FATAL JP_LOG_FATAL
#define L_GUI JP_LOG_GUI
typedef struct VFSInfo VFSInfo;
typedef struct VFSDirInfo VFSDirInfo;
typedef struct fullPath {int volRef; char *name; struct fullPath *next;} fullPath;
static const char HELP_TEXT[] =
"JPilot plugin, version: "VERSION"\n\
(c) 2008 by Dan Bodoh\n\
(c) 2022 by Ulf Zibis <[email protected]>\n\
\n\
Synchronizes media as pictures, videos and audios from\n\
the Pics&Videos storage and SDCard in the Palm with\n\
folder '"PCDIR"' in your JPilot data directory,\n\
usually \"$JPILOT_HOME/.jpilot\".\n\
\n\
For more documentation, bug reports and new versions,\n\
see https://github.com/CoSoCo/JPilotMediaPlugin";
static const char *PREFS_FILE = PACKAGE".rc";
static prefType prefs[] = {
{"prefsVersion", INTTYPE, INTTYPE, PREFS_VERSION, NULL, 0},
{"rootDirs", CHARTYPE, CHARTYPE, 0, "1>/Photos & Videos:1>/Fotos & Videos:/DCIM", 0},
{"syncThumbnailDir", INTTYPE, INTTYPE, 0, NULL, 0},
// JPEG picture
// video (GSM phones)
// video (CDMA phones)
// audio caption (GSM phones)
// audio caption (CDMA phones)
{"fileTypes", CHARTYPE, CHARTYPE, 0, "jpg:amr:qcp:3gp:3g2:avi", 0},
{"useDateModified", INTTYPE, INTTYPE, 0, NULL, 0},
{"compareContent", INTTYPE, INTTYPE, 0, NULL, 0},
{"doBackup", INTTYPE, INTTYPE, 1, NULL, 0},
{"doRestore", INTTYPE, INTTYPE, 1, NULL, 0},
{"listFiles", INTTYPE, INTTYPE, 0, NULL, 0},
{"excludeDirs", CHARTYPE, CHARTYPE, 0, "/BLAZER:2>/PALM/Launcher", 0},
{"deleteFiles", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"additionalFiles", CHARTYPE, CHARTYPE, 0, NULL, 0}
};
static const unsigned NUM_PREFS = sizeof(prefs)/sizeof(prefType);
static long prefsVersion;
static char *rootDirs; // becomes freed by jp_free_prefs()
static long syncThumbnailDir;
static char *fileTypes; // becomes freed by jp_free_prefs()
static long useDateModified;
static long compareContent;
static long doBackup;
static long doRestore;
static long listFiles;
static char *excludeDirs; // becomes freed by jp_free_prefs()
static char *deleteFiles; // becomes freed by jp_free_prefs()
static char *additionalFiles; // becomes freed by jp_free_prefs()
static const unsigned MAX_VOLUMES = 16;
static const unsigned MIN_DIR_ITEMS = 2;
static const unsigned MAX_DIR_ITEMS = 1024;
static const char *LOCALDIRS[] = {"/Internal", "/SDCard", "/Card"};
static fullPath *rootDirList = NULL;
static fullPath *fileTypeList = NULL;
static fullPath *excludeDirList = NULL;
static fullPath *deleteFileList = NULL;
static fullPath *additionalFileList = NULL;
static pi_buffer_t *piBuf, *piBuf2;
static int sd; // The central socket descriptor.
static char mediaHome[NAME_MAX];
static char syncLogEntry[128];
static int importantWarning = 0;
/* Log OOM error on malloc(). */
static void *mallocLog(size_t size) {
void *p;
if (!(p = malloc(size)))
jp_logf(L_FATAL, "%s: ERROR: Out of memory\n", MYNAME);
return p;
}
static char *errString(const int isPiErr, const int err, const int level, const char message[]) {
static char string[128];
PI_ERR piOSErr = (isPiErr && err == PI_ERR_DLP_PALMOS) ? pi_palmos_error(sd) : 0;
switch (piOSErr) {
case 10760 : message = ": Not found the file"; break;
case 10761 : message = ": The volume № is invalid;"; break;
case 10765 : message = ": Can't delete non-empty directory"; break;
case 10767 : message = ": No space left on volume"; break;
}
snprintf(string, sizeof(string), "%s%s %d%s", piOSErr ? "PalmOS " : "", level == L_FATAL ? "ERROR" : "WARNING", piOSErr ? piOSErr : err, message);
return string;
}
static PI_ERR piErrLog(const PI_ERR piErr, const int level, const int volRef, const char *rmPath,
const char *indent, const char message[], const char comment[]) {
if (piErr < 0) {
jp_logf(level, "%s: %s%s '%s' on volume %d%s\n", MYNAME, indent, errString(1, piErr, level, message), rmPath, volRef, comment);
}
return piErr;
}
void freePathList(fullPath *list) {
for (fullPath *item; (item = list);) {
list = list->next;
free(item);
}
}
int parsePaths(char *paths, fullPath **list, char *text) {
for (char *last; *paths != '\0'; *--last = '\0') {
if (!*(last = (last = strrchr(paths, ':')) ? last + 1 : paths)) {
jp_logf(L_WARN, "%s: WARNING: Empty name in %s.\n", MYNAME, text);
continue;
}
fullPath *item;
if ((item = mallocLog(sizeof(*item)))) {
char *separator = strchr(last, '>');
if (separator) {
*separator = '\0';
item->volRef = atoi(last);
item->name = separator + 1;
} else {
item->volRef = -1;
item->name = last;
}
item->next = *list;
(*list) = item;
} else {
freePathList(*list);
(*list) = NULL;
return EXIT_FAILURE;
}
jp_logf(L_DEBUG, "%s: Got %s item: '%s' for Volume %d\n", MYNAME, text, (*list)->name, (*list)->volRef);
if (last == paths)
break;
}
return EXIT_SUCCESS;
}
char *isoTime(const time_t time) {
static char isoTime[20];
strftime(isoTime, 20, "%F %T", localtime (&time));
return isoTime;
}
time_t getLocalDate(const char *path) {
struct stat fstat;
int statErr = stat(path, &fstat);
if (statErr) {
jp_logf(L_WARN, "%s: WARNING: Could not get date of file '%s', statErr=%d\n", MYNAME, path, statErr);
return 0;
}
return fstat.st_mtime;
}
void setLocalDate(const char *path, const time_t date) {
struct stat fstat;
int statErr = stat(path, &fstat);
if ((statErr = stat(path, &fstat)))
jp_logf(L_WARN, "%s: WARNING: Could not set date of file '%s', statErr=%d\n", MYNAME, path, statErr);
else {
struct utimbuf utim;
utim.actime = (time_t)fstat.st_atime;
utim.modtime = date;
statErr = utime(path, &utim);
jp_logf(L_DEBUG, "%s: setLocalDate(path='%s', date='%s') ---> done!\n", MYNAME, path, isoTime(date));
}
}
time_t getRemoteDate(FileRef fileRef, const int volRef, const char *path, const char prefix[]) {
time_t date = 0;
int close = !fileRef && dlp_VFSFileOpen(sd, volRef, path, vfsModeRead, &fileRef) >= 0;
// 'date modified' seems to be ignored by PalmOS
PI_ERR piErr = dlp_VFSFileGetDate(sd, fileRef, useDateModified ? vfsFileDateModified : vfsFileDateCreated, &date);
if (piErr < 0) {
if (prefix) // for listRemoteFiles()
jp_logf(L_DEBUG, "%s WARNING: No 'date %s from %s\n", prefix, useDateModified ? "modified'":"created' ", path);
else piErrLog(piErr, L_WARN, volRef, path, " ", useDateModified ?
": Could not get 'date modified' of file":": Could not get 'date created' of file","");
}
if (close) dlp_VFSFileClose(sd, fileRef);
return date;
}
void setRemoteDate(FileRef fileRef, const int volRef, const char *path, const time_t date) {
int close = !fileRef && dlp_VFSFileOpen(sd, volRef, path, vfsModeReadWrite, &fileRef) >= 0;
// Set both dates of the file (DateCreated is displayed in Media App on Palm device); must not be before 1980, otherwise PalmOS error.
piErrLog(dlp_VFSFileSetDate(sd, fileRef, vfsFileDateCreated, date), L_WARN, volRef, path, " ", ": Could not set 'date created' of file","");
piErrLog(dlp_VFSFileSetDate(sd, fileRef, vfsFileDateModified, date), L_WARN, volRef, path, " ", ": Could not set 'date modified' of file","");
if (close) dlp_VFSFileClose(sd, fileRef);
}
/*
* *path becomes extended by *dir as successfully created.
* If *dir is non-NULL, it should start with "/" and *path should be already existent and start with "/" or "./".
* If *dir is NULL, only the last element of *path may be non-existent and *path should start with "/" or "./".
* If *rmPath should be in sync with *path.
* EXIT_SUCCESS is returned on success, otherwise EXIT_FAILURE.
* Caller should allocate and free *path value.
*/
int createLocalDir(char *path, const char *dir, const int volRef, const char *rmPath) {
jp_logf(L_DEBUG, "%s: createLocalDir(path='%s', dir='%s', volRef=%d, rmPath='%s')\n", MYNAME, path, dir, volRef, rmPath);
char *pathBase = path + strlen(path), *subDir = NULL, parent[NAME_MAX], rmDir[NAME_MAX];
strcpy(parent, path);
if (dir) {
stpcpy(pathBase, dir);
if ((subDir = strchr(dir + 1, '/')))
*(strchr(pathBase + 1, '/')) = '\0';
} else if ((dir = strrchr(path, '/'))) {
parent[dir - path] = '\0';
} else {
strcpy(parent, ".");
}
stpcpy(stpcpy(rmDir, rmPath), pathBase);
time_t parentDate = strcmp(parent, ".") && strcmp(strrchr(parent, '/'), ADDITIONAL_FILES) ? getLocalDate(parent) : 0; // skip in case
jp_logf(L_DEBUG, "%s: path='%s', subDir='%s', parent='%s', parentDate='%s', rmDir='%s'\n", MYNAME, path, subDir, parent, isoTime(parentDate), rmDir);
int result = mkdir(path, 0777);
if (!result) {
jp_logf(L_INFO, "%s: Created local directory '%s'\n", MYNAME, path);
if (parentDate) setLocalDate(parent, parentDate); // Recover date of parent path, because mkdir() changed it.
} else if (errno != EEXIST) {
jp_logf(L_FATAL, "%s: ERROR %d: Could not create directory %s\n", MYNAME, errno, path);
*(strrchr(path, '/')) = '\0'; // truncate *path
return result;
}
time_t date = volRef >= 0 && rmDir[0] ? getRemoteDate(0, volRef, rmDir, NULL) : 0;
//~ jp_logf(L_DEBUG, "%s: path='%s', date='%s', volRef=%d, rmDir='%s'\n", MYNAME, path, isoTime(date), volRef, rmDir);
if (date) setLocalDate(path, date); // do always (repair local Media/Internal from /Photos & Videos if initial single sync on #AdditionalFiles)
if (subDir) {
return createLocalDir(path, subDir, volRef, rmDir);
}
return EXIT_SUCCESS;
}
/*
* *path becomes extended by *dir as successfully created.
* If *dir is non-NULL, it should start with "/" and *path should be already existent and start with "/".
* If *dir is NULL, only the last element of *path may be non-existent and *path should start with "/".
* If *lcPath should be in sync with *path.
* 0 is returned on success, otherwise negative PI_ERR.
* Caller should allocate and free *path value.
*/
PI_ERR createRemoteDir(const int volRef, char *path, const char *dir, const char *lcPath) {
jp_logf(L_DEBUG, "%s: createRemoteDir(volRef=%d, path='%s', dir='%s', lcPath='%s')\n", MYNAME, volRef, path, dir, lcPath);
char *pathBase = path + strlen(path), *subDir = NULL, lcDir[NAME_MAX];
if (dir) {
stpcpy(pathBase, dir);
if ((subDir = strchr(dir + 1, '/')))
*(strchr(pathBase + 1, '/')) = '\0';
}
stpcpy(stpcpy(lcDir, lcPath), pathBase);
PI_ERR piErr = dlp_VFSDirCreate(sd, volRef, path);
int piOSErr = piErr == PI_ERR_DLP_PALMOS ? pi_palmos_error(sd) : 0;
if (piErr >= 0) {
jp_logf(L_INFO, "%s: Created remote directory '%s' on volume %d\n", MYNAME, path, volRef);
importantWarning = 1;
time_t date = getLocalDate(lcDir);
if (date) setRemoteDate(0, volRef, path, date); // set remote dir date, if really created
} else if (piOSErr != 10758) { // File not already existing.
jp_logf(L_FATAL, "%s: %s: Could not create dir '%s' on volume %d\n", MYNAME, errString(1, piErr, L_FATAL, ""), path, volRef);
*(strrchr(path, '/')) = '\0'; // truncate *path
return piErr;
}
if (subDir) {
return createRemoteDir(volRef, path, subDir, lcDir);
}
return 0;
}
/*
* Return directory name on the PC, where the albums should be stored. Returned string is of the form
* "$JPILOT_HOME/.jpilot/$PCDIR/VolumeRoot/". Directories in the path are created as needed.
* Null is returned if out of memory.
* Caller should free return value.
*/
static char *localRoot(const unsigned volRef) {
static char path[NAME_MAX];
VFSInfo volInfo;
PI_ERR piErr;
if (createLocalDir(strcpy(path, mediaHome), NULL, -1, "")) return NULL;
// Get indicator of which card.
if ((piErr = dlp_VFSVolumeInfo(sd, volRef, &volInfo)) < 0) {
jp_logf(L_FATAL, "%s: %s Could not get info from volume %d\n", MYNAME, errString(1, piErr, L_FATAL, ""), volRef);
return NULL;
}
if (volInfo.mediaType == pi_mktag('T', 'F', 'F', 'S')) {
if (createLocalDir(path, LOCALDIRS[0], -1, "")) return NULL;
} else if (volInfo.mediaType == pi_mktag('s', 'd', 'i', 'g')) {
if (createLocalDir(path, LOCALDIRS[1], -1, "")) return NULL;
} else {
sprintf(path+strlen(path), "%s%d", LOCALDIRS[2], volInfo.slotRefNum);
if (createLocalDir(path, NULL, -1, "")) return NULL;
}
return path; // must not be free'd by caller as it's a static array
}
int cmpExcludeDirList(const int volRef, const char *dname) {
int result = 1;
for (fullPath *item = excludeDirList; dname && item; item = item->next) {
if ((item->volRef < 0 || volRef == item->volRef) && !(result = strcmp(dname, item->name))) break;
}
return result;
}
int enumerateOpenDir(const int volRef, const FileRef dirRef, const char *rmDir, VFSDirInfo dirInfos[]) {
int dirItems_init = MIN_DIR_ITEMS, dirItems;
PI_ERR piErr;
// Iterate over all the files in the remote dir.
//~ enum dlpVFSFileIteratorConstants itr = vfsIteratorStart; // doesn't work because of type mismatch bug <https://github.com/juddmon/jpilot/issues/39>
//~ while (itr != (unsigned long)vfsIteratorStop) { // doesn't work because of bug <https://github.com/juddmon/jpilot/issues/39>
//~ while (itr != (unsigned)vfsIteratorStop) { // doesn't work because of bug <https://github.com/juddmon/jpilot/issues/41>
unsigned long itr = (unsigned long)vfsIteratorStart;
int loops = 16; // for debugging
for (; (dirItems = dirItems_init) <= MAX_DIR_ITEMS && dirItems > 0; dirItems_init *= 2) { // WORKAROUND
if (--loops < 0) break; // for debugging
//~ jp_logf(L_DEBUG, "%s: Enumerate remote dir '%s': dirRef=%8lx, itr=%4lx, dirItems=%d\n", MYNAME, rmDir, dirRef, itr, dirItems);
if ((piErr = piErrLog(dlp_VFSDirEntryEnumerate(sd, dirRef, &itr, &dirItems, dirInfos),
L_FATAL, volRef, rmDir, " ", ": Could not enumerate dir","")) < 0) {
// Crashes on empty directory (see: <https://github.com/desrod/pilot-link/issues/11>):
// Further research is neccessary (see: <https://github.com/juddmon/jpilot/issues/41>):
// - Why in case of i.e. setting dirItems=4, itr != 0, even if there are more than 4 files?
// - Why then on SDCard itr == 1888 in the first loop, so out of allowed range?
//~ jp_logf(L_DEBUG, "%s: Enumerate ERROR %4d: dirRef=%8lx, itr=%4lx, dirItems=%d\n", MYNAME, piErr, dirRef, itr, dirItems);
return piErr;
}
//~ jp_logf(L_DEBUG, "%s: Enumerate OK: piErr=%4d, dirRef=%8lx, itr=%4lx, dirItems=%d\n", MYNAME, piErr, dirRef, itr, dirItems);
//~ for (int i = dirItems_init==MIN_DIR_ITEMS ? 0 : dirItems_init/2; i < dirItems; i++) {
//~ jp_logf(L_DEBUG, "%s: dirItem %3d: '%s' attributes %x\n", MYNAME, i, dirInfos[i].name, dirInfos[i].attr);
//~ }
if (dirItems < dirItems_init)
break;
itr = (unsigned long)vfsIteratorStart; // workaround, reset itr for next loop, if it wrongly was -1 or 1888
}
if (dirItems >= MAX_DIR_ITEMS)
jp_logf(L_FATAL, "%s: Enumerate OVERFLOW: There seem to be more than %d dir items in '%s'!\n", MYNAME, MAX_DIR_ITEMS, rmDir);
return dirItems;
}
int enumerateDir(const int volRef, const char *rmDir, VFSDirInfo dirInfos[]) { // ToDo: maybe combine with upper function
FileRef dirRef;
PI_ERR piErr = dlp_VFSFileOpen(sd, volRef, rmDir, vfsModeRead, &dirRef);
if (piErrLog(piErr, L_FATAL, volRef, rmDir, " ", ": Could not open dir","") < 0) return piErr;
int dirItems = enumerateOpenDir(volRef, dirRef, rmDir, dirInfos);
dlp_VFSFileClose(sd, dirRef);
return dirItems;
}
/* List remote files and directories recursivly up to given depth. */
PI_ERR listRemoteFiles(const int volRef, const char *rmDir, const int depth) {
FileRef fileRef;
VFSDirInfo dirInfos[MAX_DIR_ITEMS];
char prefix[] = MYNAME": ";
prefix[MIN(sizeof(prefix) - 1, strlen(MYNAME) + 1 + depth)] = '\0';
if (!cmpExcludeDirList(volRef, rmDir)) return -1; // avoid bug <https://github.com/desrod/pilot-link/issues/11>
int dirItems = enumerateDir(volRef, rmDir, dirInfos);
jp_logf(L_DEBUG, "%s%d remote files in '%s' on Volume %d ...\n", prefix, dirItems, rmDir, volRef);
for (int i = 0; i < dirItems; i++) {
char child[NAME_MAX];
int filesize = 0;
time_t date = 0;
char *temp = stpcpy(child, strcmp(rmDir, "/") ? rmDir : "");
temp = stpncpy(temp, "/", NAME_MAX-1-strlen(child));
stpncpy(temp, dirInfos[i].name, NAME_MAX-1-strlen(child));
//~ strncat(strcmp(rmDir, "/") ? strncat(strcat(child, rmDir), "/", NAME_MAX-1) : child, dirInfos[i].name, NAME_MAX-1); // for paths without '/' at start
if (dlp_VFSFileOpen(sd, volRef, child, vfsModeRead, &fileRef) < 0) {
jp_logf(L_DEBUG, "%s WARNING: Cannot get size/date from %s\n", prefix, dirInfos[i].name);
} else {
if (!(dirInfos[i].attr & vfsFileAttrDirectory) && (dlp_VFSFileSize(sd, fileRef, &filesize) < 0))
jp_logf(L_DEBUG, "%s WARNING: Could not get size of %s\n", prefix, dirInfos[i].name);
date = getRemoteDate(fileRef, 0, dirInfos[i].name, prefix);
dlp_VFSFileClose(sd, fileRef);
}
//~ jp_logf(L_DEBUG, "%s 0x%02x%10d %s %s %s\n", prefix, dirInfos[i].attr, filesize, isoTime(&dateCre), isoTime(&dateMod), dirInfos[i].name);
jp_logf(L_DEBUG, "%s 0x%02x%10d %s %s\n", prefix, dirInfos[i].attr, filesize, isoTime(date), dirInfos[i].name);
if (dirInfos[i].attr & vfsFileAttrDirectory && depth < listFiles) {
listRemoteFiles(volRef, child, depth + 1);
}
}
return (PI_ERR)dirItems;
}
int fileRead(FileRef fileRef, FILE *fileP, pi_buffer_t *buf, int remaining) {
buf->used = 0;
for (int readsize = 0, todo = remaining > buf->allocated ? buf->allocated : remaining; todo > 0; todo -= readsize) {
if (fileRef) {
readsize = dlp_VFSFileRead(sd, fileRef, buf, todo);
//readsize = dlp_VFSFileRead(sd, fileRef, buf, buf->allocated); // works too, but is very slow
} else if (fileP) {
readsize = fread(buf->data + buf->used, 1, todo, fileP);
buf->used += (size_t)readsize;
}
if (readsize < 0) {
jp_logf(L_FATAL, "\n%s: %s on file read, aborting at %d bytes left.\n",
MYNAME, errString(fileRef, readsize, L_FATAL, ""), remaining - buf->used);
return readsize;
}
}
return (int)buf->used;
}
int fileWrite(FileRef fileRef, FILE *fileP, pi_buffer_t *buf, int remaining) {
for (int writesize = 0, offset = 0; offset < buf->used; offset += writesize) {
if (fileRef) {
writesize = dlp_VFSFileWrite(sd, fileRef, buf->data + offset, buf->used - offset);
} else if (fileP) {
writesize = fwrite(buf->data + offset, 1, buf->used - offset, fileP);
}
if (writesize < 0) {
jp_logf(L_FATAL, "\n%s: %s on file write, aborting at %d bytes left.\n",
MYNAME, errString(fileRef, writesize, L_FATAL, ""), remaining - offset);
return writesize;
}
}
return (int)buf->used;
}
int fileCompare(FileRef fileRef, FILE *fileP, int filesize) {
int result;
for (int todo = filesize; todo > 0; todo -= piBuf->used) {
if (fileRead(fileRef, NULL, piBuf, todo) < 0 || fileRead(0, fileP, piBuf2, todo) < 0 || piBuf->used != piBuf2->used) {
jp_logf(L_FATAL, "%s: ERROR reading files for comparison, so assuming different ...\n", MYNAME);
jp_logf(L_DEBUG, "%s: filesize=%d, todo=%d, piBuf->used=%d, piBuf2->used=%d\n", MYNAME, filesize, todo, piBuf->used, piBuf2->used);
result = -1; // remember error
break;
}
if ((result = memcmp(piBuf->data, piBuf2->data, piBuf->used))) {
break; // Files have different content.
}
}
return result;
}
int casecmpFileTypeList(const char *fname) {
char *ext = strrchr(fname, '.');
for (fullPath *item = fileTypeList; ext && item; item = item->next) {
if (*(item->name) != '-') {
if (!strcasecmp(ext + 1, item->name)) return 1; // backup & restore
} else {
if (!strcasecmp(ext + 1, item->name + 1)) return 0; // only backup
}
}
return -1; // no match, no sync
}
int cmpRemote(VFSDirInfo dirInfos[], int dirItems, const char *fname) {
int result = 1;
for (int i=0; i<dirItems; i++) {
if (!(result = strcmp(fname, dirInfos[i].name))) break;
}
return result;
}
/*
* Backup a file from the Palm device, if not existent or different.
*/
int backupFileIfNeeded(const unsigned volRef, const char *rmDir, const char *lcDir, const char *file) {
jp_logf(L_DEBUG, "%s: backupFileIfNeeded(volRef=%d, rmDir='%s', lcDir='%s', file='%s')\n", MYNAME, volRef, rmDir, lcDir, file);
char rmPath[strlen(rmDir) + strlen(file) + 2];
char lcPath[strlen(lcDir) + strlen(file) + 4]; // prepare for possible rename
FileRef fileRef;
FILE *fileP;
int filesize; // also serves as error return code
stpcpy(stpcpy(stpcpy(rmPath, rmDir), "/"), file);
stpcpy(stpcpy(stpcpy(lcPath, lcDir), "/"), file);
if (piErrLog(dlp_VFSFileOpen(sd, volRef, rmPath, vfsModeRead, &fileRef),
L_FATAL, volRef, rmPath, " ", ": Could not open remote file","") < 0)
return -1;
else if (piErrLog(dlp_VFSFileSize(sd, fileRef, &filesize),
L_WARN, volRef, rmPath, " ", ": Could not get size of", ", so anyway backup it.") < 0)
filesize = 0;
struct stat fstat;
int statErr = stat(lcPath, &fstat);
if (!statErr) {
int equal = 0;
if (fstat.st_size != filesize) {
jp_logf(L_WARN, "%s: WARNING: File '%s' already exists, but has different size %d vs. %d,\n", MYNAME, lcPath, fstat.st_size, filesize);
} else if (!compareContent) {
equal = 1;
} else {
FILE *fileP;
if (!(fileP = fopen(lcPath, "r"))) {
jp_logf(L_WARN, "%s: WARNING: Cannot open %s for comparing %d bytes, so may have different content,\n", MYNAME, lcPath, filesize);
} else {
if (!(equal = !fileCompare(fileRef, fileP, filesize)))
jp_logf(L_WARN, "%s: WARNING: File '%s' already exists, but has different content,\n", MYNAME, lcPath);
fclose(fileP);
if (piErrLog(dlp_VFSFileSeek(sd, fileRef, vfsOriginBeginning, 0),
L_FATAL, volRef, file, " ", ": Could not rewind file", ", so can not copy it, aborting ...") < 0) {
filesize = -1; // remember error
goto Exit;
}
}
}
if (equal) {
jp_logf(L_DEBUG, "%s: File '%s' already exists, not copying it.\n", MYNAME, lcPath);
goto Exit;
}
// Find alternative destination file name, which not alredy exists, by inserting a number.
char *i = lcPath + strlen(lcPath), *insert = strrchr(lcPath, '.');
insert = insert ? insert : i; // correct if there was no '.'
for (; i >= insert; i--) *(i + 2) = *i;
*insert++ = '_'; *insert = '1';
for (; !stat(lcPath, &fstat); (*insert)++) {; // increment number by 1
if (*insert >= '9') {
jp_logf(L_WARN, "%s: and even file '%s' already exists, so no new backup for '%s'.\n", MYNAME, lcPath, file);
filesize = -1; // remember error
goto Exit;
}
}
jp_logf(L_WARN, "%s: so backup to '%s'.\n", MYNAME, lcPath);
}
// File has not already been synced, backup it.
if (!(fileP = fopen(lcPath, "wx"))) {
jp_logf(L_FATAL, "%s: ERROR: Cannot open %s for writing %d bytes!\n", MYNAME, lcPath, filesize);
filesize = -1; // remember error
goto Exit;
}
// Copy file.
jp_logf(L_INFO, "%s: Backup '%s', size %d ...", MYNAME, rmPath, filesize);
for (int remaining = filesize; remaining > 0; remaining -= piBuf->used) {
if (fileRead(fileRef, NULL, piBuf, remaining) < 0) {
filesize = -1; // remember error
break;
}
if (fileWrite(0, fileP, piBuf, remaining) < 0) {
filesize = -1; // remember error
break;
}
}
fclose(fileP);
if (filesize < 0) {
unlink(lcPath); // remove the partially created file
jp_logf(L_WARN, "%s: WARNING: Deleted incomplete local file '%s'\n", MYNAME, lcPath);
} else {
jp_logf(L_INFO, " OK\n");
// Get the date on that the picture was created.
time_t date = getRemoteDate(fileRef, volRef, rmPath, NULL);
if (date) setLocalDate(lcPath, date);
}
Exit:
dlp_VFSFileClose(sd, fileRef);
jp_logf(L_DEBUG, "%s: Backup file size / copy result: %d, statErr=%d\n", MYNAME, filesize, statErr);
return filesize;
}
/*
* Restore a file to the remote Palm device.
*/
int restoreFile(const char *lcDir, const unsigned volRef, const char *rmDir, const char *file) {
jp_logf(L_DEBUG, "%s: restoreFile(lcDir='%s', volRef=%d, rmDir='%s', file='%s')\n", MYNAME, lcDir, volRef, rmDir, file);
char lcPath[strlen(lcDir) + strlen(file) + 2];
char rmPath[strlen(rmDir) + strlen(file) + 2];
FILE *fileP;
FileRef fileRef;
int filesize; // also serves as error return
stpcpy(stpcpy(stpcpy(lcPath, lcDir), "/"), file);
stpcpy(stpcpy(stpcpy(rmPath, rmDir), "/"), file);
struct stat fstat;
int statErr;
if ((statErr = stat(lcPath, &fstat))) {
jp_logf(L_FATAL, "%s: ERROR %d: Could not read status of %s.\n", MYNAME, statErr, lcPath);
return -1;
}
filesize = fstat.st_size;
if (!(fileP = fopen(lcPath, "r"))) {
jp_logf(L_FATAL, "%s: ERROR: Could not open %s for reading %d bytes,\n", MYNAME, lcPath, filesize);
return -1;
}
if (piErrLog(dlp_VFSFileOpen(sd, volRef, rmPath, vfsModeReadWrite | vfsModeCreate, &fileRef),
L_FATAL, volRef, rmPath, " ", ": Could not open remote file", " for read/writing.") < 0) { // May not work on DLP,
//~ // ... then first create file with:
//~ if (piErrLog(dlp_VFSFileCreate(sd, volRef, rmPath), L_FATAL, volRef, rmPath, " ", ": Could not create remote file","") < 0 ||
//~ (piErrLog(dlp_VFSFileOpen(sd, volRef, rmPath, vfsModeWrite, &fileRef), // See: https://github.com/desrod/pilot-link/issues/10
//~ (piErrLog(dlp_VFSFileOpen(sd, volRef, rmPath, vfsModeReadWrite, &fileRef),
//~ L_FATAL, volRef, rmPath, " ", ": Could not open remote file", " for read/writing.") < 0) {
filesize = -1; // remember error
goto Exit;
}
// Copy file.
jp_logf(L_INFO, "%s: Restore '%s', size %d ...", MYNAME, lcPath, filesize);
for (int remaining = filesize; remaining > 0; remaining -= piBuf->used) {
if (fileRead(0, fileP, piBuf, remaining) < 0) {
filesize = -1; // remember error
break;
}
if (fileWrite(fileRef, NULL, piBuf, remaining) < 0) {
filesize = -1; // remember error
break;
}
}
setRemoteDate(fileRef, volRef, rmPath, fstat.st_mtime);
dlp_VFSFileClose(sd, fileRef);
if (filesize < 0) { // close and remove the partially created file
if (piErrLog(dlp_VFSFileDelete(sd, volRef, rmPath), L_FATAL, volRef, rmPath, " ", ": Not deleted remote file","") >= 0)
jp_logf(L_WARN, "%s: WARNING: Deleted incomplete remote file '%s' on volume %d\n", MYNAME, rmPath, volRef);
} else
jp_logf(L_INFO, " OK\n");
Exit:
fclose(fileP);
jp_logf(L_DEBUG, "%s: Restore file size / copy result: %d, statErr=%d\n", MYNAME, filesize, statErr);
return filesize;
}
/*
* Synchonize a remote album with the matching local album and backup or restore the containing files in them.
*/
PI_ERR syncAlbum(const unsigned volRef, FileRef dirRef, const char *rmRoot, DIR *dirP, const char *lcRoot, const char *album) {
char rmTmp[album ? strlen(rmRoot) + strlen(album) + 2 : 0], *rmAlbum = rmTmp, *dir;
char lcTmp[album ? strlen(lcRoot) + strlen(album) + 2 : 0], *lcAlbum = lcTmp;
VFSDirInfo dirInfos[MAX_DIR_ITEMS];
int dirItems = 0;
struct stat fstat;
int statErr;
PI_ERR result = 0;
if (album) {
stpcpy(stpcpy(dir = stpcpy(rmAlbum ,rmRoot), "/"), album);
if (!cmpExcludeDirList(volRef, rmAlbum)) return result;
if (dirP) // indicates, that we are in restore-only mode, so
dirItems = -1; // prevent search on remote album
strcpy(lcAlbum, lcRoot);
if (createLocalDir(lcAlbum, dir, dirP ? -1 : volRef, rmRoot)) { // in restore-only mode don't try to recover date
return -2;
} else if (!(dirP = opendir(lcAlbum))) {
jp_logf(L_FATAL, "%s: ERROR: Could not open dir '%s' on '%s'\n", MYNAME, album, lcRoot);
return -2;
}
if (createRemoteDir(volRef, rmAlbum, NULL, lcAlbum) < 0) {
result = -2;
goto Exit1;
} else if (piErrLog(dlp_VFSFileOpen(sd, volRef, rmAlbum, vfsModeRead, &dirRef),
L_FATAL, volRef, rmAlbum, " ", ": Could not open dir", "") < 0) {
result = -2;
goto Exit1;
}
} else {
rmAlbum = (char *)rmRoot;
lcAlbum = (char *)lcRoot;
if (!cmpExcludeDirList(volRef, rmAlbum)) return result;
}
jp_logf(L_INFO, "%s: Sync album '%s' in '%s' on volume %d ...\n", MYNAME, album ? album : ".", rmRoot, volRef);
if (!dirItems) // We are in backup mode !
dirItems = enumerateOpenDir(volRef, dirRef, rmAlbum, dirInfos);
jp_logf(L_DEBUG, "%s: Now first search of local files, which to restore ...\n", MYNAME);
// First iterate over all the local files in the album dir, to prevent from back-storing renamed files,
// so only looking for remotely unknown files ... and then restore them.
for (struct dirent *entry; doRestore && (entry = readdir(dirP));) {
jp_logf(L_DEBUG, "%s: Found local file: '%s' type=%d\n", MYNAME, entry->d_name, entry->d_type);
char lcAlbumPath[NAME_MAX];
if ((statErr = stat(strcat(strcat(strcpy(lcAlbumPath, lcAlbum), "/"), entry->d_name), &fstat))) {
jp_logf(L_FATAL, "%s: ERROR %d: Could not read status of %s; No sync possible!\n", MYNAME, statErr, lcAlbumPath);
result = MIN(result, -1);
continue;
}
if (S_ISREG(fstat.st_mode) // use fstat to follow symlinks; (entry->d_type != DT_REG) doesn't do this
&& strlen(entry->d_name) > 2
&& casecmpFileTypeList(entry->d_name) > 0
&& cmpRemote(dirInfos, dirItems, entry->d_name)) {
//~ jp_logf(L_DEBUG, "%s: Restore local file: '%s' to '%s'\n", MYNAME, entry->d_name, rmAlbum);
int restoreResult = restoreFile(lcAlbum, volRef, rmAlbum, entry->d_name);
result = MIN(result, restoreResult);
}
}
jp_logf(L_DEBUG, "%s: Now search of %d remote files, which to backup ...\n", MYNAME, dirItems);
// Iterate over all the remote files in the album dir, looking for un-synced files.
for (int i=0; doBackup && i<dirItems; i++) {
char *fname = dirInfos[i].name;
jp_logf(L_DEBUG, "%s: Found remote file '%s' attributes=%x\n", MYNAME, fname, dirInfos[i].attr);
// Grab only regular files, but ignore the 'read only' and 'archived' bits, and only with known extensions.
if (!(dirInfos[i].attr & (
vfsFileAttrHidden |
vfsFileAttrSystem |
vfsFileAttrVolumeLabel |
vfsFileAttrDirectory |
vfsFileAttrLink))
&& strlen(fname) > 1
&& casecmpFileTypeList(fname) >= 0) {
//~ jp_logf(L_DEBUG, "%s: Backup remote file: '%s' to '%s'\n", MYNAME, fname, lcAlbum);
int backupResult = backupFileIfNeeded(volRef, rmAlbum, lcAlbum, fname);
result = MIN(result, backupResult);
}
}
time_t date = getRemoteDate(dirRef, volRef, rmAlbum, NULL);
if (doBackup && date) setLocalDate(lcAlbum, date); // always recover folder date from remote // ToDo: maybe do by BackupFileIfNeeded()
if (album) dlp_VFSFileClose(sd, dirRef);
Exit1:
if (album) closedir(dirP);
else rewinddir(dirP);
jp_logf(L_DEBUG, "%s: Album '%s' done -> result=%d\n", MYNAME, rmAlbum, result);
return result;
}
/*
* Backup all albums from volume volRef.
*/
PI_ERR syncVolume(int volRef) {
PI_ERR rootResult = -3, result = 0;
jp_logf(L_DEBUG, "%s: Searching roots on volume %d\n", MYNAME, volRef);
for (fullPath *item = rootDirList; item; item = item->next) {
if ((item->volRef >= 0 && volRef != item->volRef))
continue;
char *rootDir = item->name;
VFSDirInfo dirInfos[MAX_DIR_ITEMS];
// Open the remote root directory.
FileRef dirRef;
if (piErrLog(dlp_VFSFileOpen(sd, volRef, rootDir, vfsModeRead, &dirRef), L_DEBUG, volRef, rootDir, " ", ": Root", "; seems not to exist.") < 0)
continue;
jp_logf(L_DEBUG, "%s: Opened remote root '%s' on volume %d\n", MYNAME, rootDir, volRef);
rootResult = 0;
// Open the local root directory.
char *lcRoot = localRoot(volRef);
if (!lcRoot)
goto Continue;
DIR *dirP;
if (!(dirP = opendir(lcRoot))) {
jp_logf(L_DEBUG, "%s: Root '%s' does not exist on '%s'\n", MYNAME, lcRoot + strlen(mediaHome), mediaHome);
goto Continue;
}
jp_logf(L_DEBUG, "%s: Opened local root '%s' on '%s'\n", MYNAME, lcRoot + strlen(mediaHome), mediaHome);
// Fetch the unfiled album, which is simply the root dir, and sync it.
// Apparently the Treo 650 can store media in the root dir, as well as in album dirs.
result = syncAlbum(volRef, dirRef, rootDir, dirP, lcRoot, NULL);
// Iterate through the remote root directory, looking for things that might be albums.
int dirItems = 0;
//~ enum dlpVFSFileIteratorConstants itr = vfsIteratorStart;
//~ while (itr != vfsIteratorStop) { // doesn't work because of type mismatch bug <https://github.com/juddmon/jpilot/issues/39>
unsigned long itr = (unsigned long)vfsIteratorStart;
for (int batch; (enum dlpVFSFileIteratorConstants)itr != vfsIteratorStop; dirItems += batch) {
batch = MIN(MAX_DIR_ITEMS / 2, MAX_DIR_ITEMS - dirItems);
jp_logf(L_DEBUG, "%s: Enumerate root '%s': dirRef=%8lx, itr=%4lx, batch=%d, dirItems=%d\n", MYNAME, rootDir, dirRef, itr, batch, dirItems);
PI_ERR piErr;
if ((piErr = piErrLog(dlp_VFSDirEntryEnumerate(sd, dirRef, &itr, &batch, dirInfos + dirItems),
L_FATAL, volRef, rootDir, " ", ": Could not enumerate dir", "")) < 0) {
// Crashes on empty directory (see: <https://github.com/juddmon/jpilot/issues/??>):
// Further research is neccessary (see: <https://github.com/juddmon/jpilot/issues/41> ):
// - Why in case of i.e. setting batch=4, itr == vfsIteratorStop, even if there are more than 4 files?
// - For workaround and additional bug on SDCard volume, see at syncAlbum()
//~ jp_logf(L_DEBUG, "%s: Enumerate ERROR %4d: dirRef=%8lx, itr=%4lx, batch=%d\n", MYNAME, piErr, dirRef, itr, batch);
rootResult = -3;
break;
//~ } else {
//~ jp_logf(L_DEBUG, "%s: Enumerate OK %4d, dirRef=%8lx, itr=%4lx, batch=%d\n", MYNAME, piErr, dirRef, itr, batch);
}
jp_logf(L_DEBUG, "%s: Now search for remote albums on Volume %d in '%s' to sync ...\n", MYNAME, volRef, rootDir);
for (int i = dirItems; i < (dirItems + batch); i++) {
jp_logf(L_DEBUG, "%s: Found remote album candidate '%s' in '%s'; attributes=%x\n", MYNAME, dirInfos[i].name, rootDir, dirInfos[i].attr);
if (dirInfos[i].attr & vfsFileAttrDirectory
&& (syncThumbnailDir || strcmp(dirInfos[i].name, "#Thumbnail"))) { // Treo 650 has #Thumbnail dir that is not an album
jp_logf(L_DEBUG, "%s: Found real remote album '%s' in '%s'\n", MYNAME, dirInfos[i].name, rootDir);
PI_ERR albumResult = syncAlbum(volRef, 0, rootDir, NULL, lcRoot, dirInfos[i].name);
result = MIN(result, albumResult);
}
}
}
// Now iterate over all the local files in the album dir. To prevent from back-storing renamed files,
// only looking for remotely unknown albums ... and then restore them.
jp_logf(L_DEBUG, "%s: Now search for local albums in '%s' to restore ...\n", MYNAME, lcRoot);
for (struct dirent *entry; (entry = readdir(dirP));) {
jp_logf(L_DEBUG, "%s: Found local album candidate '%s' in '%s'; type %d\n", MYNAME, entry->d_name, lcRoot + strlen(mediaHome) + 1, entry->d_type);
struct stat fstat;
int statErr;
char lcAlbum[NAME_MAX];
if ((statErr = stat(strcat(strcat(strcpy(lcAlbum, lcRoot), "/"), entry->d_name), &fstat))) {
jp_logf(L_FATAL, "%s: ERROR %d: Could not read status of %s; No sync possible!\n", MYNAME, statErr, lcAlbum);
result = MIN(result, -2);
continue;
}
if (S_ISDIR(fstat.st_mode) // use fstat to follow symlinks; (entry->d_type != DT_Dir) doesn't do this
&& strcmp(entry->d_name, ".")
&& strcmp(entry->d_name, "..")
&& (syncThumbnailDir || strcmp(entry->d_name, "#Thumbnail")) // Treo 650 has #Thumbnail dir that is not an album
&& strcmp(entry->d_name, ADDITIONAL_FILES + 1)
&& cmpRemote(dirInfos, dirItems, entry->d_name)) {
jp_logf(L_DEBUG, "%s: Found real local album '%s' in '%s'\n", MYNAME, entry->d_name, lcRoot + strlen(mediaHome) + 1);
PI_ERR albumResult = syncAlbum(volRef, 0, rootDir, dirP, lcRoot, entry->d_name);
result = MIN(result, albumResult);
}
}
closedir(dirP);
// Reset date of lcRoot
time_t date = getRemoteDate(dirRef, volRef, rootDir, NULL);
if (date) setLocalDate(lcRoot, date);
Continue:
dlp_VFSFileClose(sd, dirRef);
}
jp_logf(L_DEBUG, "%s: Volume %d done -> rootResult=%d, result=%d\n", MYNAME, volRef, rootResult, result);
return rootResult + result;
}
/***********************************************************************
*
* Function: volumeEnumerateIncludeHidden
*
* Summary: Drop-in replacement for dlp_VFSVolumeEnumerate().
* Attempts to include hidden volumes in the list,
* so that we also get the device's BUILTIN volume.
* 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 volumeEnumerateIncludeHidden(const int sd, int *numVols, int *volRefs) {
PI_ERR piErr;
VFSInfo volInfo;
// piErr on Treo 650:
// -301 : PalmOS Error. Probably no volume (SDCard) found, but maybe hidden volume 1 exists
// 4 : At least one volume found, but maybe additional hidden volume 1 exists
piErr = dlp_VFSVolumeEnumerate(sd, numVols, volRefs);
jp_logf(L_DEBUG, "%s: dlp_VFSVolumeEnumerate(): %s; found %d volumes\n", MYNAME, errString(1, piErr, L_DEBUG, ""), *numVols);
// On the Centro, Treo 650 and maybe more, 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().
if (piErr < 0) *numVols = 0; // On Error reset numVols
for (int i=0; i<*numVols; i++) { // Search for volume 1
jp_logf(L_DEBUG, "%s: *numVols=%d, volRefs[%d]=%d\n", MYNAME, *numVols, i, volRefs[i]);
if (volRefs[i]==1)
goto Exit; // No need to search for hidden volume
}
if (piErrLog(dlp_VFSVolumeInfo(sd, 1, &volInfo), L_FATAL, 1, "", "", ": Could not find info","") >= 0 && volInfo.attributes & vfsVolAttrHidden) {
jp_logf(L_DEBUG, "%s: Found hidden volume 1\n", MYNAME);
if (*numVols < MAX_VOLUMES) (*numVols)++;
else {
jp_logf(L_FATAL, "%s: ERROR: Volumes > %d were discarded\n", MYNAME, MAX_VOLUMES);
}
for (int i = (*numVols)-1; i > 0; i--) { // Move existing volRefs
jp_logf(L_DEBUG, "%s: *numVols=%d, volRefs[%d]=%d, volRefs[%d]=%d\n", MYNAME, *numVols, i-1, volRefs[i-1], i, volRefs[i]);
volRefs[i] = volRefs[i-1];
}
volRefs[0] = 1;
if (piErr < 0)
piErr = 4; // fake dlp_VFSVolumeEnumerate() with 1 volume return value
}
Exit:
jp_logf(L_DEBUG, "%s: volumeEnumerateIncludeHidden(): Found %d volumes -> piErr=%d\n", MYNAME, *numVols, piErr);
return piErr;
}
void plugin_version(int *major_version, int *minor_version) {
int dotindex = strcspn(VERSION, ".");
static char major[] = VERSION;
major[dotindex] = '\0';
*major_version = atoi(major);
*minor_version = atoi(VERSION + dotindex + 1);
}
int plugin_get_name(char *name, int len) {
//~ snprintf(name, len, "%s %d.%d", MYNAME, PLUGIN_MAJOR, PLUGIN_MINOR);
strncpy(name, PACKAGE_STRING, len);
return EXIT_SUCCESS;
}
int plugin_get_help_name(char *name, int len) {
//~ g_snprintf(name, len, _("About %s"), _(MYNAME)); // ToDo: With language support.
snprintf(name, len, "About %s", MYNAME);
return EXIT_SUCCESS;
}
int plugin_help(char **text, int *width, int *height) {
// Unfortunately JPilot app tries to free the *text memory,
// so we must copy the text to new allocated heap memory.
*text = strdup(HELP_TEXT);
*height = 0;
*width = 0;
return EXIT_SUCCESS;
}
int plugin_startup(jp_startup_info *info) {
jp_init();
return EXIT_SUCCESS;
}
int plugin_sync(int socket) {
sd = socket;
// Read and process preferences.
jp_pref_init(prefs, NUM_PREFS);
if (jp_pref_read_rc_file(PREFS_FILE, prefs, NUM_PREFS) < 0)
jp_logf(L_WARN, "%s: WARNING: Could not read prefs[] from '%s'\n", MYNAME, PREFS_FILE);
if (jp_pref_write_rc_file(PREFS_FILE, prefs, NUM_PREFS) < 0) // If pref file wasn't existent ...
jp_logf(L_WARN, "%s: WARNING: Could not write prefs[] to '%s'\n", MYNAME, PREFS_FILE); // initialize with defaults.
jp_get_pref(prefs, 0, &prefsVersion, NULL);
if (prefsVersion != PREFS_VERSION) {
jp_logf(L_FATAL, "%s: ERROR: Version of preferences file '%s' must be %d, please update it!\n", MYNAME, PREFS_FILE, PREFS_VERSION);
return EXIT_FAILURE;
}
jp_get_pref(prefs, 1, NULL, (const char **)&rootDirs);
jp_get_pref(prefs, 2, &syncThumbnailDir, NULL);
jp_get_pref(prefs, 3, NULL, (const char **)&fileTypes);
jp_get_pref(prefs, 4, &useDateModified, NULL);
jp_get_pref(prefs, 5, &compareContent, NULL);
jp_get_pref(prefs, 6, &doBackup, NULL);
jp_get_pref(prefs, 7, &doRestore, NULL);
jp_get_pref(prefs, 8, &listFiles, NULL);
jp_get_pref(prefs, 9, NULL, (const char **)&excludeDirs);
jp_get_pref(prefs, 10, NULL, (const char **)&deleteFiles);
jp_get_pref(prefs, 11, NULL, (const char **)&additionalFiles);
if ( parsePaths(rootDirs, &rootDirList, "rootDirs") != EXIT_SUCCESS ||
parsePaths(fileTypes, &fileTypeList, "fileTypes") != EXIT_SUCCESS ||
parsePaths(excludeDirs, &excludeDirList, "excludeDirs") != EXIT_SUCCESS ||
parsePaths(deleteFiles, &deleteFileList, "deleteFiles") != EXIT_SUCCESS ||
parsePaths(additionalFiles, &additionalFileList, "additionalFiles") != EXIT_SUCCESS ||
!(piBuf = pi_buffer_new(32768)) || !(piBuf2 = pi_buffer_new(32768))) {
jp_logf(L_FATAL, "%s: ERROR: Out of memory\n", MYNAME);
return EXIT_FAILURE;
}
// Use $JPILOT_HOME/.jpilot/ or current directory for PCDIR.
if (jp_get_home_file_name(PCDIR, mediaHome, NAME_MAX) < 0) {
jp_logf(L_WARN, "%s: WARNING: Could not get $JPILOT_HOME path, so using current directory.\n", MYNAME);
strcpy(mediaHome, "./"PCDIR);
}
if (listFiles)
jp_logf(L_INFO, "%s: List all files from the Palm device to the terminal, needs: 'jpilot -d'\n", MYNAME);
else {
jp_logf(L_INFO, "%s: Start syncing with '%s ...'\n", MYNAME, mediaHome);
// Check if there are any file types loaded.
if (!fileTypeList) {
jp_logf(L_FATAL, "%s: ERROR: Could not find any file types from '%s'; No media synced.\n", MYNAME, PREFS_FILE);
return EXIT_FAILURE;
}
}
// Get list of the volumes on the pilot.
int volRefs[MAX_VOLUMES];
int volumes = MAX_VOLUMES;
if (volumeEnumerateIncludeHidden(sd, &volumes, volRefs) < 0) {
jp_logf(L_FATAL, "%s: ERROR: Could not find any VFS volumes; No files to sync or list.\n", MYNAME);
return EXIT_FAILURE;
}
// Scan all the volumes for media and backup them.