Implement XDG Base Directory Specification

This commit is contained in:
Seg 2022-07-23 21:44:46 -06:00 committed by robxnano
parent 6edec590ab
commit ffb525800b
3 changed files with 158 additions and 94 deletions

View File

@ -20,15 +20,13 @@
#include "sysdeps.h" #include "sysdeps.h"
#include <stdio.h> #include <sys/stat.h>
#include <stdlib.h>
#include <string> #include <string>
using std::string; using std::string;
#include "prefs.h" #include "prefs.h"
// Platform-specific preferences items // Platform-specific preferences items
prefs_desc platform_prefs_items[] = { prefs_desc platform_prefs_items[] = {
{"fbdevicefile", TYPE_STRING, false, "path of frame buffer device specification file"}, {"fbdevicefile", TYPE_STRING, false, "path of frame buffer device specification file"},
@ -41,24 +39,37 @@ prefs_desc platform_prefs_items[] = {
{NULL, TYPE_END, false, NULL} // End of list {NULL, TYPE_END, false, NULL} // End of list
}; };
// Prefs file name and path // Prefs file name and path
const char PREFS_FILE_NAME[] = ".basilisk_ii_prefs"; static const char PREFS_FILE_NAME[] = "/prefs";
string UserPrefsPath; string UserPrefsPath;
static string prefs_path; static string prefs_path;
static string prefs_name;
extern string xpram_name;
/* /*
* Load preferences from settings file * Load preferences from settings file
*/ */
void LoadPrefs(const char *vmdir) // Comply with XDG Base Directory Specification
{ // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
if (vmdir) { static void get_prefs_path_from_env(void){
prefs_path = string(vmdir) + '/' + string("prefs"); char* env;
FILE *prefs = fopen(prefs_path.c_str(), "r"); if(env=getenv("XDG_CONFIG_HOME")){
prefs_path = string(env);
return;
}
if(env=getenv("HOME")){
prefs_path = string(env) + "/.config";
}
}
void LoadPrefs(const char* vmdir){
if(vmdir){
prefs_path = string(vmdir);
prefs_name = prefs_path + PREFS_FILE_NAME;
FILE *prefs = fopen(prefs_name.c_str(), "r");
if (!prefs) { if (!prefs) {
printf("No file at %s found.\n", prefs_path.c_str()); printf("No file at %s found.\n", prefs_name.c_str());
exit(1); exit(1);
} }
LoadPrefsFromStream(prefs); LoadPrefsFromStream(prefs);
@ -67,16 +78,20 @@ void LoadPrefs(const char *vmdir)
} }
// Construct prefs path // Construct prefs path
if (UserPrefsPath.empty()) { get_prefs_path_from_env();
char *home = getenv("HOME"); if(!prefs_path.empty()){
if (home) prefs_path += "/BasiliskII";
prefs_path = string(home) + '/'; prefs_name = prefs_path + PREFS_FILE_NAME;
prefs_path += PREFS_FILE_NAME; }
} else xpram_name = prefs_path + "/xpram";
prefs_path = UserPrefsPath;
// --config was specified
if(!UserPrefsPath.empty()){
prefs_name = UserPrefsPath;
}
// Read preferences from settings file // Read preferences from settings file
FILE *f = fopen(prefs_path.c_str(), "r"); FILE *f = fopen(prefs_name.c_str(), "r");
if (f != NULL) { if (f != NULL) {
// Prefs file found, load settings // Prefs file found, load settings
@ -92,21 +107,51 @@ void LoadPrefs(const char *vmdir)
} }
} }
static bool is_dir(const std::string& path){
struct stat info;
if(stat(path.c_str(), &info) != 0){
return false;
}
return (info.st_mode & S_IFDIR) != 0;
}
static bool create_directories(const std::string& path,mode_t mode){
if(mkdir(path.c_str(),mode)==0)
return true;
switch (errno){
case ENOENT:
{
int pos = path.find_last_of('/');
if (pos == std::string::npos)
return false;
if (!create_directories(path.substr(0,pos),mode))
return false;
}
return 0 == mkdir(path.c_str(),mode);
case EEXIST:
return is_dir(path);
default:
return false;
}
}
/* /*
* Save preferences to settings file * Save preferences to settings file
*/ */
void SavePrefs(void) void SavePrefs(void){
{
FILE *f; FILE *f;
if ((f = fopen(prefs_path.c_str(), "w")) != NULL) { if(!prefs_path.empty()&&!is_dir(prefs_path)){
create_directories(prefs_path,0700);
}
if ((f = fopen(prefs_name.c_str(), "w")) != NULL) {
SavePrefsToStream(f); SavePrefsToStream(f);
fclose(f); fclose(f);
} }
} }
/* /*
* Add defaults of platform-specific prefs items * Add defaults of platform-specific prefs items
* You may also override the defaults set in PrefsInit() * You may also override the defaults set in PrefsInit()

View File

@ -20,81 +20,54 @@
#include "sysdeps.h" #include "sysdeps.h"
#include <stdlib.h> #include <string>
using std::string;
#include "xpram.h" #include "xpram.h"
// XPRAM file name, set by LoadPrefs() in prefs_unix.cpp
// XPRAM file name and path string xpram_name;
#if POWERPC_ROM
const char XPRAM_FILE_NAME[] = ".sheepshaver_nvram";
#else
const char XPRAM_FILE_NAME[] = ".basilisk_ii_xpram";
#endif
static char xpram_path[1024];
/* /*
* Load XPRAM from settings file * Load XPRAM from settings file
*/ */
void LoadXPRAM(const char *vmdir) void LoadXPRAM(const char* vmdir){
{ if(vmdir){
if (vmdir) {
#if POWERPC_ROM #if POWERPC_ROM
snprintf(xpram_path, sizeof(xpram_path), "%s/nvram", vmdir); xpram_name = string(vmdir) + "/nvram";
#else #else
snprintf(xpram_path, sizeof(xpram_path), "%s/xpram", vmdir); xpram_name = string(vmdir) + "/xpram";
#endif #endif
} else {
// Construct XPRAM path
xpram_path[0] = 0;
char *home = getenv("HOME");
if (home != NULL && strlen(home) < 1000) {
strncpy(xpram_path, home, 1000);
strcat(xpram_path, "/");
}
strcat(xpram_path, XPRAM_FILE_NAME);
} }
// Load XPRAM from settings file assert(!xpram_name.empty());
int fd; int fd;
if ((fd = open(xpram_path, O_RDONLY)) >= 0) { if ((fd = open(xpram_name.c_str(), O_RDONLY)) >= 0) {
read(fd, XPRAM, XPRAM_SIZE); read(fd, XPRAM, XPRAM_SIZE);
close(fd); close(fd);
} }
} }
/* /*
* Save XPRAM to settings file * Save XPRAM to settings file
*/ */
void SaveXPRAM(void) void SaveXPRAM(void){
{ assert(!xpram_name.empty());
int fd; int fd;
if ((fd = open(xpram_path, O_WRONLY | O_CREAT, 0666)) >= 0) { if ((fd = open(xpram_name.c_str(), O_WRONLY | O_CREAT, 0666)) >= 0) {
write(fd, XPRAM, XPRAM_SIZE); write(fd, XPRAM, XPRAM_SIZE);
close(fd); close(fd);
} }
} }
/* /*
* Delete PRAM file * Delete PRAM file
*/ */
void ZapPRAM(void) void ZapPRAM(void){
{
// Construct PRAM path
xpram_path[0] = 0;
char *home = getenv("HOME");
if (home != NULL && strlen(home) < 1000) {
strncpy(xpram_path, home, 1000);
strcat(xpram_path, "/");
}
strcat(xpram_path, XPRAM_FILE_NAME);
// Delete file // Delete file
unlink(xpram_path); assert(!xpram_name.empty());
unlink(xpram_name.c_str());
} }

View File

@ -20,14 +20,14 @@
#include "sysdeps.h" #include "sysdeps.h"
#include <string.h> #include <cerrno>
#include <stdlib.h> #include <sys/stat.h>
#include <stdio.h>
#include <string> #include <string>
using std::string;
#include "prefs.h" #include "prefs.h"
// Platform-specific preferences items // Platform-specific preferences items
prefs_desc platform_prefs_items[] = { prefs_desc platform_prefs_items[] = {
{"ether", TYPE_STRING, false, "device name of Mac ethernet adapter"}, {"ether", TYPE_STRING, false, "device name of Mac ethernet adapter"},
@ -47,8 +47,10 @@ prefs_desc platform_prefs_items[] = {
// Prefs file name and path // Prefs file name and path
const char PREFS_FILE_NAME[] = ".sheepshaver_prefs"; static const char PREFS_FILE_NAME[] = "/prefs";
static char prefs_path[1024]; static string prefs_path;
static string prefs_name;
extern string xpram_name;
std::string UserPrefsPath; std::string UserPrefsPath;
@ -56,13 +58,26 @@ std::string UserPrefsPath;
* Load preferences from settings file * Load preferences from settings file
*/ */
void LoadPrefs(const char *vmdir) // Comply with XDG Base Directory Specification
{ // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
if (vmdir) { static void get_prefs_path_from_env(void){
snprintf(prefs_path, sizeof(prefs_path), "%s/prefs", vmdir); char* env;
FILE *prefs = fopen(prefs_path, "r"); if(env=getenv("XDG_CONFIG_HOME")){
prefs_path = string(env);
return;
}
if(env=getenv("HOME")){
prefs_path = string(env) + "/.config";
}
}
void LoadPrefs(const char* vmdir){
if(vmdir){
prefs_path = string(vmdir);
prefs_name = prefs_path + PREFS_FILE_NAME;
FILE *prefs = fopen(prefs_name.c_str(), "r");
if (!prefs) { if (!prefs) {
printf("No file at %s found.\n", prefs_path); printf("No file at %s found.\n", prefs_name.c_str());
exit(1); exit(1);
} }
LoadPrefsFromStream(prefs); LoadPrefsFromStream(prefs);
@ -70,20 +85,21 @@ void LoadPrefs(const char *vmdir)
return; return;
} }
if (!UserPrefsPath.empty()) strncpy(prefs_path, UserPrefsPath.c_str(), 1000); // Construct prefs path
else { get_prefs_path_from_env();
// Construct prefs path if(!prefs_path.empty()){
prefs_path[0] = 0; prefs_path += "/SheepShaver";
char *home = getenv("HOME"); prefs_name = prefs_path + PREFS_FILE_NAME;
if (home != NULL && strlen(home) < 1000) { }
strncpy(prefs_path, home, 1000); xpram_name = prefs_path + "/nvram";
strcat(prefs_path, "/");
} // --config was specified
strcat(prefs_path, PREFS_FILE_NAME); if(!UserPrefsPath.empty()){
prefs_name = UserPrefsPath;
} }
// Read preferences from settings file // Read preferences from settings file
FILE *f = fopen(prefs_path, "r"); FILE *f = fopen(prefs_name.c_str(), "r");
if (f != NULL) { if (f != NULL) {
// Prefs file found, load settings // Prefs file found, load settings
@ -99,21 +115,51 @@ void LoadPrefs(const char *vmdir)
} }
} }
static bool is_dir(const std::string& path){
struct stat info;
if(stat(path.c_str(), &info) != 0){
return false;
}
return (info.st_mode & S_IFDIR) != 0;
}
static bool create_directories(const std::string& path,mode_t mode){
if(mkdir(path.c_str(),mode)==0)
return true;
switch (errno){
case ENOENT:
{
int pos = path.find_last_of('/');
if (pos == std::string::npos)
return false;
if (!create_directories(path.substr(0,pos),mode))
return false;
}
return 0 == mkdir(path.c_str(),mode);
case EEXIST:
return is_dir(path);
default:
return false;
}
}
/* /*
* Save preferences to settings file * Save preferences to settings file
*/ */
void SavePrefs(void) void SavePrefs(void){
{
FILE *f; FILE *f;
if ((f = fopen(prefs_path, "w")) != NULL) { if(!prefs_path.empty()&&!is_dir(prefs_path)){
create_directories(prefs_path,0700);
}
if ((f = fopen(prefs_name.c_str(), "w")) != NULL) {
SavePrefsToStream(f); SavePrefsToStream(f);
fclose(f); fclose(f);
} }
} }
/* /*
* Add defaults of platform-specific prefs items * Add defaults of platform-specific prefs items
* You may also override the defaults set in PrefsInit() * You may also override the defaults set in PrefsInit()