@@ -78,6 +78,16 @@ std::filesystem::path GetHomeDirectoryPath() {
78
78
return std::filesystem::path (homeDir);
79
79
}
80
80
81
+ // Helper function to get XDG base directory, falling back to default if not set
82
+ std::filesystem::path GetXDGDirectoryPath (const std::string& envVar,
83
+ const std::string& defaultPath) {
84
+ if (const char * envValue = std::getenv (envVar.c_str ());
85
+ envValue && std::strlen (envValue) > 0 ) {
86
+ return std::filesystem::path (envValue);
87
+ }
88
+ return GetHomeDirectoryPath () / defaultPath;
89
+ }
90
+
81
91
std::filesystem::path GetConfigurationPath () {
82
92
#ifndef CORTEX_CONFIG_FILE_PATH
83
93
#define CORTEX_CONFIG_FILE_PATH kDefaultConfigurationPath
@@ -113,9 +123,14 @@ std::filesystem::path GetConfigurationPath() {
113
123
std::string config_file_name{kCortexConfigurationFileName };
114
124
config_file_name.append (env_postfix);
115
125
// CTL_INF("Config file name: " + config_file_name);
116
-
126
+ #if defined(__linux__)
127
+ auto config_base_path =
128
+ GetXDGDirectoryPath (" XDG_CONFIG_HOME" , " .config" ) / kCortexFolderName ;
129
+ auto configuration_path = config_base_path / config_file_name;
130
+ #else
117
131
auto home_path = GetHomeDirectoryPath ();
118
132
auto configuration_path = home_path / config_file_name;
133
+ #endif
119
134
return configuration_path;
120
135
}
121
136
@@ -150,11 +165,20 @@ cpp::result<void, std::string> UpdateCortexConfig(
150
165
config_yaml_utils::CortexConfig GetDefaultConfig () {
151
166
auto config_path = GetConfigurationPath ();
152
167
auto default_data_folder_name = GetDefaultDataFolderName ();
168
+ #if defined(__linux__)
169
+ auto default_data_folder_path =
170
+ cortex_data_folder_path.empty ()
171
+ ? file_manager_utils::GetXDGDirectoryPath (" XDG_DATA_HOME" ,
172
+ " .local/share" ) /
173
+ default_data_folder_name
174
+ : std::filesystem::path (cortex_data_folder_path);
175
+ #else
153
176
auto default_data_folder_path =
154
177
cortex_data_folder_path.empty ()
155
178
? file_manager_utils::GetHomeDirectoryPath () /
156
179
default_data_folder_name
157
180
: std::filesystem::path (cortex_data_folder_path);
181
+ #endif
158
182
159
183
return config_yaml_utils::CortexConfig{
160
184
#if defined(_WIN32)
@@ -204,6 +228,10 @@ cpp::result<void, std::string> CreateConfigFileIfNotExist() {
204
228
// already exists, no need to create
205
229
return {};
206
230
}
231
+ if (!std::filesystem::exists (config_path.parent_path ())) {
232
+ // Ensure the configuration directory exists
233
+ std::filesystem::create_directories (config_path.parent_path ());
234
+ }
207
235
208
236
CLI_LOG (" Config file not found. Creating one at " + config_path.string ());
209
237
auto config = GetDefaultConfig ();
@@ -236,8 +264,13 @@ std::filesystem::path GetCortexDataPath() {
236
264
data_folder_path = std::filesystem::path (config.dataFolderPath );
237
265
#endif
238
266
} else {
267
+ #if defined(__linux__)
268
+ auto data_base_path = GetXDGDirectoryPath (" XDG_DATA_HOME" , " .local/share" );
269
+ data_folder_path = data_base_path / GetDefaultDataFolderName ();
270
+ #else
239
271
auto home_path = GetHomeDirectoryPath ();
240
272
data_folder_path = home_path / kCortexFolderName ;
273
+ #endif
241
274
}
242
275
243
276
if (!std::filesystem::exists (data_folder_path)) {
@@ -253,13 +286,19 @@ std::filesystem::path GetCortexLogPath() {
253
286
// TODO: get the variant of cortex. As discussed, we will have: prod, beta, nightly
254
287
255
288
// currently we will store cortex data at ~/cortexcpp
289
+ // On linux, we follow the xdg directory specification
256
290
auto config = GetCortexConfig ();
257
291
std::filesystem::path log_folder_path;
258
292
if (!config.logFolderPath .empty ()) {
259
293
log_folder_path = std::filesystem::path (config.logFolderPath );
260
294
} else {
295
+ #if defined(__linux__)
296
+ auto data_base_path = GetXDGDirectoryPath (" XDG_DATA_HOME" , " .local/share" );
297
+ log_folder_path = data_base_path / GetDefaultDataFolderName () / " logs" ;
298
+ #else
261
299
auto home_path = GetHomeDirectoryPath ();
262
300
log_folder_path = home_path / kCortexFolderName ;
301
+ #endif
263
302
}
264
303
265
304
if (!std::filesystem::exists (log_folder_path)) {
0 commit comments