private void CommitWriteTransaction() { bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path); #if (!KeePassLibSD && !KeePassRT) FileSecurity bkSecurity = null; bool bEfsEncrypted = false; #endif if (IOConnection.FileExists(m_iocBase)) { #if (!KeePassLibSD && !KeePassRT) if (m_iocBase.IsLocalFile()) { try { FileAttributes faBase = File.GetAttributes(m_iocBase.Path); bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0); DateTime tCreation = File.GetCreationTime(m_iocBase.Path); bkSecurity = File.GetAccessControl(m_iocBase.Path); File.SetCreationTime(m_iocTemp.Path, tCreation); } catch (Exception) { Debug.Assert(false); } } #endif IOConnection.DeleteFile(m_iocBase); } IOConnection.RenameFile(m_iocTemp, m_iocBase); #if (!KeePassLibSD && !KeePassRT) if (m_iocBase.IsLocalFile()) { try { if (bEfsEncrypted) { try { File.Encrypt(m_iocBase.Path); } catch (Exception) { Debug.Assert(false); } } if (bkSecurity != null) { File.SetAccessControl(m_iocBase.Path, bkSecurity); } } catch (Exception) { Debug.Assert(false); } } #endif if (bMadeUnhidden) { UrlUtil.HideFile(m_iocBase.Path, true); // Hide again } }
public Stream OpenWrite() { if(!m_bTransacted) m_bMadeUnhidden = UrlUtil.UnhideFile(m_iocTemp.Path); else // m_bTransacted { try { IOConnection.DeleteFile(m_iocTemp); } catch(Exception) { } } return IOConnection.OpenWrite(m_iocTemp); }
private void CommitWriteTransaction() { bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path); #if !KeePassLibSD FileSecurity bkSecurity = null; #endif if (IOConnection.FileExists(m_iocBase)) { #if !KeePassLibSD if (m_iocBase.IsLocalFile()) { try { DateTime tCreation = File.GetCreationTime(m_iocBase.Path); bkSecurity = File.GetAccessControl(m_iocBase.Path); File.SetCreationTime(m_iocTemp.Path, tCreation); } catch (Exception) { Debug.Assert(false); } } #endif IOConnection.DeleteFile(m_iocBase); } IOConnection.RenameFile(m_iocTemp, m_iocBase); #if !KeePassLibSD if (m_iocBase.IsLocalFile()) { try { if (bkSecurity != null) { File.SetAccessControl(m_iocBase.Path, bkSecurity); } } catch (Exception) { Debug.Assert(false); } } #endif if (bMadeUnhidden) { UrlUtil.HideFile(m_iocBase.Path, true); // Hide again } }
private void Dispose(bool bDisposing) { if (m_iocLockFile == null) { return; } bool bFileDeleted = false; for (int r = 0; r < 5; ++r) { // if(!OwnLockFile()) { bFileDeleted = true; break; } try { IOConnection.DeleteFile(m_iocLockFile); bFileDeleted = true; } catch (Exception) { Debug.Assert(false); } if (bFileDeleted) { break; } if (bDisposing) { Thread.Sleep(50); } } if (bDisposing && !bFileDeleted) { IOConnection.DeleteFile(m_iocLockFile); // Possibly with exception } m_iocLockFile = null; }
private void Dispose(bool bDisposing) { if (m_iocLockFile == null) { return; } bool bFileDeleted = false; for (int r = 0; r < 5; ++r) { // if(!OwnLockFile()) { bFileDeleted = true; break; } try { IOConnection.DeleteFile(m_iocLockFile); bFileDeleted = true; } catch (Exception) { Debug.Assert(false); } if (bFileDeleted) { break; } if (bDisposing) { #if !FEATURE_TASKS Thread.Sleep(50); #else System.Threading.Tasks.Task.Delay(50).ConfigureAwait(false).GetAwaiter().GetResult(); #endif } } // if(bDisposing && !bFileDeleted) // IOConnection.DeleteFile(m_iocLockFile); // Possibly with exception m_iocLockFile = null; }
private void Dispose(bool bDisposing) { m_iocBase = null; if (!bDisposing) { return; } try { foreach (IOConnectionInfo ioc in m_lToDelete) { if (IOConnection.FileExists(ioc, false)) { IOConnection.DeleteFile(ioc); } } m_lToDelete.Clear(); } catch (Exception) { Debug.Assert(false); } }
private void CommitWriteTransaction() { if (g_bExtraSafe) { if (!IOConnection.FileExists(m_iocTemp)) { throw new FileNotFoundException(m_iocTemp.Path + MessageService.NewLine + KLRes.FileSaveFailed); } } bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path); #if !KeePassUAP // 'All' includes 'Audit' (SACL), which requires SeSecurityPrivilege, // which we usually don't have and therefore get an exception; // trying to set 'Owner' or 'Group' can result in an // UnauthorizedAccessException; thus we restore 'Access' (DACL) only const AccessControlSections acs = AccessControlSections.Access; bool bEfsEncrypted = false; byte[] pbSec = null; #endif DateTime?otCreation = null; bool bBaseExists = IOConnection.FileExists(m_iocBase); if (bBaseExists && m_iocBase.IsLocalFile()) { // FileAttributes faBase = FileAttributes.Normal; try { #if !KeePassUAP FileAttributes faBase = File.GetAttributes(m_iocBase.Path); bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0); try { if (bEfsEncrypted) { File.Decrypt(m_iocBase.Path); } } // For TxF catch (Exception) { Debug.Assert(false); } #endif otCreation = File.GetCreationTimeUtc(m_iocBase.Path); #if !KeePassUAP // May throw with Mono FileSecurity sec = File.GetAccessControl(m_iocBase.Path, acs); if (sec != null) { pbSec = sec.GetSecurityDescriptorBinaryForm(); } #endif } catch (Exception) { Debug.Assert(NativeLib.IsUnix()); } // if((long)(faBase & FileAttributes.ReadOnly) != 0) // throw new UnauthorizedAccessException(); } if (!TxfMove()) { if (bBaseExists) { IOConnection.DeleteFile(m_iocBase); } IOConnection.RenameFile(m_iocTemp, m_iocBase); } else { Debug.Assert(pbSec != null); } // TxF success => NTFS => has ACL try { // If File.GetCreationTimeUtc fails, it may return a // date with year 1601, and Unix times start in 1970, // so testing for 1971 should ensure validity; // https://msdn.microsoft.com/en-us/library/system.io.file.getcreationtimeutc.aspx if (otCreation.HasValue && (otCreation.Value.Year >= 1971)) { File.SetCreationTimeUtc(m_iocBase.Path, otCreation.Value); } #if !KeePassUAP if (bEfsEncrypted) { try { File.Encrypt(m_iocBase.Path); } catch (Exception) { Debug.Assert(false); } } // File.SetAccessControl(m_iocBase.Path, secPrev); // Directly calling File.SetAccessControl with the previous // FileSecurity object does not work; the binary form // indirection is required; // https://sourceforge.net/p/keepass/bugs/1738/ // https://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol.aspx if ((pbSec != null) && (pbSec.Length != 0)) { FileSecurity sec = new FileSecurity(); sec.SetSecurityDescriptorBinaryForm(pbSec, acs); File.SetAccessControl(m_iocBase.Path, sec); } #endif } catch (Exception) { Debug.Assert(false); } if (bMadeUnhidden) { UrlUtil.HideFile(m_iocBase.Path, true); } }
private void CommitWriteTransaction() { if (g_bExtraSafe) { if (!IOConnection.FileExists(m_iocTemp)) { throw new FileNotFoundException(m_iocTemp.Path + MessageService.NewLine + KLRes.FileSaveFailed); } } bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path); #if !KeePassUAP bool bEfsEncrypted = false; FileSecurity bkSecurity = null; #endif DateTime?otCreation = null; bool bBaseExists = IOConnection.FileExists(m_iocBase); if (bBaseExists && m_iocBase.IsLocalFile()) { // FileAttributes faBase = FileAttributes.Normal; try { #if !KeePassUAP FileAttributes faBase = File.GetAttributes(m_iocBase.Path); bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0); try { if (bEfsEncrypted) { File.Decrypt(m_iocBase.Path); } } // For TxF catch (Exception) { Debug.Assert(false); } #endif otCreation = File.GetCreationTimeUtc(m_iocBase.Path); #if !KeePassUAP // May throw with Mono bkSecurity = File.GetAccessControl(m_iocBase.Path); #endif } catch (Exception) { Debug.Assert(NativeLib.IsUnix()); } // if((long)(faBase & FileAttributes.ReadOnly) != 0) // throw new UnauthorizedAccessException(); } if (!TxfMove()) { if (bBaseExists) { IOConnection.DeleteFile(m_iocBase); } IOConnection.RenameFile(m_iocTemp, m_iocBase); } try { // If File.GetCreationTimeUtc fails, it may return a // date with year 1601, and Unix times start in 1970, // so testing for 1971 should ensure validity; // https://msdn.microsoft.com/en-us/library/system.io.file.getcreationtimeutc.aspx if (otCreation.HasValue && (otCreation.Value.Year >= 1971)) { File.SetCreationTimeUtc(m_iocBase.Path, otCreation.Value); } #if !KeePassUAP if (bEfsEncrypted) { try { File.Encrypt(m_iocBase.Path); } catch (Exception) { Debug.Assert(false); } } if (bkSecurity != null) { File.SetAccessControl(m_iocBase.Path, bkSecurity); } #endif } catch (Exception) { Debug.Assert(false); } if (bMadeUnhidden) { UrlUtil.HideFile(m_iocBase.Path, true); } }
private void CommitWriteTransaction() { bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path); #if (!KeePassLibSD && !KeePassUAP) FileSecurity bkSecurity = null; bool bEfsEncrypted = false; #endif if (g_bExtraSafe) { if (!IOConnection.FileExists(m_iocTemp)) { throw new FileNotFoundException(m_iocTemp.Path + MessageService.NewLine + KLRes.FileSaveFailed); } } if (IOConnection.FileExists(m_iocBase)) { #if !KeePassLibSD if (m_iocBase.IsLocalFile()) { try { #if !KeePassUAP FileAttributes faBase = File.GetAttributes(m_iocBase.Path); bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0); #endif DateTime tCreation = File.GetCreationTime(m_iocBase.Path); File.SetCreationTime(m_iocTemp.Path, tCreation); #if !KeePassUAP // May throw with Mono bkSecurity = File.GetAccessControl(m_iocBase.Path); #endif } catch (Exception) { Debug.Assert(NativeLib.IsUnix()); } } #endif IOConnection.DeleteFile(m_iocBase); } IOConnection.RenameFile(m_iocTemp, m_iocBase); #if (!KeePassLibSD && !KeePassUAP) if (m_iocBase.IsLocalFile()) { try { if (bEfsEncrypted) { try { File.Encrypt(m_iocBase.Path); } catch (Exception) { Debug.Assert(false); } } if (bkSecurity != null) { File.SetAccessControl(m_iocBase.Path, bkSecurity); } } catch (Exception) { Debug.Assert(false); } } #endif if (bMadeUnhidden) { UrlUtil.HideFile(m_iocBase.Path, true); // Hide again } }
private void CommitWriteTransaction() { bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path); #if (!KeePassLibSD && !KeePassUAP) FileSecurity bkSecurity = null; bool bEfsEncrypted = false; #endif // Check that temporary file exists. if (!IOConnection.FileExists(m_iocTemp)) { throw new FileNotFoundException("Temporary file disappeared before transaction completed.", m_iocTemp.Path); } if (IOConnection.FileExists(m_iocBase)) { #if !KeePassLibSD if(m_iocBase.IsLocalFile()) { try { #if !KeePassUAP FileAttributes faBase = File.GetAttributes(m_iocBase.Path); bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0); #endif DateTime tCreation = File.GetCreationTime(m_iocBase.Path); File.SetCreationTime(m_iocTemp.Path, tCreation); #if !KeePassUAP // May throw with Mono bkSecurity = File.GetAccessControl(m_iocBase.Path); #endif } catch(Exception) { Debug.Assert(NativeLib.IsUnix()); } } #endif IOConnection.DeleteFile(m_iocBase); } IOConnection.RenameFile(m_iocTemp, m_iocBase); #if (!KeePassLibSD && !KeePassUAP) if(m_iocBase.IsLocalFile()) { try { if(bEfsEncrypted) { try { File.Encrypt(m_iocBase.Path); } catch(Exception) { Debug.Assert(false); } } if(bkSecurity != null) File.SetAccessControl(m_iocBase.Path, bkSecurity); } catch(Exception) { Debug.Assert(false); } } #endif if(bMadeUnhidden) UrlUtil.HideFile(m_iocBase.Path, true); // Hide again }