Adds files to an OS table.

This commit is contained in:
2017-04-23 06:09:57 +01:00
parent 551133b76c
commit e23744c4f8
7 changed files with 171 additions and 16 deletions

View File

@@ -1,3 +1,13 @@
2017-04-23 Natalia Portillo <claunia@claunia.com>
* Core.cs:
* DBOps.cs:
* DBCore.cs:
* SQLite.cs:
* Program.cs:
* MainWindow.cs:
Adds files to an OS table.
2017-04-23 Natalia Portillo <claunia@claunia.com>
* DBOps.cs:

View File

@@ -100,11 +100,12 @@ namespace osrepodbmgr
{
try
{
MainClass.hashes = new Dictionary<string, string>();
MainClass.hashes = new Dictionary<string, DBFile>();
long counter = 1;
foreach(string file in MainClass.files)
{
string filesPath;
FileInfo fi = new FileInfo(file);
if(!string.IsNullOrEmpty(MainClass.tmpFolder) && Directory.Exists(MainClass.tmpFolder))
filesPath = MainClass.tmpFolder;
@@ -148,7 +149,17 @@ namespace osrepodbmgr
fileStream.Close();
string hash = stringify(sha256Context.Final());
MainClass.hashes.Add(relpath, hash);
DBFile dbFile = new DBFile();
dbFile.Attributes = fi.Attributes;
dbFile.CreationTimeUtc = fi.CreationTimeUtc;
dbFile.LastAccessTimeUtc = fi.LastAccessTimeUtc;
dbFile.LastWriteTimeUtc = fi.LastWriteTimeUtc;
dbFile.Length = fi.Length;
dbFile.Path = relpath;
dbFile.Sha256 = hash;
MainClass.hashes.Add(relpath, dbFile);
counter++;
}
if(Finished != null)
@@ -168,13 +179,13 @@ namespace osrepodbmgr
try
{
long counter = 0;
foreach(KeyValuePair<string, string> kvp in MainClass.hashes)
foreach(KeyValuePair<string, DBFile> kvp in MainClass.hashes)
{
if(UpdateProgress != null)
UpdateProgress(null, "Checking files in database", counter, MainClass.hashes.Count);
if(AddEntry != null)
AddEntry(kvp.Key, kvp.Value, dbCore.DBEntries.ExistsFile(kvp.Value));
AddEntry(kvp.Key, kvp.Value.Sha256, dbCore.DBOps.ExistsFile(kvp.Value.Sha256));
counter++;
}
@@ -195,20 +206,35 @@ namespace osrepodbmgr
try
{
long counter = 0;
foreach(KeyValuePair<string, string> kvp in MainClass.hashes)
foreach(KeyValuePair<string, DBFile> kvp in MainClass.hashes)
{
if(UpdateProgress != null)
UpdateProgress(null, "Adding files to database", counter, MainClass.hashes.Count);
if(!dbCore.DBEntries.ExistsFile(kvp.Value))
dbCore.DBEntries.AddFile(kvp.Value);
if(!dbCore.DBOps.ExistsFile(kvp.Value.Sha256))
dbCore.DBOps.AddFile(kvp.Value.Sha256);
counter++;
}
if(UpdateProgress != null)
UpdateProgress(null, "Adding OS information", counter, MainClass.hashes.Count);
dbCore.DBEntries.AddOS(MainClass.dbInfo);
long osId;
dbCore.DBOps.AddOS(MainClass.dbInfo, out osId);
if(UpdateProgress != null)
UpdateProgress(null, "Creating OS table", counter, MainClass.hashes.Count);
dbCore.DBOps.CreateTableForOS(osId);
counter = 0;
foreach(KeyValuePair<string, DBFile> kvp in MainClass.hashes)
{
if(UpdateProgress != null)
UpdateProgress(null, "Adding files to OS in database", counter, MainClass.hashes.Count);
dbCore.DBOps.AddFileToOS(kvp.Value, osId);
counter++;
}
if(Finished != null)
Finished();

View File

@@ -37,8 +37,13 @@ namespace osrepodbmgr
public abstract bool CreateDB(string database, string server, string user, string password);
public DBOps DBEntries;
public DBOps DBOps;
public abstract IDbDataAdapter GetNewDataAdapter();
public abstract long LastInsertRowId
{
get;
}
}
}

View File

@@ -28,6 +28,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
namespace osrepodbmgr
{
@@ -50,6 +51,18 @@ namespace osrepodbmgr
public bool netinstall;
}
public struct DBFile
{
public ulong Id;
public string Path;
public string Sha256;
public long Length;
public DateTime CreationTimeUtc;
public DateTime LastAccessTimeUtc;
public DateTime LastWriteTimeUtc;
public FileAttributes Attributes;
}
public class DBOps
{
readonly IDbConnection dbCon;
@@ -180,7 +193,7 @@ namespace osrepodbmgr
return dbcmd;
}
public bool AddOS(DBEntry entry)
public bool AddOS(DBEntry entry, out long id)
{
IDbCommand dbcmd = GetOSCommand(entry);
IDbTransaction trans = dbCon.BeginTransaction();
@@ -195,6 +208,8 @@ namespace osrepodbmgr
trans.Commit();
dbcmd.Dispose();
id = dbCore.LastInsertRowId;
return true;
}
@@ -249,6 +264,100 @@ namespace osrepodbmgr
return false;
}
IDbCommand GetFileCommand(DBFile person)
{
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 = "@path";
param2.ParameterName = "@sha256";
param3.ParameterName = "@length";
param4.ParameterName = "@creation";
param5.ParameterName = "@access";
param6.ParameterName = "@modification";
param7.ParameterName = "@attributes";
param1.DbType = DbType.String;
param2.DbType = DbType.String;
param3.DbType = DbType.String;
param4.DbType = DbType.String;
param5.DbType = DbType.String;
param6.DbType = DbType.String;
param7.DbType = DbType.Int32;
param1.Value = person.Path;
param2.Value = person.Sha256;
param3.Value = person.Length;
param4.Value = person.CreationTimeUtc.ToString("yyyy-MM-dd HH:mm");
param5.Value = person.LastAccessTimeUtc.ToString("yyyy-MM-dd HH:mm");
param6.Value = person.LastWriteTimeUtc.ToString("yyyy-MM-dd HH:mm");
param7.Value = (int)person.Attributes;
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 AddFileToOS(DBFile file, long os)
{
IDbCommand dbcmd = GetFileCommand(file);
IDbTransaction trans = dbCon.BeginTransaction();
dbcmd.Transaction = trans;
string sql = string.Format("INSERT INTO `os_{0}` (`path`, `sha256`, `length`, `creation`, `access`, `modification`, `attributes`)" +
" VALUES (@path, @sha256, @length, @creation, @access, @modification, @attributes)", os);
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
trans.Commit();
dbcmd.Dispose();
return true;
}
public bool CreateTableForOS(long id)
{
IDbCommand dbcmd = dbCon.CreateCommand();
IDbTransaction trans = dbCon.BeginTransaction();
dbcmd.Transaction = trans;
string sql = string.Format("DROP TABLE IF EXISTS `os_{0}`;\n\n" +
"CREATE TABLE IF NOT EXISTS `os_{0}` (\n"+
" `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
" `path` VARCHAR(8192) NOT NULL,\n" +
" `sha256` VARCHAR(64) NOT NULL,\n\n" +
" `length` BIGINT NOT NULL,\n" +
" `creation` DATETIME NULL,\n" +
" `access` DATETIME NULL,\n" +
" `modification` DATETIME NULL,\n" +
" `attributes` INTEGER NULL);\n\n" +
"CREATE UNIQUE INDEX `os_{0}_id_UNIQUE` ON `os_{0}` (`id` ASC);\n\n" +
"CREATE INDEX `os_{0}_path_idx` ON `os_{0}` (`path` ASC);", id);
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
trans.Commit();
dbcmd.Dispose();
return true;
}
}
}

View File

@@ -612,10 +612,10 @@ public partial class MainWindow : Window
long counter = 0;
view.Clear();
foreach(KeyValuePair<string, string> kvp in MainClass.hashes)
foreach(KeyValuePair<string, DBFile> kvp in MainClass.hashes)
{
UpdateProgress(null, "Updating table", counter, MainClass.hashes.Count);
view.AppendValues(kvp.Key, kvp.Value, true, "green", "black");
view.AppendValues(kvp.Key, kvp.Value.Path, true, "green", "black");
counter++;
}
@@ -741,7 +741,7 @@ public partial class MainWindow : Window
btnPack.Sensitive = false;
btnClose.Sensitive = true;
MessageDialog dlgMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Correctly packed to " + text);
MessageDialog dlgMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Correctly packed to " + text);
dlgMsg.Run();
dlgMsg.Destroy();
});

View File

@@ -35,7 +35,7 @@ namespace osrepodbmgr
static class MainClass
{
public static List<string> files;
public static Dictionary<string, string> hashes;
public static Dictionary<string, DBFile> hashes;
public static string path;
public static DBEntry dbInfo;
public static bool unarUsable;

View File

@@ -65,7 +65,7 @@ namespace osrepodbmgr
return false;
}
DBEntries = new DBOps(dbCon, this);
DBOps = new DBOps(dbCon, this);
return true;
}
@@ -83,7 +83,7 @@ namespace osrepodbmgr
if(dbCon != null)
dbCon.Close();
DBEntries = null;
DBOps = null;
}
public override bool CreateDB(string database, string server, string user, string password)
@@ -132,6 +132,11 @@ namespace osrepodbmgr
return new SQLiteDataAdapter();
}
public override long LastInsertRowId
{
get { return dbCon.LastInsertRowId;}
}
#endregion
}
}