コード例 #1
0
 public static void DeleteFiles(ArrayList toDelete, bool isTemp)
 {
     if (toDelete == null)
     {
         return;
     }
     for (int i = 0; i < toDelete.Count; i++)
     {
         RemoteFile rm = (RemoteFile)toDelete[i];
         if (!rm.Valid || rm.IsStarterReplacer)
         {
             continue;
         }
         string path = rm.GetPath(isTemp);
         try
         {
             if (Utils.DeleteFile(path))
             {
                 RawLog.Default.Log("Del " + path);
             }
             string dir = Path.GetDirectoryName(path);
             Utils.DeleteEmptyDir(dir, true);
         }
         catch (Exception xx) { Utils.OnError(xx); }
     }
 }
コード例 #2
0
 private static void InitLocalFiles()
 {
     localFiles     = null;
     localFileAsocs = new ArrayList();
     try
     {
         string path = Config.Default.GetFileListPath(false);
         if (File.Exists(path))
         {
             localFiles = RemoteFile.Load(path, ref localFileAsocs);
             if ((localFileAsocs != null) && (localFileAsocs.Count > 0))
             {
                 string currentStarterExePath = System.Windows.Forms.Application.ExecutablePath;
                 string np = Config.Default.KeyStore.GetString(KeyStoreIds.StarterLastPathS, null, false);
                 if (np == null)
                 {
                     Config.Default.KeyStore.SetString(KeyStoreIds.StarterLastPathS, currentStarterExePath, true);
                 }
                 else if (!np.Equals(currentStarterExePath))
                 {
                     Config.Default.KeyStore.SetString(KeyStoreIds.StarterLastPathS, currentStarterExePath, true);
                     RawLog.Default.Log("!path");
                     FileAssoc.Register(localFileAsocs, true);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         localFiles = new ArrayList();
         throw ex;
     }
 }
コード例 #3
0
        public static bool Start(bool callReplaceFiles)
        {
            if (callReplaceFiles)
            {
                Local.ReplaceFiles();
            }
            else
            {
                ExclusiveLocker replaceLock = new ExclusiveLocker();
                replaceLock.id = Local.ReplaceLockId;
                try
                {
                    if (!replaceLock.Lock(2000))
                    {
                        return(false);
                    }
                }
                finally
                {
                    replaceLock.Dispose();
                }
            }
            InitLocalFiles();
            int startCount = RemoteFile.Start(localFiles);

            return(startCount <= 0);  // true for gui
        }
コード例 #4
0
 public void Init(RemoteFile rm)
 {
     if (this.rm != null)
     {
         throw new Exception(Str.Def.Get(Str.RemoteError));
     }
     this.rm = rm;
 }
コード例 #5
0
        public bool IsSamePathFile(RemoteFile other, ref bool sameVersion)
        {
            sameVersion = false;
            int r = Compare(this.name, other.name);

            if (r == 0)
            {
                sameVersion = (Compare(this.version, other.version) == 0);
            }
            return(r == 0);
        }
コード例 #6
0
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }
            RemoteFile other = (RemoteFile)obj;
            int        r     = Compare(this.name, other.name);

            if (r != 0)
            {
                return(r);
            }
            r = Compare(this.version, other.version);
            if (r != 0)
            {
                return(r);
            }
            if (this.IsSizeValid() && other.IsSizeValid())
            {
                if (this.size < other.size)
                {
                    r = -1;
                }
                if (this.size > other.size)
                {
                    r = 1;
                }
                else
                {
                    r = 0;
                }
            }
            if (r != 0)
            {
                return(r);
            }
            if (!string.IsNullOrEmpty(this.crc) && !string.IsNullOrEmpty(other.crc))
            {
                r = Compare(this.crc.ToLower(), other.crc.ToLower());
            }
            return(r);
        }
コード例 #7
0
 public void FromString(string s)
 {
     //progId|ext|desc|ipath|iidx|epath
     if (string.IsNullOrEmpty(s))
     {
         return;
     }
     if (s.StartsWith(Prefix))
     {
         s = RemoteFile.CleanString(s.Substring(Prefix.Length));
     }
     if (string.IsNullOrEmpty(s))
     {
         return;
     }
     string[] p = s.Split('|');
     if ((p == null) || (p.Length < 2))
     {
         return;
     }
     this.progId    = RemoteFile.CleanString(p[0]);
     this.extension = RemoteFile.CleanString(p[1]);
     if (!string.IsNullOrEmpty(extension) && !this.extension.StartsWith("."))
     {
         this.extension = "." + this.extension;
     }
     if (p.Length > 2)
     {
         this.desc = RemoteFile.CleanString(p[2]);
     }
     if (p.Length > 3)
     {
         this.iconPath = RemoteFile.CleanString(p[3]);
     }
     if (p.Length > 4)
     {
         this.iconIdx = Convert.ToInt32(RemoteFile.CleanString(p[4]), System.Globalization.CultureInfo.InvariantCulture);
     }
     if (p.Length > 5)
     {
         this.exePath = RemoteFile.CleanString(p[5]);
     }
 }
コード例 #8
0
        public static void KillApp(bool killSelf, bool unregister)
        {
            ArrayList files     = null;
            ArrayList filesAsoc = new ArrayList();

            try
            {
                files = RemoteFile.Load(Config.Default.GetFileListPath(false), ref filesAsoc);
            }
            catch (Exception xx) { Utils.OnError(xx); }
            RemoteFile.Start(files, true);
            if (unregister)
            {
                try
                {
                    FileAssoc.Register(filesAsoc, false);
                }
                catch (Exception xx) { Utils.OnError(xx); }
            }
            if (killSelf)
            {
                Utils.KillProcess(Path.GetFileNameWithoutExtension(Application.ExecutablePath));
            }
        }
コード例 #9
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));
                }
            }
        }
コード例 #10
0
        // false mean nothing new
        private bool GetRemoteVersion(Monitor m, ref long lastTimeTicks)
        {
            Stream          s        = null;
            HttpWebResponse response = null;

            remoteFiles.Clear();
            try
            {
                string url = Config.Default.AppUrl;
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request  = HttpGetter.SetRequestDefaults(request);
                    response = (HttpWebResponse)request.GetResponse();
                    if (response == null)
                    {
                        throw new Exception();
                    }
                }
                catch
                {
                    url = Config.Default.AppUrl2;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request  = HttpGetter.SetRequestDefaults(request);
                    response = (HttpWebResponse)request.GetResponse();
                }

                if (response == null)
                {
                    throw new Exception();
                }

                // date
                try
                {
                    DateTime lastTime        = new DateTime(lastTimeTicks);
                    DateTime remoteFilesDate = response.LastModified;
                    if (Config.Default.checkRemoteFileDate)
                    {
                        if (DateTime.Compare(lastTime, remoteFilesDate) == 0)
                        {
                            return(false);
                        }
                    }
                    lastTimeTicks = remoteFilesDate.Ticks;
                }
                catch (Exception xx) { Utils.OnError(xx); }

                s = response.GetResponseStream();
                if ((m != null) && m.ShouldStop())
                {
                    return(false);
                }
                if (s == null)
                {
                    throw new Exception(Str.Def.Get(Str.RemoteError));
                }
                remoteFiles = RemoteFile.Load(s, ref this.remoteFileAsocs);
            }
            finally
            {
                if (s != null)
                {
                    try { s.Close(); }
                    catch {  }
                }
                s = null;
                if (response != null)
                {
                    try { response.Close(); }
                    catch {  }
                }
                response = null;
            }
            return(true);
        }
コード例 #11
0
        public static ArrayList GetNewFiles(ArrayList localFiles, ArrayList remoteFiles, bool checkOnlyPath, bool isTemp)
        {
            if ((remoteFiles == null) || (remoteFiles.Count <= 0))
            {
                return(null);
            }
            if ((localFiles == null) || (localFiles.Count <= 0))
            {
                return(remoteFiles);
            }
            ArrayList newFiles = new ArrayList();

            for (int i = 0; i < remoteFiles.Count; i++)
            {
                RemoteFile rm    = (RemoteFile)remoteFiles[i];
                bool       found = false;
                for (int j = 0; j < localFiles.Count; j++)
                {
                    RemoteFile lc          = (RemoteFile)localFiles[j];
                    bool       sameFile    = false;
                    bool       sameVersion = false;
                    bool       sameName    = lc.IsSamePathFile(rm, ref sameVersion);
                    if (checkOnlyPath)
                    {
                        sameFile = lc.name.Equals(rm.name);
                    }
                    else
                    {
                        sameFile = lc.IsSameFile(rm);
                    }
                    if (sameFile)
                    {
                        if (checkOnlyPath)
                        {
                            found = true;
                        }
                        else
                        {
                            string file   = rm.GetPath(isTemp);
                            bool   exists = File.Exists(file);
                            if (exists)
                            {
                                if (rm.ValidateSize(file))
                                {
                                    if (rm.ShouldCheckCrc)
                                    {
                                        if (Crc.HashFile(file, false).ToLower().Equals(rm.crc))
                                        {
                                            found = true;
                                        }
                                    }
                                    else
                                    {
                                        found = true;
                                    }
                                }
                            }
                            else
                            {
                                // handle partial files
                                string tempFile   = HttpGetter.GetTempFilePath(file);
                                bool   tempExists = File.Exists(tempFile);
                                if (tempExists)
                                {
                                    if (rm.IsSizeValid())
                                    {
                                        if (rm.ValidateSize(tempFile))
                                        {
                                            if (rm.ShouldCheckCrc)
                                            {
                                                if (Crc.HashFile(tempFile, false).ToLower().Equals(rm.crc))
                                                {
                                                    File.Move(tempFile, file);
                                                    found = true;
                                                }
                                                else
                                                {
                                                    Utils.DeleteFile(tempFile);
                                                }
                                            }
                                            else
                                            {
                                                File.Move(tempFile, file);
                                                found = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                    else
                    {
                        if (sameName && !sameVersion)
                        {
                            string file = rm.GetPath(isTemp);
                            Utils.DeleteFile(HttpGetter.GetTempFilePath(file));
                        }
                    }
                }
                if (!found)
                {
                    newFiles.Add(rm);
                }
            }
            return(newFiles);
        }
コード例 #12
0
        public static ArrayList Load(StreamReader sr, ref ArrayList fileAssoc)
        {
            if (fileAssoc == null)
            {
                fileAssoc = new ArrayList();
            }
            fileAssoc.Clear();

            ArrayList files     = new ArrayList();
            Hashtable variables = new Hashtable();
            string    crc       = string.Empty;
            Crc       hash      = new Crc(true);

            for (string line = sr.ReadLine(); line != null; line = sr.ReadLine())
            {
                line = line.Trim(' ', '\t', '\r', '\n');
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                else if (line.StartsWith(Crc.Prefix))
                {
                    crc = line.Substring(Crc.Prefix.Length);
                    continue;
                }
                else if (line.StartsWith("#") && !line.StartsWith("##"))
                {
                    continue;
                }
                else if (line.StartsWith("$$"))
                {
                    int idx = line.IndexOf('=');
                    if (idx < 0)
                    {
                        continue;
                    }
                    string key = line.Substring(1, idx - 1).Trim();
                    string val = line.Substring(idx + 1).Trim();
                    variables[key] = val;
                    continue;
                }

                foreach (string k in variables.Keys)
                {
                    line = line.Replace(k, (string)variables[k]);
                }

                if (line.StartsWith(FileAssoc.Prefix))
                {
                    FileAssoc fa = new FileAssoc();
                    fa.FromString(line);
                    if (fa.Valid)
                    {
                        hash.Update(fa.ToString());
                        fileAssoc.Add(fa);
                    }
                    continue;
                }

                RemoteFile rm = new RemoteFile();
                rm.Init(line);
                if (rm.Valid)
                {
                    // add starter only if new
                    if (rm.IsStarterReplacer)
                    {
                        if (rm.version.Equals(Config.Default.StarterLastVersion))
                        {
                            continue;
                        }
                        else
                        {
                            string newExePath = Config.Default.GetNewStarterPath();
                            if (File.Exists(newExePath))
                            {
                                string newExeVersion = Config.Default.StarterNewVersion;
                                if (newExeVersion.Equals(rm.version))
                                {
                                    // already downloaded this starter version
                                    continue;
                                }
                            }
                        }
                    }
                    hash.Update(rm.ToString());
                    files.Add(rm);
                }
            }
            if (!string.IsNullOrEmpty(crc))
            {
                string newCrc = hash.GetValue();
                if (!crc.ToLower().Equals(newCrc))
                {
                    throw new Exception(Str.Def.Get(Str.FailedCrc));
                }
            }
            return(files);
        }
コード例 #13
0
 public bool IsSameFile(RemoteFile other)
 {
     return(CompareTo(other) == 0);
 }
コード例 #14
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);
        }