static void DownloadThreadRunner() { try { int Direction = -1; lock (FileLock) { if (FilesystemData.FileTransferStatus.ServerID == null) { return; } Direction = FilesystemData.FileTransferStatus.Direction; } #region Client to Server if (Direction == 1) { string LocalFilename = ""; string MD5; Int64 ServerID = 0; Int64 CurrentSZ; Int64 TotalSZ; bool OverrideMetered; bool ReqOnly; DateTime LastModified; try { lock (FileLock) { LocalFilename = FilesystemData.FileTransferStatus.RemoteFileLocation; ServerID = FilesystemData.FileTransferStatus.ServerID.Value; CurrentSZ = FilesystemData.FileTransferStatus.ProgressSize; TotalSZ = FilesystemData.FileTransferStatus.Size; MD5 = FilesystemData.FileTransferStatus.MD5CheckSum; OverrideMetered = FilesystemData.FileTransferStatus.OverrideMeteredConnection; ReqOnly = FilesystemData.FileTransferStatus.RequestOnly; LastModified = FilesystemData.FileTransferStatus.LastModfied; } //won't start when in metered connection! if (OverrideMetered == false) { try { if (MeteredConnection.IsMeteredConnection() == true) { FoxEventLog.VerboseWriteEventLog("Upload paused = metered connection detected", System.Diagnostics.EventLogEntryType.Information); return; } } catch { } } Int64 ReallyCurrentSZ = 0; try { if (File.Exists(LocalFilename) == true) { FileInfo fi = new FileInfo(LocalFilename); ReallyCurrentSZ = fi.Length; } else { FoxEventLog.WriteEventLog("File " + LocalFilename + " does not exist for upload.", System.Diagnostics.EventLogEntryType.Error); return; } } catch (Exception ee) { FoxEventLog.WriteEventLog("Checking upload file " + LocalFilename + " ID: " + ServerID.ToString() + " failed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); return; } downloadnet = Utilities.ConnectNetwork(7); if (downloadnet == null) { return; } if (ReqOnly == true) { Status.UpdateMessage(7, "Checking " + LocalFilename); MD5 = MD5Utilities.CalcMD5File(LocalFilename); FileInfo fi = new FileInfo(LocalFilename); lock (FileLock) { FilesystemData.FileTransferStatus.MD5CheckSum = MD5; FilesystemData.FileTransferStatus.LastModfied = fi.LastWriteTimeUtc; FilesystemData.WriteFileTransferStatus(); } Int64?NewID = downloadnet.File_Agent_NewUploadReq(LocalFilename, OverrideMetered, MD5); if (NewID == null) { FoxEventLog.VerboseWriteEventLog("Cannot create a new upload req for " + LocalFilename, System.Diagnostics.EventLogEntryType.Warning); return; } if (downloadnet.File_Agent_CancelUpload(ServerID) == false) { FoxEventLog.VerboseWriteEventLog("Cannot delete temp upload req for " + LocalFilename + " ID: " + ServerID.ToString(), System.Diagnostics.EventLogEntryType.Warning); return; } lock (FileLock) { //wait for the new upload req ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); UnlockTimer = true; return; } } else { FileStream fs = null; do { try { if (CancelAndDeleteDL == true) { return; } FileInfo fi = new FileInfo(LocalFilename); if (roughDT(fi.LastWriteTimeUtc, LastModified) == false || fi.Length != TotalSZ) { if (downloadnet.File_Agent_CancelUpload(ServerID) == false) { FoxEventLog.VerboseWriteEventLog("Cannot cancel upload req for " + LocalFilename + " ID: " + ServerID.ToString() + ", due file changes", System.Diagnostics.EventLogEntryType.Warning); lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); } return; } else { FoxEventLog.VerboseWriteEventLog("Cannot upload req for " + LocalFilename + " ID: " + ServerID.ToString() + ": File has been changed!", System.Diagnostics.EventLogEntryType.Warning); } return; } try { if (fs == null) { fs = File.Open(LocalFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); } } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot upload file " + LocalFilename + " ID: " + ServerID.ToString() + " failed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Warning); return; } try { fs.Seek(CurrentSZ, SeekOrigin.Begin); } catch { FoxEventLog.WriteEventLog("Cannot properly seek in file " + LocalFilename + " ID: " + ServerID.ToString() + " Pos: 0x" + CurrentSZ.ToString("X"), System.Diagnostics.EventLogEntryType.Warning); return; } byte[] data = null; try { int read = 1024 * 1024; data = new byte[read]; read = fs.Read(data, 0, read); if (data.Length != read) { byte[] ddd = new byte[read]; Array.Copy(data, ddd, read); data = ddd; } } catch { FoxEventLog.WriteEventLog("Cannot read file " + LocalFilename + " ID: " + ServerID.ToString() + " Pos: 0x" + CurrentSZ.ToString("X"), System.Diagnostics.EventLogEntryType.Warning); return; } Status.UpdateMessage(7, "Uploading " + LocalFilename + "\r\n" + CommonUtilities.NiceSize(CurrentSZ) + " of " + CommonUtilities.NiceSize(TotalSZ)); bool res = downloadnet.File_Agent_AppendUpload(ServerID, data); if (res == false) { FoxEventLog.VerboseWriteEventLog("Cannot upload append req for " + LocalFilename + " ID: " + ServerID.ToString(), System.Diagnostics.EventLogEntryType.Warning); return; } CurrentSZ += data.Length; lock (FileLock) { FilesystemData.FileTransferStatus.ProgressSize = CurrentSZ; FilesystemData.WriteFileTransferStatus(); } if (CurrentSZ == TotalSZ) { FoxEventLog.WriteEventLog("Upload file success " + LocalFilename + " ID: " + ServerID.ToString(), System.Diagnostics.EventLogEntryType.Information); lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); } UnlockTimer = true; return; } } finally { if (fs != null) { fs.Close(); fs = null; } } } while (true); } } catch (Exception ee) { FoxEventLog.WriteEventLog("Downloading file " + LocalFilename + " ID: " + ServerID.ToString() + " crashed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } finally { Status.UpdateMessage(7); } } #endregion #region Server to Client if (Direction == 0) { string LocalFilename; string MD5; Int64 ServerID; Int64 CurrentSZ; Int64 TotalSZ; bool OverrideMetered; lock (FileLock) { LocalFilename = FilesystemData.FileTransferStatus.RemoteFileLocation; ServerID = FilesystemData.FileTransferStatus.ServerID.Value; CurrentSZ = FilesystemData.FileTransferStatus.ProgressSize; TotalSZ = FilesystemData.FileTransferStatus.Size; MD5 = FilesystemData.FileTransferStatus.MD5CheckSum; OverrideMetered = FilesystemData.FileTransferStatus.OverrideMeteredConnection; } //won't start when in metered connection! if (OverrideMetered == false) { try { if (MeteredConnection.IsMeteredConnection() == true) { FoxEventLog.VerboseWriteEventLog("Download paused = metered connection detected", System.Diagnostics.EventLogEntryType.Information); return; } } catch { } } Int64 ReallyCurrentSZ = 0; try { string Dir = Path.GetDirectoryName(LocalFilename); if (Directory.Exists(Dir) == false) { Directory.CreateDirectory(Dir); } if (File.Exists(LocalFilename) == true) { FileInfo fi = new FileInfo(LocalFilename); ReallyCurrentSZ = fi.Length; } else { ReallyCurrentSZ = 0; } } catch (Exception ee) { FoxEventLog.WriteEventLog("Checking download file " + LocalFilename + " ID: " + ServerID.ToString() + " failed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } if (ReallyCurrentSZ != CurrentSZ) { FoxEventLog.WriteEventLog("File Size does not match: deleting the file " + LocalFilename, System.Diagnostics.EventLogEntryType.Warning); try { File.Delete(LocalFilename); } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); return; } } downloadnet = Utilities.ConnectNetwork(7); if (downloadnet == null) { return; } try { downloadnet.DownloadNotify += Downloadnet_DownloadNotify; if (downloadnet.DownloadFile("api/agent/filefiledownload/" + ServerID.ToString(), LocalFilename, TotalSZ) == false) { FileInfo fi = new FileInfo(LocalFilename); lock (FileLock) { FilesystemData.FileTransferStatus.ProgressSize = fi.Length; FilesystemData.WriteFileTransferStatus(); } } else { if (downloadnet.StopDownload == false) { Status.UpdateMessage(7, "Checking " + FilesystemData.FileTransferStatus.RemoteFileLocation + "..."); string CalcMD5 = MD5Utilities.CalcMD5File(LocalFilename); if (MD5.ToLower() != CalcMD5.ToLower()) { FoxEventLog.WriteEventLog("File MD5 does not match: deleting the file " + LocalFilename, System.Diagnostics.EventLogEntryType.Warning); try { File.Delete(LocalFilename); } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); return; } } else { //success! FoxEventLog.WriteEventLog("File download success: " + LocalFilename, System.Diagnostics.EventLogEntryType.Information); downloadnet.File_Agent_CancelUpload(ServerID); lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); } UnlockTimer = true; return; } } else { if (CancelAndDeleteDL == true) { FoxEventLog.VerboseWriteEventLog("File " + LocalFilename + " canceled by server req.", System.Diagnostics.EventLogEntryType.Information); try { File.Delete(LocalFilename); } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); return; } } } } } catch (Exception ee) { FoxEventLog.WriteEventLog("Downloading file " + LocalFilename + " ID: " + ServerID.ToString() + " crashed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } finally { downloadnet.DownloadNotify -= Downloadnet_DownloadNotify; Status.UpdateMessage(7); } } #endregion } finally { try { if (downloadnet != null) { downloadnet.CloseConnection(); } } catch { } downloadnet = null; } }
static void DownloadThready(object dd) { if (!(dd is UploadDownloadData)) { return; } try { UploadDownloadData d = (UploadDownloadData)dd; Debug.Assert(d.UploadID != null); #region Download Code HttpWebRequest client = (HttpWebRequest)WebRequest.Create(net.ConnectedURL + "api/agent/filefiledownload/" + d.UploadID.Value.ToString()); client.Pipelined = false; client.ServicePoint.Expect100Continue = false; client.AllowAutoRedirect = true; if (net.Session != "") { client.Headers.Add("Authorization", "Bearer " + net.Session); } #if DEBUG client.ReadWriteTimeout = 5000; client.Timeout = 5000; #else client.ReadWriteTimeout = 60000; client.Timeout = 60000; #endif client.UserAgent = "FoxSDC Client"; client.Method = "GET"; Int64 SeekTo = 0; FileMode fm = FileMode.Create; if (File.Exists(d.RemoteFilename) == true) { FileInfo f = new FileInfo(d.RemoteFilename); SeekTo = f.Length; fm = FileMode.Open; } using (Stream FileStream = File.Open(d.RemoteFilename, fm, FileAccess.ReadWrite, FileShare.Read)) { FileStream.Seek(SeekTo, SeekOrigin.Begin); HttpWebResponse resp = (HttpWebResponse)client.GetResponse(); using (Stream HTTPStream = resp.GetResponseStream()) { const int ReadBufferSZ = 2048; d.ProgressSize = 0; StopDownload = false; byte[] data = new byte[ReadBufferSZ]; int ReadSZ = HTTPStream.Read(data, 0, ReadBufferSZ); while (ReadSZ > 0) { d.ProgressSize += ReadSZ; FileStream.Write(data, 0, ReadSZ); ReadSZ = HTTPStream.Read(data, 0, ReadBufferSZ); if (StopDownload == true) { break; } } } } if (StopDownload == true) { return; } if (MD5Utilities.CalcMD5File(d.RemoteFilename).ToLower() != d.MD5CheckSum.ToLower()) { File.Delete(d.RemoteFilename); d.Failed = true; d.ErrorText = "MD5 mismatch"; d.DownloadThread = null; return; } net.File_MGMT_CancelUpload(d.UploadID.Value); d.DownloadThread = null; lock (DataLock) { Data.Remove(d); } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); } #endregion }
public RESTStatus AppendUpload(SQLLib sql, FileUploadAppendData upload, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } if (upload == null) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } lock (ni.sqllock) { if (Computers.MachineExists(sql, upload.MachineID) == false) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } } if (upload.Data.Length != upload.Size) { ni.Error = "Invalid SZ"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(upload.MD5) == true) { ni.Error = "No MD5"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (upload.MD5.ToLower() != MD5Utilities.CalcMD5(upload.Data).ToLower()) { ni.Error = "MD5 Error"; ni.ErrorID = ErrorFlags.CheckSumError; return(RESTStatus.Fail); } lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM FileTransfers WHERE ID=@id AND MachineID=@mid AND [Size]!=[ProgressSize] AND Direction=3", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))) == 0) { ni.Error = "Invalid ID"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } } Int64 TotalSZ; Int64 ProgressSize; lock (ni.sqllock) { TotalSZ = Convert.ToInt64(sql.ExecSQLScalar("SELECT [Size] FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } lock (ni.sqllock) { ProgressSize = Convert.ToInt64(sql.ExecSQLScalar("SELECT ProgressSize FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (ProgressSize + upload.Size > TotalSZ) { ni.Error = "Too many data"; ni.ErrorID = ErrorFlags.ChunkTooLarge; return(RESTStatus.Fail); } string Filename = null; lock (ni.sqllock) { Filename = Settings.Default.DataPath + Convert.ToString(sql.ExecSQLScalar("SELECT ServerFile FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (File.Exists(Filename) == false) { if (ProgressSize > 0) { ni.Error = "FS Error - Missing"; ni.ErrorID = ErrorFlags.FileSystemError; return(RESTStatus.Fail); } } using (FileStream str = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { str.Seek(0, SeekOrigin.End); str.Write(upload.Data, 0, upload.Size); } FileInfo fi = new FileInfo(Filename); if (ProgressSize + upload.Size != fi.Length) { ni.Error = "FS Error - Final SZ Error"; ni.ErrorID = ErrorFlags.FileSystemError; return(RESTStatus.Fail); } lock (ni.sqllock) { sql.ExecSQL("UPDATE FileTransfers SET ProgressSize=@psz, DTUpdated=getutcdate() WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@psz", fi.Length), new SQLParam("@id", upload.ID)); } if (fi.Length == TotalSZ) { string MD5 = MD5Utilities.CalcMD5File(Filename); string MD5DB = ""; lock (ni.sqllock) { MD5DB = Convert.ToString(sql.ExecSQLScalar("SELECT MD5Sum FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (MD5.ToLower() != MD5DB.ToLower()) { ni.Error = "Final MD5 error"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } lock (ni.sqllock) { sql.ExecSQL("UPDATE FileTransfers SET Direction=0 WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID)); } } return(RESTStatus.Success); }
static void Thready() { do { UploadDownloadData d = null; lock (DataLock) { if (Data.Count == 0) { d = null; Thread.Sleep(500); continue; } foreach (UploadDownloadData dd in Data) { if (dd.Failed == false) { d = dd; } } } if (d == null) { Thread.Sleep(1000); } else { //check if another download thread is running foreach (UploadDownloadData dd in Data) { if (dd.ID == d.ID) { continue; } if (dd.DownloadThread != null) { StopDownload = true; dd.DownloadThread.Join(60000); dd.DownloadThread = null; } } //process element switch (d.Direction) { case Direction.DownloadFromServer: if (d.DownloadThread != null) { if (d.DownloadThread.IsAlive == true) { Thread.Sleep(1000); break; } } Debug.Assert(d.MD5CheckSum != null); Debug.Assert(d.RemoteFilename != null); if (d.ProgressSize != null) { if (d.ProgressSize != d.Size) { d.ProgressSize = null; } } if (d.UploadID == null) { d.Failed = true; d.ErrorText = "Missing Download ID"; continue; } try { // u.RemoteFilename - here // u.LocalFilename - agent if (d.DownloadThread == null) { StopDownload = false; d.DownloadThread = new Thread(new ParameterizedThreadStart(DownloadThready)); d.DownloadThread.Start(d); } } catch (Exception ee) { d.Failed = true; d.ErrorText = "Cannot start download: " + ee.Message; continue; } finally { } break; case Direction.UploadToServer: if (string.IsNullOrWhiteSpace(d.MD5CheckSum) == true) { d.MD5CheckSum = MD5Utilities.CalcMD5File(d.LocalFilename); } else { if (d.FileStream == null) { try { d.FileStream = File.Open(d.LocalFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); } catch (Exception ee) { d.Failed = true; d.ErrorText = "Cannot open " + d.LocalFilename + ": " + ee.Message; continue; } } else { #region Upload code if (d.UploadID == null) { d.UploadID = net.File_MGMT_NewUploadReq(d.LocalFilename, d.RemoteFilename, d.MachineID, d.MD5CheckSum, d.IgnoreMeteredConnection); if (d.UploadID == null) { d.FileStream.Close(); d.FileStream = null; d.Failed = true; d.ErrorText = net.GetLastError(); continue; } d.ProgressSize = 0; } else { int read = 1024 * 1024; byte[] data = new byte[read]; read = d.FileStream.Read(data, 0, read); if (data.Length != read) { byte[] ddd = new byte[read]; Array.Copy(data, ddd, read); data = ddd; } bool res = net.File_MGMT_AppendUpload(d.MachineID, d.UploadID.Value, data); if (res == false) { d.FileStream.Close(); d.FileStream = null; d.Failed = true; d.ErrorText = net.GetLastError(); net.File_MGMT_CancelUpload(d.UploadID.Value); continue; } d.ProgressSize += read; if (d.ProgressSize == d.Size) { d.FileStream.Close(); d.FileStream = null; lock (DataLock) { Data.Remove(d); } } } #endregion } } break; } } } while (CancelThread == false); lock (DataLock) { foreach (UploadDownloadData d in Data) { if (d.UploadID != null) { switch (d.Direction) { case Direction.UploadToServer: net.File_MGMT_CancelUpload(d.UploadID.Value); break; case Direction.DownloadFromServer: // u.RemoteFilename - here // u.LocalFilename - agent try { File.Delete(d.RemoteFilename); } catch { } break; } } } } ThreadClosed = true; }