2022-11-14 19:06:09 +00:00
|
|
|
/**
|
|
|
|
** Supermodel
|
|
|
|
** A Sega Model 3 Arcade Emulator.
|
|
|
|
** Copyright 2003-2022 The Supermodel Team
|
|
|
|
**
|
|
|
|
** This file is part of Supermodel.
|
|
|
|
**
|
|
|
|
** Supermodel is free software: you can redistribute it and/or modify it under
|
|
|
|
** the terms of the GNU General Public License as published by the Free
|
|
|
|
** Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
** any later version.
|
|
|
|
**
|
|
|
|
** Supermodel is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
** more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License along
|
|
|
|
** with Supermodel. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "FileSystemPath.h"
|
|
|
|
#include "Util/Format.h"
|
|
|
|
#include <string>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace FileSystemPath
|
|
|
|
{
|
|
|
|
// Checks if a directory exists (returns true if exists, false if it doesn't)
|
|
|
|
bool PathExists(std::string fileSystemPath)
|
|
|
|
{
|
|
|
|
bool pathExists = false;
|
|
|
|
struct stat pathInfo;
|
|
|
|
|
|
|
|
if (stat(fileSystemPath.c_str(), &pathInfo) == 0 && S_ISDIR(pathInfo.st_mode))
|
|
|
|
{
|
|
|
|
pathExists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathExists;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a path to be used by Supermodel files
|
2022-11-21 21:27:15 +00:00
|
|
|
std::string GetPath(PathType pathType)
|
2022-11-14 19:06:09 +00:00
|
|
|
{
|
|
|
|
std::string finalPath = "";
|
|
|
|
std::string homePath = "";
|
2022-11-15 16:40:01 +00:00
|
|
|
std::string strPathType = "";
|
2022-11-21 21:27:15 +00:00
|
|
|
struct passwd* pwd = getpwuid(getuid());
|
2022-11-14 19:06:09 +00:00
|
|
|
|
2022-11-15 16:40:01 +00:00
|
|
|
// Resolve pathType to string for later use
|
|
|
|
switch (pathType)
|
|
|
|
{
|
2022-11-21 21:27:15 +00:00
|
|
|
case Analysis:
|
|
|
|
strPathType = "Analysis";
|
|
|
|
break;
|
|
|
|
case Config:
|
|
|
|
strPathType = "Config";
|
|
|
|
break;
|
|
|
|
case Log:
|
|
|
|
strPathType = "Log";
|
|
|
|
break;
|
|
|
|
case NVRAM:
|
|
|
|
strPathType = "NVRAM";
|
|
|
|
break;
|
|
|
|
case Saves:
|
|
|
|
strPathType = "Saves";
|
|
|
|
break;
|
|
|
|
case Screenshots:
|
|
|
|
strPathType = "Screenshots";
|
|
|
|
break;
|
2022-11-15 16:40:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 19:06:09 +00:00
|
|
|
// Get user's HOME directory
|
2022-11-21 21:27:15 +00:00
|
|
|
if (pwd)
|
|
|
|
{
|
|
|
|
homePath = pwd->pw_dir;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
homePath = getenv("HOME");
|
|
|
|
}
|
2022-11-14 19:06:09 +00:00
|
|
|
|
|
|
|
// If Config path exists in current directory or the user doesn't have a HOME directory use current directory
|
|
|
|
if (FileSystemPath::PathExists("Config") || homePath.empty())
|
|
|
|
{
|
|
|
|
// Use current directory
|
2022-11-15 16:40:01 +00:00
|
|
|
if (pathType == Screenshots || pathType == Log)
|
2022-11-14 19:06:09 +00:00
|
|
|
{
|
|
|
|
finalPath = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If directory doesn't exist, create it
|
2022-11-21 21:27:15 +00:00
|
|
|
if (!FileSystemPath::PathExists(strPathType))
|
|
|
|
{
|
|
|
|
mkdir(strPathType.c_str(), 0775);
|
|
|
|
}
|
2022-11-15 16:40:01 +00:00
|
|
|
finalPath = strPathType;
|
2022-11-14 19:06:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if $HOME/.supermodel exists
|
|
|
|
else if (FileSystemPath::PathExists(Util::Format() << homePath << "/.supermodel"))
|
|
|
|
{
|
|
|
|
// Use $HOME/.supermodel
|
2022-11-15 16:40:01 +00:00
|
|
|
finalPath = Util::Format() << homePath << "/.supermodel/" << strPathType;
|
2022-11-14 19:06:09 +00:00
|
|
|
// If directory doesn't exist, create it
|
|
|
|
if (!FileSystemPath::PathExists(finalPath))
|
|
|
|
{
|
|
|
|
mkdir(finalPath.c_str(), 0775);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// On Linux one may want to follow the XDG base directory specs (https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Use $HOME/.config/supermodel or $HOME/.local/share/supermodel depending on the file type
|
2022-11-15 16:40:01 +00:00
|
|
|
if (pathType == Config)
|
2022-11-14 19:06:09 +00:00
|
|
|
{
|
|
|
|
finalPath = Util::Format() << homePath << "/.config/supermodel";
|
|
|
|
// If directory doesn't exist, create it
|
2022-11-21 21:27:15 +00:00
|
|
|
if (!FileSystemPath::PathExists(finalPath))
|
|
|
|
{
|
|
|
|
mkdir(finalPath.c_str(), 0775);
|
|
|
|
}
|
2022-11-14 19:06:09 +00:00
|
|
|
// If directory doesn't exist, create it
|
|
|
|
finalPath = Util::Format() << homePath << "/.config/supermodel/Config";
|
2022-11-21 21:27:15 +00:00
|
|
|
if (!FileSystemPath::PathExists(finalPath))
|
|
|
|
{
|
|
|
|
mkdir(finalPath.c_str(), 0775);
|
|
|
|
}
|
2022-11-14 19:06:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
finalPath = Util::Format() << homePath << "/.local/share/supermodel";
|
|
|
|
// If directory doesn't exist, create it
|
2022-11-21 21:27:15 +00:00
|
|
|
if (!FileSystemPath::PathExists(finalPath))
|
|
|
|
{
|
|
|
|
mkdir(finalPath.c_str(), 0775);
|
|
|
|
}
|
2022-11-14 19:06:09 +00:00
|
|
|
// If directory doesn't exist, create it
|
2022-11-15 16:40:01 +00:00
|
|
|
finalPath = Util::Format() << homePath << "/.local/share/supermodel/" << strPathType;
|
2022-11-21 21:27:15 +00:00
|
|
|
if (!FileSystemPath::PathExists(finalPath))
|
|
|
|
{
|
|
|
|
mkdir(finalPath.c_str(), 0775);
|
|
|
|
}
|
2022-11-14 19:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-21 21:27:15 +00:00
|
|
|
if (finalPath != "")
|
|
|
|
{
|
|
|
|
finalPath = Util::Format() << finalPath << "/";
|
|
|
|
}
|
2022-11-14 19:06:09 +00:00
|
|
|
|
|
|
|
return finalPath;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|