Skip to content

Commit a8685e4

Browse files
committed
(chocolatey#996) Create config directory if it doesn't exist
If the directory for a LiteDatabase doesn't exist, LiteDatabase will not attempt to create it, and will throw an exception because it can't create the file without the parent directory. This ensures that the folder exists before trying to create the database.
1 parent 86e6f63 commit a8685e4

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,21 @@ protected override void Load(ContainerBuilder builder)
148148
var userDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.LocalAppDataPath, "data.db")};upgrade=true");
149149

150150
LiteDatabase globalDatabase;
151+
var globalConfigDir = Path.Combine(Bootstrapper.AppDataPath, "Config");
152+
var globalConfigDatabaseFile = Path.Combine(globalConfigDir, "data.db");
153+
151154
if (Hacks.IsElevated)
152155
{
153-
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};upgrade=true");
156+
if (!Directory.Exists(globalConfigDir))
157+
{
158+
Directory.CreateDirectory(globalConfigDir);
159+
}
160+
161+
globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};upgrade=true");
154162
}
155163
else
156164
{
157-
if (!File.Exists(Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")))
165+
if (!File.Exists(globalConfigDatabaseFile))
158166
{
159167
// Since the global configuration database file doesn't exist, we must be running in a state where an administrator user
160168
// has never run Chocolatey GUI. In this case, use null, which will mean attempts to use the global database will be ignored.
@@ -163,7 +171,7 @@ protected override void Load(ContainerBuilder builder)
163171
else
164172
{
165173
// Since this is a non-administrator user, they should only have read permissions to this database
166-
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};readonly=true");
174+
globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};readonly=true");
167175
}
168176
}
169177

Source/ChocolateyGuiCli/Startup/ChocolateyGuiCliModule.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ protected override void Load(ContainerBuilder builder)
4848
var userDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.LocalAppDataPath, "data.db")};upgrade=true");
4949

5050
LiteDatabase globalDatabase;
51+
var globalConfigDir = Path.Combine(Bootstrapper.AppDataPath, "Config");
52+
var globalConfigDatabaseFile = Path.Combine(globalConfigDir, "data.db");
53+
5154
if (Hacks.IsElevated)
5255
{
53-
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};upgrade=true");
56+
globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};upgrade=true");
5457
}
5558
else
5659
{
57-
if (!File.Exists(Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")))
60+
if (!File.Exists(globalConfigDatabaseFile))
5861
{
5962
// Since the global configuration database file doesn't exist, we must be running in a state where an administrator user
6063
// has never run Chocolatey GUI. In this case, use null, which will mean attempts to use the global database will be ignored.
@@ -63,7 +66,7 @@ protected override void Load(ContainerBuilder builder)
6366
else
6467
{
6568
// Since this is a non-administrator user, they should only have read permissions to this database
66-
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};readonly=true");
69+
globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};readonly=true");
6770
}
6871
}
6972

0 commit comments

Comments
 (0)