forked from mapsme/osmctools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osmupdate.c
1678 lines (1581 loc) · 60.3 KB
/
osmupdate.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
// osmupdate 2017-02-26 16:40
#define VERSION "0.4.4"
//
// compile this file:
// gcc osmupdate.c -o osmupdate
//
// (c) 2011..2017 Markus Weber, Nuernberg
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public License
// version 3 as published by the Free Software Foundation.
// 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 Affero General Public License for more details.
// You should have received a copy of this license along
// with this program; if not, see http://www.gnu.org/licenses/.
//
// Other licenses are available on request; please ask the author.
#define MAXLOGLEVEL 2
const char* helptext=
"\nosmupdate " VERSION "\n"
"\n"
"This program cares about updating an .osm, .o5m or .pbf file. It\n"
"will download and apply OSM Change files (.osc) from the servers of\n"
"\"planet.openstreetmap.org\".\n"
"It also can assemble a new .osc or .o5c file which can be used to\n"
"update your OSM data file at a later time.\n"
"\n"
"Prequesites\n"
"\n"
"To run this program, please download and install two other programs\n"
"first: \"osmconvert\" and \"wget\".\n"
"\n"
"Usage\n"
"\n"
"Two command line arguments are mandatory: the name of the old and the\n"
"name of the new OSM data file. If the old data file does not have a\n"
"file timestamp, you may want to specify this timestamp manually on\n"
"the command line. If you do not, the program will try to determine\n"
"the timestamp by examining the whole old data file.\n"
"Instead of the second parameter, you alternatively may specify the\n"
"name of a change file (.osc or .o5c). In this case, you also may\n"
"replace the name of the old OSM data file by a timestamp.\n"
"Command line arguments which are not recognized by osmupdate will be\n"
"passed to osmconvert. Use this opportunity to supply a bounding box\n"
"or a bounding polygon if you are going to update a regional change\n"
"file. You also may exclude unneeded meta data from your file by\n"
"specifying this osmconvert option: --drop-author\n"
"\n"
"Usage Examples\n"
"\n"
" ./osmupdate old_file.o5m new_file.o5m\n"
" ./osmupdate old_file.pbf new_file.pbf\n"
" ./osmupdate old_file.osm new_file.osm\n"
" The old OSM data will be updated and written as new_file.o5m\n"
" or new_file.o5m. For safety reasons osmupdate will not delete\n"
" the old file. If you do not need it as backup file, please\n"
" delete it by yourself.\n"
"\n"
" ./osmupdate old_file.osm 2011-07-15T23:30:00Z new_file.osm\n"
" ./osmupdate old_file.osm NOW-86400 new_file.osm\n"
" If your old OSM data file does not contain a file timestamp,\n"
" or you do not want to rely on this timestamp, it can be\n"
" specified manually. Relative times are in seconds to NOW.\n"
"\n"
" ./osmupdate old_file.o5m change_file.o5c\n"
" ./osmupdate old_file.osm change_file.osc\n"
" ./osmupdate 2011-07-15T23:30:00Z change_file.o5c\n"
" ./osmupdate 2011-07-15T23:30:00Z change_file.osc.gz\n"
" ./osmupdate NOW-3600 change_file.osc.gz\n"
" Here, the old OSM data file is not updated directly. An OSM\n"
" changefile is written instead. This changefile can be used to\n"
" update the OSM data file afterwards.\n"
" You will have recognized the extension .gz in the last\n"
" example. In this case, the OSM Change file will be written\n"
" with gzip compression. To accomplish this, you need to have\n"
" the program gzip installed on your system.\n"
"\n"
" ./osmupdate london_old.o5m london_new.o5m -B=london.poly\n"
" The OSM data file london_old.o5m will be updated. Hence the\n"
" downloaded OSM changefiles contain not only London, but the\n"
" whole planet, a lot of unneeded data will be added to this\n"
" regional file. The -B= argument will clip these superfluous\n"
" data.\n"
"\n"
"The program osmupdate recognizes a few command line options:\n"
"\n"
"--max-days=UPDATE_RANGE\n"
" By default, the maximum time range for to assemble a\n"
" cumulated changefile is 250 days. You can change this by\n"
" giving a different maximum number of days, for example 300.\n"
" If you do, please ensure that there are daily change files\n"
" available for such a wide range of time.\n"
"\n"
"--minute\n"
"--hour\n"
"--day\n"
"--sporadic\n"
" By default, osmupdate uses a combination of minutely, hourly\n"
" and daily changefiles. If you want to limit these changefile\n"
" categories, use one or two of these options and choose that\n"
" category/ies you want to be used.\n"
" The option --sporadic allows processing changefile sources\n"
" which do not have the usual \"minute\", \"hour\" and \"day\"\n"
" subdirectories.\n"
"\n"
"--max-merge=COUNT\n"
" The subprogram osmconvert is able to merge more than two\n"
" changefiles in one run. This ability increases merging speed.\n"
" Unfortunately, every changefile consumes about 200 MB of main\n"
" memory while being processed. For this reason, the number of\n"
" parallely processable changefiles is limited.\n"
" Use this commandline argument to determine the maximum number\n"
" of parallely processed changefiles. The default value is 7.\n"
"\n"
"-t=TEMPPATH\n"
"--tempfiles=TEMPPATH\n"
" On order to cache changefiles, osmupdate needs a separate\n"
" directory. This parameter defines the name of this directory,\n"
" including the prefix of the tempfiles' names.\n"
" The default value is \"osmupdate_temp/temp\".\n"
"\n"
"--keep-tempfiles\n"
" Use this option if you want to keep local copies of every\n"
" downloaded file. This is strongly recommended if you are\n"
" going to assemble different changefiles which overlap in\n"
" time ranges. Your data traffic will be minimized.\n"
" Do not invoke this option if you are going to use different\n"
" change file sources (option --base-url). This would cause\n"
" severe data corruption.\n"
"\n"
"--trust-tempfiles\n"
" Use this option if you want to use the saved local copies\n"
" of already downloaded changefiles without checking their\n"
" lengths against to their server-hosted originals.\n"
" Downloads will be limited to files not saved yet.\n"
" Do not invoke this option if you suspect incomplete\n"
" downloads.\n"
"\n"
"--compression-level=LEVEL\n"
" Define level for gzip compression. Values between 1 (low\n"
" compression, but fast) and 9 (high compression, but slow).\n"
"\n"
"--base-url=BASE_URL\n"
" To accelerate downloads or to get regional file updates you\n"
" may specify an alternative download location. Please enter\n"
" its URL, or simply the word \"mirror\" if you want to use\n"
" gwdg's planet server.\n"
"\n"
"--base-url-suffix=BASE_URL_SUFFIX\n"
" To use old planet URLs, you may need to add the suffix\n"
" \"-replicate\" because it was custom to have this word in the\n"
" URL, right after the period identifier \"day\" etc.\n"
"\n"
"-v\n"
"--verbose\n"
" With activated \'verbose\' mode, some statistical data and\n"
" diagnosis data will be displayed.\n"
" If -v resp. --verbose is the first parameter in the line,\n"
" osmupdate will display all input parameters.\n"
"\n"
"This program is for experimental use. Expect malfunctions and data\n"
"loss. Do not use the program in productive or commercial systems.\n"
"\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"Please send any bug reports to [email protected]\n\n";
#define _FILE_OFFSET_BITS 64
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
typedef enum {false= 0,true= 1} bool;
typedef uint8_t byte;
typedef unsigned int uint;
#define isdig(x) isdigit((unsigned char)(x))
static int loglevel= 0; // logging to stderr;
// 0: no logging; 1: small logging; 2: normal logging;
// 3: extended logging;
#define DP(f) fprintf(stderr,"Debug: " #f "\n");
#define DPv(f,...) fprintf(stderr,"Debug: " #f "\n",__VA_ARGS__);
#define DPM(f,p,m) { byte* pp; int i,mm; static int msgn= 3; \
if(--msgn>=0) { fprintf(stderr,"Debug memory: " #f); \
pp= (byte*)(p); mm= (m); if(pp==NULL) fprintf(stderr,"\n (null)"); \
else for(i= 0; i<mm; i++) { \
if((i%16)==0) fprintf(stderr,"\n "); \
fprintf(stderr," %02x",*pp++); } \
fprintf(stderr,"\n"); } }
#if __WIN32__
#define NL "\r\n" // use CR/LF as new-line sequence
#define DIRSEP '\\'
#define DIRSEPS "\\"
#define DELFILE "del"
#define off_t off64_t
#define lseek lseek64
#else
#define NL "\n" // use LF as new-line sequence
#define DIRSEP '/'
#define DIRSEPS "/"
#define DELFILE "rm"
#define O_BINARY 0
#endif
static inline char *strmcpy(char *dest, const char *src, size_t maxlen) {
// similar to strcpy(), this procedure copies a character string;
// here, the length is cared about, i.e. the target string will
// be limited in case it is too long;
// src[]: source string which is to be copied;
// maxlen: maximum length of the destination string
// (including terminator null);
// return:
// dest[]: destination string of the copy; this is the
// function's return value too;
char* d;
if(maxlen==0)
return dest;
d= dest;
while(--maxlen>0 && *src!=0)
*d++= *src++;
*d= 0;
return dest;
} // end strmcpy()
#define strMcpy(d,s) strmcpy((d),(s),sizeof(d))
static inline char *stecpy(char** destp, char* destend,
const char* src) {
// same as stppcpy(), but you may define a pointer which the
// destination will not be allowed to cross, whether or not the
// string will be completed at that moment;
// in either case, the destination string will be terminated with 0;
char* dest;
dest= *destp;
if(dest>=destend)
return dest;
destend--;
while(*src!=0 && dest<destend)
*dest++= *src++;
*dest= 0;
*destp= dest;
return dest;
} // end stecpy()
static inline char *stpesccpy(char *dest, const char *src) {
// same as C99's stpcpy(), but all quotation marks, apostrophes
// and backslashes will be escaped (i.e., preceded) by backslashes;
// for windows, backslashes will not be escaped;
while(*src!=0) {
if(*src=='\'' || *src=='\"'
#if !__WIN32__
|| *src=='\\'
#endif
)
*dest++= '\\';
*dest++= *src++;
}
*dest= 0;
return dest;
} // stpesccpy()
static inline char *steesccpy(char **destp,char *destend,
const char *src) {
// same as stppesccpy(), but you may define a pointer which the
// destination will not be allowed to cross, whether or not the
// string will be completed at that moment;
// in either case, the destination string will be terminated with 0;
char* dest;
dest= *destp;
if(dest>=destend)
return dest;
destend-= 2;
while(*src!=0 && dest<destend) {
if(*src=='\'' || *src=='\"'
#if !__WIN32__
|| *src=='\\'
#endif
)
*dest++= '\\';
*dest++= *src++;
}
*dest= 0;
*destp= dest;
return dest;
} // steesccpy()
static inline char *stpmcpy(char *dest, const char *src, size_t maxlen) {
// similar to strmcpy(), this procedure copies a character string;
// however, it returns the address of the destination string's
// terminating zero character;
// this makes it easier to concatenate strings;
char* d;
if(maxlen==0)
return dest;
d= dest;
while(--maxlen>0 && *src!=0)
*d++= *src++;
*d= 0;
return d;
} // end stpmcpy()
#define stpMcpy(d,s) stpmcpy(d,s,sizeof(d))
static inline int strzcmp(const char* s1,const char* s2) {
// similar to strcmp(), this procedure compares two character strings;
// here, the number of characters which are to be compared is limited
// to the length of the second string;
// i.e., this procedure can be used to identify a short string s2
// within a long string s1;
// s1[]: first string;
// s2[]: string to compare with the first string;
// return:
// 0: both strings are identical; the first string may be longer than
// the second;
// -1: the first string is alphabetical smaller than the second;
// 1: the first string is alphabetical greater than the second;
while(*s1==*s2 && *s1!=0) { s1++; s2++; }
if(*s2==0)
return 0;
return *(unsigned char*)s1 < *(unsigned char*)s2? -1: 1;
} // end strzcmp()
static inline int strycmp(const char* s1,const char* s2) {
// similar to strcmp(), this procedure compares two character strings;
// here, both strings are end-aligned;
// not more characters will be compared than are existing in string s2;
// i.e., this procedure can be used to identify a file name extension;
const char* s1e;
int l;
l= strchr(s2,0)-s2;
s1e= strchr(s1,0);
if(s1e-s1<l)
return 1;
s1= s1e-l;
while(*s1==*s2 && *s1!=0) { s1++; s2++; }
if(*s2==0)
return 0;
return *(unsigned char*)s1 < *(unsigned char*)s2? -1: 1;
} // end strycmp()
static inline uint32_t strtouint32(const char* s) {
// read a number and convert it to an unsigned 32-bit integer;
// return: number;
int32_t i;
byte b;
i= 0;
for(;;) {
b= (byte)(*s++ -'0');
if(b>=10)
break;
i= i*10+b;
}
return i;
} // end strtouint32()
static inline int64_t strtosint64(const char* s) {
// read a number and convert it to a signed 64-bit integer;
// return: number;
int sign;
int64_t i;
byte b;
if(*s=='-') { s++; sign= -1; } else sign= 1;
i= 0;
for(;;) {
b= (byte)(*s++ -'0');
if(b>=10)
break;
i= i*10+b;
}
return i*sign;
} // end strtosint64()
static inline int64_t strtimetosint64(const char* s) {
// read a timestamp in OSM format, e.g.: "2010-09-30T19:23:30Z",
// and convert it to a signed 64-bit integer;
// also allowed: relative time to NOW, e.g.: "NOW-86400",
// which means '24 hours ago';
// return: time as a number (seconds since 1970);
// ==0: syntax error;
if(s[0]=='N') { // presumably a relative time to 'now'
if(s[1]!='O' || s[2]!='W' || (s[3]!='+' && s[3]!='-') ||
!isdig(s[4])) // wrong syntax
return 0;
s+= 3; // jump over "NOW"
if(*s=='+') s++; // jump over '+', if any
return time(NULL)+strtosint64(s);
} // presumably a relative time to 'now'
if((s[0]!='1' && s[0]!='2') ||
!isdig(s[1]) || !isdig(s[2]) || !isdig(s[3]) ||
s[4]!='-' || !isdig(s[5]) || !isdig(s[6]) ||
s[7]!='-' || !isdig(s[8]) || !isdig(s[9]) ||
s[10]!='T' || !isdig(s[11]) || !isdig(s[12]) ||
s[13]!=':' || !isdig(s[14]) || !isdig(s[15]) ||
s[16]!=':' || !isdig(s[17]) || !isdig(s[18]) ||
s[19]!='Z') // wrong syntax
return 0;
/* regular timestamp */ {
struct tm tm;
tm.tm_isdst= 0;
tm.tm_year=
(s[0]-'0')*1000+(s[1]-'0')*100+(s[2]-'0')*10+(s[3]-'0')-1900;
tm.tm_mon= (s[5]-'0')*10+s[6]-'0'-1;
tm.tm_mday= (s[8]-'0')*10+s[9]-'0';
tm.tm_hour= (s[11]-'0')*10+s[12]-'0';
tm.tm_min= (s[14]-'0')*10+s[15]-'0';
tm.tm_sec= (s[17]-'0')*10+s[18]-'0';
#if __WIN32__
return mktime(&tm)-timezone;
#else
return timegm(&tm);
#endif
} // regular timestamp
} // end strtimetosint64()
static inline void int64tostrtime(uint64_t v,char* sp) {
// write a timestamp in OSM format, e.g.: "2010-09-30T19:23:30Z",
// into a string;
// v: value of the timestamp;
// sp[21]: destination string;
time_t vtime;
struct tm tm;
int i;
vtime= v;
#if __WIN32__
memcpy(&tm,gmtime(&vtime),sizeof(tm));
#else
gmtime_r(&vtime,&tm);
#endif
i= tm.tm_year+1900;
sp+= 3; *sp--= i%10+'0';
i/=10; *sp--= i%10+'0';
i/=10; *sp--= i%10+'0';
i/=10; *sp= i%10+'0';
sp+= 4; *sp++= '-';
i= tm.tm_mon+1;
*sp++= i/10+'0'; *sp++= i%10+'0'; *sp++= '-';
i= tm.tm_mday;
*sp++= i/10+'0'; *sp++= i%10+'0'; *sp++= 'T';
i= tm.tm_hour;
*sp++= i/10+'0'; *sp++= i%10+'0'; *sp++= ':';
i= tm.tm_min;
*sp++= i/10+'0'; *sp++= i%10+'0'; *sp++= ':';
i= tm.tm_sec%60;
*sp++= i/10+'0'; *sp++= i%10+'0'; *sp++= 'Z'; *sp= 0;
} // end int64tostrtime()
static inline bool file_exists(const char* file_name) {
// query if a file exists;
// file_name[]: name of the file in question;
// return: the file exists;
return access(file_name,R_OK)==0;
} // file_exists()
static inline int64_t file_length(const char* file_name) {
// retrieve length of a file;
// file_name[]: file name;
// return: number of bytes of this file;
// if the file could not be accessed, the return value is -1;
struct stat s;
int r;
r= stat(file_name,&s);
if(r==0)
return s.st_size;
#if !__WIN32__
if(errno==EOVERFLOW) // size larger than 2^31
return 0x7fffffff;
#endif
return -1;
} // end file_length()
//------------------------------------------------------------
// Module Global global variables for this program
//------------------------------------------------------------
// to distinguish global variable from local or module global
// variables, they are preceded by 'global_';
static const char global_osmconvert_program_here_in_dir[]=
"./osmconvert";
static const char* global_osmconvert_program=
global_osmconvert_program_here_in_dir+2;
// path to osmconvert program
static char global_tempfile_name[450]= "";
// prefix of names for temporary files
static bool global_keep_tempfiles= false;
// temporary files shall not be deleted at program end
static bool global_trust_tempfiles= false;
// Cached files are considered to be intact
static char global_osmconvert_arguments[2000]= "";
// general command line arguments for osmconvert;
#define max_number_of_changefiles_in_cache 100
static int global_max_merge= 7;
// maximum number of parallely processed changefiles
static const char* global_gzip_parameters= "";
// parameters for gzip compression
static char global_base_url[400]=
"https://planet.openstreetmap.org/replication";
static char global_base_url_suffix[100]="";
// for old replication URL, to get "day-replication" instead of "day"
#define PERR(f) \
fprintf(stderr,"osmupdate Error: " f "\n");
// print error message
#define PERRv(f,...) \
fprintf(stderr,"osmupdate Error: " f "\n",__VA_ARGS__);
// print error message with value(s)
#define WARN(f) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmupdate Warning: " f "\n"); }
// print a warning message, do it maximal 3 times
#define WARNv(f,...) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmupdate Warning: " f "\n",__VA_ARGS__); }
// print a warning message with value(s), do it maximal 3 times
#define PINFO(f) \
fprintf(stderr,"osmupdate: " f "\n"); // print info message
#define PINFOv(f,...) \
fprintf(stderr,"osmupdate: " f "\n",__VA_ARGS__);
#define ONAME(i) \
(i==0? "node": i==1? "way": i==2? "relation": "unknown object")
//------------------------------------------------------------
// end Module Global global variables for this program
//------------------------------------------------------------
static void shell_command(const char* command,char* result) {
// execute a shell command;
// command[]: shell command;
// result[1000]: result of the command;
FILE* fp;
char* result_p;
int maxlen;
int r;
if(loglevel>=2)
PINFOv("Executing shell command:\n%s",command)
fp= popen(command,"r");
if(fp==NULL) {
PERR("Could not execute shell command.")
result[0]= 0;
exit(1);
}
result_p= result;
maxlen= 1000-1;
while(maxlen>0) {
r= read(fileno(fp),result_p,maxlen);
if(r==0) // end of data
break;
if(r<0)
exit(errno); // (thanks to Ben Konrath)
result_p+= r;
maxlen-= r;
}
*result_p= 0;
if(pclose(fp)==-1)
exit(errno); // (thanks to Ben Konrath)
if(loglevel>=2)
PINFOv("Got shell command result:\n%s",result)
} // end shell_command()
typedef enum {cft_UNKNOWN,cft_MINUTELY,cft_HOURLY,cft_DAILY,cft_SPORADIC}
changefile_type_t;
#define CFTNAME(i) \
(i==cft_MINUTELY? "minutely": i==cft_HOURLY? "hourly": \
i==cft_DAILY? "daily": i==cft_SPORADIC? "sporadic": "unknown")
static int64_t get_file_timestamp(const char* file_name) {
// get the timestamp of a specific file;
// if the file timestamp is not available, this procedure tries
// to retrieve the timestamp from the file's statistics;
// return: timestamp of the file (seconds from Jan 01 1970);
// ==0: no file timestamp available;
char command[500],*command_p,result[1000];
char* command_e= command+sizeof(command);
int64_t file_timestamp;
command_p= command;
stecpy(&command_p,command_e,global_osmconvert_program);
stecpy(&command_p,command_e," --out-timestamp \"");
steesccpy(&command_p,command_e,file_name);
stecpy(&command_p,command_e,"\" 2>&1");
shell_command(command,result);
if(result[0]!='(' && (result[0]<'1' || result[0]>'2')) {
// command not found
PERR("Please install program osmconvert first.")
exit(1);
} // command not found
file_timestamp= strtimetosint64(result);
if(file_timestamp==0) { // the file has no file timestamp
// try to get the timestamp from the file's statistics
char* p;
if(loglevel>0) { // verbose mode
PINFOv("file %s has no file timestamp.",file_name)
PINFO("Running statistics to get the timestamp.")
}
command_p= command;
stecpy(&command_p,command_e,global_osmconvert_program);
stecpy(&command_p,command_e," --out-statistics \"");
steesccpy(&command_p,command_e,file_name);
stecpy(&command_p,command_e,"\" 2>&1");
shell_command(command,result);
p= strstr(result,"timestamp max: ");
if(p!=NULL) {
file_timestamp= strtimetosint64(p+15);
PINFO("Aging the timestamp by 4 hours for safety reasons.")
file_timestamp-= 4*3600;
}
} // the file has no file timestamp
if(loglevel>0) { // verbose mode
char ts[30];
if(file_timestamp==0)
strcpy(ts,"(no timestamp)");
else
int64tostrtime(file_timestamp,ts);
PINFOv("timestamp of %s: %s",file_name,ts)
} // verbose mode
return file_timestamp;
} // get_file_timestamp()
static int64_t get_newest_changefile_timestamp(
changefile_type_t changefile_type,int32_t* file_sequence_number) {
// get sequence number and timestamp of the newest changefile
// of a specific changefile type;
// changefile_type: minutely, hourly, daily, sporadic changefile;
// return: timestamp of the file (seconds from Jan 01 1970);
// ==0: no file timestamp available;
// *file_sequence_number: sequence number of the newest changefile;
static bool firstrun= true;
char command[1000],*command_p;
char* command_e= command+sizeof(command);
char result[1000];
int64_t changefile_timestamp;
#if __WIN32__
static char newest_timestamp_file_name[400];
if(firstrun) {
// create the file name for the newest timestamp;
// this is only needed for Windows as Windows' wget
// cannot write the downloaded file to standard output;
// usually: "osmupdate_temp/temp.7"
strcpy(stpmcpy(newest_timestamp_file_name,global_tempfile_name,
sizeof(newest_timestamp_file_name)-5),".7");
}
#endif
command_p= command;
stecpy(&command_p,command_e,
"wget -q ");
stecpy(&command_p,command_e,global_base_url);
switch(changefile_type) { // changefile type
case cft_MINUTELY:
stecpy(&command_p,command_e,"/minute");
break;
case cft_HOURLY:
stecpy(&command_p,command_e,"/hour");
break;
case cft_DAILY:
stecpy(&command_p,command_e,"/day");
break;
case cft_SPORADIC:
break;
default: // invalid change file type
return 0;
} // changefile type
stecpy(&command_p,command_e,global_base_url_suffix);
stecpy(&command_p,command_e,"/state.txt");
#if __WIN32__
stecpy(&command_p,command_e," -O \"");
steesccpy(&command_p,command_e,newest_timestamp_file_name);
stecpy(&command_p,command_e,"\" 2>&1");
#else
stecpy(&command_p,command_e," -O - 2>&1");
#endif
shell_command(command,result);
if(firstrun) { // first run
firstrun= false;
if(result[0]!='#' && (result[0]<'1' || result[0]>'2') && (
strstr(result,"not found")!=NULL ||
strstr(result,"cannot find")!=NULL)) { // command not found
PERR("Please install program wget first.")
exit(1);
} // command not found
} // first run
#if __WIN32__
result[0]= 0;
/* copy tempfile contents to result[] */ {
int fd,r;
fd= open(newest_timestamp_file_name,O_RDONLY);
if(fd>0) {
r= read(fd,result,sizeof(result)-1);
if(r>=0)
result[r]= 0;
close(fd);
}
}
if(loglevel<2)
unlink(newest_timestamp_file_name);
#endif
if(result[0]=='#') { // full status information
// get sequence number
char* sequence_number_p;
sequence_number_p= strstr(result,"sequenceNumber=");
if(sequence_number_p!=NULL) // found sequence number
*file_sequence_number= strtouint32(sequence_number_p+15);
// get timestamp
char* timestamp_p;
timestamp_p= strstr(result,"timestamp=");
// search timestamp line
if(timestamp_p!=NULL && timestamp_p-result<sizeof(result)-30) {
// found timestamp line
// cpy timestam to begin of result[]
timestamp_p+= 10; // jump over text
memcpy(result,timestamp_p,13);
memcpy(result+13,timestamp_p+14,3);
memcpy(result+16,timestamp_p+18,4);
} // found timestamp line
} // full status information
changefile_timestamp= strtimetosint64(result);
if(loglevel>0) { // verbose mode
char ts[30];
if(changefile_timestamp==0)
strcpy(ts,"(no timestamp)");
else
int64tostrtime(changefile_timestamp,ts);
PINFOv("newest %s timestamp: %s",CFTNAME(changefile_type),ts)
} // verbose mode
return changefile_timestamp;
} // get_newest_changefile_timestamp
static int64_t get_changefile_timestamp(
changefile_type_t changefile_type,int32_t file_sequence_number) {
// download and inspect the timestamp of a specific changefile which
// is available in the Internet;
// a timestamp file will not be downloaded if it
// already exists locally as temporary file;
// changefile_type: minutely, hourly, daily, sporadic changefile;
// file_sequence_number: sequence number of the file;
// uses:
// global_tempfile_name
// return: timestamp of the changefile (seconds from Jan 01 1970);
// ==0: no file timestamp available;
char command[2000]; char* command_p;
char* command_e= command+sizeof(command);
char result[1000];
char timestamp_cachefile_name[400];
int fd,r; // file descriptor; number of bytes which have been read
char timestamp_contents[1000]; // contents of the timestamp
int64_t changefile_timestamp;
char* sp;
// create the file name for the cached timestamp; example:
// "osmupdate_temp/temp.m000012345.txt"
sp= stpmcpy(timestamp_cachefile_name,global_tempfile_name,
sizeof(timestamp_cachefile_name[0])-20);
*sp++= '.';
*sp++= CFTNAME(changefile_type)[0];
// 'm', 'h', 'd', 's': minutely, hourly, daily, sporadic timestamps
sprintf(sp,"%09"PRIi32".txt",file_sequence_number);
// add sequence number and file name extension
// download the timestamp into a cache file
if(file_length(timestamp_cachefile_name)<10) {
// timestamp has not been downloaded yet
command_p= command;
stecpy(&command_p,command_e,"wget -nv -c ");
stecpy(&command_p,command_e,global_base_url);
if(changefile_type==cft_MINUTELY)
stecpy(&command_p,command_e,"/minute");
else if(changefile_type==cft_HOURLY)
stecpy(&command_p,command_e,"/hour");
else if(changefile_type==cft_DAILY)
stecpy(&command_p,command_e,"/day");
else if(changefile_type==cft_SPORADIC)
;
else // invalid change file type
return 0;
stecpy(&command_p,command_e,global_base_url_suffix);
stecpy(&command_p,command_e,"/");
/* assemble Sequence path */ {
int l;
l= sprintf(command_p,"%03i/%03i/%03i",
file_sequence_number/1000000,file_sequence_number/1000%1000,
file_sequence_number%1000);
command_p+= l;
}
stecpy(&command_p,command_e,".state.txt -O \"");
steesccpy(&command_p,command_e,timestamp_cachefile_name);
stecpy(&command_p,command_e,"\" 2>&1");
shell_command(command,result);
} // timestamp has not been downloaded yet
// read the timestamp cache file
fd= open(timestamp_cachefile_name,O_RDONLY|O_BINARY);
if(fd<=0) // could not open the file
timestamp_contents[0]= 0; // hence we did not read anything
else { // could open the file
r= read(fd,timestamp_contents,sizeof(timestamp_contents)-1);
if(r<0) r= 0;
timestamp_contents[r]= 0; // set string terminator
close(fd);
} // could open the file
// parse the timestamp information
if(timestamp_contents[0]=='#') { // full status information
// get timestamp
char* timestamp_p;
timestamp_p= strstr(timestamp_contents,"timestamp=");
// search timestamp line
if(timestamp_p!=NULL &&
timestamp_p-timestamp_contents<sizeof(timestamp_contents)-30) {
// found timestamp line
// copy timestamp to begin of timestamp_contents[]
timestamp_p+= 10; // jump over text
memcpy(timestamp_contents,timestamp_p,13);
memcpy(timestamp_contents+13,timestamp_p+14,3);
memcpy(timestamp_contents+16,timestamp_p+18,4);
} // found timestamp line
} // full status information
changefile_timestamp= strtimetosint64(timestamp_contents);
if(loglevel>0) { // verbose mode
char ts[30];
if(changefile_timestamp==0)
strcpy(ts,"(no timestamp)");
else
int64tostrtime(changefile_timestamp,ts);
PINFOv("%s changefile %i: %s",
CFTNAME(changefile_type),file_sequence_number,ts)
} // verbose mode
if(changefile_timestamp==0) { // no timestamp
if(file_sequence_number==0) // first file in repository
changefile_timestamp= 1; // set virtual very old timestamp
else {
PERRv("no timestamp for %s changefile %i.",
CFTNAME(changefile_type),file_sequence_number)
exit(1);
}
} // no timestamp
return changefile_timestamp;
} // get_changefile_timestamp
static void process_changefile(
changefile_type_t changefile_type,int32_t file_sequence_number,
int64_t new_timestamp) {
// download and process a change file;
// change files will not be processed one by one, but cumulated
// until some files have been downloaded and then processed in a group;
// a file will not be downloaded if it already exists locally as
// temporary file;
// changefile_type: minutely, hourly, daily, sporadic changefile;
// file_sequence_number: sequence number of the file;
// ==0: process the remaining files which
// are waiting in the cache; cleanup;
// new_timestamp: timestamp of the new file which is to be created;
// ==0: the procedure will assume the newest of all
// timestamps which has been passed since the
// program has been started;
// uses:
// global_max_merge
// global_tempfile_name
static bool firstrun= true;
static int number_of_changefiles_in_cache= 0;
static int64_t newest_new_timestamp= 0;
static char master_cachefile_name[400];
static char master_cachefile_name_temp[400];
static char cachefile_name[max_number_of_changefiles_in_cache][400];
char command[4000+200*max_number_of_changefiles_in_cache];
char* command_e= command+sizeof(command);
char* command_p;
char result[1000];
if(firstrun) {
firstrun= false;
// create the file name for the cached master changefile;
// usually: "osmupdate_temp/temp.8"
strcpy(stpmcpy(master_cachefile_name,global_tempfile_name,
sizeof(master_cachefile_name)-5),".8");
strcpy(stpmcpy(master_cachefile_name_temp,global_tempfile_name,
sizeof(master_cachefile_name_temp)-5),".9");
unlink(master_cachefile_name);
unlink(master_cachefile_name_temp);
}
if(new_timestamp>newest_new_timestamp)
newest_new_timestamp= new_timestamp;
if(file_sequence_number!=0) { // changefile download requested
char* this_cachefile_name=
cachefile_name[number_of_changefiles_in_cache];
int64_t old_file_length;
char* sp;
// create the file name for the cached changefile; example:
// "osmupdate_temp/temp.m000012345.osc.gz"
sp= stpmcpy(this_cachefile_name,global_tempfile_name,
sizeof(cachefile_name[0])-20);
*sp++= '.';
*sp++= CFTNAME(changefile_type)[0];
// 'm', 'h', 'd', 's': minutely, hourly, daily,
// sporadic changefiles
sprintf(sp,"%09"PRIi32".osc.gz",file_sequence_number);
// add sequence number and file name extension
// assemble the URL and download the changefile
old_file_length= file_length(this_cachefile_name);
if(global_trust_tempfiles && old_file_length>=10) {
// trusted file already in cache
if(loglevel>0) // verbose mode
PINFOv("%s changefile %i: trusting local copy",
CFTNAME(changefile_type),file_sequence_number)
} // trusted file already in cache
else { // file not in cache or not trusted
if(loglevel>0) { // verbose mode
if(old_file_length<10) // file not downloaded yet
PINFOv("%s changefile %i: downloading",
CFTNAME(changefile_type),file_sequence_number)
else // file had been downloaded at least partially
PINFOv("%s changefile %i: checking",
CFTNAME(changefile_type),file_sequence_number)
} // verbose mode
command_p= command;
stecpy(&command_p,command_e,"wget -nv -c ");
stecpy(&command_p,command_e,global_base_url);
switch(changefile_type) { // changefile type
case cft_MINUTELY:
stecpy(&command_p,command_e,"/minute");
break;
case cft_HOURLY:
stecpy(&command_p,command_e,"/hour");
break;
case cft_DAILY:
stecpy(&command_p,command_e,"/day");
break;
case cft_SPORADIC:
break;
default: // invalid change file type
return;
} // changefile type
stecpy(&command_p,command_e,global_base_url_suffix);
stecpy(&command_p,command_e,"/");
/* process sequence number */ {
int l;
l= sprintf(command_p,"%03i/%03i/%03i.osc.gz",
file_sequence_number/1000000,file_sequence_number/1000%1000,
file_sequence_number%1000);
command_p+= l;
} // process sequence number
stecpy(&command_p,command_e," -O \"");
steesccpy(&command_p,command_e,this_cachefile_name);
stecpy(&command_p,command_e,"\" 2>&1 && echo \"Wget Command Ok\"");
shell_command(command,result);
if(strstr(result,"Wget Command Ok")==NULL) { // download error
PERRv("Could not download %s changefile %i",
CFTNAME(changefile_type),file_sequence_number)
PINFOv("wget Error message:\n%s",result)
exit(1);
}
if(loglevel>0 && old_file_length>=10) {
// verbose mode AND file was already in cache
if(file_length(this_cachefile_name)!=old_file_length)
PINFOv("%s changefile %i: download completed",
CFTNAME(changefile_type),file_sequence_number)
else
PINFOv("%s changefile %i: already in cache",
CFTNAME(changefile_type),file_sequence_number)
} // verbose mode
} // file not in cache or not trusted
number_of_changefiles_in_cache++;
} // changefile download requested
if(number_of_changefiles_in_cache>=global_max_merge
|| (file_sequence_number==0 && number_of_changefiles_in_cache>0)) {
// at least one change files must be merged
// merge all changefiles which are waiting in cache
if(loglevel>0)