File size: 28,419 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************************************
 *                                                                                                 *
 *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>                                      *
 *   Copyright (c) 2025 The FreeCAD project association AISBL                                      *
 *                                                                                                 *
 *   This file is part of FreeCAD.                                                                 *
 *                                                                                                 *
 *   FreeCAD is free software: you can redistribute it and/or modify it under the terms of the     *
 *   GNU Lesser General Public License as published by the Free Software Foundation, either        *
 *   version 2.1 of the License, or (at your option) any later version.                            *
 *                                                                                                 *
 *   FreeCAD 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 Lesser General Public License for more details.                                           *
 *                                                                                                 *
 *   You should have received a copy of the GNU Lesser General Public License along with FreeCAD.  *
 *   If not, see <https://www.gnu.org/licenses/>.                                                  *
 *                                                                                                 *
 **************************************************************************************************/

#include <fmt/format.h>
#include <utility>
#include <QDir>
#include <QProcessEnvironment>
#include <QStandardPaths>
#include <QCoreApplication>

#include "ApplicationDirectories.h"

#include <FCConfig.h>

#if defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
#include <pwd.h>
#endif

#include <Base/FileInfo.h>
#include <Base/Exception.h>
#include "SafeMode.h"

#include <Python.h>
#include <QString>

#include "Base/Console.h"


using namespace App;
namespace fs = std::filesystem;

fs::path qstringToPath(const QString& path)
{
#if defined(FC_OS_WIN32)
    return {path.toStdWString()};
#else
    return {path.toStdString()};
#endif
}

ApplicationDirectories::ApplicationDirectories(std::map<std::string,std::string> &config)
{
    _currentVersion = extractVersionFromConfigMap(config);
    configurePaths(config);
    configureResourceDirectory(config);
    configureLibraryDirectory(config);
    configureHelpDirectory(config);
}

const fs::path& ApplicationDirectories::getHomePath() const
{
    return this->_home;
}

const fs::path& ApplicationDirectories::getUserHomePath() const
{
    return this->_userHome;
}

const fs::path& ApplicationDirectories::getTempPath() const {
    return this->_temp;
}

fs::path ApplicationDirectories::getTempFileName(const std::string & filename) const {
    auto tempPath = Base::FileInfo::pathToString(getTempPath());
    if (filename.empty()) {
        return Base::FileInfo::getTempFileName(nullptr, tempPath.c_str());
    }
    return Base::FileInfo::getTempFileName(filename.c_str(), tempPath.c_str());
}

const fs::path& ApplicationDirectories::getUserCachePath() const
{
    return this->_userCache;
}

const fs::path& ApplicationDirectories::getUserAppDataDir() const
{
    return this->_userAppData;
}

const fs::path& ApplicationDirectories::getUserMacroDir() const
{
    return this->_userMacro;
}

const fs::path& ApplicationDirectories::getResourceDir() const
{
    return this->_resource;
}

const fs::path& ApplicationDirectories::getHelpDir() const
{
    return this->_help;
}

const fs::path& ApplicationDirectories::getUserConfigPath() const {
    return this->_userConfig;
}

const fs::path& ApplicationDirectories::getLibraryDir() const {
    return this->_library;
}


/*!
 * \brief findPath
 * Returns the path where to store application files to.
 * If \a customHome is not empty, it will be used, otherwise a path starting from \a stdHome will be
 * used.
 */
fs::path ApplicationDirectories::findPath(const fs::path& stdHome, const fs::path& customHome,
                                          const std::vector<std::string>& subdirs, bool create) {
    fs::path appData = customHome;
    if (appData.empty()) {
        appData = stdHome;
    }

    // If a custom user home path is given, then don't modify it
    if (customHome.empty()) {
        for (const auto& it : subdirs) {
            appData = appData / it;
        }
    }

    // To write to our data path, we must create some directories, first.
    if (create && !fs::exists(appData) && !Py_IsInitialized()) {
        try {
            fs::create_directories(appData);
        } catch (const fs::filesystem_error& e) {
            throw Base::FileSystemError("Could not create directories. Failed with: " + e.code().message());
        }
    }

    return appData;
}

void ApplicationDirectories::appendVersionIfPossible(const fs::path& basePath, std::vector<std::string> &subdirs) const
{
    fs::path pathToCheck = basePath;
    for (const auto& it : subdirs) {
        pathToCheck = pathToCheck / it;
    }
    if (isVersionedPath(pathToCheck)) {
        return; // Bail out if it's already versioned
    }
    if (fs::exists(pathToCheck)) {
        std::string version = mostRecentAvailableConfigVersion(pathToCheck);
        if (!version.empty()) {
            subdirs.emplace_back(std::move(version));
        }
    } else {
        auto [major, minor] = _currentVersion;
        subdirs.emplace_back(versionStringForPath(major, minor));
    }
}

void ApplicationDirectories::configurePaths(std::map<std::string,std::string>& mConfig)
{
    bool keepDeprecatedPaths = mConfig.contains("KeepDeprecatedPaths");

    // std paths
    _home = fs::path(mConfig.at("AppHomePath"));
    mConfig["BinPath"] = mConfig.at("AppHomePath") + "bin" + PATHSEP;
    mConfig["DocPath"] = mConfig.at("AppHomePath") + "doc" + PATHSEP;

    // this is to support a portable version of FreeCAD
    auto [customHome, customData, customTemp] = getCustomPaths();
    _usingCustomDirectories = !customHome.empty() || !customData.empty();

    // get the system standard paths
    auto [configHome, dataHome, cacheHome, tempPath] = getStandardPaths();

    if (mConfig.contains("SafeMode")) {
        if (startSafeMode(mConfig)) {
            // If we're in safe mode, don't try to set any directories here, they've been overridden
            // by temp directories in the SafeMode setup.
            return;
        }
    }

    // User home path
    //
    fs::path homePath = findUserHomePath(customHome);
    mConfig["UserHomePath"] = Base::FileInfo::pathToString(homePath);
    _userHome = homePath;

    // the old path name to save config and data files
    std::vector<std::string> subdirs;
    if (keepDeprecatedPaths) {
        configHome = homePath;
        dataHome = homePath;
        cacheHome = homePath;
        getOldDataLocation(mConfig, subdirs);
    }
    else {
        getSubDirectories(mConfig, subdirs);
    }


    // User data path
    //
    auto dataSubdirs = subdirs;
    appendVersionIfPossible(dataHome, dataSubdirs);
    fs::path data = findPath(dataHome, customData, dataSubdirs, true);
    _userAppData = data;
    mConfig["UserAppData"] = Base::FileInfo::pathToString(data) + PATHSEP;


    // User config path
    //
    auto configSubdirs = subdirs;
    appendVersionIfPossible(configHome, configSubdirs);
    fs::path config = findPath(configHome, customHome, configSubdirs, true);
    _userConfig = config;
    mConfig["UserConfigPath"] = Base::FileInfo::pathToString(config) + PATHSEP;


    // User cache path
    //
    std::vector<std::string> cachedirs = subdirs;
    cachedirs.emplace_back("Cache");
    fs::path cache = findPath(cacheHome, customTemp, cachedirs, true);
    _userCache = cache;
    mConfig["UserCachePath"] = Base::FileInfo::pathToString(cache) + PATHSEP;


    // Set application temporary directory
    //
    std::vector<std::string> empty;
    fs::path tmp = findPath(tempPath, customTemp, empty, true);
    _temp = tmp;
    mConfig["AppTempPath"] = Base::FileInfo::pathToString(tmp) + PATHSEP;


    // Set the default macro directory
    //
    std::vector<std::string> macrodirs{"Macro"};
    fs::path macro = findPath(_userAppData, customData, macrodirs, true);
    _userMacro = macro;
    mConfig["UserMacroPath"] = Base::FileInfo::pathToString(macro) + PATHSEP;
}

bool ApplicationDirectories::startSafeMode(std::map<std::string,std::string>& mConfig)
{
    SafeMode::StartSafeMode();
    if (SafeMode::SafeModeEnabled()) {
        _userAppData = mConfig["UserAppData"];
        _userConfig = mConfig["UserConfigPath"];
        _userCache = mConfig["UserCachePath"];
        _temp = mConfig["AppTempPath"];
        _userMacro = mConfig["UserMacroPath"];
        _userHome = mConfig["UserHomePath"];
        _usingCustomDirectories = true;
        return true;
    }
    return false;
}

std::filesystem::path ApplicationDirectories::sanitizePath(const std::string& pathAsString)
{
    size_t positionOfFirstNull = pathAsString.find('\0');
    if (positionOfFirstNull != std::string::npos) {
        return {pathAsString.substr(0, positionOfFirstNull)};
    }
    return {pathAsString};
}

void ApplicationDirectories::configureResourceDirectory(const std::map<std::string,std::string>& mConfig) {
#ifdef RESOURCEDIR
    // #6892: Conda may inject null characters
    fs::path path = sanitizePath(RESOURCEDIR);
    if (path.is_absolute()) {
        _resource = path;
    } else {
        _resource = Base::FileInfo::stringToPath(mConfig.at("AppHomePath")) / path;
    }
#else
    _resource = fs::path(mConfig.at("AppHomePath"));
#endif
}

void ApplicationDirectories::configureLibraryDirectory(const std::map<std::string,std::string>& mConfig) {
#ifdef LIBRARYDIR
    // #6892: Conda may inject null characters
    fs::path path = sanitizePath(LIBRARYDIR);
    if (path.is_absolute()) {
        _library = path;
    } else {
        _library = Base::FileInfo::stringToPath(mConfig.at("AppHomePath")) / path;
    }
#else
    _library = Base::FileInfo::stringToPath(mConfig.at("AppHomePath")) / "lib";
#endif
}


void ApplicationDirectories::configureHelpDirectory(const std::map<std::string,std::string>& mConfig)
{
#ifdef DOCDIR
    // #6892: Conda may inject null characters
    fs::path path = sanitizePath(DOCDIR);
    if (path.is_absolute()) {
        _help = path;
    } else {
        _help = Base::FileInfo::stringToPath(mConfig.at("AppHomePath")) / path;
    }
#else
    _help = Base::FileInfo::stringToPath(mConfig.at("DocPath"));
#endif
}


fs::path ApplicationDirectories::getUserHome()
{
    fs::path path;
#if defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_BSD) || defined(FC_OS_MACOSX)
    // Default paths for the user-specific stuff
    struct passwd pwd {};
    struct passwd *result {};
    constexpr std::size_t bufferLength = 16384;
    std::vector<char> buffer(bufferLength);
    const int error = getpwuid_r(getuid(), &pwd, buffer.data(), buffer.size(), &result);
    if (!result || error != 0) {
        throw Base::RuntimeError("Getting HOME path from system failed!");
    }
    std::string sanitizedPath = sanitizePath(pwd.pw_dir);
    path = Base::FileInfo::stringToPath(sanitizedPath);
#else
    path = Base::FileInfo::stringToPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString());
#endif
    return path;
}

bool ApplicationDirectories::usingCustomDirectories() const
{
    return _usingCustomDirectories;
}

#if defined(FC_OS_WIN32)  // This is ONLY used on Windows now, so don't even compile it elsewhere
#include <codecvt>
#include "ShlObj.h"
QString ApplicationDirectories::getOldGenericDataLocation()
{
    WCHAR szPath[MAX_PATH];
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, szPath))) {
        return QString::fromStdString(converter.to_bytes(szPath));
    }
    return {};
}
#endif

void ApplicationDirectories::getSubDirectories(const std::map<std::string,std::string>& mConfig,
                                               std::vector<std::string>& appData)
{
    // If 'AppDataSkipVendor' is defined, the value of 'ExeVendor' must not be part of
    // the path.
    if (!mConfig.contains("AppDataSkipVendor") && mConfig.contains("ExeVendor")) {
        appData.push_back(mConfig.at("ExeVendor"));
    }
    appData.push_back(mConfig.at("ExeName"));
}

void ApplicationDirectories::getOldDataLocation(const std::map<std::string,std::string>& mConfig,
                                                std::vector<std::string>& appData)
{
    // The name of the directory where the parameters are stored should be the name of
    // the application (for branding reasons).
#if defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_BSD)
    // If 'AppDataSkipVendor' is defined, the value of 'ExeVendor' must not be part of
    // the path.
    if (!mConfig.contains("AppDataSkipVendor")) {
        appData.push_back(std::string(".") + mConfig.at("ExeVendor"));
        appData.push_back(mConfig.at("ExeName"));
    } else {
        appData.push_back(std::string(".") + mConfig.at("ExeName"));
    }

#elif defined(FC_OS_MACOSX) || defined(FC_OS_WIN32)
    getSubDirectories(mConfig, appData);
#endif
}

fs::path ApplicationDirectories::findUserHomePath(const fs::path& userHome)
{
    return userHome.empty() ? getUserHome() : userHome;
}

std::tuple<fs::path, fs::path, fs::path> ApplicationDirectories::getCustomPaths()
{
    const QProcessEnvironment env(QProcessEnvironment::systemEnvironment());
    QString userHome = env.value(QStringLiteral("FREECAD_USER_HOME"));
    QString userData = env.value(QStringLiteral("FREECAD_USER_DATA"));
    QString userTemp = env.value(QStringLiteral("FREECAD_USER_TEMP"));

    auto toNativePath = [](QString& path) {
        if (!path.isEmpty()) {
            if (const QDir dir(path); dir.exists()) {
                path = QDir::toNativeSeparators(dir.canonicalPath());
            }
            else {
                path.clear();
            }
        }
    };

    // verify env. variables
    toNativePath(userHome);
    toNativePath(userData);
    toNativePath(userTemp);

    // if FREECAD_USER_HOME is set but not FREECAD_USER_DATA
    if (!userHome.isEmpty() && userData.isEmpty()) {
        userData = userHome;
    }

    // if FREECAD_USER_HOME is set but not FREECAD_USER_TEMP
    if (!userHome.isEmpty() && userTemp.isEmpty()) {
        const QDir dir(userHome);
        dir.mkdir(QStringLiteral("temp"));
        const QFileInfo fi(dir, QStringLiteral("temp"));
        userTemp = fi.absoluteFilePath();
    }

    return {qstringToPath(userHome),
            qstringToPath(userData),
            qstringToPath(userTemp)};
}

std::tuple<fs::path, fs::path, fs::path, fs::path> ApplicationDirectories::getStandardPaths()
{
    QString configHome = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
    QString dataHome = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
    QString cacheHome = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
    QString tempPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation);

    // Keep the old behaviour
#if defined(FC_OS_WIN32)
    configHome = getOldGenericDataLocation();
    dataHome = configHome;

    // On systems with non-7-bit-ASCII application data directories,
    // GetTempPathW will return a path in DOS format. This path will be
    // accepted by boost's file_lock class.
    // Since boost 1.76, there is now a version that accepts a wide string.
#if (BOOST_VERSION < 107600)
    tempPath = QString::fromStdString(Base::FileInfo::getTempPath());
    cacheHome = tempPath;
#endif
#endif

    return {qstringToPath(configHome),
            qstringToPath(dataHome),
            qstringToPath(cacheHome),
            qstringToPath(tempPath)};
}



std::string ApplicationDirectories::versionStringForPath(int major, int minor)
{
    // NOTE: This is intended to be stable over time, so if the format changes, a condition should be added to check for
    // older versions and return this format for them, even if the new format differs.
    return fmt::format("v{}-{}", major, minor);
}

bool ApplicationDirectories::isVersionedPath(const fs::path &startingPath) const {
    for (int major = std::get<0>(_currentVersion); major >= 1; --major) {
        constexpr int largestPossibleMinor = 99;  // We have to start someplace
        int startingMinor = largestPossibleMinor;
        if (major == std::get<0>(_currentVersion)) {
            startingMinor = std::get<1>(_currentVersion);
        }
        for (int minor = startingMinor; minor >= 0; --minor) {
            if (startingPath.filename() == versionStringForPath(major, minor)) {
                return true;
            }
        }
    }
    return false;
}

std::string ApplicationDirectories::mostRecentAvailableConfigVersion(const fs::path &startingPath) const {
    for (int major = std::get<0>(_currentVersion); major >= 1; --major) {
        constexpr int largestPossibleMinor = 99;  // We have to start someplace
        int startingMinor = largestPossibleMinor;
        if (major == std::get<0>(_currentVersion)) {
            startingMinor = std::get<1>(_currentVersion);
        }
        for (int minor = startingMinor; minor >= 0; --minor) {
            auto version = startingPath / versionStringForPath(major, minor);
            if (fs::is_directory(version)) {
                return versionStringForPath(major, minor);
            }
        }
    }
    return "";
}

fs::path ApplicationDirectories::mostRecentConfigFromBase(const fs::path &startingPath) const {
    // Starting in FreeCAD v1.1, we switched to using a versioned config path for the three configuration
    // directories:
    // UserAppData
    // UserConfigPath
    // UserMacroPath
    //
    // Migration to the versioned structured is NOT automatic: at the App level, we just find the most
    // recent directory and use it, regardless of which version of the program is currently running.
    // It is up to user-facing code in Gui to determine whether to ask a user if they want to migrate
    // and to call the App-level functions that do that work.

    // The simplest and most common case is if the current version subfolder already exists
    auto current = startingPath / versionStringForPath(std::get<0>(_currentVersion), std::get<1>(_currentVersion));
    if (fs::is_directory(current)) {
        return current;
    }

    // If the current version doesn't exist, see if a previous version does
    std::string bestVersion = mostRecentAvailableConfigVersion(startingPath);
    if (!bestVersion.empty()) {
        return startingPath / bestVersion;
    }
    return startingPath;  // No versioned config found
}

bool ApplicationDirectories::usingCurrentVersionConfig(fs::path config) const {
    if (config.filename().empty()) {
        config = config.parent_path();
    }
    auto version = Base::FileInfo::pathToString(config.filename());
    return version == versionStringForPath(std::get<0>(_currentVersion), std::get<1>(_currentVersion));
}

void ApplicationDirectories::migrateConfig(const fs::path &oldPath, const fs::path &newPath)
{
    fs::create_directories(newPath);
    for (auto& file : fs::directory_iterator(oldPath)) {
        if (file == newPath) {
            // Handle the case where newPath is a subdirectory of oldPath
            continue;
        }
        fs::copy(file.path(),
                 newPath / file.path().filename(),
                 fs::copy_options::recursive | fs::copy_options::copy_symlinks);
    }
}

void ApplicationDirectories::migrateAllPaths(const std::vector<fs::path> &paths) const {
    auto [major, minor] = _currentVersion;
    std::set<fs::path> uniquePaths (paths.begin(), paths.end());
    for (auto path : uniquePaths) {
        if (path.filename().empty()) {
            // Handle the case where the path was constructed from a std::string with a trailing /
            path = path.parent_path();
        }
        fs::path newPath;
        if (isVersionedPath(path)) {
            newPath = path.parent_path() / versionStringForPath(major, minor);
        } else {
            newPath = path / versionStringForPath(major, minor);
        }
        Base::Console().message("Migrating config from %s to %s\n", Base::FileInfo::pathToString(path), Base::FileInfo::pathToString(newPath));
        if (fs::exists(newPath)) {
            continue;  // Ignore an existing path: not an error, just a migration that was already done
        }
        fs::create_directories(newPath);
        migrateConfig(path, newPath);
    }
}

// TODO: Consider using this for all UNIX-like OSes
#if defined(__OpenBSD__)
#include <cstdio>
#include <cstdlib>
#include <sys/param.h>
#include <QCoreApplication>

fs::path ApplicationDirectories::findHomePath(const char* sCall)
{
    // We have three ways to start this application either use one of the two executables or
    // import the FreeCAD.so module from a running Python session. In the latter case the
    // Python interpreter is already initialized.
    std::string absPath;
    std::string homePath;
    if (Py_IsInitialized()) {
        // Note: `realpath` is known to cause a buffer overflow because it
        // expands the given path to an absolute path of unknown length.
        // Even setting PATH_MAX does not necessarily solve the problem
        // for sure, but the risk of overflow is rather small.
        char resolved[PATH_MAX];
        char* path = realpath(sCall, resolved);
        if (path)
            absPath = path;
    }
    else {
        int argc = 1;
        QCoreApplication app(argc, (char**)(&sCall));
        absPath = QCoreApplication::applicationFilePath().toStdString();
    }

    // should be an absolute path now
    std::string::size_type pos = absPath.find_last_of("/");
    homePath.assign(absPath,0,pos);
    pos = homePath.find_last_of("/");
    homePath.assign(homePath,0,pos+1);

    return Base::FileInfo::stringToPath(homePath);
}

#elif defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_BSD)
#include <cstdio>
#include <cstdlib>
#include <sys/param.h>
#if defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif

fs::path ApplicationDirectories::findHomePath(const char* sCall)
{
    // We have three ways to start this application either use one of the two executables or
    // import the FreeCAD.so module from a running Python session. In the latter case the
    // Python interpreter is already initialized.
    std::string absPath;
    std::string homePath;
    if (Py_IsInitialized()) {
        // Note: `realpath` is known to cause a buffer overflow because it
        // expands the given path to an absolute path of unknown length.
        // Even setting PATH_MAX does not necessarily solve the problem
        // for sure, but the risk of overflow is rather small.
        char resolved[PATH_MAX];
        char* path = realpath(sCall, resolved);
        if (path)
            absPath = path;
    }
    else {
        // Find the path of the executable. Theoretically, there could occur a
        // race condition when using readlink, but we only use this method to
        // get the absolute path of the executable to compute the actual home
        // path. In the worst case we simply get q wrong path, and FreeCAD is not
        // able to load its modules.
        char resolved[PATH_MAX];
#if defined(FC_OS_BSD)
        int mib[4];
        mib[0] = CTL_KERN;
        mib[1] = KERN_PROC;
        mib[2] = KERN_PROC_PATHNAME;
        mib[3] = -1;
        size_t cb = sizeof(resolved);
        sysctl(mib, 4, resolved, &cb, NULL, 0);
        int nchars = strlen(resolved);
#else
        int nchars = readlink("/proc/self/exe", resolved, PATH_MAX);
#endif
        if (nchars < 0 || nchars >= PATH_MAX)
            throw Base::FileSystemError("Cannot determine the absolute path of the executable");
        resolved[nchars] = '\0'; // enforce null termination
        absPath = resolved;
    }

    // should be an absolute path now
    std::string::size_type pos = absPath.find_last_of("/");
    homePath.assign(absPath,0,pos);
    pos = homePath.find_last_of("/");
    homePath.assign(homePath,0,pos+1);

    return Base::FileInfo::stringToPath(homePath);
}

#elif defined(FC_OS_MACOSX)
#include <mach-o/dyld.h>
#include <string>
#include <cstdlib>
#include <sys/param.h>

fs::path ApplicationDirectories::findHomePath(const char* sCall)
{
    // If Python is initialized at this point, then we're being run from
    // MainPy.cpp, which hopefully rewrote argv[0] to point at the
    // FreeCAD shared library.
    if (!Py_IsInitialized()) {
        uint32_t sz = 0;

        // function only returns "sz" if the first arg is too small to hold value
        _NSGetExecutablePath(nullptr, &sz);

        if (const auto buf = new char[++sz]; _NSGetExecutablePath(buf, &sz) == 0) {
            std::array<char, PATH_MAX> resolved{};
            const char* path = realpath(buf, resolved.data());
            delete [] buf;

            if (path) {
                const std::string Call(resolved.data());
                std::string TempHomePath;
                std::string::size_type pos = Call.find_last_of(fs::path::preferred_separator);
                TempHomePath.assign(Call,0,pos);
                pos = TempHomePath.find_last_of(fs::path::preferred_separator);
                TempHomePath.assign(TempHomePath,0,pos+1);
                return Base::FileInfo::stringToPath(TempHomePath);
            }
        } else {
            delete [] buf;
        }
    }

    return Base::FileInfo::stringToPath(sCall);
}

#elif defined (FC_OS_WIN32)
fs::path ApplicationDirectories::findHomePath(const char* sCall)
{
    // We have several ways to start this application:
    // * use one of the two executables
    // * import the FreeCAD.pyd module from a running Python session. In this case the
    //   Python interpreter is already initialized.
    // * use a custom dll that links FreeCAD core dlls and that is loaded by its host application
    //   In this case the calling name should be set to FreeCADBase.dll or FreeCADApp.dll in order
    //   to locate the correct home directory
    wchar_t szFileName [MAX_PATH];
    QString dll(QString::fromUtf8(sCall));
    if (Py_IsInitialized() || dll.endsWith(QLatin1String(".dll"))) {
        GetModuleFileNameW(GetModuleHandleA(sCall),szFileName, MAX_PATH-1);
    }
    else {
        GetModuleFileNameW(0, szFileName, MAX_PATH-1);
    }

    std::wstring Call(szFileName), homePath;
    std::wstring::size_type pos = Call.find_last_of(fs::path::preferred_separator);
    homePath.assign(Call,0,pos);
    pos = homePath.find_last_of(fs::path::preferred_separator);
    homePath.assign(homePath,0,pos+1);

    // fixes #0001638 to avoid loading DLLs from Windows' system directories before FreeCAD's bin folder
    std::wstring binPath = homePath;
    binPath += L"bin";
    SetDllDirectoryW(binPath.c_str());

    // https://stackoverflow.com/questions/5625884/conversion-of-stdwstring-to-qstring-throws-linker-error
#ifdef _MSC_VER
    QString str = QString::fromUtf16(reinterpret_cast<const ushort *>(homePath.c_str()));
#else
    QString str = QString::fromStdWString(homePath);
#endif
    return qstringToPath(str);
}

#else
# error "std::string ApplicationDirectories::findHomePath(const char*) not implemented"
#endif

std::tuple<int, int> ApplicationDirectories::extractVersionFromConfigMap(const std::map<std::string,std::string> &config)
{
    try {
        int major = std::stoi(config.at("BuildVersionMajor"));
        int minor = std::stoi(config.at("BuildVersionMinor"));
        return std::make_tuple(major, minor);
    } catch (const std::exception& e) {
        throw Base::RuntimeError("Failed to parse version from config: " + std::string(e.what()));
    }
}