Exemplo n.º 1
0
        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 ModernKeePassLib
                if (bDisposing)
                {
                    Task.Delay(50).Wait();
                }
#else
                if (bDisposing)
                {
                    Thread.Sleep(50);
                }
#endif
            }

            // if(bDisposing && !bFileDeleted)
            //	IOConnection.DeleteFile(m_iocLockFile); // Possibly with exception

            m_iocLockFile = null;
        }
Exemplo n.º 2
0
        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); }
        }
Exemplo n.º 3
0
        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 !ModernKeePassLib
            // '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;
#endif
            bool     bEfsEncrypted = false;
            byte[]   pbSec         = null;
            DateTime?otCreation    = null;

            bool bBaseExists = IOConnection.FileExists(m_iocBase);
            if (bBaseExists && m_iocBase.IsLocalFile())
            {
                // FileAttributes faBase = FileAttributes.Normal;
                try
                {
#if !ModernKeePassLib
                    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
#if ModernKeePassLib
                    otCreation = m_iocBase.StorageFile.DateCreated.UtcDateTime;
#else
                    otCreation = File.GetCreationTimeUtc(m_iocBase.Path);
#endif
#if !ModernKeePassLib
                    // 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 !ModernKeePassLib
                if (otCreation.HasValue && (otCreation.Value.Year >= 1971))
                {
                    File.SetCreationTimeUtc(m_iocBase.Path, otCreation.Value);
                }
#endif

#if !ModernKeePassLib
                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);
            }
        }