2017-03-13 19:03:07 -07:00
|
|
|
#include <cstdlib>
|
2025-02-03 18:50:02 -08:00
|
|
|
#include <filesystem>
|
2017-03-13 19:03:07 -07:00
|
|
|
#include <fstream>
|
2017-03-22 19:02:14 -07:00
|
|
|
#include <map>
|
2017-03-13 19:03:07 -07:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
2025-01-18 23:09:28 -08:00
|
|
|
#include <boost/filesystem.hpp>
|
2018-03-13 20:06:15 -07:00
|
|
|
#include "nlohmann/json.hpp"
|
2017-03-13 19:03:07 -07:00
|
|
|
|
|
|
|
#include "bottles.hpp"
|
2025-01-27 16:51:54 -08:00
|
|
|
#include "cmake.hpp"
|
2017-03-23 13:17:07 -07:00
|
|
|
#include "internal/bottles.hpp"
|
2017-03-13 19:03:07 -07:00
|
|
|
#include "fs.hpp"
|
2017-03-23 18:51:11 -07:00
|
|
|
#include "output.hpp"
|
2025-01-27 16:51:54 -08:00
|
|
|
#ifdef ENABLE_STEAM
|
|
|
|
#include "steam.hpp"
|
|
|
|
#endif
|
2017-03-13 19:03:07 -07:00
|
|
|
|
|
|
|
using namespace std;
|
2017-03-23 18:51:11 -07:00
|
|
|
using namespace cellar;
|
2017-03-22 19:02:14 -07:00
|
|
|
using namespace cellar::bottles;
|
2017-03-13 19:03:07 -07:00
|
|
|
|
2017-03-23 12:06:29 -07:00
|
|
|
using CommandFunction = cellar::commands::CommandFunction;
|
2017-03-13 19:03:07 -07:00
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2025-02-03 17:28:18 -08:00
|
|
|
/**
|
|
|
|
* @brief Construct an empty bottle object.
|
|
|
|
* This empty bottle object can then be used to help create new ones.
|
|
|
|
*/
|
2017-03-22 19:02:14 -07:00
|
|
|
Bottle::Bottle() {
|
|
|
|
// define a null bottle
|
|
|
|
// strings handle themselves
|
|
|
|
config = json({});
|
|
|
|
type = bottle_anonymous;
|
2025-02-03 18:50:02 -08:00
|
|
|
manager = manager_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cellar::bottles::bottle_home;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets up bottle home.
|
|
|
|
* Called once from early mainloop.
|
|
|
|
* @todo Have this not be an effectively hardcoded path. Respect xdg paths.
|
|
|
|
*/
|
|
|
|
void cellar::bottles::setup_bottle_home() {
|
|
|
|
stringstream sstr_bottle_home;
|
|
|
|
sstr_bottle_home << std::getenv("HOME");
|
|
|
|
sstr_bottle_home << "/.local/share/cellar/bottles";
|
|
|
|
bottle_home = sstr_bottle_home.str();
|
2017-03-22 19:02:14 -07:00
|
|
|
}
|
|
|
|
|
2025-02-03 17:28:18 -08:00
|
|
|
/**
|
|
|
|
* @brief Construct a bottle object from at a given path.
|
|
|
|
*
|
|
|
|
* @param patharg The path to load a bottle from.
|
|
|
|
*/
|
2017-03-24 17:58:24 -07:00
|
|
|
Bottle::Bottle(string patharg) {
|
2017-03-24 21:50:32 -07:00
|
|
|
output::statement("loading bottle from " + patharg, true);
|
2017-03-24 17:58:24 -07:00
|
|
|
config = json({});
|
|
|
|
path = patharg;
|
|
|
|
|
2025-02-03 18:50:02 -08:00
|
|
|
//boost::filesystem::file_status path_status = boost::filesystem::symlink_status(path);
|
|
|
|
//bool symlink = boost::filesystem::is_symlink(path_status);
|
|
|
|
auto path_status = std::filesystem::path(path);
|
|
|
|
auto path_canon = std::filesystem::canonical(path_status);
|
|
|
|
canonical_path = path_canon.string();
|
2017-03-24 17:58:24 -07:00
|
|
|
|
2025-02-03 18:50:02 -08:00
|
|
|
if (std::filesystem::is_symlink(path_canon)) {
|
2017-03-24 17:58:24 -07:00
|
|
|
type = bottle_symlink;
|
|
|
|
} else {
|
|
|
|
try {
|
2025-02-03 18:50:02 -08:00
|
|
|
load_config();
|
|
|
|
auto cur_manager = get_config("manager");
|
|
|
|
if (cur_manager == "cellar") {
|
|
|
|
manager = manager_cellar;
|
|
|
|
|
|
|
|
if (get_config("name") != "") {
|
|
|
|
type = bottle_labelled;
|
|
|
|
} else {
|
|
|
|
type = bottle_anonymous;
|
|
|
|
}
|
|
|
|
} else if (path_canon.parent_path() == bottle_home) {
|
|
|
|
manager = manager_cellar;
|
|
|
|
set_config("manager", "cellar"); // migrate from older cellar (or correct for something weird happening)
|
|
|
|
save_config();
|
|
|
|
|
|
|
|
if (get_config("name") != "") {
|
|
|
|
type = bottle_labelled;
|
|
|
|
} else {
|
|
|
|
type = bottle_anonymous;
|
|
|
|
}
|
|
|
|
} else if (cur_manager == "steam") {
|
|
|
|
type = bottle_steam;
|
|
|
|
manager = manager_steam;
|
2017-03-24 17:58:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const exception &exc) {
|
|
|
|
type = bottle_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-03 17:28:18 -08:00
|
|
|
/**
|
|
|
|
* @brief Lists all bottles cellar manages or is otherwise aware of.
|
|
|
|
* This includes bottles managed by other tools like Steam or Lutris,
|
|
|
|
* assuming support for those tools is compiled into cellar. (Steam
|
|
|
|
* support is present; Lutris support is planned for the future.)
|
|
|
|
*
|
|
|
|
* @return map<string, Bottle> All bottles. Bottles managed by other tools have prefixed keys, e.g. Steam bottles use "steam:"
|
|
|
|
*/
|
2017-03-28 12:41:25 -07:00
|
|
|
map<string, Bottle> cellar::bottles::get_bottles() {
|
2017-03-22 19:02:14 -07:00
|
|
|
map<string, Bottle> result;
|
2017-03-13 19:03:07 -07:00
|
|
|
|
|
|
|
string homepath = getenv("HOME");
|
2018-03-13 20:06:15 -07:00
|
|
|
vector<string> homedir = fs::listdir(homepath + "/.local/share/cellar/bottles");
|
2017-03-13 19:03:07 -07:00
|
|
|
for (string item : homedir) {
|
2018-03-13 20:06:15 -07:00
|
|
|
string fullitem = homepath + "/.local/share/cellar/bottles/" + item;
|
|
|
|
Bottle output(fullitem);
|
2017-03-13 19:03:07 -07:00
|
|
|
|
2018-03-13 20:06:15 -07:00
|
|
|
result[item] = output;
|
2017-03-13 19:03:07 -07:00
|
|
|
}
|
|
|
|
|
2025-01-27 16:51:54 -08:00
|
|
|
#ifdef ENABLE_STEAM
|
|
|
|
map<string, Bottle> bottles_proton = cellar::steam::get_app_bottles();
|
|
|
|
for (auto item : bottles_proton) {
|
|
|
|
result.insert_or_assign(item.first, item.second);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-13 19:03:07 -07:00
|
|
|
return result;
|
|
|
|
}
|
2017-03-23 00:02:38 -07:00
|
|
|
|
2025-02-03 17:28:18 -08:00
|
|
|
/**
|
|
|
|
* @brief Takes an input that refers to a bottle and returns a path to that bottle.
|
|
|
|
* This input can be any of the following:
|
|
|
|
* * A bottle name as managed by cellar
|
|
|
|
* * A prefixed bottle name, in the format `<tool>:<identifier>`. (e.g. `steam:78000`)
|
|
|
|
* * An absolute or relative path, in which case it is returned with no modification.
|
|
|
|
* * A path relative to (and starting with ~). cellar will throw a warning in verbose mode,
|
|
|
|
* since usually the shell expands this for us, but will do a naive replacement on its own
|
|
|
|
* and try to prevent confused users.
|
|
|
|
*
|
|
|
|
* @param bottlechoice Input, usually referring to a specific bottle known to cellar.
|
|
|
|
* @return string Path to referenced bottle.
|
|
|
|
*/
|
2017-03-28 12:41:25 -07:00
|
|
|
string cellar::bottles::resolve_bottle(string bottlechoice) {
|
|
|
|
string result;
|
|
|
|
if (bottlechoice.substr(0,1) == "/" || bottlechoice.substr(0,1) == ".") { // absolute or relative path
|
|
|
|
result = bottlechoice;
|
|
|
|
} else if (bottlechoice.substr(0,1) == "~") { // "absolute" path in home directory, not expanded by the shell for some reason (i've seen some shit)
|
2025-02-02 15:24:17 -08:00
|
|
|
// this is a naive replacement and will fail if the user tries something like ~nicole/.wine
|
2017-03-28 12:41:25 -07:00
|
|
|
// i'm figuring at that point if you're doing that, you'll also recognize if your shell
|
|
|
|
// isn't actually expanding your path...
|
|
|
|
bottlechoice.replace(0,1,getenv("HOME"));
|
|
|
|
// or at least you'll think to use verbose mode to make sure it's loading the right directory
|
|
|
|
output::warning("your shell didn't expand your given path properly, doing a naive replacement", true);
|
|
|
|
result = bottlechoice;
|
2025-02-02 15:24:17 -08:00
|
|
|
#ifdef ENABLE_STEAM
|
|
|
|
} else if (bottlechoice.substr(0,6) == "steam:") { // steam bottles
|
|
|
|
string str_appid = bottlechoice.substr(6);
|
|
|
|
unsigned long uint_appid = std::stoul(str_appid);
|
|
|
|
auto steambottle = cellar::steam::app_bottle(uint_appid);
|
|
|
|
result = steambottle.path;
|
|
|
|
#endif
|
2017-03-28 12:41:25 -07:00
|
|
|
} else {
|
|
|
|
string homepath = getenv("HOME");
|
2025-06-03 12:26:30 -07:00
|
|
|
string fullbottlepath = homepath + "/.local/share/cellar/bottles/" + bottlechoice;
|
2017-03-28 12:41:25 -07:00
|
|
|
result = fullbottlepath;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2025-02-03 17:28:18 -08:00
|
|
|
/**
|
|
|
|
* @brief Prints bottles. Used as a command in CLI.
|
|
|
|
*/
|
2017-03-23 15:13:52 -07:00
|
|
|
void cellar::bottles::print_bottles(int argc, vector<string> argv) {
|
2017-03-23 00:02:38 -07:00
|
|
|
map<string, Bottle> bottles = get_bottles();
|
|
|
|
|
2017-03-23 18:51:11 -07:00
|
|
|
stringstream outstr;
|
|
|
|
|
2017-03-23 00:02:38 -07:00
|
|
|
for (auto item : bottles) {
|
2018-03-13 20:06:15 -07:00
|
|
|
if (item.first == ".wine" || item.first == ".local/share/cellar/bottles/template") {
|
|
|
|
// .wine is considered to be "active", and .local/share/cellar/bottles/template is used as a template
|
2017-03-23 14:23:58 -07:00
|
|
|
// and therefore treated specially
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-23 00:02:38 -07:00
|
|
|
Bottle bottle = item.second;
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << item.first << " - ";
|
2017-03-23 00:02:38 -07:00
|
|
|
switch (bottle.type) {
|
|
|
|
case bottle_anonymous:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << "anonymous wine bottle";
|
2017-03-23 00:02:38 -07:00
|
|
|
break;
|
|
|
|
case bottle_symlink:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << "symlink to " << bottle.canonical_path;
|
2017-03-23 00:02:38 -07:00
|
|
|
break;
|
|
|
|
case bottle_labelled:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << bottle.config["name"];
|
2017-03-23 00:02:38 -07:00
|
|
|
break;
|
2025-02-03 17:50:33 -08:00
|
|
|
case bottle_steam:
|
|
|
|
outstr << "Steam managed bottle for " << bottle.config["name"];
|
|
|
|
break;
|
2017-03-23 00:02:38 -07:00
|
|
|
default:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << "broken or unsupported wine bottle";
|
2017-03-23 00:02:38 -07:00
|
|
|
}
|
2017-03-23 18:51:11 -07:00
|
|
|
output::statement(outstr.str());
|
|
|
|
outstr.str("");
|
2017-03-23 00:02:38 -07:00
|
|
|
}
|
|
|
|
}
|