-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
130 lines (122 loc) · 4.86 KB
/
Database.cs
File metadata and controls
130 lines (122 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Data;
using Microsoft.Data.Sqlite;
using System.IO;
namespace LogFileCollector
{
/// <summary>
/// SQLite persistence wrapper.
/// Schema:
/// CREATE TABLE IF NOT EXISTS Copied(
/// FullPath TEXT NOT NULL,
/// LastWriteTimeUtc INTEGER NOT NULL, -- ticks
/// Length INTEGER NOT NULL,
/// PRIMARY KEY(FullPath, LastWriteTimeUtc, Length)
/// );
/// </summary>
public class Database
{
private readonly string _dbPath;
private readonly string _connectionString;
public Database(string dbPath)
{
_dbPath = dbPath;
_connectionString = "Data Source=" + _dbPath + ";Cache=Shared";
EnsureSchema();
}
private void EnsureSchema()
{
Directory.CreateDirectory(Path.GetDirectoryName(_dbPath) ?? ".");
using (var conn = new SqliteConnection(_connectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText =
"CREATE TABLE IF NOT EXISTS Copied(" +
" FullPath TEXT NOT NULL," +
" LastWriteTimeUtc INTEGER NOT NULL," +
" Length INTEGER NOT NULL," +
" PRIMARY KEY(FullPath, LastWriteTimeUtc, Length)" +
");";
cmd.ExecuteNonQuery();
}
}
}
/// <summary>
/// Returns true if a file with the same identity has already been copied.
/// Identity = (FullPath, LastWriteTimeUtc, Length)
/// </summary>
public bool IsFileAlreadyCopied(string fullPath, DateTime lastWriteUtc, long length)
{
using (var conn = new SqliteConnection(_connectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT 1 FROM Copied WHERE FullPath=$p AND LastWriteTimeUtc=$t AND Length=$l LIMIT 1;";
cmd.Parameters.AddWithValue("$p", fullPath);
cmd.Parameters.AddWithValue("$t", lastWriteUtc.Ticks);
cmd.Parameters.AddWithValue("$l", length);
object o = cmd.ExecuteScalar();
return o != null;
}
}
}
/// <summary>
/// Inserts a record for the provided file identity.
/// </summary>
public void MarkFileCopied(string fullPath, DateTime lastWriteUtc, long length)
{
using (var conn = new SqliteConnection(_connectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "INSERT OR IGNORE INTO Copied(FullPath, LastWriteTimeUtc, Length) VALUES($p,$t,$l);";
cmd.Parameters.AddWithValue("$p", fullPath);
cmd.Parameters.AddWithValue("$t", lastWriteUtc.Ticks);
cmd.Parameters.AddWithValue("$l", length);
cmd.ExecuteNonQuery();
}
}
}
/// <summary>
/// Generates a unique path in the target directory when a name collision happens.
/// Strategies:
/// - counter => file_1.ext, file_2.ext
/// - timestamp => file_yyyyMMdd_HHmmssfff.ext
/// - guid => file_XXXXXXXX.ext
/// </summary>
public string GetUniqueTargetPath(string targetFolder, string fileName, string renameStrategy)
{
string name = Path.GetFileNameWithoutExtension(fileName);
string ext = Path.GetExtension(fileName);
string candidate = Path.Combine(targetFolder, fileName);
if (!File.Exists(candidate)) return candidate;
string strategy = (renameStrategy ?? "counter").Trim().ToLowerInvariant();
if (strategy == "timestamp")
{
string ts = DateTime.UtcNow.ToString("yyyyMMdd_HHmmssfff");
return Path.Combine(targetFolder, name + "_" + ts + ext);
}
else if (strategy == "guid")
{
string g = Guid.NewGuid().ToString("N").Substring(0, 8);
return Path.Combine(targetFolder, name + "_" + g + ext);
}
else
{
// counter (default)
int i = 1;
while (true)
{
candidate = Path.Combine(targetFolder, string.Format("{0}_{1}{2}", name, i, ext));
if (!File.Exists(candidate)) return candidate;
i++;
if (i > 1000000) throw new IOException("Could not find a unique file name.");
}
}
}
}
}