mirror of
https://github.com/claunia/osrepodbmgr.git
synced 2025-12-16 19:14:25 +00:00
Add folders to database. This way empty folders get
replicated.
This commit is contained in:
@@ -1,3 +1,11 @@
|
|||||||
|
2017-05-12 Natalia Portillo <claunia@claunia.com>
|
||||||
|
|
||||||
|
* DBOps.cs:
|
||||||
|
* Context.cs:
|
||||||
|
* Workers.cs:
|
||||||
|
Add folders to database. This way empty folders get
|
||||||
|
replicated.
|
||||||
|
|
||||||
2017-05-11 Natalia Portillo <claunia@claunia.com>
|
2017-05-11 Natalia Portillo <claunia@claunia.com>
|
||||||
|
|
||||||
* Context.cs:
|
* Context.cs:
|
||||||
|
|||||||
@@ -35,7 +35,9 @@ namespace osrepodbmgr.Core
|
|||||||
public static class Context
|
public static class Context
|
||||||
{
|
{
|
||||||
public static List<string> files;
|
public static List<string> files;
|
||||||
|
public static List<string> folders;
|
||||||
public static Dictionary<string, DBFile> hashes;
|
public static Dictionary<string, DBFile> hashes;
|
||||||
|
public static Dictionary<string, DBFolder> foldersDict;
|
||||||
public static string path;
|
public static string path;
|
||||||
public static DBEntry dbInfo;
|
public static DBEntry dbInfo;
|
||||||
public static bool unarUsable;
|
public static bool unarUsable;
|
||||||
|
|||||||
@@ -66,6 +66,16 @@ namespace osrepodbmgr.Core
|
|||||||
public FileAttributes Attributes;
|
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
|
public class DBOps
|
||||||
{
|
{
|
||||||
readonly IDbConnection dbCon;
|
readonly IDbConnection dbCon;
|
||||||
@@ -354,6 +364,61 @@ namespace osrepodbmgr.Core
|
|||||||
return true;
|
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)
|
public bool RemoveOS(long id)
|
||||||
{
|
{
|
||||||
IDbCommand dbcmd = dbCon.CreateCommand();
|
IDbCommand dbcmd = dbCon.CreateCommand();
|
||||||
@@ -372,6 +437,18 @@ namespace osrepodbmgr.Core
|
|||||||
trans = dbCon.BeginTransaction();
|
trans = dbCon.BeginTransaction();
|
||||||
dbcmd.Transaction = trans;
|
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);
|
sql = string.Format("DELETE FROM oses WHERE id = '{0}';", id);
|
||||||
|
|
||||||
dbcmd.CommandText = sql;
|
dbcmd.CommandText = sql;
|
||||||
@@ -390,17 +467,38 @@ namespace osrepodbmgr.Core
|
|||||||
dbcmd.Transaction = trans;
|
dbcmd.Transaction = trans;
|
||||||
|
|
||||||
string sql = string.Format("DROP TABLE IF EXISTS `os_{0}`;\n\n" +
|
string sql = string.Format("DROP TABLE IF EXISTS `os_{0}`;\n\n" +
|
||||||
"CREATE TABLE IF NOT EXISTS `os_{0}` (\n" +
|
"CREATE TABLE IF NOT EXISTS `os_{0}` (\n" +
|
||||||
" `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
|
" `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
|
||||||
" `path` VARCHAR(8192) NOT NULL,\n" +
|
" `path` VARCHAR(8192) NOT NULL,\n" +
|
||||||
" `sha256` VARCHAR(64) NOT NULL,\n\n" +
|
" `sha256` VARCHAR(64) NOT NULL,\n\n" +
|
||||||
" `length` BIGINT NOT NULL,\n" +
|
" `length` BIGINT NOT NULL,\n" +
|
||||||
" `creation` DATETIME NULL,\n" +
|
" `creation` DATETIME NULL,\n" +
|
||||||
" `access` DATETIME NULL,\n" +
|
" `access` DATETIME NULL,\n" +
|
||||||
" `modification` DATETIME NULL,\n" +
|
" `modification` DATETIME NULL,\n" +
|
||||||
" `attributes` INTEGER NULL);\n\n" +
|
" `attributes` INTEGER NULL);\n\n" +
|
||||||
"CREATE UNIQUE INDEX `os_{0}_id_UNIQUE` ON `os_{0}` (`id` ASC);\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 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;
|
dbcmd.CommandText = sql;
|
||||||
|
|
||||||
|
|||||||
@@ -94,6 +94,8 @@ namespace osrepodbmgr.Core
|
|||||||
{
|
{
|
||||||
Context.files = new List<string>(Directory.EnumerateFiles(filesPath, "*", SearchOption.AllDirectories));
|
Context.files = new List<string>(Directory.EnumerateFiles(filesPath, "*", SearchOption.AllDirectories));
|
||||||
Context.files.Sort();
|
Context.files.Sort();
|
||||||
|
Context.folders = new List<string>(Directory.EnumerateDirectories(filesPath, "*", SearchOption.AllDirectories));
|
||||||
|
Context.folders.Sort();
|
||||||
if(Finished != null)
|
if(Finished != null)
|
||||||
Finished();
|
Finished();
|
||||||
}
|
}
|
||||||
@@ -111,6 +113,7 @@ namespace osrepodbmgr.Core
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Context.hashes = new Dictionary<string, DBFile>();
|
Context.hashes = new Dictionary<string, DBFile>();
|
||||||
|
Context.foldersDict = new Dictionary<string, DBFolder>();
|
||||||
List<string> alreadyMetadata = new List<string>();
|
List<string> alreadyMetadata = new List<string>();
|
||||||
bool foundMetadata = false;
|
bool foundMetadata = false;
|
||||||
|
|
||||||
@@ -396,6 +399,33 @@ namespace osrepodbmgr.Core
|
|||||||
counter++;
|
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)
|
if(foundMetadata)
|
||||||
{
|
{
|
||||||
Context.metadata = new CICMMetadataType();
|
Context.metadata = new CICMMetadataType();
|
||||||
@@ -611,6 +641,17 @@ namespace osrepodbmgr.Core
|
|||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
counter = 0;
|
||||||
|
foreach(KeyValuePair<string, DBFolder> 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)
|
if(Finished != null)
|
||||||
Finished();
|
Finished();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user