Esempio n. 1
0
        public void Modify(ModifyCallback action)
        {
            Dictionary <string, SshKey> keys = new Dictionary <string, SshKey>();

            File.Copy(this.Filename, this.Filename + ".bak");
            try
            {
                using (StreamReader sr = new StreamReader(this.Filename))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        if (!string.IsNullOrEmpty(line))
                        {
                            if (line.Trim() != string.Empty)
                            {
                                string[] values = line.Split(' ');
                                SshKey   sshKey = new SshKey()
                                {
                                    Type = values[0], Key = values[1], Comment = values[2]
                                };
                                keys[sshKey.Comment] = sshKey;
                            }
                        }
                    }
                }
                Dictionary <string, SshKey> oldKeys = new Dictionary <string, SshKey>(keys);

                action(keys);

                // TODO compare new keys with old keys before overwriting

                using (File.Create(this.Filename)) { }
                using (StreamWriter sw = new StreamWriter(this.Filename))
                {
                    foreach (SshKey value in keys.Values)
                    {
                        sw.WriteLine(string.Format("{0} {1} {2}", value.Type, value.Key, value.Comment));
                    }
                }
            }
            catch (Exception ex)
            {
                File.Copy(this.Filename + ".bak", this.Filename, true);
                throw ex;
            }
            finally
            {
                File.Delete(this.Filename + ".bak");
            }
        }
        public void Modify(ModifyCallback action)
        {
            Dictionary<string, SshKey> keys = new Dictionary<string, SshKey>();
            File.Copy(this.Filename, this.Filename + ".bak");
            try
            {
                using (StreamReader sr = new StreamReader(this.Filename))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        if (!string.IsNullOrEmpty(line))
                        {
                            if (line.Trim() != string.Empty)
                            {
                                string[] values = line.Split(' ');
                                SshKey sshKey = new SshKey() { Type = values[0], Key = values[1], Comment = values[2] };
                                keys[sshKey.Comment] = sshKey;
                            }
                        }
                    }
                }
                Dictionary<string, SshKey> oldKeys = new Dictionary<string, SshKey>(keys);
                
                action(keys);

                // TODO compare new keys with old keys before overwriting

                using (File.Create(this.Filename)) { }
                using (StreamWriter sw = new StreamWriter(this.Filename))
                {
                    foreach (SshKey value in keys.Values)
                    {
                        sw.WriteLine(string.Format("{0} {1} {2}", value.Type, value.Key, value.Comment));
                    }
                }
            }
            catch (Exception ex)
            {
                File.Copy(this.Filename + ".bak", this.Filename, true);
                throw ex;
            }
            finally
            {
                File.Delete(this.Filename + ".bak");
            }
        }