Exemplo n.º 1
0
            // Throws on error
            public static LockFileInfo Create(IOConnectionInfo iocLockFile)
            {
                LockFileInfo lfi;
                Stream       s = null;

                try
                {
                    byte[] pbID    = CryptoRandom.Instance.GetRandomBytes(16);
                    string strTime = TimeUtil.SerializeUtc(DateTime.UtcNow);

                    lfi = new LockFileInfo(Convert.ToBase64String(pbID), strTime,
#if KeePassUAP
                                           EnvironmentExt.UserName, EnvironmentExt.MachineName,
                                           EnvironmentExt.UserDomainName);
#elif KeePassLibSD
                                           string.Empty, string.Empty, string.Empty);
#else
                                           Environment.UserName, Environment.MachineName,
                                           Environment.UserDomainName);
#endif

                    StringBuilder sb = new StringBuilder();
#if !KeePassLibSD
                    sb.AppendLine(LockFileHeader);
                    sb.AppendLine(lfi.ID);
                    sb.AppendLine(strTime);
                    sb.AppendLine(lfi.UserName);
                    sb.AppendLine(lfi.Machine);
                    sb.AppendLine(lfi.Domain);
#else
                    sb.Append(LockFileHeader + MessageService.NewLine);
                    sb.Append(lfi.ID + MessageService.NewLine);
                    sb.Append(strTime + MessageService.NewLine);
                    sb.Append(lfi.UserName + MessageService.NewLine);
                    sb.Append(lfi.Machine + MessageService.NewLine);
                    sb.Append(lfi.Domain + MessageService.NewLine);
#endif

                    byte[] pbFile = StrUtil.Utf8.GetBytes(sb.ToString());

                    s = IOConnection.OpenWrite(iocLockFile);
                    if (s == null)
                    {
                        throw new IOException(iocLockFile.GetDisplayName());
                    }
                    s.Write(pbFile, 0, pbFile.Length);
                }
                finally { if (s != null)
                          {
                              s.Close();
                          }
                }

                return(lfi);
            }
Exemplo n.º 2
0
        public FileLock(IOConnectionInfo iocBaseFile)
        {
            if (iocBaseFile == null)
            {
                throw new ArgumentNullException("strBaseFile");
            }

            m_iocLockFile       = iocBaseFile.CloneDeep();
            m_iocLockFile.Path += LockFileExt;

            LockFileInfo lfiEx = LockFileInfo.Load(m_iocLockFile);

            if (lfiEx != null)
            {
                m_iocLockFile = null;                 // Otherwise Dispose deletes the existing one
                throw new FileLockException(iocBaseFile.GetDisplayName(),
                                            lfiEx.GetOwner());
            }

            LockFileInfo.Create(m_iocLockFile);
        }
Exemplo n.º 3
0
            // Throws on error
            public static LockFileInfo Create(IOConnectionInfo iocLockFile)
            {
                LockFileInfo lfi;
                Stream s = null;
                try
                {
                    byte[] pbID = CryptoRandom.Instance.GetRandomBytes(16);
                    string strTime = TimeUtil.SerializeUtc(DateTime.Now);

                    lfi = new LockFileInfo(Convert.ToBase64String(pbID), strTime,
                #if KeePassUAP
                        EnvironmentExt.UserName, EnvironmentExt.MachineName,
                        EnvironmentExt.UserDomainName);
                #elif KeePassLibSD
                        string.Empty, string.Empty, string.Empty);
                #else
                        Environment.UserName, Environment.MachineName,
                        Environment.UserDomainName);
                #endif

                    StringBuilder sb = new StringBuilder();
                #if !KeePassLibSD
                    sb.AppendLine(LockFileHeader);
                    sb.AppendLine(lfi.ID);
                    sb.AppendLine(strTime);
                    sb.AppendLine(lfi.UserName);
                    sb.AppendLine(lfi.Machine);
                    sb.AppendLine(lfi.Domain);
                #else
                    sb.Append(LockFileHeader + MessageService.NewLine);
                    sb.Append(lfi.ID + MessageService.NewLine);
                    sb.Append(strTime + MessageService.NewLine);
                    sb.Append(lfi.UserName + MessageService.NewLine);
                    sb.Append(lfi.Machine + MessageService.NewLine);
                    sb.Append(lfi.Domain + MessageService.NewLine);
                #endif

                    byte[] pbFile = StrUtil.Utf8.GetBytes(sb.ToString());

                    s = IOConnection.OpenWrite(iocLockFile);
                    if(s == null) throw new IOException(iocLockFile.GetDisplayName());
                    s.Write(pbFile, 0, pbFile.Length);
                }
                finally { if(s != null) s.Close(); }

                return lfi;
            }