@@ -687,7 +687,7 @@ void gmtremote_lock_off (struct GMT_CTRL *GMT, struct LOCFILE_FP **P) {
687
687
688
688
/* Deal with hash values of cache/data files */
689
689
690
- GMT_LOCAL int gmtremote_get_url (struct GMT_CTRL * GMT , char * url , char * file , char * orig , unsigned int index ) {
690
+ GMT_LOCAL int gmtremote_get_url (struct GMT_CTRL * GMT , char * url , char * file , char * orig , unsigned int index , bool do_lock ) {
691
691
bool turn_ctrl_C_off = false;
692
692
int curl_err = 0 , error = GMT_NOERROR ;
693
693
long time_spent ;
@@ -704,13 +704,13 @@ GMT_LOCAL int gmtremote_get_url (struct GMT_CTRL *GMT, char *url, char *file, ch
704
704
if (GMT -> current .io .internet_error ) return 1 ; /* Not able to use remote copying in this session */
705
705
706
706
/* Make a lock */
707
- if ((LF = gmtremote_lock_on (GMT , file )) == NULL )
707
+ if (do_lock && (LF = gmtremote_lock_on (GMT , file )) == NULL )
708
708
return 1 ;
709
709
710
710
/* If file locking held us up as another process was downloading the same file,
711
711
* then that file should now be available. So we check again if it is before proceeding */
712
712
713
- if (!access (file , F_OK ))
713
+ if (do_lock && !access (file , F_OK ))
714
714
goto unlocking1 ; /* Yes it was, unlock and return no error */
715
715
716
716
/* Initialize the curl session */
@@ -750,7 +750,7 @@ GMT_LOCAL int gmtremote_get_url (struct GMT_CTRL *GMT, char *url, char *file, ch
750
750
unlocking1 :
751
751
752
752
/* Remove lock file after successful download */
753
- gmtremote_lock_off (GMT , & LF );
753
+ if ( do_lock ) gmtremote_lock_off (GMT , & LF );
754
754
755
755
if (turn_ctrl_C_off ) gmtremote_turn_off_ctrl_C_check ();
756
756
@@ -812,6 +812,7 @@ GMT_LOCAL int gmtremote_refresh (struct GMTAPI_CTRL *API, unsigned int index) {
812
812
time_t mod_time , right_now = time (NULL ); /* Unix time right now */
813
813
char indexpath [PATH_MAX ] = {"" }, old_indexpath [PATH_MAX ] = {"" }, new_indexpath [PATH_MAX ] = {"" }, url [PATH_MAX ] = {"" };
814
814
const char * index_file = (index == GMT_HASH_INDEX ) ? GMT_HASH_SERVER_FILE : GMT_INFO_SERVER_FILE ;
815
+ struct LOCFILE_FP * LF = NULL ;
815
816
struct GMT_CTRL * GMT = API -> GMT ; /* Short hand */
816
817
817
818
if (GMT -> current .io .refreshed [index ]) return GMT_NOERROR ; /* Already been here */
@@ -827,7 +828,7 @@ GMT_LOCAL int gmtremote_refresh (struct GMTAPI_CTRL *API, unsigned int index) {
827
828
}
828
829
snprintf (url , PATH_MAX , "%s/%s" , gmt_dataserver_url (API ), index_file );
829
830
GMT_Report (API , GMT_MSG_DEBUG , "Download remote file %s for the first time\n" , url );
830
- if (gmtremote_get_url (GMT , url , indexpath , NULL , index )) {
831
+ if (gmtremote_get_url (GMT , url , indexpath , NULL , index , true )) {
831
832
GMT_Report (API , GMT_MSG_INFORMATION , "Failed to get remote file %s\n" , url );
832
833
if (!access (indexpath , F_OK )) gmt_remove_file (GMT , indexpath ); /* Remove index file just in case it got corrupted or zero size */
833
834
GMT -> current .setting .auto_download = GMT_NO_DOWNLOAD ; /* Temporarily turn off auto download in this session only */
@@ -862,21 +863,39 @@ GMT_LOCAL int gmtremote_refresh (struct GMTAPI_CTRL *API, unsigned int index) {
862
863
strcpy (old_indexpath , indexpath ); /* Duplicate path name */
863
864
strcat (old_indexpath , ".old" ); /* Append .old to the copied path */
864
865
snprintf (url , PATH_MAX , "%s/%s" , gmt_dataserver_url (API ), index_file ); /* Set remote path to new index file */
865
- if (gmtremote_get_url (GMT , url , new_indexpath , indexpath , index )) { /* Get the new index file from server */
866
+
867
+ /* Here we will try to download a file */
868
+
869
+ /* Make a lock on the file */
870
+ if ((LF = gmtremote_lock_on (GMT , (char * )new_indexpath )) == NULL )
871
+ return 1 ;
872
+
873
+ /* If file locking held us up as another process was downloading the same file,
874
+ * then that file should now be available. So we check again if it is before proceeding */
875
+
876
+ if (!access (new_indexpath , F_OK )) { /* Yes it was! Undo lock and return no error */
877
+ gmtremote_lock_off (GMT , & LF ); /* Remove lock file after successful download (unless query) */
878
+ return GMT_NOERROR ;
879
+ }
880
+
881
+ if (gmtremote_get_url (GMT , url , new_indexpath , indexpath , index , false)) { /* Get the new index file from server */
866
882
GMT_Report (API , GMT_MSG_DEBUG , "Failed to download %s - Internet troubles?\n" , url );
867
883
if (!access (new_indexpath , F_OK )) gmt_remove_file (GMT , new_indexpath ); /* Remove index file just in case it got corrupted or zero size */
884
+ gmtremote_lock_off (GMT , & LF );
868
885
return 1 ; /* Unable to update the file (no Internet?) - skip the tests */
869
886
}
870
887
if (!access (old_indexpath , F_OK ))
871
888
remove (old_indexpath ); /* Remove old index file if it exists */
872
889
GMT_Report (API , GMT_MSG_DEBUG , "Rename %s to %s\n" , indexpath , old_indexpath );
873
890
if (gmt_rename_file (GMT , indexpath , old_indexpath , GMT_RENAME_FILE )) { /* Rename existing file to .old */
874
891
GMT_Report (API , GMT_MSG_ERROR , "Failed to rename %s to %s.\n" , indexpath , old_indexpath );
892
+ gmtremote_lock_off (GMT , & LF );
875
893
return 1 ;
876
894
}
877
895
GMT_Report (API , GMT_MSG_DEBUG , "Rename %s to %s\n" , new_indexpath , indexpath );
878
896
if (gmt_rename_file (GMT , new_indexpath , indexpath , GMT_RENAME_FILE )) { /* Rename newly copied file to existing file */
879
897
GMT_Report (API , GMT_MSG_ERROR , "Failed to rename %s to %s.\n" , new_indexpath , indexpath );
898
+ gmtremote_lock_off (GMT , & LF );
880
899
return 1 ;
881
900
}
882
901
@@ -887,6 +906,7 @@ GMT_LOCAL int gmtremote_refresh (struct GMTAPI_CTRL *API, unsigned int index) {
887
906
888
907
if ((N = gmtremote_hash_load (GMT , indexpath , & nN )) == 0 ) { /* Read in the new array of hash structs, will return 0 if mismatch of entries */
889
908
gmt_remove_file (GMT , indexpath ); /* Remove corrupted index file */
909
+ gmtremote_lock_off (GMT , & LF );
890
910
return 1 ;
891
911
}
892
912
@@ -930,6 +950,8 @@ GMT_LOCAL int gmtremote_refresh (struct GMTAPI_CTRL *API, unsigned int index) {
930
950
}
931
951
else
932
952
GMT -> current .io .new_data_list = true; /* Flag that we wish to delete datasets older than entries in this file */
953
+ /* Remove lock file after successful download */
954
+ gmtremote_lock_off (GMT , & LF );
933
955
}
934
956
else
935
957
GMT_Report (API , GMT_MSG_DEBUG , "File %s less than 24 hours old, refresh is premature.\n" , indexpath );
0 commit comments