VirtualBox

Ignore:
Timestamp:
Feb 4, 2021 12:47:38 AM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
142635
Message:

NAT/Net: Factor out finding the home folder. It's used as the base
directory for the default release log file and for the TFTP root
directory. Don do it twice using two different methods. bugref:9929.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NAT/VBoxNetLwipNAT.cpp

    r87599 r87601  
    131131    friend class NATNetworkListener;
    132132
     133    /** Home folder location; used as default directory for several paths. */
     134    com::Utf8Str m_strHome;
     135
    133136    struct proxy_options m_ProxyOptions;
    134137    struct sockaddr_in m_src4;
     
    172175private:
    173176    int comInit();
     177    int homeInit();
    174178    int logInit();
    175179
     
    328332        return rc;
    329333
     334    /* Get the home folder location.  It's ok if it fails. */
     335    homeInit();
     336
    330337    /*
    331338     * We get the network name on the command line.  Get hold of its
     
    474481    }
    475482
    476     com::Bstr bstr;
    477     hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
    478     AssertComRCReturn(hrc, VERR_NOT_FOUND);
    479     if (!bstr.isEmpty())
    480     {
    481         com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
    482                                      bstr.raw(), RTPATH_DELIMITER, "TFTP"));
     483
     484    if (m_strHome.isNotEmpty())
     485    {
     486        com::Utf8Str strTftpRoot(com::Utf8StrFmt("%s%c%s",
     487                                     m_strHome.c_str(), RTPATH_DELIMITER, "TFTP"));
    483488        char *pszStrTemp;       // avoid const char ** vs char **
    484489        rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
     
    556561
    557562    return VINF_SUCCESS;
     563}
     564
     565
     566/**
     567 * Get the VirtualBox home folder.
     568 *
     569 * It is used as the base directory for the default release log file
     570 * and for the TFTP root location.
     571 */
     572int VBoxNetLwipNAT::homeInit()
     573{
     574    HRESULT hrc;
     575    int rc;
     576
     577    com::Bstr bstrHome;
     578    hrc = virtualbox->COMGETTER(HomeFolder)(bstrHome.asOutParam());
     579    if (SUCCEEDED(hrc))
     580    {
     581        m_strHome = bstrHome;
     582        return VINF_SUCCESS;
     583    }
     584
     585    /*
     586     * In the unlikely event that we have failed to retrieve
     587     * HomeFolder via the API, try the fallback method.  Note that
     588     * despite "com" namespace it does not use COM.
     589     */
     590    char szHome[RTPATH_MAX] = "";
     591    rc = com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome), false);
     592    if (RT_SUCCESS(rc))
     593    {
     594        m_strHome = szHome;
     595        return VINF_SUCCESS;
     596    }
     597
     598    return rc;
    558599}
    559600
     
    15261567    int rc;
    15271568
    1528     /*
    1529      * NB: Contrary to what the "com" namespace might suggest, both
    1530      * this call, and the call below to create the release logger are
    1531      * NOT actually COM related in any way and can be used before COM
    1532      * is initialized.
    1533      */
    1534     char szHome[RTPATH_MAX];
    1535     rc = com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome), false);
    1536     if (RT_FAILURE(rc))
    1537         return rc;
    1538 
    15391569    const std::string &strNetworkName = getNetworkName();
    15401570    if (strNetworkName.empty())
     
    15531583    }
    15541584
     1585    const char *pcszLogFile = NULL;
    15551586    char szLogFile[RTPATH_MAX];
    1556     cch = RTStrPrintf(szLogFile, sizeof(szLogFile),
    1557                       "%s%c%s.log", szHome, RTPATH_DELIMITER, szNetwork);
    1558     if (cch >= sizeof(szLogFile))
    1559     {
    1560         return VERR_BUFFER_OVERFLOW;
     1587    if (m_strHome.isNotEmpty())
     1588    {
     1589        cch = RTStrPrintf(szLogFile, sizeof(szLogFile),
     1590                          "%s%c%s.log", m_strHome.c_str(), RTPATH_DELIMITER, szNetwork);
     1591        if (cch < sizeof(szLogFile))
     1592            pcszLogFile = szLogFile;
    15611593    }
    15621594
     
    15741606
    15751607    char szEnvVarBase[128];
     1608    const char *pcszEnvVarBase = szEnvVarBase;
    15761609    cch = RTStrPrintf(szEnvVarBase, sizeof(szEnvVarBase),
    15771610                      "VBOXNET_%s_RELEASE_LOG", szNetwork);
    15781611    if (cch >= sizeof(szEnvVarBase))
    1579         return VERR_BUFFER_OVERFLOW;
     1612        pcszEnvVarBase = NULL;
    15801613
    15811614    rc = com::VBoxLogRelCreate("NAT Network",
    1582                                szLogFile,
     1615                               pcszLogFile,
    15831616                               RTLOGFLAGS_PREFIX_TIME_PROG,
    15841617                               "all all.restrict -default.restrict",
    1585                                szEnvVarBase,
     1618                               pcszEnvVarBase,
    15861619                               RTLOGDEST_FILE,
    15871620                               32768 /* cMaxEntriesPerGroup */,
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette