Пример #1
0
        public void DoWork(Monitor m, bool callStart, DBeforeCallStart beforeCallStart)
        {
            // check time interval if not gui
            if (!callStart && !CanCheckServer())
            {
                return;
            }

            // clean up temp dir as neccesary
            string    remoteFilesList        = Config.Default.GetFileListPath(true);
            ArrayList remoteCurrentFiles     = null;
            ArrayList remoteCurrentFilesAsoc = new ArrayList();

            if (File.Exists(remoteFilesList))
            {
                try
                {
                    remoteCurrentFiles = RemoteFile.Load(remoteFilesList, ref remoteCurrentFilesAsoc);
                }
                catch (Exception ex)
                {
                    RawLog.Default.Log(ex.Message, true);
                    Config.Default.NewVersionFileTag = false;
                    string tempDir = Config.Default.GetPath(null, true, true);
                    Utils.DeleteDir(tempDir);
                }
            }

            // get data from server
            if (m != null)
            {
                m.Log(Str.Def.Get(Str.Connecting));
            }
            long lastTimeTicks = Config.Default.KeyStore.GetLong(KeyStoreIds.RemoteFilesLastDateL, DateTime.MinValue.Ticks, false);

            if (!GetRemoteVersion(m, ref lastTimeTicks))
            {
                if ((m != null) && m.ShouldStop())
                {
                    return;
                }
                SetLastCheckDate(lastTimeTicks);
                if (Config.Default.updateBeforeStart)
                {
                    if (callStart)
                    {
                        if (beforeCallStart != null)
                        {
                            beforeCallStart();
                        }
                        if (Local.Start())
                        {
                            throw new Exception("Nothing to execute");
                        }
                        if (m != null)
                        {
                            m.Log(string.Empty);
                        }
                    }
                }
                return;
            }
            if ((m != null) && m.ShouldStop())
            {
                return;
            }
            if (remoteFiles == null)
            {
                throw new Exception(Str.Def.Get(Str.RemoteError));
            }
            ArrayList newRemoteFiles = this.remoteFiles;

            if ((remoteCurrentFiles != null) && (remoteCurrentFiles.Count > 0))
            {
                newRemoteFiles = RemoteFile.GetNewFiles(remoteCurrentFiles, this.remoteFiles, false, true);
                ArrayList tempFileToDelete = RemoteFile.GetRemovedFiles(remoteCurrentFiles, this.remoteFiles, true);
                RemoteFile.DeleteFiles(tempFileToDelete, true);
            }
            if ((m != null) && m.ShouldStop())
            {
                return;
            }
            //string tempDir = Config.Default.GetPath(null, true, true);
            //Utils.DeleteDir(tempDir);
            ArrayList files = RemoteFile.GetNewFiles(Local.localFiles, newRemoteFiles); // this.remoteFiles

            if ((files == null) || (callStart && (files.Count <= 0)))
            {
                throw new Exception(Str.Def.Get(Str.RemoteError));
            }
            if (files.Count <= 0)
            {
                RemoteFile.Save(Config.Default.GetFileListPath(true), this.remoteFiles, this.remoteFileAsocs);
                SetLastCheckDate(lastTimeTicks);
                ArrayList toDelete = RemoteFile.GetRemovedFiles(Local.localFiles, this.remoteFiles, false);
                bool      newAssoc = !FileAssoc.AreEqual(Local.localFileAsocs, this.remoteFileAsocs);
                if (newAssoc || ((toDelete != null) && (toDelete.Count > 0)))
                {
                    Config.Default.NewVersionFileTag = true;
                }
                //if (m != null) m.Log(Str.Def.Get(Str.NothingToDo));
                return;
            }

            // invalidate current update if any
            Config.Default.NewVersionFileTag = false;
            RemoteFile.Save(Config.Default.GetFileListPath(true), remoteFiles, this.remoteFileAsocs);

            // delete new files in any, and check for space
            RemoteFile.DeleteFiles(files, true);
            long outSize = 0;

            if (!RemoteFile.CheckDiskSpace(files, ref outSize))
            {
                throw new Exception(Str.Def.Get(Str.NoSpace) + " " + Utils.SizeStr(outSize));
            }

            if (m != null)
            {
                m.Log("(" + files.Count.ToString(System.Globalization.CultureInfo.InvariantCulture) + ")");
            }
            bool isStarter = false;

            for (int i = 0; i < files.Count; i++)
            {
                if ((m != null) && m.ShouldStop())
                {
                    return;
                }
                RemoteFile rm = (RemoteFile)files[i];
                isStarter = rm.IsStarterReplacer;
                if (isStarter)
                {
                    Config.Default.StarterNewVersion = Config.Default.StarterLastVersion;
                }
                if ((m != null) && m.ShouldStop())
                {
                    return;
                }
                string msgRaw = "[" + (i + 1) + " / " + files.Count + "] " + rm.DisplayName;
                string msg    = msgRaw;
                if (Config.Default.ReportFileSize && rm.IsSizeValid())
                {
                    msg += " " + Utils.SizeStr(rm.size);
                }
                m.Log(msg);
                using (HttpGetter hg = new HttpGetter())
                {
                    hg.Init(rm);
                    string outFile = rm.GetPath(true);
                    try
                    {
                        hg.Dump(outFile, m, msgRaw);
                    }
                    catch (Exception ex)
                    {
                        Utils.DeleteFile(outFile);
                        throw ex;
                    }
                    finally
                    {
                        hg.Dispose();
                    }
                    if ((m != null) && m.ShouldStop())
                    {
                        Utils.DeleteFile(outFile);
                        break;
                    }
                    // file ok
                    if (isStarter)
                    {
                        RawLog.Default.Log("snver " + rm.version);
                        Config.Default.StarterNewVersion = rm.version;
                    }
                }
            }
            if ((m != null) && m.ShouldStop())
            {
                return;
            }

            // normal finish
            RemoteFile.Save(Config.Default.GetFileListPath(true), remoteFiles, this.remoteFileAsocs); // again
            SetLastCheckDate(lastTimeTicks);
            if (isStarter && (files.Count == 1))
            {
                //nothing new
            }
            else
            {
                Config.Default.NewVersionFileTag = true;
            }
            if (callStart)
            {
                if (beforeCallStart != null)
                {
                    beforeCallStart();
                }
                if (Local.Start())
                {
                    throw new Exception("Nothing to execute");
                }
                if (m != null)
                {
                    m.Log(string.Empty);
                }
            }
            else
            {
                if (Config.Default.NewVersionFileTag)
                {
                    //if (m != null) m.Log(Str.Def.Get(Str.RestartNeeded));
                }
            }
        }
Пример #2
0
        // false if replace needed but could not get lock
        public static bool ReplaceFiles()
        {
            ExclusiveLocker replaceLock = new ExclusiveLocker();

            replaceLock.id = Local.ReplaceLockId;
            try
            {
                if (!replaceLock.Lock(0))
                {
                    return(false);
                }
                if (!Config.Default.NewVersionFileTag)
                {
                    return(true);
                }
                RawLog.Default.Log("rep>");
                string tempDir    = Config.Default.GetPath(null, true, true);
                string workingDir = Config.Default.GetPath(null, false, true);
                if (!Directory.Exists(tempDir))
                {
                    Config.Default.NewVersionFileTag = false;
                    return(true);
                }
                if (!Utils.CheckFullAccess(workingDir))
                {
                    RawLog.Default.Log("!work");
                    return(true);
                }
                if (!Utils.CheckFullAccess(tempDir))
                {
                    RawLog.Default.Log("!temp");
                    return(true);
                }

                ArrayList localFilesTmp      = null;
                ArrayList remoteFilesTmp     = null;
                ArrayList localFilesAsocTmp  = new ArrayList();
                ArrayList remoteFilesAsocTmp = new ArrayList();

                string localFileTmp  = Config.Default.GetFileListPath(false);
                string remoteFileTmp = Config.Default.GetFileListPath(true);
                bool   localExits    = File.Exists(localFileTmp);
                bool   remoteExits   = File.Exists(remoteFileTmp);
                try
                {
                    if (localExits && remoteExits)
                    {
                        localFilesTmp  = RemoteFile.Load(localFileTmp, ref localFilesAsocTmp);
                        remoteFilesTmp = RemoteFile.Load(remoteFileTmp, ref remoteFilesAsocTmp);
                    }
                }
                catch (Exception xx) { Utils.OnError(xx); }


                // copy / replace new files
                //Utils.DeleteDir(workingDir);
                //Directory.Move(tempDir, workingDir);
                Utils.CopyReplaceFiles(tempDir, workingDir);
                InitLocalFiles();

                // delete removed files
                try
                {
                    if (localExits && remoteExits)
                    {
                        ArrayList toDelete = RemoteFile.GetRemovedFiles(localFilesTmp, remoteFilesTmp, false);
                        RemoteFile.DeleteFiles(toDelete, false);
                    }
                }
                catch (Exception xx) { Utils.OnError(xx); }
                // set file associations
                if (!FileAssoc.AreEqual(localFilesAsocTmp, localFileAsocs))
                {
                    try
                    {
                        FileAssoc.Register(localFilesAsocTmp, false);
                    }
                    catch (Exception xx) { Utils.OnError(xx); }
                    try
                    {
                        FileAssoc.Register(localFileAsocs, true);
                    }
                    catch (Exception xx) { Utils.OnError(xx); }
                }

                RawLog.Default.Log("rep<");
                Config.Default.NewVersionFileTag = false;
            }
            finally
            {
                replaceLock.Dispose();
            }
            return(true);
        }