Skip to content

Commit

Permalink
(#996) Create config directory if it doesn't exist
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
corbob committed Apr 22, 2024
1 parent 86e6f63 commit a8685e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
14 changes: 11 additions & 3 deletions Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,21 @@ protected override void Load(ContainerBuilder builder)
var userDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.LocalAppDataPath, "data.db")};upgrade=true");

LiteDatabase globalDatabase;
var globalConfigDir = Path.Combine(Bootstrapper.AppDataPath, "Config");
var globalConfigDatabaseFile = Path.Combine(globalConfigDir, "data.db");

if (Hacks.IsElevated)
{
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};upgrade=true");
if (!Directory.Exists(globalConfigDir))
{
Directory.CreateDirectory(globalConfigDir);
}

globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};upgrade=true");
}
else
{
if (!File.Exists(Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")))
if (!File.Exists(globalConfigDatabaseFile))
{
// Since the global configuration database file doesn't exist, we must be running in a state where an administrator user
// has never run Chocolatey GUI. In this case, use null, which will mean attempts to use the global database will be ignored.
Expand All @@ -163,7 +171,7 @@ protected override void Load(ContainerBuilder builder)
else
{
// Since this is a non-administrator user, they should only have read permissions to this database
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};readonly=true");
globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};readonly=true");
}
}

Expand Down
9 changes: 6 additions & 3 deletions Source/ChocolateyGuiCli/Startup/ChocolateyGuiCliModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ protected override void Load(ContainerBuilder builder)
var userDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.LocalAppDataPath, "data.db")};upgrade=true");

LiteDatabase globalDatabase;
var globalConfigDir = Path.Combine(Bootstrapper.AppDataPath, "Config");
var globalConfigDatabaseFile = Path.Combine(globalConfigDir, "data.db");

if (Hacks.IsElevated)
{
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};upgrade=true");
globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};upgrade=true");
}
else
{
if (!File.Exists(Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")))
if (!File.Exists(globalConfigDatabaseFile))
{
// Since the global configuration database file doesn't exist, we must be running in a state where an administrator user
// has never run Chocolatey GUI. In this case, use null, which will mean attempts to use the global database will be ignored.
Expand All @@ -63,7 +66,7 @@ protected override void Load(ContainerBuilder builder)
else
{
// Since this is a non-administrator user, they should only have read permissions to this database
globalDatabase = new LiteDatabase($"filename={Path.Combine(Bootstrapper.AppDataPath, "Config", "data.db")};readonly=true");
globalDatabase = new LiteDatabase($"filename={globalConfigDatabaseFile};readonly=true");
}
}

Expand Down

0 comments on commit a8685e4

Please sign in to comment.