Skip to content

Commit 3909151

Browse files
authored
Static server issues (#7879)
* WIP Fix issues when running tests on static Still stuff not working. Looks like setting expert GMT_DATA_SERVER=static works differently (better?) than having gmtest.in do it. * Updates with simpler path management * Update gmtest.in * Update gmt_remote.c
1 parent ea53f83 commit 3909151

File tree

5 files changed

+55
-74
lines changed

5 files changed

+55
-74
lines changed

src/gmt_internals.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ struct GMT_XINGS {
5353
EXTERN_MSC char *dlerror (void);
5454
#endif
5555

56-
EXTERN_MSC char * gmtlib_prepend_server_name (struct GMT_CTRL *GMT, bool cache);
5756
EXTERN_MSC int gmtlib_adjust_we_if_central_lon_set (struct GMT_CTRL *GMT, double *west, double *east);
5857
EXTERN_MSC int gmtlib_colon_pos (struct GMT_CTRL *GMT, char *text);
5958
EXTERN_MSC bool gmtlib_invalid_symbolname (struct GMT_CTRL *GMT, char *name);

src/gmt_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct GMTAPI_CTRL {
166166
bool use_gridline_registration; /* true if default remote grid registration should be gridline, not pixel */
167167
bool use_gridline_registration_warn; /* true if we should warn about the above */
168168
bool ignore_BC; /* temporarily set true for, say, weight grids in grdfilter which do not need a BC wrap-around check */
169+
bool paths_initialized; /* True when we set default USERDIR and CACHEDIR */
169170
size_t n_objects_alloc; /* Allocation counter for data objects */
170171
int error; /* Error code from latest API call [GMT_OK] */
171172
int last_error; /* Error code from previous API call [GMT_OK] */

src/gmt_remote.c

Lines changed: 51 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
219208
GMT_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+
893919
GMT_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-
14721453
int 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] = {""};

test/gmtest.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ export HAVE_GLIB_GTHREAD="@HAVE_GLIB_GTHREAD@"
286286
export GS_FONTPATH="@CMAKE_CURRENT_SOURCE_DIR@/ex31/fonts"
287287
# Disable gmt end show from displaying plots
288288
export GMT_END_SHOW=off
289-
# Set the GMT data server
290-
export GMT_DATA_SERVER=@GMT_DATA_SERVER@
289+
# Set the GMT data server if not already set in the environment
290+
[[ -z "${GMT_DATA_SERVER}" ]] && export GMT_DATA_SERVER=@GMT_DATA_SERVER@
291291
# Start with proper GMT defaults
292292
# Use different settings for three groups of tests
293293
if [[ $script_name == doc/examples/* ]]; then

test/psxy/gallo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cat << EOF > chicks.txt
1313
-15 55 3i
1414
-40 65 2i
1515
EOF
16-
gmt psxy -Skgallo -R-45/0/0/70 -JX15c/0 -B5 -BWSen -P -K chicks.txt -Xc > $ps
16+
gmt psxy -Sk@gallo -R-45/0/0/70 -JX15c/0 -B5 -BWSen -P -K chicks.txt -Xc > $ps
1717
awk '{printf "%s %s %s %g%s\n", $1, $2, $3, substr($3,1,1)*'"$scale"', substr($3,2,1)}' chicks.txt > r.txt
1818
gmt psxy -R -J -O -K r.txt -Sr -Wfaint,blue >> $ps
1919
gmt psxy -R -J -O -K chicks.txt -S+4i -Wfaint -Gred >> $ps

0 commit comments

Comments
 (0)