Example #1
0
        public void LoadConfigFile(string configFile)
        {
            Log.Trace(@"Loading config file {0}", configFile);

            ConfigErrors.Clear();

            ConfigWarnings.Clear();

            ConfigPath = configFile;

            if (Read())
            {
                DoValidation();
            }
            else
            {
                ConfigErrors.Add($"Failed to read the configuration file {configFile}");
            }
        }
Example #2
0
        /// <summary>
        /// Reads the config file
        /// </summary>
        /// <returns>an empty string if no exception occurs</returns>
        public bool Read()
        {
            Log.Trace(@"ConfigFileHelper.Read() ...");

            try
            {
                if (!ConfigFileExists)
                {
                    return(false);
                }

                bool isConfigRead = true;

                ParityFile1 = string.Empty;
                ParityFile2 = string.Empty;
                ZParityFile = string.Empty;
                ParityFile3 = string.Empty;
                ParityFile4 = string.Empty;
                ParityFile5 = string.Empty;
                ParityFile6 = string.Empty;

                ConfigErrors.Clear();

                ConfigWarnings.Clear();

                ContentFiles.Clear();

                SnapShotSources.Clear();

                ExcludePatterns.Clear();

                IncludePatterns.Clear();

                BlockSizeKB = Constants.DefaultBlockSize;

                AutoSaveGB = Constants.DefaultAutoSave;

                foreach (string line in File.ReadLines(ConfigPath))
                {
                    string lineStart = line.Trim();

                    if (string.IsNullOrWhiteSpace(lineStart) || lineStart.StartsWith(@"#"))
                    {
                        continue;
                    }

                    // Not a comment so process the line

                    // split the line by the first space encountered
                    string[] configItem = lineStart.Split(new[] { ' ' }, 2);
                    Log.Trace(@"configItem [{0}]", string.Join(" ", configItem));
                    string configItemName = configItem[0] ?? string.Empty;
                    Log.Trace(@"configItemName [{0}]", configItemName);
                    string configItemValue = (configItem.Length > 1) ? configItem[1] : string.Empty;
                    Log.Trace(@"configItemValue [{0}]", configItemValue);

                    // ignore the line if it is not an a recognized setting
                    if (!validConfigNames.Contains(configItemName))
                    {
                        continue;
                    }
                    Log.Trace(@"configItemName found in validConfigNames");

                    switch (configItemName)
                    {
                    case @"parity":
                        ParityFile1 = configItemValue;
                        break;

                    case @"q-parity":
                        Log.Warn(@"'q-parity' entry in config file will be changed to '2-parity' when config is saved");
                        ParityFile2 = configItemValue;
                        break;

                    case @"2-parity":
                        // handle legacy 'q-parity' entry by giving it priority over any '2-parity' entry; saving config will rename 'q-parity' to '2-parity' and leave the path unchanged
                        if (string.IsNullOrEmpty(ParityFile2))
                        {
                            ParityFile2 = configItemValue;
                        }
                        break;

                    case @"z-parity":
                        // #WARNING! Your CPU doesn't have a fast implementation for triple parity.
                        // #WARNING! It's recommended to switch to 'z-parity' instead than '3-parity'.
                        ZParityFile = configItemValue;
                        ParityFile3 = string.Empty;
                        break;

                    case @"3-parity":
                        if (string.IsNullOrWhiteSpace(ZParityFile))
                        {
                            ParityFile3 = configItemValue;
                        }
                        break;

                    case @"4-parity":
                        ParityFile4 = configItemValue;
                        break;

                    case @"5-parity":
                        ParityFile5 = configItemValue;
                        break;

                    case @"6-parity":
                        ParityFile6 = configItemValue;
                        break;

                    case @"content":
                        ContentFiles.Add(configItemValue);
                        break;

                    case @"disk":
                    case @"data":     // Handle older configs
                    {
                        // get the data name, d1,d2,d3 etc
                        string diskName = configItemValue.Split(' ')[0];

                        // get the path
                        int diskSplitIndex = configItemValue.IndexOf(' ');

                        string diskPath = configItemValue.Substring(diskSplitIndex + 1);

                        // special handling of data sources since order preservation is extremely important
                        if (!string.IsNullOrEmpty(diskName) && !string.IsNullOrEmpty(diskPath))
                        {
                            SnapShotSources.Add(new SnapShotSource {
                                    Name = diskName, DirSource = diskPath
                                });
                        }
                    }
                    break;

                    case @"exclude":
                        ExcludePatterns.Add(configItemValue);
                        break;

                    case @"include":
                        IncludePatterns.Add(configItemValue);
                        break;

                    case @"block_size":
                        BlockSizeKB = uint.Parse(configItemValue);
                        if (BlockSizeKB < Constants.MinBlockSize || BlockSizeKB > Constants.MaxBlockSize)
                        {
                            isConfigRead = false;
                        }
                        break;

                    case @"autosave":
                        AutoSaveGB = uint.Parse(configItemValue);
                        // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                        if (AutoSaveGB < Constants.MinAutoSave || AutoSaveGB > Constants.MaxAutoSave)
                        {
                            isConfigRead = false;
                        }
                        break;

                    case @"nohidden":
                        Nohidden = true;
                        break;
                    }
                }

                return(isConfigRead);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return(false);
            }
        }