Skip to content
Snippets Groups Projects
Commit 45793a57 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Use XDG paths, clean up code

Instead of ~/.syncterm, use paths as specified in the XDG Base Directory Specification
https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

This should really have something that notifies a user everything
moved and instructs them on where each thing should be placed to
keep data from older versions.
parent f6e70c6e
No related branches found
No related tags found
No related merge requests found
Pipeline #6130 failed
...@@ -974,7 +974,7 @@ parse_url(char *url, struct bbslist *bbs, int dflt_conn_type, int force_defaults ...@@ -974,7 +974,7 @@ parse_url(char *url, struct bbslist *bbs, int dflt_conn_type, int force_defaults
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
static char * static char *
get_new_OSX_filename(char *fn, int fnlen, int type, int shared) get_OSX_filename(char *fn, int fnlen, int type, int shared)
{ {
FSRef ref; FSRef ref;
...@@ -1034,23 +1034,11 @@ get_new_OSX_filename(char *fn, int fnlen, int type, int shared) ...@@ -1034,23 +1034,11 @@ get_new_OSX_filename(char *fn, int fnlen, int type, int shared)
return NULL; return NULL;
} }
#endif /* if defined(__APPLE__) && defined(__MACH__) */ #elif defined(_WIN32) /* if defined(__APPLE__) && defined(__MACH__) */
char * static char *
get_syncterm_filename(char *fn, int fnlen, int type, bool shared) get_win32_path(char *fn, int fnlen, int type, int shared)
{ {
char oldlst[MAX_PATH + 1];
if ((config_override != NULL) && (type == SYNCTERM_PATH_INI) && !shared) {
sprintf(fn, "%.*s", fnlen - 1, config_override);
return fn;
}
if ((list_override != NULL) && (type == SYNCTERM_PATH_LIST) && !shared) {
sprintf(fn, "%.*s", fnlen - 1, list_override);
return fn;
}
memset(fn, 0, fnlen);
#ifdef _WIN32
char *home; char *home;
static dll_handle shell32 = NULL; static dll_handle shell32 = NULL;
bool we_got_this = false; bool we_got_this = false;
...@@ -1064,13 +1052,8 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared) ...@@ -1064,13 +1052,8 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
home = getenv("HOME"); home = getenv("HOME");
if (home == NULL) if (home == NULL)
home = getenv("USERPROFILE"); home = getenv("USERPROFILE");
if (home == NULL) { if (home == NULL)
strcpy(oldlst, "./syncterm.lst"); home = ".";
}
else {
SAFECOPY(oldlst, home);
strcat(oldlst, "/syncterm.lst");
}
if (shell32 == NULL) if (shell32 == NULL)
shell32 = xp_dlopen(shell32dll, RTLD_LAZY, 6); shell32 = xp_dlopen(shell32dll, RTLD_LAZY, 6);
...@@ -1138,7 +1121,7 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared) ...@@ -1138,7 +1121,7 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
break; break;
default: default:
backslash(fn); backslash(fn);
strcat(fn, "SyncTERM"); strncat(fn, "SyncTERM", fnlen - strlen(fn) - 1);
break; break;
} }
if (!isdir(fn)) if (!isdir(fn))
...@@ -1153,7 +1136,7 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared) ...@@ -1153,7 +1136,7 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
break; break;
default: default:
backslash(fn); backslash(fn);
strcat(fn, "SyncTERM"); strncat(fn, "SyncTERM", fnlen - strlen(fn) - 1);
break; break;
} }
#else /* ifdef CSIDL_FLAG_CREATE */ #else /* ifdef CSIDL_FLAG_CREATE */
...@@ -1165,7 +1148,7 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared) ...@@ -1165,7 +1148,7 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
/* Create if it doesn't exist */ /* Create if it doesn't exist */
if (*fn && !isdir(fn)) { if (*fn && !isdir(fn)) {
if (MKDIR(fn)) if (MKDIR(fn))
fn[0] = 0; return NULL;
} }
switch (type) { switch (type) {
...@@ -1187,67 +1170,107 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared) ...@@ -1187,67 +1170,107 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
break; break;
} }
} }
strncat(fn, "cache", fnlen - strlen(fn) - 1);
backslash(fn);
if (!isdir(fn)) {
if (MKDIR(fn))
fn[0] = 0;
}
break; break;
case SYNCTERM_PATH_KEYS: case SYNCTERM_PATH_KEYS:
backslash(fn); backslash(fn);
strncat(fn, "syncterm.ssh", fnlen - strlen(fn) - 1); strncat(fn, "syncterm.ssh", fnlen - strlen(fn) - 1);
break; break;
} }
#else /* ifdef _WIN32 */ }
/* UNIX */
char *home = getenv("HOME");
if (!shared) { #else
if (((home == NULL) || (strlen(home) > MAX_PATH - 32))) { /* $HOME just too damn big */ enum xdg_paths {
if ((type == SYNCTERM_DEFAULT_TRANSFER_PATH) || (type == SYNCTERM_PATH_CACHE)) { XDG_DATA_HOME,
if(getcwd(fn, fnlen) == NULL) XDG_CONFIG_HOME,
return NULL; XDG_CACHE_HOME,
backslash(fn); XDG_NONE, // Download dir
if (type == SYNCTERM_PATH_CACHE) { };
strcat(fn, "cache"); static char *
backslash(fn); get_xdg_path(enum xdg_paths type, char *buf, size_t bufsz)
} {
return fn; char *env;
}
SAFECOPY(oldlst, "syncterm.lst"); // First, check if XDG variable is set
strcpy(fn, "./"); switch(type) {
case XDG_DATA_HOME:
env = getenv("XDG_DATA_HOME");
break;
case XDG_CONFIG_HOME:
env = getenv("XDG_CONFIG_HOME");
break;
case XDG_CACHE_HOME:
env = getenv("XDG_CACHE_HOME");
break;
case XDG_NONE:
// Always use HOME...
env = NULL;
break;
default:
fprintf(stderr, "Invalid XDG type %d\n", type);
return NULL;
}
// Not set, get default
if (env == NULL) {
char *home;
home = getenv("HOME"); // Standard
if (home == NULL)
home = ".";
switch(type) {
case XDG_DATA_HOME:
snprintf(buf, bufsz, "%s/.local/share", home);
break;
case XDG_CONFIG_HOME:
snprintf(buf, bufsz, "%s/.config", home);
break;
case XDG_CACHE_HOME:
snprintf(buf, bufsz, "%s/.cache", home);
break;
case XDG_NONE:
snprintf(buf, bufsz, "%s", home);
break;
} }
else { }
if (type == SYNCTERM_DEFAULT_TRANSFER_PATH) { else {
strcpy(fn, home); snprintf(buf, bufsz, "%s", env);
backslash(fn); }
#if defined(__APPLE__) && defined(__MACH__)
if (get_new_OSX_filename(oldlst, sizeof(oldlst), type, shared) != NULL)
strcpy(fn, oldlst);
#endif
if (!isdir(fn))
if (MKDIR(fn))
fn[0] = 0;
return fn;
}
SAFECOPY(oldlst, home);
backslash(oldlst);
strcat(oldlst, "syncterm.lst");
sprintf(fn, "%.*s", fnlen, home);
strncat(fn, "/.syncterm", fnlen - strlen(fn) - 1);
backslash(fn);
if (type == SYNCTERM_PATH_CACHE) {
if (!isdir(fn))
if (MKDIR(fn))
fn[0] = 0;
// Add "syncterm" to the end
if (type != XDG_NONE) {
backslash(buf);
strcat(buf, "syncterm");
}
strcat(fn, "cache"); return buf;
backslash(fn); }
return fn;
} static char *
get_unix_filename(char *fn, int fnlen, int type, int shared)
{
if (!shared) {
switch(type) {
case SYNCTERM_PATH_INI:
if (get_xdg_path(XDG_CONFIG_HOME, fn, fnlen) == NULL)
return NULL;
break;
case SYNCTERM_PATH_LIST:
if (get_xdg_path(XDG_DATA_HOME, fn, fnlen) == NULL)
return NULL;
break;
case SYNCTERM_DEFAULT_TRANSFER_PATH:
if (get_xdg_path(XDG_NONE, fn, fnlen) == NULL)
return NULL;
break;
case SYNCTERM_PATH_CACHE:
if (get_xdg_path(XDG_CACHE_HOME, fn, fnlen) == NULL)
return NULL;
break;
case SYNCTERM_PATH_KEYS:
if (get_xdg_path(XDG_DATA_HOME, fn, fnlen) == NULL)
return NULL;
break;
} }
backslash(fn);
} }
else { else {
#ifdef SYSTEM_LIST_DIR #ifdef SYSTEM_LIST_DIR
...@@ -1258,13 +1281,11 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared) ...@@ -1258,13 +1281,11 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
#endif #endif
} }
#if !(defined(__APPLE__) && defined(__MACH__)) /* Create if it doesn't exist */
/* Create if it doesn't exist */
if (!isdir(fn) && !shared) { if (!isdir(fn) && !shared) {
if (MKDIR(fn)) if (MKDIR(fn))
fn[0] = 0; return NULL;
} }
#endif
switch (type) { switch (type) {
case SYNCTERM_PATH_INI: case SYNCTERM_PATH_INI:
...@@ -1274,44 +1295,40 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared) ...@@ -1274,44 +1295,40 @@ get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
strncat(fn, "syncterm.lst", fnlen - strlen(fn) - 1); strncat(fn, "syncterm.lst", fnlen - strlen(fn) - 1);
break; break;
case SYNCTERM_PATH_CACHE: case SYNCTERM_PATH_CACHE:
strncat(fn, "cache", fnlen - strlen(fn) - 1);
backslash(fn);
#if !(defined(__APPLE__) && defined(__MACH__))
if (!isdir(fn)) {
if (MKDIR(fn))
fn[0] = 0;
}
#endif
break; break;
case SYNCTERM_PATH_KEYS: case SYNCTERM_PATH_KEYS:
strncat(fn, "syncterm.ssh", fnlen - strlen(fn) - 1); strncat(fn, "syncterm.ssh", fnlen - strlen(fn) - 1);
break; break;
} }
#if defined(__APPLE__) && defined(__MACH__)
strcpy(oldlst, fn);
if (get_new_OSX_filename(fn, fnlen, type, shared) != NULL) {
if (fexist(oldlst)) {
if (!isdir(oldlst)) {
char *lastslash = strrchr(oldlst, '/');
rename(oldlst, fn);
if (lastslash) {
*(lastslash + 1) = '*';
*(lastslash + 2) = 0;
if (!fexist(oldlst)) {
*lastslash = 0;
rmdir(oldlst);
}
}
}
}
}
#endif /* OS X */
#endif /* !Win32 */
return fn; return fn;
} }
#endif /* if defined(__APPLE__) && defined(__MACH__) */
char *
get_syncterm_filename(char *fn, int fnlen, int type, bool shared)
{
if ((config_override != NULL) && (type == SYNCTERM_PATH_INI) && !shared) {
sprintf(fn, "%.*s", fnlen - 1, config_override);
return fn;
}
if ((list_override != NULL) && (type == SYNCTERM_PATH_LIST) && !shared) {
sprintf(fn, "%.*s", fnlen - 1, list_override);
return fn;
}
memset(fn, 0, fnlen);
#if defined(__APPLE__) && defined(__MACH__)
return get_OSX_filename(fn, fnlen, type, shared);
#elif defined(_WIN32)
return get_win_filename(fn, fnlen, type, shared);
#else
return get_unix_filename(fn, fnlen, type, shared);
#endif
return NULL;
}
void void
load_settings(struct syncterm_settings *set) load_settings(struct syncterm_settings *set)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment