diff --git a/osrepodbmgr.Core/Context.cs b/osrepodbmgr.Core/Context.cs index 8a80fe8..4a255ca 100644 --- a/osrepodbmgr.Core/Context.cs +++ b/osrepodbmgr.Core/Context.cs @@ -1,4 +1,4 @@ -// +// // Author: // Natalia Portillo claunia@claunia.com // @@ -36,7 +36,7 @@ namespace osrepodbmgr.Core { public static List files; public static List folders; - public static Dictionary hashes; + public static Dictionary hashes; public static Dictionary foldersDict; public static string path; public static DBEntry dbInfo; diff --git a/osrepodbmgr.Core/DBOps.cs b/osrepodbmgr.Core/DBOps.cs index 551e2e1..a348a9d 100644 --- a/osrepodbmgr.Core/DBOps.cs +++ b/osrepodbmgr.Core/DBOps.cs @@ -55,6 +55,18 @@ namespace osrepodbmgr.Core } public struct DBFile + { + public ulong Id; + public string Sha256; + public bool Crack; + public bool? VirusScanned; + public DateTime? ClamTime; + public DateTime? VirusTotalTime; + public string Virus; + public long Length; + } + + public struct DBOSFile { public ulong Id; public string Path; @@ -247,24 +259,67 @@ namespace osrepodbmgr.Core return true; } - public bool AddFile(string hash) + IDbCommand GetFileCommand(DBFile entry) { - //Console.WriteLine("Adding {0}", hash); IDbCommand dbcmd = dbCon.CreateCommand(); IDbDataParameter param1 = dbcmd.CreateParameter(); + IDbDataParameter param2 = dbcmd.CreateParameter(); + IDbDataParameter param3 = dbcmd.CreateParameter(); + IDbDataParameter param4 = dbcmd.CreateParameter(); + IDbDataParameter param5 = dbcmd.CreateParameter(); + IDbDataParameter param6 = dbcmd.CreateParameter(); + IDbDataParameter param7 = dbcmd.CreateParameter(); - param1.ParameterName = "@hash"; + param1.ParameterName = "@sha256"; + param2.ParameterName = "@crack"; + param3.ParameterName = "@virscan"; + param4.ParameterName = "@clamtime"; + param5.ParameterName = "@vtotaltime"; + param6.ParameterName = "@virus"; + param7.ParameterName = "@length"; param1.DbType = DbType.String; + param2.DbType = DbType.Boolean; + param3.DbType = DbType.String; + param4.DbType = DbType.String; + param5.DbType = DbType.String; + param7.DbType = DbType.UInt64; - param1.Value = hash; + param1.Value = entry.Sha256; + param2.Value = entry.Crack; + param3.Value = entry.VirusScanned; + if(entry.ClamTime != null) + param4.Value = entry.ClamTime.Value.ToString("yyyy-MM-dd HH:mm"); + else + param4.Value = null; + if(entry.VirusTotalTime != null) + param5.Value = entry.VirusTotalTime.Value.ToString("yyyy-MM-dd HH:mm"); + else + param5.Value = null; + param6.Value = entry.Virus; + param7.Value = entry.Length; dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + dbcmd.Parameters.Add(param3); + dbcmd.Parameters.Add(param4); + dbcmd.Parameters.Add(param5); + dbcmd.Parameters.Add(param6); + dbcmd.Parameters.Add(param7); + + return dbcmd; + } + + + public bool AddFile(DBFile file) + { + IDbCommand dbcmd = GetFileCommand(file); IDbTransaction trans = dbCon.BeginTransaction(); dbcmd.Transaction = trans; - const string sql = "INSERT INTO files (sha256) VALUES (@hash)"; + const string sql = "INSERT INTO `files` (`sha256`, `crack`, `virscan`, `clamtime`, `vtotaltime`, `virus`, `length`)" + + " VALUES (@sha256, @crack, @virscan, @clamtime, @vtotaltime, @virus, @length)"; dbcmd.CommandText = sql; @@ -299,7 +354,62 @@ namespace osrepodbmgr.Core return false; } - IDbCommand GetFileCommand(DBFile person) + public ulong GetFilesCount() + { + IDbCommand dbcmd = dbCon.CreateCommand(); + dbcmd.CommandText = "SELECT COUNT(*) FROM files"; + object count = dbcmd.ExecuteScalar(); + dbcmd.Dispose(); + try + { + return Convert.ToUInt64(count); + } + catch { return 0; } + } + + public bool GetFiles(out List entries, long start, long count) + { + entries = new List(); + + string sql = string.Format("SELECT * FROM files LIMIT {0}, {1} ORDER BY id", count, start); + + IDbCommand dbcmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbcmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbcmd; + dataAdapter.Fill(dataSet); + DataTable dataTable = dataSet.Tables[0]; + + foreach(DataRow dRow in dataTable.Rows) + { + DBFile fEntry = new DBFile(); + + fEntry.Id = ulong.Parse(dRow["id"].ToString()); + fEntry.Sha256 = dRow["sha256"].ToString(); + fEntry.Crack = bool.Parse(dRow["crack"].ToString()); + if(dRow["virscan"] == DBNull.Value) + fEntry.VirusScanned = null; + else + fEntry.VirusScanned = bool.Parse(dRow["virscan"].ToString()); + if(dRow["clamtime"] == DBNull.Value) + fEntry.ClamTime = null; + else + fEntry.ClamTime = DateTime.Parse(dRow["clamtime"].ToString()); + if(dRow["vtotaltime"] == DBNull.Value) + fEntry.VirusTotalTime = null; + else + fEntry.VirusTotalTime = DateTime.Parse(dRow["vtotaltime"].ToString()); + fEntry.Virus = dRow["virus"].ToString(); + fEntry.Length = long.Parse(dRow["length"].ToString()); + + entries.Add(fEntry); + } + + return true; + } + + IDbCommand GetOSFileCommand(DBOSFile person) { IDbCommand dbcmd = dbCon.CreateCommand(); @@ -346,9 +456,9 @@ namespace osrepodbmgr.Core return dbcmd; } - public bool AddFileToOS(DBFile file, long os) + public bool AddFileToOS(DBOSFile file, long os) { - IDbCommand dbcmd = GetFileCommand(file); + IDbCommand dbcmd = GetOSFileCommand(file); IDbTransaction trans = dbCon.BeginTransaction(); dbcmd.Transaction = trans; @@ -557,9 +667,9 @@ namespace osrepodbmgr.Core return false; } - public bool GetAllFiles(out List entries, long id) + public bool GetAllFilesInOS(out List entries, long id) { - entries = new List(); + entries = new List(); string sql = string.Format("SELECT * from os_{0}", id); @@ -573,7 +683,7 @@ namespace osrepodbmgr.Core foreach(DataRow dRow in dataTable.Rows) { - DBFile fEntry = new DBFile(); + DBOSFile fEntry = new DBOSFile(); fEntry.Id = ulong.Parse(dRow["id"].ToString()); fEntry.Path = dRow["path"].ToString(); fEntry.Sha256 = dRow["sha256"].ToString(); diff --git a/osrepodbmgr.Core/Schema.cs b/osrepodbmgr.Core/Schema.cs index fc0234b..1c5947e 100644 --- a/osrepodbmgr.Core/Schema.cs +++ b/osrepodbmgr.Core/Schema.cs @@ -35,9 +35,18 @@ namespace osrepodbmgr.Core "DROP TABLE IF EXISTS `files` ;\n\n" + "CREATE TABLE IF NOT EXISTS `files` (\n" + " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" + - " `sha256` VARCHAR(64) NOT NULL);\n\n" + + " `sha256` VARCHAR(64) NOT NULL,\n" + + " `crack` BOOLEAN NOT NULL,\n" + + " `virscan` BOOLEAN NULL,\n" + + " `clamtime` DATETIME NULL,\n" + + " `vtotaltime` DATETIME NULL,\n" + + " `virus` VARCHAR(128) NULL,\n" + + " `length` BIGINT NOT NULL);\n\n" + "CREATE UNIQUE INDEX `files_id_UNIQUE` ON `files` (`id` ASC);\n\n" + - "CREATE UNIQUE INDEX `files_sha256_UNIQUE` ON `files` (`sha256` ASC);"; + "CREATE UNIQUE INDEX `files_sha256_UNIQUE` ON `files` (`sha256` ASC);\n\n" + + "CREATE INDEX `files_virscan_idx` ON `files` (`virscan` ASC);\n\n" + + "CREATE INDEX `files_virus_idx` ON `files` (`virus` ASC);\n\n" + + "CREATE INDEX `files_length_idx` ON `files` (`length` ASC);"; public const string OSesTableSql = "-- -----------------------------------------------------\n" + "-- Table `oses`\n" + @@ -54,12 +63,12 @@ namespace osrepodbmgr.Core " `machine` VARCHAR(45) NULL,\n" + " `format` VARCHAR(45) NULL,\n" + " `description` VARCHAR(45) NULL,\n" + - " `oem` BOOLEAN NULL,\n" + - " `upgrade` BOOLEAN NULL,\n" + - " `update` BOOLEAN NULL,\n" + - " `source` BOOLEAN NULL,\n" + - " `files` BOOLEAN NULL,\n" + - " `netinstall` BOOLEAN NULL,\n" + + " `oem` BOOLEAN NOT NULL,\n" + + " `upgrade` BOOLEAN NOT NULL,\n" + + " `update` BOOLEAN NOT NULL,\n" + + " `source` BOOLEAN NOT NULL,\n" + + " `files` BOOLEAN NOT NULL,\n" + + " `netinstall` BOOLEAN NOT NULL,\n" + " `xml` BLOB NULL,\n" + " `json` BLOB NULL);\n\n" + "CREATE UNIQUE INDEX `oses_id_UNIQUE` ON `oses` (`id` ASC);\n\n" + diff --git a/osrepodbmgr.Core/Workers.cs b/osrepodbmgr.Core/Workers.cs index d2bdd45..37d0dea 100644 --- a/osrepodbmgr.Core/Workers.cs +++ b/osrepodbmgr.Core/Workers.cs @@ -1,4 +1,4 @@ -// +// // Author: // Natalia Portillo claunia@claunia.com // @@ -112,7 +112,7 @@ namespace osrepodbmgr.Core { try { - Context.hashes = new Dictionary(); + Context.hashes = new Dictionary(); Context.foldersDict = new Dictionary(); List alreadyMetadata = new List(); bool foundMetadata = false; @@ -375,7 +375,8 @@ namespace osrepodbmgr.Core fileStream.Read(dataBuffer, 0, (int)remainder); sha256Context.Update(dataBuffer); } - else { + else + { if(UpdateProgress2 != null) UpdateProgress2(string.Format("{0:P}", 0 / (double)fileStream.Length), relpath, 0, fileStream.Length); dataBuffer = new byte[fileStream.Length]; @@ -386,7 +387,7 @@ namespace osrepodbmgr.Core fileStream.Close(); string hash = stringify(sha256Context.Final()); - DBFile dbFile = new DBFile(); + DBOSFile dbFile = new DBOSFile(); dbFile.Attributes = fi.Attributes; dbFile.CreationTimeUtc = fi.CreationTimeUtc; dbFile.LastAccessTimeUtc = fi.LastAccessTimeUtc; @@ -531,7 +532,7 @@ namespace osrepodbmgr.Core try { long counter = 0; - foreach(KeyValuePair kvp in Context.hashes) + foreach(KeyValuePair kvp in Context.hashes) { if(UpdateProgress != null) UpdateProgress(null, "Checking files in database", counter, Context.hashes.Count); @@ -559,7 +560,7 @@ namespace osrepodbmgr.Core UpdateProgress(null, string.Format("Check OS id {0}", os.id), osCounter, osesArray.Length); counter = 0; - foreach(KeyValuePair kvp in Context.hashes) + foreach(KeyValuePair kvp in Context.hashes) { if(UpdateProgress2 != null) UpdateProgress2(null, string.Format("Checking for file {0}", kvp.Value.Path), counter, Context.hashes.Count); @@ -612,13 +613,17 @@ namespace osrepodbmgr.Core try { long counter = 0; - foreach(KeyValuePair kvp in Context.hashes) + foreach(KeyValuePair kvp in Context.hashes) { if(UpdateProgress != null) UpdateProgress(null, "Adding files to database", counter, Context.hashes.Count); if(!dbCore.DBOps.ExistsFile(kvp.Value.Sha256)) - dbCore.DBOps.AddFile(kvp.Value.Sha256); + dbCore.DBOps.AddFile(new DBFile + { + Sha256 = kvp.Value.Sha256, ClamTime = null, Crack = false, + Length = kvp.Value.Length, Virus = null, VirusScanned = null, VirusTotalTime = null + }); counter++; } @@ -631,7 +636,7 @@ namespace osrepodbmgr.Core dbCore.DBOps.CreateTableForOS(Context.dbInfo.id); counter = 0; - foreach(KeyValuePair kvp in Context.hashes) + foreach(KeyValuePair kvp in Context.hashes) { if(UpdateProgress != null) UpdateProgress(null, "Adding files to OS in database", counter, Context.hashes.Count); @@ -701,7 +706,8 @@ namespace osrepodbmgr.Core return; } } - else { + else + { if(!dbCore.CreateDB(Settings.Current.DatabasePath, null, null, null)) { if(Failed != null) @@ -879,7 +885,7 @@ namespace osrepodbmgr.Core break; } - foreach(KeyValuePair file in Context.hashes) + foreach(KeyValuePair file in Context.hashes) { if(UpdateProgress != null) UpdateProgress("Compressing...", file.Value.Path, counter, Context.hashes.Count); @@ -934,8 +940,8 @@ namespace osrepodbmgr.Core if(UpdateProgress2 != null) UpdateProgress2(string.Format("{0:P}", inFs.Length / (double)inFs.Length), - "Finishing...",inFs.Length, inFs.Length); - + "Finishing...", inFs.Length, inFs.Length); + inFs.Close(); zStream.Close(); } @@ -974,7 +980,7 @@ namespace osrepodbmgr.Core jms.Position = 0; } - if(FinishedWithText!= null) + if(FinishedWithText != null) FinishedWithText(string.Format("Correctly added operating system with MDID {0}", mdid)); } catch(Exception ex) @@ -1419,14 +1425,14 @@ namespace osrepodbmgr.Core return; } - List files; + List files; List folders; long counter; if(UpdateProgress != null) UpdateProgress("", "Asking DB for files...", 1, 100); - dbCore.DBOps.GetAllFiles(out files, Context.dbInfo.id); + dbCore.DBOps.GetAllFilesInOS(out files, Context.dbInfo.id); if(UpdateProgress != null) UpdateProgress("", "Asking DB for folders...", 2, 100); @@ -1452,7 +1458,7 @@ namespace osrepodbmgr.Core } counter = 3; - foreach(DBFile file in files) + foreach(DBOSFile file in files) { if(UpdateProgress != null) UpdateProgress("", string.Format("Creating {0}...", file.Path), counter, 3 + files.Count); @@ -1610,14 +1616,14 @@ namespace osrepodbmgr.Core zf.EmitTimesInWindowsFormatWhenSaving = true; zf.UseZip64WhenSaving = Zip64Option.AsNecessary; zf.SortEntriesBeforeSaving = true; - List files; + List files; List folders; long counter; if(UpdateProgress != null) UpdateProgress("", "Asking DB for files...", 1, 100); - dbCore.DBOps.GetAllFiles(out files, Context.dbInfo.id); + dbCore.DBOps.GetAllFilesInOS(out files, Context.dbInfo.id); if(UpdateProgress != null) UpdateProgress("", "Asking DB for folders...", 2, 100); @@ -1644,8 +1650,8 @@ namespace osrepodbmgr.Core } counter = 3; - Context.hashes = new Dictionary(); - foreach(DBFile file in files) + Context.hashes = new Dictionary(); + foreach(DBOSFile file in files) { if(UpdateProgress != null) UpdateProgress("", string.Format("Adding {0}...", file.Path), counter, 3 + files.Count); @@ -1677,10 +1683,10 @@ namespace osrepodbmgr.Core static Stream Zf_HandleOpen(string entryName) { - DBFile file; + DBOSFile file; if(!Context.hashes.TryGetValue(entryName, out file)) { - if (!Context.hashes.TryGetValue(entryName.Replace('/', '\\'), out file)) + if(!Context.hashes.TryGetValue(entryName.Replace('/', '\\'), out file)) throw new ArgumentException("Cannot find requested zip entry in hashes dictionary"); } @@ -1771,7 +1777,7 @@ namespace osrepodbmgr.Core if(e.EventType == ZipProgressEventType.Error_Saving && Failed != null) Failed("An error occurred creating ZIP file."); - + if(e.EventType == ZipProgressEventType.Saving_Completed && Finished != null) Finished(); } diff --git a/osrepodbmgr.Eto/dlgAdd.xeto.cs b/osrepodbmgr.Eto/dlgAdd.xeto.cs index 65e75e9..03b9c10 100644 --- a/osrepodbmgr.Eto/dlgAdd.xeto.cs +++ b/osrepodbmgr.Eto/dlgAdd.xeto.cs @@ -1,4 +1,4 @@ -// +// // Author: // Natalia Portillo claunia@claunia.com // @@ -798,7 +798,7 @@ namespace osrepodbmgr.Eto long counter = 0; fileView.Clear(); - foreach(KeyValuePair kvp in Context.hashes) + foreach(KeyValuePair kvp in Context.hashes) { UpdateProgress(null, "Updating table", counter, Context.hashes.Count); fileView.Add(new FileEntry { path = kvp.Key, hash = kvp.Value.Sha256, known = true }); diff --git a/osrepodbmgr.Eto/dlgMetadata.xeto.cs b/osrepodbmgr.Eto/dlgMetadata.xeto.cs index 9584ea1..3121296 100644 --- a/osrepodbmgr.Eto/dlgMetadata.xeto.cs +++ b/osrepodbmgr.Eto/dlgMetadata.xeto.cs @@ -1,4 +1,4 @@ -// +// // Author: // Natalia Portillo claunia@claunia.com // @@ -258,7 +258,7 @@ namespace osrepodbmgr.Eto void FillFilesCombos() { - foreach(KeyValuePair files in Context.hashes) + foreach(KeyValuePair files in Context.hashes) lstFilesForMedia.Add(files.Key); } diff --git a/osrepodbmgr/dlgAdd.cs b/osrepodbmgr/dlgAdd.cs index d653b5b..5f3d97f 100644 --- a/osrepodbmgr/dlgAdd.cs +++ b/osrepodbmgr/dlgAdd.cs @@ -1,4 +1,4 @@ -// +// // Author: // Natalia Portillo claunia@claunia.com // @@ -786,7 +786,7 @@ public partial class dlgAdd : Dialog long counter = 0; fileView.Clear(); - foreach(KeyValuePair kvp in Context.hashes) + foreach(KeyValuePair kvp in Context.hashes) { UpdateProgress(null, "Updating table", counter, Context.hashes.Count); fileView.AppendValues(kvp.Key, kvp.Value.Sha256, true, "green", "black"); diff --git a/osrepodbmgr/dlgMetadata.cs b/osrepodbmgr/dlgMetadata.cs index 4cc448f..2507624 100644 --- a/osrepodbmgr/dlgMetadata.cs +++ b/osrepodbmgr/dlgMetadata.cs @@ -1,4 +1,4 @@ -// +// // Author: // Natalia Portillo claunia@claunia.com // @@ -217,7 +217,7 @@ namespace osrepodbmgr void FillFilesCombos() { - foreach(KeyValuePair files in Context.hashes) + foreach(KeyValuePair files in Context.hashes) { lstFilesForDisc.AppendValues(files.Key); lstFilesForDisk.AppendValues(files.Key); @@ -228,7 +228,7 @@ namespace osrepodbmgr { // TODO: Check that files are not already added as disks lstFilesForDisc.Clear(); - foreach(KeyValuePair files in Context.hashes) + foreach(KeyValuePair files in Context.hashes) lstFilesForDisc.AppendValues(files.Key); } @@ -236,7 +236,7 @@ namespace osrepodbmgr { // TODO: Check that files are not already added as discs lstFilesForDisk.Clear(); - foreach(KeyValuePair files in Context.hashes) + foreach(KeyValuePair files in Context.hashes) lstFilesForDisk.AppendValues(files.Key); }