Josh Larson
0d63656df8
When either HOME or XDG_CONFIG_HOME are unset getenv() can return NULL. C++ strings don't like being set to NULL, so we need to check for this case. Bryan: Edited Sconstruct and commit message. Signed-off-by: Josh Larson <theMutatedShrimp@gmail.com> Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
65 lines
1.2 KiB
C++
65 lines
1.2 KiB
C++
|
|
#include <dirent.h>
|
|
#include <sys/stat.h>
|
|
#include <stdlib.h>
|
|
#include <libsaria/path.h>
|
|
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
/* I want to use the d_type field in a dirent */
|
|
#ifndef __USE_BSD
|
|
#define __USE_BSD
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
|
static string SARIA_DIR = "/saria-debug";
|
|
#else /* DEBUG */
|
|
static string SARIA_DIR = "/saria";
|
|
#endif /* DEBUG */
|
|
|
|
string get_saria_env(const char *env_name, string default_env)
|
|
{
|
|
char *result = getenv(env_name);
|
|
if (result)
|
|
return string(result);
|
|
return default_env;
|
|
}
|
|
|
|
string get_saria_dir()
|
|
{
|
|
string saria = get_saria_env("HOME", "");
|
|
string xdg = get_saria_env("XDG_CONFIG_HOME", "~/.config");
|
|
|
|
if (xdg == "")
|
|
saria += "/.config";
|
|
else if (xdg.compare(0, saria.size(), saria) == 0)
|
|
saria = xdg + "/";
|
|
else
|
|
saria += "/" + xdg += "/";
|
|
|
|
return saria += SARIA_DIR;
|
|
}
|
|
|
|
void make_saria_dir()
|
|
{
|
|
string saria = get_saria_dir();
|
|
mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
|
mkdir(saria.c_str(), mode);
|
|
}
|
|
|
|
bool exists(string path)
|
|
{
|
|
struct stat stat;
|
|
return lstat(path.c_str(), &stat) == 0;
|
|
}
|
|
|
|
sid_t lookup_songid(string &filepath)
|
|
{
|
|
struct stat stat;
|
|
int err = lstat(filepath.c_str(), &stat);
|
|
if (err != 0)
|
|
return err;
|
|
return stat.st_ino;
|
|
}
|