Changeset 1777


Ignore:
Timestamp:
26/07/2007 23:11:03 (5 years ago)
Author:
chris
Message:

Make Configuration take a std::string filename instead of a char array,
in C++ style.

Add a function to get default config file paths at runtime, dependent on
the location of the executable being run.

Pass the config file name directly to Daemon::Main, instead of faking argv.

No default raid file path at compile time on Windows, depends on
executable location when run.

Determine RaidFile? path at runtime if not supplied in config file
on Windows.

Don't define default locations for config files at compile time on Windows,
provide macros to determine them at runtime instead.

Make FileHandleGuard? take a std::string instead of a char array, C++ style.

Determine config file location at runtime instead of hard-coding on
Windows. Thanks to Paul MacKenzie?, Per Thomsen, Pete Jalajas, Stuart
Sanders, Dave Bamford and Gary for pushing me to do this. (fixes #12)

Determine config file path at runtime. Call Daemon::Main with config file
name instead of building fake argv.

(refs #3, merges [1684] [1685] [1686] [1687] [1688] [1689] [1690]
[1691] [1692])

Location:
box/chris/merge
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • box/chris/merge/bin/bbackupctl/bbackupctl.cpp

    r1349 r1777  
    6767 
    6868        // Filename for configuration file? 
    69         const char *configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG; 
     69        std::string configFilename; 
     70 
     71        #ifdef WIN32 
     72                configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE; 
     73        #else 
     74                configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG; 
     75        #endif 
    7076         
    7177        // Quiet? 
     
    104110 
    105111        // Read in the configuration file 
    106         if(!quiet) printf("Using configuration file %s\n", configFilename); 
     112        if(!quiet) printf("Using configuration file %s\n",  
     113                configFilename.c_str()); 
     114 
    107115        std::string errs; 
    108         std::auto_ptr<Configuration> config(Configuration::LoadAndVerify(configFilename, &BackupDaemonConfigVerify, errs)); 
     116        std::auto_ptr<Configuration> config( 
     117                Configuration::LoadAndVerify 
     118                        (configFilename, &BackupDaemonConfigVerify, errs)); 
     119 
    109120        if(config.get() == 0 || !errs.empty()) 
    110121        { 
  • box/chris/merge/bin/bbackupd/Win32BackupService.cpp

    r1031 r1777  
    3030DWORD Win32BackupService::WinService(const char* pConfigFileName) 
    3131{ 
    32         char exepath[MAX_PATH]; 
    33         GetModuleFileName(NULL, exepath, sizeof(exepath)); 
     32        DWORD ret; 
    3433 
    35         std::string configfile; 
    36          
     34        // keep MAINHELPER_START happy 
     35        int argc = 0; 
     36        char* argv[] = {NULL}; 
     37 
     38        MAINHELPER_START 
     39 
    3740        if (pConfigFileName != NULL) 
    3841        { 
    39                 configfile = pConfigFileName; 
     42                ret = this->Main(pConfigFileName); 
    4043        } 
    4144        else 
    4245        { 
    43                 // make the default config file name, 
    44                 // based on the program path 
    45                 configfile = exepath; 
    46                 configfile = configfile.substr(0, 
    47                         configfile.rfind(DIRECTORY_SEPARATOR_ASCHAR)); 
    48                 configfile += DIRECTORY_SEPARATOR "bbackupd.conf"; 
     46                ret = this->Main(BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE); 
    4947        } 
    5048 
    51         const char *argv[] = {exepath, "-c", configfile.c_str()}; 
    52         int argc = sizeof(argv) / sizeof(*argv); 
    53         DWORD ret; 
    54  
    55         MAINHELPER_START 
    56         ret = this->Main(BOX_FILE_BBACKUPD_DEFAULT_CONFIG, argc, argv); 
    5749        MAINHELPER_END 
    5850 
  • box/chris/merge/bin/bbackupd/bbackupd.cpp

    r1254 r1777  
    9090        { 
    9191                ExitCode = gpDaemonService->Main( 
    92                         BOX_FILE_BBACKUPD_DEFAULT_CONFIG, argc, argv); 
     92                        BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE, argc, argv); 
    9393        } 
    9494 
  • box/chris/merge/bin/bbackupquery/bbackupquery.cpp

    r1602 r1777  
    6767        int returnCode = 0; 
    6868 
    69         MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbackupquery.memleaks", "bbackupquery") 
     69        MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbackupquery.memleaks", 
     70                "bbackupquery") 
    7071        MAINHELPER_START 
    7172 
     
    7879        if (WSAStartup(0x0101, &info) == SOCKET_ERROR)  
    7980        { 
    80                 // throw error?    perhaps give it its own id in the furture 
     81                // throw error? perhaps give it its own id in the future 
    8182                THROW_EXCEPTION(BackupStoreException, Internal) 
    8283        } 
     
    9192 
    9293        // Filename for configuration file? 
    93         const char *configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG; 
     94        std::string configFilename; 
     95 
     96        #ifdef WIN32 
     97                configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE; 
     98        #else 
     99                configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG; 
     100        #endif 
    94101         
    95102        // Flags 
     
    216223 
    217224        // Read in the configuration file 
    218         if(!quiet) printf("Using configuration file %s\n", configFilename); 
     225        if(!quiet) printf("Using configuration file %s\n",  
     226                configFilename.c_str()); 
     227 
    219228        std::string errs; 
    220         std::auto_ptr<Configuration> config(Configuration::LoadAndVerify(configFilename, &BackupDaemonConfigVerify, errs)); 
     229        std::auto_ptr<Configuration> config( 
     230                Configuration::LoadAndVerify 
     231                        (configFilename, &BackupDaemonConfigVerify, errs)); 
     232 
    221233        if(config.get() == 0 || !errs.empty()) 
    222234        { 
  • box/chris/merge/bin/bbstoreaccounts/bbstoreaccounts.cpp

    r217 r1777  
    403403int main(int argc, const char *argv[]) 
    404404{ 
    405         MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbstoreaccounts.memleaks", "bbstoreaccounts") 
     405        MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbstoreaccounts.memleaks", 
     406                "bbstoreaccounts") 
    406407 
    407408        MAINHELPER_START 
    408409 
    409         // Filename for configuraiton file? 
    410         const char *configFilename = BOX_FILE_BBSTORED_DEFAULT_CONFIG; 
     410        // Filename for configuration file? 
     411        std::string configFilename; 
     412 
     413        #ifdef WIN32 
     414                configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE; 
     415        #else 
     416                configFilename = BOX_FILE_BBSTORED_DEFAULT_CONFIG; 
     417        #endif 
    411418         
    412419        // See if there's another entry on the command line 
     
    432439        // Read in the configuration file 
    433440        std::string errs; 
    434         std::auto_ptr<Configuration> config(Configuration::LoadAndVerify(configFilename, &BackupConfigFileVerify, errs)); 
     441        std::auto_ptr<Configuration> config( 
     442                Configuration::LoadAndVerify 
     443                        (configFilename, &BackupConfigFileVerify, errs)); 
     444 
    435445        if(config.get() == 0 || !errs.empty()) 
    436446        { 
  • box/chris/merge/bin/bbstored/BackupStoreDaemon.cpp

    r1476 r1777  
    133133        // Initialise the raid files controller 
    134134        RaidFileController &rcontroller = RaidFileController::GetController(); 
    135         rcontroller.Initialise(config.GetKeyValue("RaidFileConf").c_str()); 
     135 
     136        std::string raidFileConfig; 
     137 
     138        #ifdef WIN32 
     139                if (!config.KeyExists("RaidFileConf")) 
     140                { 
     141                        raidFileConfig = BOX_GET_DEFAULT_RAIDFILE_CONFIG_FILE; 
     142                } 
     143                else 
     144                { 
     145                        raidFileConfig = config.GetKeyValue("RaidFileConf"); 
     146                } 
     147        #else 
     148                raidFileConfig = config.GetKeyValue("RaidFileConf"); 
     149        #endif 
     150 
     151        rcontroller.Initialise(raidFileConfig); 
    136152         
    137153        // Load the account database 
  • box/chris/merge/bin/bbstored/bbstored.cpp

    r1488 r1777  
    2424 
    2525        BackupStoreDaemon daemon; 
    26         return daemon.Main(BOX_FILE_BBSTORED_DEFAULT_CONFIG, argc, argv); 
     26 
     27        #ifdef WIN32 
     28                return daemon.Main(BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE, 
     29                        argc, argv); 
     30        #else 
     31                return daemon.Main(BOX_FILE_BBSTORED_DEFAULT_CONFIG, 
     32                        argc, argv); 
     33        #endif 
    2734         
    2835        MAINHELPER_END 
  • box/chris/merge/lib/backupstore/BackupStoreConfigVerify.cpp

    r217 r1777  
    3636        {"TimeBetweenHousekeeping",     0, ConfigTest_Exists | ConfigTest_IsInt, 0}, 
    3737        {"ExtendedLogging",     "no", ConfigTest_IsBool, 0},                    // make value "yes" to enable in config file 
    38         {"RaidFileConf",        BOX_FILE_RAIDFILE_DEFAULT_CONFIG, ConfigTest_LastEntry,         0} 
     38 
     39        #ifdef WIN32 
     40                {"RaidFileConf", "", ConfigTest_LastEntry, 0} 
     41        #else 
     42                {"RaidFileConf", BOX_FILE_RAIDFILE_DEFAULT_CONFIG, ConfigTest_LastEntry, 0} 
     43        #endif 
     44 
    3945}; 
    4046 
  • box/chris/merge/lib/common/BoxPortsAndFiles.h

    r217 r1777  
    1515 
    1616// Backup store daemon 
    17 #define BOX_PORT_BBSTORED                                       (BOX_PORT_BASE+1) 
    18 #define BOX_FILE_BBSTORED_DEFAULT_CONFIG        "/etc/box/bbstored.conf" 
     17#define BOX_PORT_BBSTORED                       (BOX_PORT_BASE+1) 
     18 
    1919// directory within the RAIDFILE root for the backup store daemon 
    20 #define BOX_RAIDFILE_ROOT_BBSTORED                      "backup" 
     20#define BOX_RAIDFILE_ROOT_BBSTORED              "backup" 
    2121 
    22 // Backup client daemon 
     22// configuration file paths 
    2323#ifdef WIN32 
    24 #define BOX_FILE_BBACKUPD_DEFAULT_CONFIG        "C:\\Program Files\\Box Backup\\bbackupd.conf" 
     24        // no default config file path, use these macros to call 
     25        // GetDefaultConfigFilePath() instead. 
     26 
     27        #define BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE \ 
     28                GetDefaultConfigFilePath("bbackupd.conf").c_str() 
     29        #define BOX_GET_DEFAULT_RAIDFILE_CONFIG_FILE \ 
     30                GetDefaultConfigFilePath("raidfile.conf").c_str() 
     31        #define BOX_GET_DEFAULT_BBSTORED_CONFIG_FILE \ 
     32                GetDefaultConfigFilePath("bbstored.conf").c_str() 
    2533#else 
    2634#define BOX_FILE_BBACKUPD_DEFAULT_CONFIG        "/etc/box/bbackupd.conf" 
     35#define BOX_FILE_RAIDFILE_DEFAULT_CONFIG        "/etc/box/raidfile.conf" 
     36#define BOX_FILE_BBSTORED_DEFAULT_CONFIG        "/etc/box/bbstored.conf" 
    2737#endif 
    28  
    29 // RaidFile conf location default 
    30 #define BOX_FILE_RAIDFILE_DEFAULT_CONFIG        "/etc/box/raidfile.conf" 
    3138 
    3239// Default name of the named pipe 
  • box/chris/merge/lib/common/Guards.h

    r1437 r1777  
    3333{ 
    3434public: 
    35         FileHandleGuard(const char *filename) 
    36                 : mOSFileHandle(::open(filename, flags, mode)) 
     35        FileHandleGuard(const std::string& rFilename) 
     36                : mOSFileHandle(::open(rFilename.c_str(), flags, mode)) 
    3737        { 
    3838                if(mOSFileHandle < 0) 
    3939                { 
    4040                        BOX_ERROR("FileHandleGuard: failed to open file '" << 
    41                                 filename << "': " << strerror(errno)); 
     41                                rFilename << "': " << strerror(errno)); 
    4242                        THROW_EXCEPTION(CommonException, OSFileOpenError) 
    4343                } 
  • box/chris/merge/lib/win32/emu.cpp

    r1756 r1777  
    217217                return true; 
    218218        } 
     219} 
     220 
     221// forward declaration 
     222char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage); 
     223 
     224// -------------------------------------------------------------------------- 
     225// 
     226// Function 
     227//              Name:    GetDefaultConfigFilePath(std::string name) 
     228//              Purpose: Calculates the default configuration file name, 
     229//                       by using the directory location of the currently 
     230//                       executing program, and appending the provided name. 
     231//                       In case of fire, returns an empty string. 
     232//              Created: 26th May 2007 
     233// 
     234// -------------------------------------------------------------------------- 
     235std::string GetDefaultConfigFilePath(const std::string& rName) 
     236{ 
     237        WCHAR exePathWide[MAX_PATH]; 
     238        GetModuleFileNameW(NULL, exePathWide, MAX_PATH-1); 
     239 
     240        char* exePathUtf8 = ConvertFromWideString(exePathWide, CP_UTF8); 
     241        if (exePathUtf8 == NULL) 
     242        { 
     243                return ""; 
     244        } 
     245 
     246        std::string configfile = exePathUtf8; 
     247        delete [] exePathUtf8; 
     248         
     249        // make the default config file name, 
     250        // based on the program path 
     251        configfile = configfile.substr(0, 
     252                configfile.rfind('\\')); 
     253        configfile += "\\"; 
     254        configfile += rName; 
     255 
     256        return configfile; 
    219257} 
    220258 
     
    12531291        // as the event source 
    12541292 
    1255         char cmd[MAX_PATH]; 
    1256         if (GetModuleFileName(NULL, cmd, sizeof(cmd)-1) == 0) 
     1293        WCHAR cmd[MAX_PATH]; 
     1294        DWORD len = GetModuleFileNameW(NULL, cmd, MAX_PATH); 
     1295 
     1296        if (len == 0) 
    12571297        { 
    12581298                ::syslog(LOG_ERR, "Failed to get the program file name: %s", 
     
    12601300                return FALSE; 
    12611301        } 
    1262         cmd[sizeof(cmd)-1] = 0; 
    1263         std::string exepath(cmd); 
    12641302 
    12651303        // Create the event source as a subkey of the log.  
     
    12831321        // Set the name of the message file.  
    12841322  
    1285         if (RegSetValueEx(hk,                // subkey handle  
    1286                          "EventMessageFile", // value name  
    1287                          0,                  // must be zero  
    1288                          REG_EXPAND_SZ,      // value type  
    1289                          (LPBYTE) exepath.c_str(),  // pointer to value data  
    1290                          (DWORD) (exepath.size()))) // data size 
     1323        if (RegSetValueExW(hk,                 // subkey handle  
     1324                           L"EventMessageFile", // value name  
     1325                           0,                  // must be zero  
     1326                           REG_EXPAND_SZ,      // value type  
     1327                           (LPBYTE)cmd,        // pointer to value data  
     1328                           len*sizeof(WCHAR))) // data size 
    12911329        { 
    12921330                ::syslog(LOG_ERR, "Failed to set the event message file: %s", 
     
    13161354        // Set the category message file and number of categories. 
    13171355 
    1318         if (RegSetValueEx(hk,                        // subkey handle  
    1319                           "CategoryMessageFile",    // value name  
    1320                           0,                         // must be zero  
    1321                           REG_EXPAND_SZ,             // value type  
    1322                           (LPBYTE) exepath.c_str(),  // pointer to value data  
    1323                           (DWORD) (exepath.size()))) // data size 
     1356        if (RegSetValueExW(hk,                    // subkey handle  
     1357                           L"CategoryMessageFile", // value name  
     1358                           0,                     // must be zero  
     1359                           REG_EXPAND_SZ,         // value type  
     1360                           (LPBYTE)cmd,           // pointer to value data  
     1361                           len*sizeof(WCHAR)))    // data size 
    13241362        { 
    13251363                ::syslog(LOG_ERR, "Failed to set the category message file: " 
  • box/chris/merge/lib/win32/emu.h

    r1770 r1777  
    378378bool ConvertConsoleToUtf8(const char* pString, std::string& rDest); 
    379379 
     380// Utility function which returns a default config file name, 
     381// based on the path of the current executable. 
     382std::string GetDefaultConfigFilePath(const std::string& rName); 
     383 
    380384// GetErrorMessage() returns a system error message, like strerror()  
    381385// but for Windows error codes. 
  • box/chris/merge/test/bbackupd/testbbackupd.cpp

    r1757 r1777  
    613613                 
    614614        BackupDaemon daemon; 
    615         const char* fake_argv[] = { "bbackupd", "testfiles/bbackupd.conf" }; 
    616          
    617         int result = daemon.Main(BOX_FILE_BBACKUPD_DEFAULT_CONFIG, 2,  
    618                 fake_argv); 
     615        int result = daemon.Main("testfiles/bbackupd.conf"); 
    619616         
    620617        TEST_THAT(result == 0); 
Note: See TracChangeset for help on using the changeset viewer.