@@ -205,29 +205,17 @@ GMT_LOCAL int gmtremote_remove_item (struct GMTAPI_CTRL *API, char *path, bool d
205205 return error ;
206206}
207207
208- GMT_LOCAL void gmtremote_set_local_path (struct GMT_CTRL * GMT , char * local_path ) {
209- /* Build the local path to the remote data files, optionally append specific file */
210- char * srv_dir = gmtlib_prepend_server_name (GMT , false); /* The server directory to use [server] */
211-
212- /* Set the top-level directory for user data */
213- if (strcmp (srv_dir , "server" )) /* One of the ghost-servers */
214- snprintf (local_path , PATH_MAX , "%s/%s" , GMT -> session .USERDIR , srv_dir );
215- else
216- snprintf (local_path , PATH_MAX , "%s" , GMT -> session .USERDIR );
217- }
218-
219208GMT_LOCAL struct GMT_DATA_INFO * gmtremote_data_load (struct GMTAPI_CTRL * API , int * n ) {
220209 /* Read contents of the info file into an array of structs */
221210 bool parse_extra_data = false;
222211 int k = 0 , nr , start_here = 0 ;
223212 FILE * fp = NULL ;
224213 struct GMT_DATA_INFO * I = NULL ;
225214 char unit , line [GMT_LEN512 ] = {"" }, file [PATH_MAX ] = {"" }, * c = NULL ;
226- char * srv_dir = gmtlib_prepend_server_name (API -> GMT , false); /* The server directory to use [server] */
227215
228216 struct GMT_CTRL * GMT = API -> GMT ;
229217
230- snprintf (file , PATH_MAX , "%s/%s/%s " , GMT -> session .USERDIR , srv_dir , GMT_INFO_SERVER_FILE );
218+ snprintf (file , PATH_MAX , "%s/%s" , GMT -> session .USERDIR , GMT_INFO_SERVER_FILE );
231219
232220 GMT_Report (API , GMT_MSG_DEBUG , "Load contents from %s\n" , file );
233221 * n = 0 ;
@@ -890,6 +878,44 @@ GMT_LOCAL struct GMT_DATA_HASH *gmtremote_hash_load (struct GMT_CTRL *GMT, char
890878 return (L );
891879};
892880
881+ GMT_LOCAL char * gmtlib_prepend_server_name (struct GMT_CTRL * GMT , bool cache ) {
882+ /* If the current GMT server is one of candidate, static, or test, then
883+ * we append that directory to the local path so that we do not overwrite
884+ * what we obtain from the official server [e.g., oceania, europe, ...]
885+ * which places data under the "server" directory. */
886+ static char * ghost_server [5 ] = {"candidate" , "static" , "test" , "server" , GMT_DATA_SERVER };
887+ unsigned int k ;
888+ if (GMT -> session .DATASERVER == NULL ) return NULL ; /* Not initialized yet */
889+ for (k = 0 ; k < 3 ; k ++ ) {
890+ if (!strcmp (GMT -> session .DATASERVER , ghost_server [k ]))
891+ return (ghost_server [k ]);
892+ }
893+ if (k > 3 ) k -- ; /* Since oceania is the same as server in this context */
894+ return (cache ) ? NULL : ghost_server [k ]; /* The users default data server (k = 3) except cache is not under server on oceania */
895+ }
896+
897+ GMT_LOCAL void gmtremote_init_paths (struct GMTAPI_CTRL * API ) {
898+ /* If GMT_DATA_SERVER is static|test|candidate then we need to update internal paths */
899+ char * srv_dir = NULL , path [PATH_MAX ] = {"" };
900+ struct GMT_CTRL * GMT = API -> GMT ; /* Short hand */
901+
902+ if (API -> paths_initialized ) return ; /* Been here done that */
903+ GMT_Report (API , GMT_MSG_NOTICE , "GMT_DATA_SERVER is %s\n" , (GMT -> session .DATASERVER == NULL ) ? "NOT set" : GMT -> session .DATASERVER );
904+ if (GMT -> session .DATASERVER == NULL ) return ;
905+ if ((srv_dir = gmtlib_prepend_server_name (GMT , false)) == NULL ) return ;
906+ if (!strcmp (srv_dir , "server" ) || !strcmp ("oceania" , GMT_DATA_SERVER )) return ;
907+
908+ /* One of the ghost-servers */
909+ gmt_M_str_free (GMT -> session .USERDIR );
910+ gmt_M_str_free (GMT -> session .CACHEDIR );
911+ snprintf (path , PATH_MAX , "%s/.gmt/%s" , GMT -> session .HOMEDIR , srv_dir );
912+ GMT -> session .USERDIR = gmt_strdup_noquote (path );
913+ snprintf (path , PATH_MAX , "%s/.gmt/%s/cache" , GMT -> session .HOMEDIR , srv_dir );
914+ GMT -> session .CACHEDIR = gmt_strdup_noquote (path );
915+ API -> paths_initialized = true;
916+ GMT_Report (API , GMT_MSG_DEBUG , "USERDIR is now %s and CACHEDIR is now %s\n" , GMT -> session .USERDIR , GMT -> session .CACHEDIR );
917+ }
918+
893919GMT_LOCAL int gmtremote_refresh (struct GMTAPI_CTRL * API , unsigned int index ) {
894920 /* This function is called every time we are about to access a @remotefile.
895921 * It is called twice: Once for the hash table and once for the info table.
@@ -914,17 +940,17 @@ GMT_LOCAL int gmtremote_refresh (struct GMTAPI_CTRL *API, unsigned int index) {
914940 char new_indexpath [PATH_MAX ] = {"" }, url [PATH_MAX ] = {"" };
915941 const char * index_file = (index == GMT_HASH_INDEX ) ? GMT_HASH_SERVER_FILE : GMT_INFO_SERVER_FILE ;
916942 struct GMT_CTRL * GMT = API -> GMT ; /* Short hand */
917- char * srv_dir = gmtlib_prepend_server_name (GMT , false); /* The server directory to use [server] */
918943 struct LOCFILE_FP * LF = NULL ;
919944
920945 if (GMT -> current .io .refreshed [index ]) return GMT_NOERROR ; /* Already been here */
946+ gmtremote_init_paths (API ); /* Initialize USERDIR and CACHEDIR for ghost-servers */
921947
922948 /* Set the full local path to the index_file */
923- snprintf (indexpath , PATH_MAX , "%s/%s/%s " , GMT -> session .USERDIR , srv_dir , index_file );
949+ snprintf (indexpath , PATH_MAX , "%s/%s" , GMT -> session .USERDIR , index_file );
924950
925951 if (access (indexpath , R_OK )) { /* Not found locally so need to download the first time */
926952 char serverdir [PATH_MAX ] = {"" };
927- snprintf (serverdir , PATH_MAX , "%s/%s " , GMT -> session .USERDIR , srv_dir );
953+ snprintf (serverdir , PATH_MAX , "%s" , GMT -> session .USERDIR );
928954 if (access (serverdir , R_OK ) && gmt_mkdir (serverdir )) {
929955 GMT_Report (API , GMT_MSG_ERROR , "Unable to create GMT server directory : %s\n" , serverdir );
930956 return 1 ;
@@ -1190,19 +1216,10 @@ int gmt_set_remote_and_local_filenames (struct GMT_CTRL *GMT, const char * file,
11901216 int k_data = GMT_NOTSET , t_data = GMT_NOTSET ;
11911217 unsigned int pos ;
11921218 bool is_url = false, is_query = false, is_tile = false, srtm_switch = false, skip_checks = false;
1193- char was , * c = NULL , * jp2_file = NULL , * clean_file = NULL , * srv_dir = NULL ;
1219+ char was , * c = NULL , * jp2_file = NULL , * clean_file = NULL ;
11941220 char cache_dir [PATH_MAX ] = {"" };
11951221 struct GMTAPI_CTRL * API = GMT -> parent ;
11961222
1197- if (GMT -> session .DATASERVER && GMT -> session .CACHEDIR == NULL ) { /* Set via --GMT_DATA_SERVER=xxxx */
1198- char * srv_dir = gmtlib_prepend_server_name (GMT , true); /* The server directory to use [server] */
1199- if (srv_dir ) /* Place cache under ghostserver dir */
1200- sprintf (cache_dir , "%s/%s/cache" , GMT -> session .USERDIR , srv_dir );
1201- else /* Place in user dir */
1202- sprintf (cache_dir , "%s/cache" , GMT -> session .USERDIR );
1203- GMT -> session .CACHEDIR = strdup (cache_dir );
1204- }
1205-
12061223 local_path [0 ] = remote_path [0 ] = '\0' ;
12071224
12081225 /* 0. Were we even given an argument? */
@@ -1250,7 +1267,7 @@ int gmt_set_remote_and_local_filenames (struct GMT_CTRL *GMT, const char * file,
12501267 GMT_Report (API , GMT_MSG_DEBUG , "No user directory yet to store %s!\n" , file );
12511268 goto not_local ; /* Cannot have server data if no user directory created yet */
12521269 }
1253- gmtremote_set_local_path ( GMT , local_path ); /* This is the top-level directory for user data */
1270+ snprintf ( local_path , PATH_MAX , "%s" , GMT -> session . USERDIR );
12541271 if (access (local_path , R_OK )) goto not_local ; /* Have not made a user directory yet, so cannot have the file yet either */
12551272 strcat (local_path , GMT -> parent -> remote_info [k_data ].dir ); /* Append the subdir (/ or /server/earth/earth_relief/, etc) */
12561273 strcat (local_path , GMT -> parent -> remote_info [k_data ].file ); /* Append filename */
@@ -1262,7 +1279,7 @@ int gmt_set_remote_and_local_filenames (struct GMT_CTRL *GMT, const char * file,
12621279 GMT_Report (API , GMT_MSG_DEBUG , "No user directory yet to store %s!\n" , file );
12631280 goto not_local ; /* Cannot have server data if no user directory created yet */
12641281 }
1265- gmtremote_set_local_path ( GMT , local_path ); /* This is the top-level directory for user data */
1282+ snprintf ( local_path , PATH_MAX , "%s" , GMT -> session . USERDIR );
12661283 if (access (local_path , R_OK )) goto not_local ; /* Have not made a user directory yet, so cannot have the file yet either */
12671284 strcat (local_path , GMT -> parent -> remote_info [t_data ].dir ); /* Append the subdir (/ or /server/earth/earth_relief/, etc) */
12681285 strcat (local_path , GMT -> parent -> remote_info [t_data ].file ); /* Append the tiledir to get full path to dir for this type of tiles */
@@ -1281,17 +1298,9 @@ int gmt_set_remote_and_local_filenames (struct GMT_CTRL *GMT, const char * file,
12811298 }
12821299 }
12831300 else { /* Must be cache file */
1284- char * srv_dir = gmtlib_prepend_server_name (GMT , true); /* The server directory to use [server] */
1285- if (srv_dir && GMT -> session .CACHEDIR && strstr (GMT -> session .CACHEDIR , srv_dir ) == NULL ) { /* Must look for cache under the ghostservers instead of in .gmt */
1286- sprintf (cache_dir , "%s/%s/cache" , GMT -> session .USERDIR , srv_dir );
1287- if (GMT -> session .CACHEDIR == NULL ) gmt_M_str_free (GMT -> session .CACHEDIR );
1288- GMT -> session .CACHEDIR = strdup (cache_dir );
1289- }
12901301 if (GMT -> session .CACHEDIR == NULL ) { /* Create the needed path now */
1291- if (srv_dir ) /* Place cache under ghostserver dir */
1292- sprintf (cache_dir , "%s/%s/cache" , GMT -> session .USERDIR , srv_dir );
1293- else /* Place in user dir */
1294- sprintf (cache_dir , "%s/cache" , GMT -> session .USERDIR );
1302+ sprintf (cache_dir , "%s/cache" , GMT -> session .USERDIR );
1303+ GMT -> session .CACHEDIR = gmt_strdup_noquote (cache_dir );
12951304 }
12961305 clean_file = gmt_get_filename (API , file , gmtlib_valid_filemodifiers (GMT )); /* Strip off any file modifier or netCDF directives */
12971306 snprintf (local_path , PATH_MAX , "%s/%s" , GMT -> session .CACHEDIR , & clean_file [1 ]); /* This is where all cache files live */
@@ -1326,12 +1335,6 @@ int gmt_set_remote_and_local_filenames (struct GMT_CTRL *GMT, const char * file,
13261335 /* Set local path */
13271336 switch (mode ) {
13281337 case GMT_CACHE_DIR :
1329- if (srv_dir && GMT -> session .CACHEDIR && strstr (GMT -> session .CACHEDIR , srv_dir ) == NULL ) { /* Must look for cache under the ghostservers instead of in .gmt */
1330- char cache_dir [PATH_MAX ] = {"" };
1331- sprintf (cache_dir , "%s/%s/cache" , GMT -> session .USERDIR , srv_dir );
1332- if (GMT -> session .CACHEDIR == NULL ) gmt_M_str_free (GMT -> session .CACHEDIR );
1333- GMT -> session .CACHEDIR = strdup (cache_dir );
1334- }
13351338 if (GMT -> session .CACHEDIR == NULL ) {
13361339 GMT_Report (API , GMT_MSG_ERROR , "Cache directory storage requested for %s but your cache directory is undefined\n" , file );
13371340 return GMT_FILE_NOT_FOUND ;
@@ -1357,28 +1360,22 @@ int gmt_set_remote_and_local_filenames (struct GMT_CTRL *GMT, const char * file,
13571360 if (GMT -> session .USERDIR == NULL || access (GMT -> session .USERDIR , R_OK ))
13581361 GMT_Report (API , GMT_MSG_ERROR , "User directory storage requested for %s but your user directory is undefined or does not exist\n" , file );
13591362 else { /* Have a user dir */
1360- char * srv_dir = gmtlib_prepend_server_name (GMT , true); /* The server directory to use [server] */
1361- if (srv_dir ) /* Must add in ghostserver name since actual file starts with /server */
1362- snprintf (local_path , PATH_MAX , "%s/%s" , GMT -> session .USERDIR , srv_dir );
1363- else
1364- snprintf (local_path , PATH_MAX , "%s" , GMT -> session .USERDIR );
1363+ snprintf (local_path , PATH_MAX , "%s" , GMT -> session .USERDIR );
13651364 if (access (local_path , R_OK ) && gmt_mkdir (local_path )) /* Have or just made a server subdirectory */
13661365 GMT_Report (API , GMT_MSG_ERROR , "Unable to create GMT data directory : %s\n" , local_path );
13671366 if (is_tile ) { /* One of the tiles */
13681367 if (jp2_file ) gmt_M_str_free (jp2_file );
13691368 jp2_file = gmt_strrep (& file [1 ], GMT_TILE_EXTENSION_LOCAL , GMT_TILE_EXTENSION_REMOTE );
13701369 snprintf (local_path , PATH_MAX , "%s%s%s" , GMT -> session .USERDIR , GMT -> parent -> remote_info [t_data ].dir , GMT -> parent -> remote_info [t_data ].file );
1370+
13711371 if (access (local_path , R_OK ) && gmt_mkdir (local_path )) /* Have or just made a server/tile subdirectory */
13721372 GMT_Report (API , GMT_MSG_ERROR , "Unable to create GMT data directory : %s\n" , local_path );
13731373 strcat (local_path , jp2_file );
13741374 }
13751375 else if (!strcmp (API -> remote_info [k_data ].dir , "/" )) /* One of the symbolic links in server */
1376- snprintf (local_path , PATH_MAX , "%s/%s/%s " , GMT -> session .USERDIR , srv_dir , API -> remote_info [k_data ].file );
1376+ snprintf (local_path , PATH_MAX , "%s/%s" , GMT -> session .USERDIR , API -> remote_info [k_data ].file );
13771377 else {
1378- if (srv_dir ) /* Must add in ghostserver name since actual file starts with /server */
1379- snprintf (local_path , PATH_MAX , "%s/%s%s" , GMT -> session .USERDIR , srv_dir , API -> remote_info [k_data ].dir );
1380- else
1381- snprintf (local_path , PATH_MAX , "%s%s" , GMT -> session .USERDIR , API -> remote_info [k_data ].dir );
1378+ snprintf (local_path , PATH_MAX , "%s%s" , GMT -> session .USERDIR , API -> remote_info [k_data ].dir );
13821379 if (access (local_path , R_OK ) && gmt_mkdir (local_path )) /* Have or just made a subdirectory under server */
13831380 GMT_Report (API , GMT_MSG_ERROR , "Unable to create GMT data directory : %s\n" , local_path );
13841381 strcat (local_path , API -> remote_info [k_data ].file );
@@ -1453,22 +1450,6 @@ int gmt_set_remote_and_local_filenames (struct GMT_CTRL *GMT, const char * file,
14531450 return GMT_FILE_NOT_FOUND ;
14541451}
14551452
1456- char * gmtlib_prepend_server_name (struct GMT_CTRL * GMT , bool cache ) {
1457- /* If the current GMT server is one of candidate, static, or test, then
1458- * we append that directory to the local path so that we do not overwrite
1459- * what we obtain from the official server [e.g., oceania, europe, ...]
1460- * which places data under the "server" directory. */
1461- static char * ghost_server [5 ] = {"candidate" , "static" , "test" , "server" , GMT_DATA_SERVER };
1462- unsigned int k ;
1463- if (GMT -> session .DATASERVER == NULL ) return NULL ; /* Not initialized yet */
1464- for (k = 0 ; k < 3 ; k ++ ) {
1465- if (!strcmp (GMT -> session .DATASERVER , ghost_server [k ]))
1466- return (ghost_server [k ]);
1467- }
1468- if (k > 3 ) k -- ; /* Since oceania is the same as server in this context */
1469- return (cache ) ? NULL : ghost_server [k ]; /* The users default data server (k = 3) except cache is not under server on oceania */
1470- }
1471-
14721453int gmtlib_file_is_jpeg2000_tile (struct GMTAPI_CTRL * API , char * file ) {
14731454 /* Detect if a file matches the name <path>/[N|S]yy[E|W]xxx.tag.jp2 (e.g., N22W160.earth_relief_01m_p.jp2) */
14741455 char * c , tmp [PATH_MAX ] = {"" };
0 commit comments