// summary: // Takes a single necessary file action // Returns false if no more actions needed bool TakeFileAction(UpdatedFile a_File) { Utility.AssertEnumCount(typeof(FileStatus), 7); switch (a_File.Status) { case FileStatus.Initial: return(false); case FileStatus.AlreadyOk: return(false); case FileStatus.NeedsUpdate: DownloadFile(a_File); return(true); case FileStatus.Downloading: Thread.Sleep(1000); return(true); case FileStatus.Downloaded: return(false); case FileStatus.Updated: return(false); case FileStatus.Error: return(false); } return(false); }
void MoveDownloadedFile(UpdatedFile a_File) { if (!CheckFileHash(a_File, a_File.DownloadedPath, true)) { a_File.Status = FileStatus.Error; a_File.LastError = "Downloaded file is damaged"; a_File.LastException = null; return; } string backupPath = m_BackupPath + "\\" + a_File.FileName; Directory.CreateDirectory(m_BackupPath); File.Delete(backupPath); if (File.Exists(a_File.FilePath)) { File.Move(a_File.FilePath, backupPath); } ExtractGZipFile(a_File.DownloadedPath, a_File.FilePath); a_File.Status = FileStatus.Updated; File.Delete(a_File.DownloadedPath); }
private static void DownloadProgressCB(object a_Sender, DownloadProgressChangedEventArgs a_Args) { UpdatedFile a_File = (UpdatedFile)a_Args.UserState; a_File.BytesDone = (UInt64)a_Args.BytesReceived; a_File.BytesTotal = (UInt64)a_Args.TotalBytesToReceive; }
//Constructor public BookStoreClass() { Book = new BookClass(); EmployeeList = new EmployeeListClass(); currentBookFile = new currentFile(currentBookFilePath); updatedBookFile = new UpdatedFile(updatedBookFilePath); currentEmployeeFile = new currentFile(currentEmployeeFilePath); updatedEmployeeFile = new UpdatedFile(updatedEmployeeFilePath); }
void DownloadFile(UpdatedFile a_File) { Directory.CreateDirectory(m_DownloadPath); File.Delete(a_File.DownloadedPath); WebClient webClient = new WebClient(); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompletedCB); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCB); webClient.DownloadFileAsync(new Uri(a_File.SourcePath), a_File.DownloadedPath, a_File); a_File.Status = FileStatus.Downloading; }
static bool CheckFileHash(UpdatedFile a_File, string a_FilePath, bool a_IsPacked) { try { string wantedHash = a_IsPacked ? a_File.PackedHash : a_File.FileHash; string actualHash = Utility.BytesToHex(Utility.Md5File(a_FilePath)); return(actualHash == wantedHash); } catch (System.Exception a_Exception) { a_File.Status = FileStatus.Error; a_File.LastException = a_Exception; return(false); } }
void FileUpdaterThread(Object a_Parameter) { UpdatedFile a_File = (UpdatedFile)a_Parameter; try { for (;;) { if (!TakeFileAction(a_File)) { break; } } } catch (System.Exception a_Exception) { a_File.Status = FileStatus.Error; a_File.LastException = a_Exception; } }
public void LoadFileList(string a_UpdateXmlUrl) { XmlDocument updatesXml = new XmlDocument(); updatesXml.Load(a_UpdateXmlUrl); XmlNodeList xmlFiles = updatesXml.GetElementsByTagName("file"); m_Files.Clear(); foreach (XmlNode currNode in xmlFiles) { UpdatedFile newFile = new UpdatedFile(); newFile.Status = FileStatus.Initial; newFile.FileName = currNode.Attributes["name"].Value; newFile.FilePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + newFile.FileName; newFile.SourcePath = currNode.Attributes["url"].Value; newFile.FileHash = currNode.Attributes["filehash"].Value; newFile.PackedHash = currNode.Attributes["packedhash"].Value; newFile.DownloadedPath = m_DownloadPath + "\\" + newFile.FileName + ".gz"; m_Files.Add(newFile); } }
private static void DownloadCompletedCB(object a_Sender, AsyncCompletedEventArgs a_Args) { UpdatedFile a_File = (UpdatedFile)a_Args.UserState; a_File.Status = FileStatus.Downloaded; }