コード例 #1
0
        public void Dump(string outFile, Monitor m, string msg)
        {
            string tempFile = HttpGetter.GetTempFilePath(outFile);
            Stream outs     = null;

            try
            {
                if (File.Exists(tempFile))
                {
                    outs = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 1024 * 1024 * 5);
                    outs.Seek(0, SeekOrigin.End);
                }
                else
                {
                    outs = Utils.OpenWrite(tempFile);
                }


                if (outs.Position > 0)
                {
                    try
                    {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rm.url);
                        request = HttpGetter.SetRequestDefaults(request);
                        SetRange(request, outs.Position);
                        response = (HttpWebResponse)request.GetResponse();
                        if (m.ShouldStop())
                        {
                            return;
                        }
                    }
                    catch (System.Net.WebException ex)
                    {
                        if (response != null)
                        {
                            response.Close();
                        }
                        response = null;
                        if (ex.Message.IndexOf("416") > 0)
                        {
                            if (outs != null)
                            {
                                outs.Close();
                                outs = null;
                            }
                            Utils.DeleteFile(outFile);
                            Utils.DeleteFile(tempFile);
                            outs = Utils.OpenWrite(tempFile);
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                }
                if (response == null)
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rm.url);
                    request  = HttpGetter.SetRequestDefaults(request);
                    response = (HttpWebResponse)request.GetResponse();
                    if (m.ShouldStop())
                    {
                        return;
                    }
                }
                if (response.StatusCode != HttpStatusCode.PartialContent)
                {
                    outs.Seek(0, SeekOrigin.Begin);
                    outs.SetLength(0);
                }
                else
                {
                    RawLog.Default.Log("resume");
                }
                stream = response.GetResponseStream();
                long cl = response.ContentLength;
                long fileTotalLength = -1;
                if (cl >= 0)
                {
                    fileTotalLength = outs.Position + cl;
                }

                // verify size if we can
                if (!Config.Default.AppEncOn && (fileTotalLength >= 0) && rm.IsSizeValid())
                {
                    if (rm.size != fileTotalLength)
                    {
                        if (outs != null)
                        {
                            outs.Close();
                            outs = null;
                        }
                        Utils.DeleteFile(tempFile);
                        throw new Exception(Str.Def.Get(Str.FailedLength) + rm.name);
                    }
                }

                long len = rm.size;
                if ((cl >= 0) && (len != cl))
                {
                    len = cl;

                    // update gui
                    if (Config.Default.ReportFileSize)
                    {
                        msg += " " + Utils.SizeStr(len);
                        m.Log(msg);
                    }

                    // update rm
                    if (!rm.IsSizeValid())
                    {
                        rm.size = fileTotalLength;
                    }
                }

                if (m.ShouldStop())
                {
                    return;
                }
                Utils.WriteStream(stream, outs, m, len);
                outs.Close();
                outs = null;
                Dispose();

                if (m.ShouldStop())
                {
                    return;
                }

                Encoder.Encode(tempFile, Config.Default.AppEnc, false);

                // check length
                m.Log(Str.Def.Get(Str.VerifyingFile));
                if (!rm.ValidateSize(tempFile))
                {
                    Utils.DeleteFile(tempFile);
                    throw new Exception(Str.Def.Get(Str.FailedLength) + rm.name);
                }
                // check crc
                if (rm.ShouldCheckCrc)
                {
                    CheckCrc(tempFile, m);
                }

                Utils.DeleteFile(outFile);
                File.Move(tempFile, outFile);
            }
            finally
            {
                if (outs != null)
                {
                    outs.Close();
                    outs = null;
                }
                Dispose();
            }
        }
コード例 #2
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);
        }
コード例 #3
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));
                }
            }
        }
コード例 #4
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);
        }