Skip to content

Commit

Permalink
Resolve segfault when running 'onedrive --display-sync-status' when r…
Browse files Browse the repository at this point in the history
…un as 2nd process (#2105)

* Rather than force exit if unable to lock the database, add a function and boolean to control if the database access has been init was successful. If not, use the exit scopes to exit the application
  • Loading branch information
abraunegg authored Aug 30, 2022
1 parent 3b7a06c commit 5288f94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/itemdb.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class ItemDatabase
string selectItemByIdStmt;
string selectItemByParentIdStmt;
string deleteItemByIdStmt;
bool databaseInitialised = false;

this(const(char)[] filename)
{
Expand All @@ -61,7 +62,7 @@ final class ItemDatabase
log.error("ERROR: An internal database error occurred: " ~ e.msg);
writeln();
}
exit(-1);
return;
}

if (dbVersion == 0) {
Expand Down Expand Up @@ -114,6 +115,14 @@ final class ItemDatabase
";
selectItemByParentIdStmt = "SELECT * FROM item WHERE driveId = ? AND parentId = ?";
deleteItemByIdStmt = "DELETE FROM item WHERE driveId = ? AND id = ?";

// flag that the database is accessible and we have control
databaseInitialised = true;
}

bool isDatabaseInitialised()
{
return databaseInitialised;
}

void createTable()
Expand Down
10 changes: 9 additions & 1 deletion src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,14 @@ int main(string[] args)
log.vdebug("Using database file: ", asNormalizedPath(cfg.databaseFilePath));
itemDb = new ItemDatabase(cfg.databaseFilePath);
}

// did we successfully initialise the database class?
if (!itemDb.isDatabaseInitialised()) {
// no .. destroy class
itemDb = null;
// exit application
return EXIT_FAILURE;
}

// What are the permission that have been set for the application?
// These are relevant for:
Expand Down Expand Up @@ -1901,7 +1909,7 @@ extern(C) nothrow @nogc @system void exitHandler(int value) {
oneDrive.shutdown();
}
// was itemDb initialised?
if (itemDb !is null) {
if (itemDb.isDatabaseInitialised()) {
// Make sure the .wal file is incorporated into the main db before we exit
log.log("Shutting down db connection and merging temporary data");
itemDb.performVacuum();
Expand Down

0 comments on commit 5288f94

Please sign in to comment.