diff --git a/osrepodbmgr.Core/ChangeLog b/osrepodbmgr.Core/ChangeLog index a01a2a7..a732043 100644 --- a/osrepodbmgr.Core/ChangeLog +++ b/osrepodbmgr.Core/ChangeLog @@ -1,3 +1,11 @@ +2017-05-12 Natalia Portillo + + * DBOps.cs: + * Context.cs: + * Workers.cs: + Add folders to database. This way empty folders get + replicated. + 2017-05-11 Natalia Portillo * Context.cs: diff --git a/osrepodbmgr.Core/Context.cs b/osrepodbmgr.Core/Context.cs index 1e07edc..52602da 100644 --- a/osrepodbmgr.Core/Context.cs +++ b/osrepodbmgr.Core/Context.cs @@ -35,7 +35,9 @@ namespace osrepodbmgr.Core public static class Context { public static List files; + public static List folders; public static Dictionary hashes; + public static Dictionary foldersDict; public static string path; public static DBEntry dbInfo; public static bool unarUsable; diff --git a/osrepodbmgr.Core/DBOps.cs b/osrepodbmgr.Core/DBOps.cs index 42eea7d..eda9159 100644 --- a/osrepodbmgr.Core/DBOps.cs +++ b/osrepodbmgr.Core/DBOps.cs @@ -66,6 +66,16 @@ namespace osrepodbmgr.Core public FileAttributes Attributes; } + public struct DBFolder + { + public ulong Id; + public string Path; + public DateTime CreationTimeUtc; + public DateTime LastAccessTimeUtc; + public DateTime LastWriteTimeUtc; + public FileAttributes Attributes; + } + public class DBOps { readonly IDbConnection dbCon; @@ -354,6 +364,61 @@ namespace osrepodbmgr.Core return true; } + IDbCommand GetFolderCommand(DBFolder person) + { + IDbCommand dbcmd = dbCon.CreateCommand(); + + IDbDataParameter param1 = dbcmd.CreateParameter(); + IDbDataParameter param4 = dbcmd.CreateParameter(); + IDbDataParameter param5 = dbcmd.CreateParameter(); + IDbDataParameter param6 = dbcmd.CreateParameter(); + IDbDataParameter param7 = dbcmd.CreateParameter(); + + param1.ParameterName = "@path"; + param4.ParameterName = "@creation"; + param5.ParameterName = "@access"; + param6.ParameterName = "@modification"; + param7.ParameterName = "@attributes"; + + param1.DbType = DbType.String; + param4.DbType = DbType.String; + param5.DbType = DbType.String; + param6.DbType = DbType.String; + param7.DbType = DbType.Int32; + + param1.Value = person.Path; + 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(param4); + dbcmd.Parameters.Add(param5); + dbcmd.Parameters.Add(param6); + dbcmd.Parameters.Add(param7); + + return dbcmd; + } + + public bool AddFolderToOS(DBFolder folder, long os) + { + IDbCommand dbcmd = GetFolderCommand(folder); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + string sql = string.Format("INSERT INTO `os_{0}_folders` (`path`, `creation`, `access`, `modification`, `attributes`)" + + " VALUES (@path, @creation, @access, @modification, @attributes)", os); + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + return true; + } + public bool RemoveOS(long id) { IDbCommand dbcmd = dbCon.CreateCommand(); @@ -372,6 +437,18 @@ namespace osrepodbmgr.Core trans = dbCon.BeginTransaction(); dbcmd.Transaction = trans; + sql = string.Format("DROP TABLE IF EXISTS `os_{0}_folders`;", id); + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + dbcmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + sql = string.Format("DELETE FROM oses WHERE id = '{0}';", id); dbcmd.CommandText = sql; @@ -390,17 +467,38 @@ namespace osrepodbmgr.Core 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); + "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(); + + dbcmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + sql = string.Format("DROP TABLE IF EXISTS `os_{0}_folders`;\n\n" + + "CREATE TABLE IF NOT EXISTS `os_{0}_folders` (\n" + + " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" + + " `path` VARCHAR(8192) 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}_folders_id_UNIQUE` ON `os_{0}_folders` (`id` ASC);\n\n" + + "CREATE INDEX `os_{0}_folders_path_idx` ON `os_{0}_folders` (`path` ASC);", id); dbcmd.CommandText = sql; diff --git a/osrepodbmgr.Core/Workers.cs b/osrepodbmgr.Core/Workers.cs index 12a2970..97df167 100644 --- a/osrepodbmgr.Core/Workers.cs +++ b/osrepodbmgr.Core/Workers.cs @@ -94,6 +94,8 @@ namespace osrepodbmgr.Core { Context.files = new List(Directory.EnumerateFiles(filesPath, "*", SearchOption.AllDirectories)); Context.files.Sort(); + Context.folders = new List(Directory.EnumerateDirectories(filesPath, "*", SearchOption.AllDirectories)); + Context.folders.Sort(); if(Finished != null) Finished(); } @@ -111,6 +113,7 @@ namespace osrepodbmgr.Core try { Context.hashes = new Dictionary(); + Context.foldersDict = new Dictionary(); List alreadyMetadata = new List(); bool foundMetadata = false; @@ -396,6 +399,33 @@ namespace osrepodbmgr.Core counter++; } + counter = 1; + foreach(string folder in Context.folders) + { + + string filesPath; + DirectoryInfo di = new DirectoryInfo(folder); + + if(!string.IsNullOrEmpty(Context.tmpFolder) && Directory.Exists(Context.tmpFolder)) + filesPath = Context.tmpFolder; + else + filesPath = Context.path; + + string relpath = folder.Substring(filesPath.Length + 1); + if(UpdateProgress != null) + UpdateProgress(string.Format("Checking folder {0} of {1}", counter, Context.folders.Count), null, counter, Context.folders.Count); + + DBFolder dbFolder = new DBFolder(); + dbFolder.Attributes = di.Attributes; + dbFolder.CreationTimeUtc = di.CreationTimeUtc; + dbFolder.LastAccessTimeUtc = di.LastAccessTimeUtc; + dbFolder.LastWriteTimeUtc = di.LastWriteTimeUtc; + dbFolder.Path = relpath; + + Context.foldersDict.Add(relpath, dbFolder); + counter++; + } + if(foundMetadata) { Context.metadata = new CICMMetadataType(); @@ -611,6 +641,17 @@ namespace osrepodbmgr.Core counter++; } + counter = 0; + foreach(KeyValuePair kvp in Context.foldersDict) + { + if(UpdateProgress != null) + UpdateProgress(null, "Adding folders to OS in database", counter, Context.foldersDict.Count); + + dbCore.DBOps.AddFolderToOS(kvp.Value, Context.dbInfo.id); + + counter++; + } + if(Finished != null) Finished(); }