@@ -520,8 +520,8 @@ GMT_LOCAL size_t gmtremote_skip_large_files (struct GMT_CTRL *GMT, char * URL, s
520
520
#define GMT_HASH_TIME_OUT 10L /* Not waiting longer than this to time out on getting the hash file */
521
521
522
522
GMT_LOCAL int gmtremote_get_url (struct GMT_CTRL * GMT , char * url , char * file , char * orig , unsigned int index ) {
523
- bool query = gmt_M_file_is_query (url );
524
- int curl_err = 0 ;
523
+ bool query = gmt_M_file_is_query (url ), turn_off = false ;
524
+ int curl_err = 0 , error = 0 ;
525
525
long time_spent ;
526
526
char * Lfile = NULL ;
527
527
FILE * fp = NULL ;
@@ -534,48 +534,49 @@ GMT_LOCAL int gmtremote_get_url (struct GMT_CTRL *GMT, char *url, char *file, ch
534
534
Lfile = gmtremote_lockfile (API , file );
535
535
if ((fp = fopen (Lfile , "w" )) == NULL ) {
536
536
GMT_Report (API , GMT_MSG_ERROR , "Failed to create lock file %s\n" , Lfile );
537
+ gmt_M_str_free (Lfile );
537
538
return 1 ;
538
539
}
539
540
gmtlib_file_lock (GMT , fileno (fp )); /* Attempt exclusive lock */
540
541
}
541
542
542
543
if ((Curl = curl_easy_init ()) == NULL ) {
543
544
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to initiate curl - cannot obtain %s\n" , url );
544
- return 1 ;
545
+ error = 1 ; goto unlocking1 ;
545
546
}
546
547
if (curl_easy_setopt (Curl , CURLOPT_SSL_VERIFYPEER , 0L )) { /* Tell libcurl to not verify the peer */
547
548
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to set curl option to not verify the peer\n" );
548
- return 1 ;
549
+ error = 1 ; goto unlocking1 ;
549
550
}
550
551
if (curl_easy_setopt (Curl , CURLOPT_FOLLOWLOCATION , 1L )) { /* Tell libcurl to follow 30x redirects */
551
552
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to set curl option to follow redirects\n" );
552
- return 1 ;
553
+ error = 1 ; goto unlocking1 ;
553
554
}
554
555
if (curl_easy_setopt (Curl , CURLOPT_FAILONERROR , 1L )) { /* Tell libcurl to fail on 4xx responses (e.g. 404) */
555
556
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to set curl option to fail for 4xx responses\n" );
556
- return 1 ;
557
+ error = 1 ; goto unlocking1 ;
557
558
}
558
559
if (curl_easy_setopt (Curl , CURLOPT_URL , url )) { /* Set the URL to copy */
559
560
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to set curl option to read from %s\n" , url );
560
- return 1 ;
561
+ error = 1 ; goto unlocking1 ;
561
562
}
562
563
if (curl_easy_setopt (Curl , CURLOPT_TIMEOUT , GMT_HASH_TIME_OUT )) { /* Set a max timeout */
563
564
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to set curl option to time out after %ld seconds\n" , GMT_HASH_TIME_OUT );
564
- return 1 ;
565
+ error = 1 ; goto unlocking1 ;
565
566
}
566
567
urlfile .filename = file ; /* Set pointer to local filename */
567
568
/* Define our callback to get called when there's data to be written */
568
569
if (curl_easy_setopt (Curl , CURLOPT_WRITEFUNCTION , gmtremote_fwrite_callback )) {
569
570
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to set curl output callback function\n" );
570
- return 1 ;
571
+ error = 1 ; goto unlocking1 ;
571
572
}
572
573
/* Set a pointer to our struct to pass to the callback */
573
574
if (curl_easy_setopt (Curl , CURLOPT_WRITEDATA , & urlfile )) {
574
575
GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Failed to set curl option to write to %s\n" , file );
575
- return 1 ;
576
+ error = 1 ; goto unlocking1 ;
576
577
}
577
578
GMT_Report (GMT -> parent , GMT_MSG_INFORMATION , "Downloading file %s ...\n" , url );
578
- gmtremote_turn_on_ctrl_C_check (file );
579
+ gmtremote_turn_on_ctrl_C_check (file ); turn_off = true;
579
580
begin = time (NULL );
580
581
if ((curl_err = curl_easy_perform (Curl ))) { /* Failed, give error message */
581
582
end = time (NULL );
@@ -598,21 +599,23 @@ GMT_LOCAL int gmtremote_get_url (struct GMT_CTRL *GMT, char *url, char *file, ch
598
599
GMT -> current .io .refreshed [index ] = GMT -> current .io .internet_error = true;
599
600
}
600
601
}
601
- return 1 ;
602
+ error = 1 ; goto unlocking1 ;
602
603
}
603
604
curl_easy_cleanup (Curl );
604
605
if (urlfile .fp ) /* close the local file */
605
606
fclose (urlfile .fp );
606
607
608
+ unlocking1 :
609
+
607
610
if (!query ) { /* Remove lock file after successful download */
608
611
gmtlib_file_unlock (GMT , fileno (fp ));
609
612
fclose (fp );
610
613
gmt_remove_file (GMT , Lfile );
611
614
gmt_M_str_free (Lfile );
612
615
}
613
616
614
- gmtremote_turn_off_ctrl_C_check ();
615
- return 0 ;
617
+ if ( turn_off ) gmtremote_turn_off_ctrl_C_check ();
618
+ return error ;
616
619
}
617
620
618
621
GMT_LOCAL struct GMT_DATA_HASH * gmtremote_hash_load (struct GMT_CTRL * GMT , char * file , int * n ) {
@@ -1111,8 +1114,8 @@ int gmtlib_file_is_jpeg2000_tile (struct GMTAPI_CTRL *API, char *file) {
1111
1114
}
1112
1115
1113
1116
int gmt_download_file (struct GMT_CTRL * GMT , const char * name , char * url , char * localfile , bool be_fussy ) {
1114
- bool query = gmt_M_file_is_query (url );
1115
- int curl_err , error ;
1117
+ bool query = gmt_M_file_is_query (url ), turn_off = false ;
1118
+ int curl_err , error = 0 ;
1116
1119
size_t fsize ;
1117
1120
char * Lfile = NULL ;
1118
1121
CURL * Curl = NULL ;
@@ -1141,47 +1144,48 @@ int gmt_download_file (struct GMT_CTRL *GMT, const char *name, char *url, char *
1141
1144
Lfile = gmtremote_lockfile (API , (char * )name );
1142
1145
if ((fp = fopen (Lfile , "w" )) == NULL ) {
1143
1146
GMT_Report (API , GMT_MSG_ERROR , "Failed to create lock file %s\n" , Lfile );
1147
+ gmt_M_str_free (Lfile );
1144
1148
return 1 ;
1145
1149
}
1146
1150
gmtlib_file_lock (GMT , fileno (fp )); /* Attempt exclusive lock */
1147
1151
}
1148
1152
1149
1153
if ((Curl = curl_easy_init ()) == NULL ) {
1150
1154
GMT_Report (API , GMT_MSG_ERROR , "Failed to initiate curl\n" );
1151
- return 1 ;
1155
+ error = 1 ; goto unlocking2 ;
1152
1156
}
1153
1157
if (curl_easy_setopt (Curl , CURLOPT_SSL_VERIFYPEER , 0L )) { /* Tell libcurl to not verify the peer */
1154
1158
GMT_Report (API , GMT_MSG_ERROR , "Failed to set curl option to not verify the peer\n" );
1155
- return 1 ;
1159
+ error = 1 ; goto unlocking2 ;
1156
1160
}
1157
1161
if (curl_easy_setopt (Curl , CURLOPT_FOLLOWLOCATION , 1L )) { /* Tell libcurl to follow 30x redirects */
1158
1162
GMT_Report (API , GMT_MSG_ERROR , "Failed to set curl option to follow redirects\n" );
1159
- return 1 ;
1163
+ error = 1 ; goto unlocking2 ;
1160
1164
}
1161
1165
if (curl_easy_setopt (Curl , CURLOPT_FAILONERROR , 1L )) { /* Tell libcurl to fail on 4xx responses (e.g. 404) */
1162
- return 1 ;
1166
+ error = 1 ; goto unlocking2 ;
1163
1167
}
1164
1168
1165
1169
if (curl_easy_setopt (Curl , CURLOPT_URL , url )) { /* Set the URL to copy */
1166
1170
GMT_Report (API , GMT_MSG_ERROR , "Failed to set curl option to read from %s\n" , url );
1167
- return 1 ;
1171
+ error = 1 ; goto unlocking2 ;
1168
1172
}
1169
1173
urlfile .filename = localfile ; /* Set pointer to local filename */
1170
1174
/* Define our callback to get called when there's data to be written */
1171
1175
if (curl_easy_setopt (Curl , CURLOPT_WRITEFUNCTION , gmtremote_fwrite_callback )) {
1172
1176
GMT_Report (API , GMT_MSG_ERROR , "Failed to set curl output callback function\n" );
1173
- return 1 ;
1177
+ error = 1 ; goto unlocking2 ;
1174
1178
}
1175
1179
/* Set a pointer to our struct to pass to the callback */
1176
1180
if (curl_easy_setopt (Curl , CURLOPT_WRITEDATA , & urlfile )) {
1177
1181
GMT_Report (API , GMT_MSG_ERROR , "Failed to set curl option to write to %s\n" , localfile );
1178
- return 1 ;
1182
+ error = 1 ; goto unlocking2 ;
1179
1183
}
1180
1184
1181
1185
gmtremote_find_and_give_data_attribution (API , name );
1182
1186
1183
1187
GMT_Report (API , GMT_MSG_INFORMATION , "Downloading file %s ...\n" , url );
1184
- gmtremote_turn_on_ctrl_C_check (localfile );
1188
+ gmtremote_turn_on_ctrl_C_check (localfile ); turn_off = true;
1185
1189
if ((curl_err = curl_easy_perform (Curl ))) { /* Failed, give error message */
1186
1190
if (be_fussy || !(curl_err == CURLE_REMOTE_FILE_NOT_FOUND || curl_err == CURLE_HTTP_RETURNED_ERROR )) { /* Unexpected failure - want to bitch about it */
1187
1191
GMT_Report (API , GMT_MSG_ERROR , "Libcurl Error: %s\n" , curl_easy_strerror (curl_err ));
@@ -1200,16 +1204,18 @@ int gmt_download_file (struct GMT_CTRL *GMT, const char *name, char *url, char *
1200
1204
if (urlfile .fp ) /* close the local file */
1201
1205
fclose (urlfile .fp );
1202
1206
1207
+ unlocking2 :
1208
+
1203
1209
if (!query ) { /* Remove lock file after successful download */
1204
1210
gmtlib_file_unlock (GMT , fileno (fp ));
1205
1211
fclose (fp );
1206
1212
gmt_remove_file (GMT , Lfile );
1207
1213
gmt_M_str_free (Lfile );
1208
1214
}
1209
1215
1210
- gmtremote_turn_off_ctrl_C_check ();
1216
+ if ( turn_off ) gmtremote_turn_off_ctrl_C_check ();
1211
1217
1212
- error = gmtremote_convert_jp2_to_nc (API , localfile );
1218
+ if ( error == 0 ) error = gmtremote_convert_jp2_to_nc (API , localfile );
1213
1219
1214
1220
return (error );
1215
1221
}
0 commit comments