public void CommitWrite() { if (_offline) { throw new IOException("offline"); } _transaction.CommitWrite(); }
public void CommitWrite() { try { _transaction.CommitWrite(); } catch (WebException ex) { _fileStorage.ConvertException(_ioc, ex); throw; } }
private static bool SaveConfigFileEx(AppConfigEx tConfig, string strFilePath, bool bRemoveConfigPref) { tConfig.OnSavePre(); // Temporarily remove user file preference (restore after saving) bool bConfigPref = tConfig.Meta.PreferUserConfiguration; if (bRemoveConfigPref) { tConfig.Meta.PreferUserConfiguration = false; } bool bResult = true; try { Debug.Assert(!string.IsNullOrEmpty(strFilePath)); IOConnectionInfo iocPath = IOConnectionInfo.FromPath(strFilePath); using (FileTransactionEx ft = new FileTransactionEx(iocPath, Program.Config.Application.UseTransactedConfigWrites)) { using (Stream s = ft.OpenWrite()) { using (XmlWriter xw = XmlUtilEx.CreateXmlWriter(s)) { XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx)); xs.Serialize(xw, tConfig); } } ft.CommitWrite(); } } catch (Exception) { Debug.Assert(false); bResult = false; } if (bRemoveConfigPref) { tConfig.Meta.PreferUserConfiguration = bConfigPref; } AssertConfigPref(tConfig); tConfig.OnSavePost(); return(bResult); }
/// <summary> /// Save the currently opened database. The file is written to the location /// it has been opened from. /// </summary> /// <param name="slLogger">Logger that recieves status information.</param> public void Save(IStatusLogger slLogger) { Debug.Assert(ValidateUuidUniqueness()); // bool bMadeUnhidden = UrlUtil.UnhideFile(m_ioSource.Path); // Stream s = IOConnection.OpenWrite(m_ioSource); FileTransactionEx ft = new FileTransactionEx(m_ioSource, m_bUseFileTransactions); Stream s = ft.OpenWrite(); Kdb4File kdb = new Kdb4File(this); kdb.Save(s, null, Kdb4Format.Default, slLogger); ft.CommitWrite(); // if(bMadeUnhidden) UrlUtil.HideFile(m_ioSource.Path, true); // Hide again m_pbHashOfLastIO = kdb.HashOfFileOnDisk; m_pbHashOfFileOnDisk = kdb.HashOfFileOnDisk; Debug.Assert(m_pbHashOfFileOnDisk != null); m_bModified = false; }
private bool EncryptAndSave(byte[] secret) { //generate a random challenge for use next time byte[] challenge = GenerateChallenge(); //generate the expected HMAC-SHA1 response for the challenge based on the secret byte[] resp = GenerateResponse(challenge, secret); //use the response to encrypt the secret SHA256 sha = SHA256Managed.Create(); byte[] key = sha.ComputeHash(resp); // get a 256 bit key from the 160 bit hmac response byte[] secretHash = sha.ComputeHash(secret); AesManaged aes = new AesManaged(); aes.KeySize = key.Length * sizeof(byte) * 8; //pedantic, but foolproof aes.Key = key; aes.GenerateIV(); aes.Padding = PaddingMode.PKCS7; byte[] iv = aes.IV; byte[] encrypted; ICryptoTransform enc = aes.CreateEncryptor(); using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, enc, CryptoStreamMode.Write)) { csEncrypt.Write(secret, 0, secret.Length); csEncrypt.FlushFinalBlock(); encrypted = msEncrypt.ToArray(); csEncrypt.Close(); csEncrypt.Clear(); } msEncrypt.Close(); } sha.Clear(); aes.Clear(); Stream s = null; try { FileTransactionEx ft = new FileTransactionEx(mInfo, false); s = ft.OpenWrite(); XmlWriterSettings settings = new XmlWriterSettings(); settings.CloseOutput = true; settings.Indent = true; settings.IndentChars = "\t"; settings.NewLineOnAttributes = true; XmlWriter xml = XmlWriter.Create(s, settings); xml.WriteStartDocument(); xml.WriteStartElement("data"); xml.WriteStartElement("aes"); xml.WriteElementString("encrypted", Convert.ToBase64String(encrypted)); xml.WriteElementString("iv", Convert.ToBase64String(iv)); xml.WriteEndElement(); xml.WriteElementString("challenge", Convert.ToBase64String(challenge)); xml.WriteElementString("verification", Convert.ToBase64String(secretHash)); xml.WriteElementString("lt64", LT64.ToString()); xml.WriteEndElement(); xml.WriteEndDocument(); xml.Close(); ft.CommitWrite(); } catch (Exception) { MessageService.ShowWarning(String.Format("Error: unable to write to file {0}", mInfo.Path)); return(false); } finally { s.Close(); } return(true); }
private static bool SaveConfigFileEx(AppConfigEx tConfig, string strFilePath, bool bRemoveConfigPref) { tConfig.OnSavePre(); XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); bool bResult = true; // FileStream fs = null; IOConnectionInfo iocPath = IOConnectionInfo.FromPath(strFilePath); FileTransactionEx fts = new FileTransactionEx(iocPath, true); Stream fs = null; // Temporarily remove user file preference (restore after saving) bool bConfigPref = tConfig.Meta.PreferUserConfiguration; if (bRemoveConfigPref) { tConfig.Meta.PreferUserConfiguration = false; } XmlWriterSettings xws = new XmlWriterSettings(); xws.Encoding = StrUtil.Utf8; xws.Indent = true; xws.IndentChars = "\t"; try { // fs = new FileStream(strFilePath, FileMode.Create, // FileAccess.Write, FileShare.None); fs = fts.OpenWrite(); if (fs == null) { throw new InvalidOperationException(); } XmlWriter xw = XmlWriter.Create(fs, xws); xmlSerial.Serialize(xw, tConfig); xw.Close(); } catch (Exception) { bResult = false; } // Do not assert if (fs != null) { fs.Close(); fs = null; } if (bResult) { try { fts.CommitWrite(); } catch (Exception) { Debug.Assert(false); } } if (bRemoveConfigPref) { tConfig.Meta.PreferUserConfiguration = bConfigPref; } tConfig.OnSavePost(); return(bResult); }