2023-08-27 01:35:51 +02:00
// What is this module called?
module main ;
// What does this module require to function?
2018-12-28 03:19:20 +01:00
import core.stdc.stdlib : EXIT_SUCCESS , EXIT_FAILURE , exit ;
import core.stdc.signal ;
2023-08-27 01:35:51 +02:00
import core.time ;
import core.thread ;
import std.stdio ;
import std.getopt ;
import std.string ;
import std.file ;
import std.process ;
import std.algorithm ;
import std.path ;
import std.concurrency ;
import std.parallelism ;
import std.conv ;
import std.traits ;
import std.net.curl : CurlException ;
2022-10-12 04:34:42 +02:00
import std.datetime ;
2018-12-28 03:19:20 +01:00
2023-08-27 01:35:51 +02:00
// What other modules that we have created do we need to import?
import config ;
import log ;
import curlEngine ;
import util ;
import onedrive ;
import syncEngine ;
import itemdb ;
import clientSideFiltering ;
import monitor ;
// What other constant variables do we require?
2022-08-17 05:19:51 +02:00
const int EXIT_RESYNC_REQUIRED = 126 ;
2019-07-06 00:01:04 +02:00
2023-08-27 01:35:51 +02:00
// Class objects
ApplicationConfig appConfig ;
OneDriveApi oneDriveApiInstance ;
SyncEngine syncEngineInstance ;
ItemDatabase itemDB ;
ClientSideFiltering selectiveSync ;
2019-02-26 21:21:23 +01:00
2023-08-27 01:35:51 +02:00
int main ( string [ ] cliArgs ) {
// Disable buffering on stdout - this is needed so that when we are using plain write() it will go to the terminal
2018-12-23 01:15:10 +01:00
stdout . setvbuf ( 0 , _IONBF ) ;
2023-08-27 01:35:51 +02:00
// Required main function variables
string genericHelpMessage = "Try 'onedrive --help' for more information" ;
// If the user passes in --confdir we need to store this as a variable
2019-04-11 04:26:20 +02:00
string confdirOption ;
2023-08-27 01:35:51 +02:00
// Are we online?
2020-05-20 03:37:11 +02:00
bool online = false ;
2023-08-27 01:35:51 +02:00
// Does the operating environment have shell environment variables set
bool shellEnvSet = false ;
// What is the runtime syncronisation directory that will be used
// Typically this will be '~/OneDrive' .. however tilde expansion is unreliable
string runtimeSyncDirectory ;
// Configure the runtime database file path. Typically this will be the default, but in a --dry-run scenario, we use a separate database file
string runtimeDatabaseFile ;
2023-08-29 01:56:55 +02:00
// Application Start Time - used during monitor loop to detail how long it has been running for
auto applicationStartTime = Clock . currTime ( ) ;
2023-08-27 01:35:51 +02:00
// DEVELOPER OPTIONS OUTPUT VARIABLES
2020-05-20 03:37:11 +02:00
bool displayMemoryUsage = false ;
2020-05-25 03:30:04 +02:00
bool displaySyncOptions = false ;
2022-05-19 08:12:53 +02:00
2023-08-27 01:35:51 +02:00
// Define 'exit' and 'failure' scopes
2020-05-20 03:37:11 +02:00
scope ( exit ) {
2022-11-18 22:53:49 +01:00
// detail what scope was called
2023-09-08 22:34:52 +02:00
log . vdebug ( "Exit scope was called" ) ;
2023-08-27 01:35:51 +02:00
// Was itemDB initialised?
if ( itemDB ! is null ) {
2020-11-06 00:28:15 +01:00
// Make sure the .wal file is incorporated into the main db before we exit
2023-08-27 01:35:51 +02:00
itemDB . performVacuum ( ) ;
destroy ( itemDB ) ;
2023-09-08 22:34:52 +02:00
itemDB = null ;
2020-05-20 03:37:11 +02:00
}
}
2023-08-27 01:35:51 +02:00
2020-05-20 03:37:11 +02:00
scope ( failure ) {
2022-11-18 22:53:49 +01:00
// detail what scope was called
2023-09-08 22:34:52 +02:00
log . vdebug ( "Failure scope was called" ) ;
2023-08-27 01:35:51 +02:00
// Was itemDB initialised?
if ( itemDB ! is null ) {
2020-11-06 00:28:15 +01:00
// Make sure the .wal file is incorporated into the main db before we exit
2023-08-27 01:35:51 +02:00
itemDB . performVacuum ( ) ;
destroy ( itemDB ) ;
2023-09-08 22:34:52 +02:00
itemDB = null ;
2020-05-20 03:37:11 +02:00
}
}
2023-08-27 01:35:51 +02:00
// Read in application options as passed in
2019-01-26 01:03:00 +01:00
try {
2019-04-11 04:26:20 +02:00
bool printVersion = false ;
2023-08-27 01:35:51 +02:00
auto cliOptions = getopt (
cliArgs ,
2019-04-11 04:26:20 +02:00
std . getopt . config . passThrough ,
2019-01-26 01:03:00 +01:00
std . getopt . config . bundling ,
std . getopt . config . caseSensitive ,
2019-04-11 04:26:20 +02:00
"confdir" , "Set the directory used to store the configuration files" , & confdirOption ,
2019-03-17 05:10:41 +01:00
"verbose|v+" , "Print more details, useful for debugging (repeat for extra debugging)" , & log . verbose ,
2019-01-26 01:03:00 +01:00
"version" , "Print the version and exit" , & printVersion
) ;
2022-01-27 22:58:38 +01:00
2023-08-27 01:35:51 +02:00
// Print help and exit
if ( cliOptions . helpWanted ) {
cliArgs ~ = "--help" ;
2019-04-11 04:26:20 +02:00
}
2023-08-27 01:35:51 +02:00
// Print the version and exit
2019-04-11 04:26:20 +02:00
if ( printVersion ) {
2023-08-27 01:42:40 +02:00
//writeln("onedrive ", strip(import("version")));
2023-09-02 23:38:36 +02:00
string tempVersion = "v2.5.0-alpha-0" ~ " GitHub version: " ~ strip ( import ( "version" ) ) ;
writeln ( tempVersion ) ;
2019-01-26 01:03:00 +01:00
return EXIT_SUCCESS ;
}
} catch ( GetOptException e ) {
2023-08-27 01:35:51 +02:00
// Option errors
2019-01-26 01:03:00 +01:00
log . error ( e . msg ) ;
2023-08-27 01:35:51 +02:00
log . error ( genericHelpMessage ) ;
2019-01-26 01:03:00 +01:00
return EXIT_FAILURE ;
} catch ( Exception e ) {
2023-08-27 01:35:51 +02:00
// Generic error
2019-01-26 01:03:00 +01:00
log . error ( e . msg ) ;
2023-08-27 01:35:51 +02:00
log . error ( genericHelpMessage ) ;
2019-01-26 01:03:00 +01:00
return EXIT_FAILURE ;
}
2023-08-27 01:35:51 +02:00
// How was this application started - what options were passed in
log . vdebug ( "passed in options: " , cliArgs ) ;
log . vdebug ( "note --confdir and --verbose not listed in 'cliArgs'" ) ;
// Create a new AppConfig object with default values,
appConfig = new ApplicationConfig ( ) ;
// Initialise the application configuration, utilising --confdir if it was passed in
// Otherwise application defaults will be used to configure the application
if ( ! appConfig . initialize ( confdirOption ) ) {
// There was an error loading the user specified application configuration
2019-04-11 04:26:20 +02:00
// Error message already printed
return EXIT_FAILURE ;
2019-04-01 19:51:25 +02:00
}
2022-01-27 22:58:38 +01:00
2023-08-27 01:35:51 +02:00
// Update the existing application configuration (default or 'config' file) from any passed in command line arguments
appConfig . updateFromArgs ( cliArgs ) ;
2022-01-27 22:58:38 +01:00
2023-08-27 01:35:51 +02:00
// Configure Client Side Filtering (selective sync) by parsing and getting a usable regex for skip_file, skip_dir and sync_list config components
selectiveSync = new ClientSideFiltering ( appConfig ) ;
if ( ! selectiveSync . initialise ( ) ) {
// exit here as something triggered a selective sync configuration failure
return EXIT_FAILURE ;
2020-06-27 11:10:37 +02:00
}
2022-12-05 20:02:41 +01:00
2023-08-27 01:35:51 +02:00
// Set runtimeDatabaseFile, this will get updated if we are using --dry-run
runtimeDatabaseFile = appConfig . databaseFilePath ;
2023-09-08 23:36:24 +02:00
// Expand any ~ in the configuration for our operational environment. Read in 'sync_dir' from appConfig
2023-08-27 01:35:51 +02:00
runtimeSyncDirectory = updateTildeConfigDirectives ( appConfig . getValueString ( "sync_dir" ) ) ;
// DEVELOPER OPTIONS OUTPUT
// Set to display memory details as early as possible
displayMemoryUsage = appConfig . getValueBool ( "display_memory" ) ;
// set to display sync options
displaySyncOptions = appConfig . getValueBool ( "display_sync_options" ) ;
// Display the current application configuration (based on all defaults, 'config' file parsing and/or options passed in via the CLI) and exit if --display-config has been used
if ( ( appConfig . getValueBool ( "display_config" ) ) | | ( appConfig . getValueBool ( "display_running_config" ) ) ) {
// Display the application configuration
appConfig . displayApplicationConfiguration ( ) ;
// Do we exit? We exit only if '--display-config' has been used
if ( appConfig . getValueBool ( "display_config" ) ) {
return EXIT_SUCCESS ;
2019-08-24 09:18:58 +02:00
}
}
2023-08-27 01:35:51 +02:00
// Check for basic application option conflicts - flags that should not be used together and/or flag combinations that conflict with each other
if ( appConfig . checkForBasicOptionConflicts ) {
// Any error will have been printed by the function itself
return EXIT_FAILURE ;
2019-08-24 09:18:58 +02:00
}
2023-08-27 01:35:51 +02:00
// Check for --dry-run operation
// If this has been requested, we need to ensure that all actions are performed against the dry-run database copy, and,
// no actual action takes place - such as deleting files if deleted online, moving files if moved online or local, downloading new & changed files, uploading new & changed files
if ( appConfig . getValueBool ( "dry_run" ) ) {
// this is a --dry-run operation
log . log ( "DRY-RUN Configured. Output below shows what 'would' have occurred." ) ;
runtimeDatabaseFile = appConfig . databaseFilePathDryRun ;
2022-08-26 01:11:10 +02:00
2023-08-27 01:35:51 +02:00
// Cleanup any existing dry-run elements ... these should never be left hanging around
cleanupDryRunDatabaseFiles ( runtimeDatabaseFile ) ;
2022-08-05 01:16:54 +02:00
2019-03-11 07:57:47 +01:00
// Make a copy of the original items.sqlite3 for use as the dry run copy if it exists
2023-08-27 01:35:51 +02:00
if ( exists ( appConfig . databaseFilePath ) ) {
// In a --dry-run --resync scenario, we should not copy the existing database file
if ( ! appConfig . getValueBool ( "resync" ) ) {
// Copy the existing DB file to the dry-run copy
log . log ( "DRY-RUN: Copying items.sqlite3 to items-dryrun.sqlite3 to use for dry run operations" ) ;
copy ( appConfig . databaseFilePath , runtimeDatabaseFile ) ;
2021-02-06 10:46:56 +01:00
} else {
2023-08-27 01:35:51 +02:00
// No database copy due to --resync
log . log ( "DRY-RUN: No database copy created for --dry-run due to --resync also being used" ) ;
2021-02-06 10:46:56 +01:00
}
2019-03-11 07:57:47 +01:00
}
2016-12-25 19:23:33 +01:00
}
2023-08-27 01:35:51 +02:00
2022-03-07 19:35:00 +01:00
// Handle --logout as separate item, do not 'resync' on a --logout
2023-08-27 01:35:51 +02:00
if ( appConfig . getValueBool ( "logout" ) ) {
2021-11-16 20:05:11 +01:00
log . vdebug ( "--logout requested" ) ;
log . log ( "Deleting the saved authentication status ..." ) ;
2023-08-27 01:35:51 +02:00
if ( ! appConfig . getValueBool ( "dry_run" ) ) {
safeRemove ( appConfig . refreshTokenFilePath ) ;
} else {
// --dry-run scenario ... technically we should not be making any local file changes .......
log . log ( "DRY RUN: Not removing the saved authentication status" ) ;
2021-11-16 20:05:11 +01:00
}
// Exit
return EXIT_SUCCESS ;
}
2022-03-07 19:35:00 +01:00
// Handle --reauth to re-authenticate the client
2023-08-27 01:35:51 +02:00
if ( appConfig . getValueBool ( "reauth" ) ) {
2022-03-07 19:35:00 +01:00
log . vdebug ( "--reauth requested" ) ;
log . log ( "Deleting the saved authentication status ... re-authentication requested" ) ;
2023-08-27 01:35:51 +02:00
if ( ! appConfig . getValueBool ( "dry_run" ) ) {
safeRemove ( appConfig . refreshTokenFilePath ) ;
} else {
// --dry-run scenario ... technically we should not be making any local file changes .......
log . log ( "DRY RUN: Not removing the saved authentication status" ) ;
2022-03-07 19:35:00 +01:00
}
}
2023-08-27 01:35:51 +02:00
// --resync should be considered a 'last resort item' or if the application configuration has changed, where a resync is needed .. the user needs to 'accept' this warning to proceed
// If --resync has not been used (bool value is false), check the application configuration for 'changes' that require a --resync to ensure that the data locally reflects the users requested configuration
if ( appConfig . getValueBool ( "resync" ) ) {
// what is the risk acceptance for --resync?
bool resyncRiskAcceptance = appConfig . displayResyncRiskForAcceptance ( ) ;
log . vdebug ( "Returned Risk Acceptance: " , resyncRiskAcceptance ) ;
// Action based on user response
if ( ! resyncRiskAcceptance ) {
// --resync risk not accepted
return EXIT_FAILURE ;
2018-12-19 19:42:28 +01:00
} else {
2023-08-27 01:35:51 +02:00
log . vdebug ( "--resync issued and risk accepted" ) ;
// --resync risk accepted, perform a cleanup of items that require a cleanup
appConfig . cleanupHashFilesDueToResync ( ) ;
// Make a backup of the applicable configuration file
appConfig . createBackupConfigFile ( ) ;
// Update hash files and generate a new config backup
appConfig . updateHashContentsForConfigFiles ( ) ;
// Remove the items database
processResyncDatabaseRemoval ( runtimeDatabaseFile ) ;
2018-12-19 19:42:28 +01:00
}
2023-08-27 01:35:51 +02:00
} else {
// Has any of our application configuration that would require a --resync been changed?
if ( appConfig . applicationChangeWhereResyncRequired ( ) ) {
// Application configuration has changed however --resync not issued, fail fast
log . error ( "\nAn application configuration change has been detected where a --resync is required\n" ) ;
return EXIT_RESYNC_REQUIRED ;
2020-06-27 11:10:37 +02:00
} else {
2023-08-27 01:35:51 +02:00
// No configuration change that requires a --resync to be issued
// Make a backup of the applicable configuration file
appConfig . createBackupConfigFile ( ) ;
// Update hash files and generate a new config backup
appConfig . updateHashContentsForConfigFiles ( ) ;
2022-05-05 20:48:50 +02:00
}
}
2020-12-17 20:17:52 +01:00
// Test if OneDrive service can be reached, exit if it cant be reached
2020-03-14 20:29:44 +01:00
log . vdebug ( "Testing network to ensure network connectivity to Microsoft OneDrive Service" ) ;
2023-08-27 01:35:51 +02:00
online = testInternetReachability ( appConfig ) ;
// If we are not 'online' - how do we handle this situation?
2020-12-17 20:17:52 +01:00
if ( ! online ) {
2023-08-27 01:35:51 +02:00
// We are unable to initialise the OneDrive API as we are not online
if ( ! appConfig . getValueBool ( "monitor" ) ) {
2020-12-17 20:17:52 +01:00
// Running as --synchronize
2023-08-27 01:35:51 +02:00
log . error ( "Unable to reach Microsoft OneDrive API service, unable to initialise application\n" ) ;
2018-12-04 01:15:44 +01:00
return EXIT_FAILURE ;
2020-12-17 20:17:52 +01:00
} else {
// Running as --monitor
2023-08-27 01:35:51 +02:00
log . error ( "Unable to reach the Microsoft OneDrive API service at this point in time, re-trying network tests based on applicable intervals\n" ) ;
if ( ! retryInternetConnectivtyTest ( appConfig ) ) {
2020-12-17 20:17:52 +01:00
return EXIT_FAILURE ;
}
2018-12-04 01:15:44 +01:00
}
2022-03-07 04:02:50 +01:00
}
2023-08-27 01:35:51 +02:00
// This needs to be a separate 'if' statement, as, if this was an 'if-else' from above, if we were originally offline and using --monitor, we would never get to this point
2022-03-07 04:02:50 +01:00
if ( online ) {
2022-03-07 00:57:20 +01:00
// Check Application Version
log . vlog ( "Checking Application Version ..." ) ;
checkApplicationVersion ( ) ;
2023-08-27 01:35:51 +02:00
// Initialise the OneDrive API
log . vlog ( "Attempting to initialise the OneDrive API ..." ) ;
oneDriveApiInstance = new OneDriveApi ( appConfig ) ;
appConfig . apiWasInitialised = oneDriveApiInstance . initialise ( ) ;
if ( appConfig . apiWasInitialised ) {
log . vlog ( "The OneDrive API was initialised successfully" ) ;
// Flag that we were able to initalise the API in the application config
oneDriveApiInstance . debugOutputConfiguredAPIItems ( ) ;
// are we doing a --sync or a --monitor operation?
if ( ( appConfig . getValueBool ( "synchronize" ) ) | | ( appConfig . getValueBool ( "monitor" ) ) ) {
log . vlog ( "Opening the item database ..." ) ;
// Configure the Item Database
itemDB = new ItemDatabase ( runtimeDatabaseFile ) ;
// Was the database successfully initialised?
if ( ! itemDB . isDatabaseInitialised ( ) ) {
// no .. destroy class
itemDB = null ;
// exit application
return EXIT_FAILURE ;
}
// Initialise the syncEngine
syncEngineInstance = new SyncEngine ( appConfig , itemDB , selectiveSync ) ;
appConfig . syncEngineWasInitialised = syncEngineInstance . initialise ( ) ;
2020-11-20 20:46:05 +01:00
} else {
2023-08-27 01:35:51 +02:00
log . error ( "\n --sync or --monitor switches missing from your command line input. Please add one (not both) of these switches to your command line or use 'onedrive --help' for further assistance.\n" ) ;
log . error ( "No OneDrive sync will be performed without one of these two arguments being present.\n" ) ;
2022-11-18 22:53:49 +01:00
// Use exit scopes to shutdown API
2023-08-27 01:35:51 +02:00
// invalidSyncExit = true;
2022-11-18 22:53:49 +01:00
return EXIT_FAILURE ;
}
2023-09-02 04:27:10 +02:00
// We do not need this instance, as the API was initialised, and individual instances are used during sync process
oneDriveApiInstance . shutdown ( ) ;
2019-09-11 12:26:21 +02:00
} else {
2023-08-27 01:35:51 +02:00
// API could not be initialised
log . error ( "The OneDrive API could not be initialised" ) ;
2018-09-24 21:25:40 +02:00
return EXIT_FAILURE ;
}
2018-07-16 01:58:36 +02:00
}
2022-06-29 23:08:29 +02:00
2023-09-08 22:34:52 +02:00
// Configure the sync direcory based on the runtimeSyncDirectory configured directory
try {
if ( ! exists ( runtimeSyncDirectory ) ) {
log . vdebug ( "runtimeSyncDirectory: Configured 'sync_dir' is missing locally. Creating: " , runtimeSyncDirectory ) ;
try {
// Attempt to create the sync dir we have been configured with
mkdirRecurse ( runtimeSyncDirectory ) ;
// Configure the applicable permissions for the folder
log . vdebug ( "Setting directory permissions for: " , runtimeSyncDirectory ) ;
runtimeSyncDirectory . setAttributes ( appConfig . returnRequiredDirectoryPermisions ( ) ) ;
} catch ( std . file . FileException e ) {
// Creating the sync directory failed
log . error ( "ERROR: Unable to create local OneDrive 'sync_dir' - " , e . msg ) ;
// Use exit scopes to shutdown API
return EXIT_FAILURE ;
}
}
} catch ( std . file . FileException e ) {
// Creating the sync directory failed
log . error ( "ERROR: Unable to test the existence of the configured OneDrive 'sync_dir' - " , e . msg ) ;
// Use exit scopes to shutdown API
return EXIT_FAILURE ;
}
// Change the working directory to the 'sync_dir' as configured
2023-09-08 22:57:52 +02:00
log . log ( "All application operations will be performed in: " , runtimeSyncDirectory ) ;
2023-08-27 01:35:51 +02:00
chdir ( runtimeSyncDirectory ) ;
2022-10-12 04:34:42 +02:00
2023-08-27 01:35:51 +02:00
// Do we need to validate the runtimeSyncDirectory to check for the presence of a '.nosync' file
// If this is a 'mounted' folder, the 'mount point' should have this file to help the application stop any action to preserve data because the drive to mount is not currently mounted
if ( appConfig . getValueBool ( "check_nomount" ) ) {
// we were asked to check the mount point for the presence of a '.nosync' file
if ( exists ( ".nosync" ) ) {
log . logAndNotify ( "ERROR: .nosync file found. Aborting application startup process to safeguard data." ) ;
2018-06-17 00:27:43 +02:00
return EXIT_FAILURE ;
}
}
2022-03-09 21:00:07 +01:00
2023-08-27 01:35:51 +02:00
// Set the default thread pool value - hard coded to 16
defaultPoolThreads ( to ! int ( appConfig . concurrentThreads ) ) ;
2023-03-17 23:53:58 +01:00
2023-08-27 01:35:51 +02:00
// Is the sync engine initiallised correctly?
if ( appConfig . syncEngineWasInitialised ) {
// Configure some initial variables
string singleDirectoryPath ;
string localPath = "." ;
2018-12-28 02:26:03 +01:00
string remotePath = "/" ;
2023-08-27 01:35:51 +02:00
// Are we doing a single directory operation (--single-directory) ?
if ( ! appConfig . getValueString ( "single_directory" ) . empty ) {
// Set singleDirectoryPath
singleDirectoryPath = appConfig . getValueString ( "single_directory" ) ;
// Ensure that this is a normalised relative path to runtimeSyncDirectory
string normalisedRelativePath = replace ( buildNormalizedPath ( absolutePath ( singleDirectoryPath ) ) , buildNormalizedPath ( absolutePath ( runtimeSyncDirectory ) ) , "." ) ;
// The user provided a directory to sync within the configured 'sync_dir' path
// This also validates if the path being used exists online and/or does not have a 'case-insensitive match'
syncEngineInstance . setSingleDirectoryScope ( normalisedRelativePath ) ;
// Does the directory we want to sync actually exist locally?
if ( ! exists ( singleDirectoryPath ) ) {
// The requested path to use with --single-directory does not exist locally within the configured 'sync_dir'
log . logAndNotify ( "WARNING: The requested path for --single-directory does not exist locally. Creating requested path within " , runtimeSyncDirectory ) ;
// Make the required --single-directory path locally
mkdirRecurse ( singleDirectoryPath ) ;
// Configure the applicable permissions for the folder
log . vdebug ( "Setting directory permissions for: " , singleDirectoryPath ) ;
singleDirectoryPath . setAttributes ( appConfig . returnRequiredDirectoryPermisions ( ) ) ;
}
// Update the paths that we use to perform the sync actions
localPath = singleDirectoryPath ;
remotePath = singleDirectoryPath ;
// Display that we are syncing from a specific path due to --single-directory
log . vlog ( "Syncing changes from this selected path: " , singleDirectoryPath ) ;
2018-12-28 02:26:03 +01:00
}
2021-11-23 20:54:28 +01:00
2023-08-27 01:35:51 +02:00
// Are we doing a --sync operation? This includes doing any --single-directory operations
if ( appConfig . getValueBool ( "synchronize" ) ) {
// Did the user specify --upload-only?
if ( appConfig . getValueBool ( "upload_only" ) ) {
// Perform the --upload-only sync process
performUploadOnlySyncProcess ( localPath ) ;
}
// Did the user specify --download-only?
if ( appConfig . getValueBool ( "download_only" ) ) {
// Only download data from OneDrive
syncEngineInstance . syncOneDriveAccountToLocalDisk ( ) ;
2023-09-12 07:31:59 +02:00
// Perform the DB consistency check
// This will also delete any out-of-sync flagged items if configured to do so
syncEngineInstance . performDatabaseConsistencyAndIntegrityCheck ( ) ;
2023-08-27 01:35:51 +02:00
// Do we cleanup local files?
2023-09-12 07:31:59 +02:00
// - Deletes of data from online will already have been performed, but what we are now doing is searching the local filesystem
2023-08-27 01:35:51 +02:00
// for any new data locally, that usually would be uploaded to OneDrive, but instead, because of the options being
2023-09-12 07:31:59 +02:00
// used, will need to be deleted from the local filesystem
2023-08-27 01:35:51 +02:00
if ( appConfig . getValueBool ( "cleanup_local_files" ) ) {
// Perform the filesystem walk
syncEngineInstance . scanLocalFilesystemPathForNewData ( localPath ) ;
2018-03-14 05:43:40 +01:00
}
2015-09-20 21:21:51 +02:00
}
2023-08-27 01:35:51 +02:00
// If no use of --upload-only or --download-only
if ( ( ! appConfig . getValueBool ( "upload_only" ) ) & & ( ! appConfig . getValueBool ( "download_only" ) ) ) {
// Perform the standard sync process
performStandardSyncProcess ( localPath ) ;
}
// Detail the outcome of the sync process
displaySyncOutcome ( ) ;
2018-03-14 05:43:40 +01:00
}
2023-08-27 01:35:51 +02:00
// Are we doing a --monitor operation?
if ( appConfig . getValueBool ( "monitor" ) ) {
2023-08-30 22:29:24 +02:00
// What are the current values for the platform we are running on
// Max number of open files /proc/sys/fs/file-max
string maxOpenFiles = strip ( readText ( "/proc/sys/fs/file-max" ) ) ;
// What is the currently configured maximum inotify watches that can be used
// /proc/sys/user/max_inotify_watches
string maxInotifyWatches = strip ( readText ( "/proc/sys/user/max_inotify_watches" ) ) ;
// Start the monitor process
2023-08-27 01:35:51 +02:00
log . log ( "OneDrive syncronisation interval (seconds): " , appConfig . getValueLong ( "monitor_interval" ) ) ;
2023-08-30 22:29:24 +02:00
log . vlog ( "Maximum allowed open files: " , maxOpenFiles ) ;
log . vlog ( "Maximum allowed inotify watches: " , maxInotifyWatches ) ;
2023-08-27 01:35:51 +02:00
// Configure the monitor class
Monitor filesystemMonitor = new Monitor ( appConfig , selectiveSync ) ;
// Delegated function for when inotify detects a new local directory has been created
filesystemMonitor . onDirCreated = delegate ( string path ) {
2020-04-06 12:05:06 +02:00
// Handle .folder creation if skip_dotfiles is enabled
2023-08-27 01:35:51 +02:00
if ( ( appConfig . getValueBool ( "skip_dotfiles" ) ) & & ( isDotFile ( path ) ) ) {
2022-04-21 00:19:24 +02:00
log . vlog ( "[M] Skipping watching local path - .folder found & --skip-dot-files enabled: " , path ) ;
2020-04-06 12:05:06 +02:00
} else {
2022-04-21 00:19:24 +02:00
log . vlog ( "[M] Local directory created: " , path ) ;
2020-04-06 12:05:06 +02:00
try {
2023-08-27 01:35:51 +02:00
syncEngineInstance . scanLocalFilesystemPathForNewData ( path ) ;
2020-04-06 12:05:06 +02:00
} catch ( CurlException e ) {
log . vlog ( "Offline, cannot create remote dir!" ) ;
} catch ( Exception e ) {
log . logAndNotify ( "Cannot create remote directory: " , e . msg ) ;
}
2018-03-14 05:43:40 +01:00
}
} ;
2023-08-27 01:35:51 +02:00
// Delegated function for when inotify detects a local file has been changed
filesystemMonitor . onFileChanged = delegate ( string path ) {
2022-04-21 00:19:24 +02:00
log . vlog ( "[M] Local file changed: " , path ) ;
2018-03-14 05:43:40 +01:00
try {
2023-08-27 06:22:06 +02:00
syncEngineInstance . scanLocalFilesystemPathForNewData ( dirName ( path ) ) ;
2019-02-21 09:51:15 +01:00
} catch ( CurlException e ) {
log . vlog ( "Offline, cannot upload changed item!" ) ;
2018-03-14 05:43:40 +01:00
} catch ( Exception e ) {
2018-12-06 08:05:52 +01:00
log . logAndNotify ( "Cannot upload file changes/creation: " , e . msg ) ;
2018-03-14 05:43:40 +01:00
}
} ;
2023-08-27 01:35:51 +02:00
// Delegated function for when inotify detects a delete event
filesystemMonitor . onDelete = delegate ( string path ) {
2022-04-21 00:19:24 +02:00
log . log ( "Received inotify delete event from operating system .. attempting item deletion as requested" ) ;
log . vlog ( "[M] Local item deleted: " , path ) ;
2018-03-14 05:43:40 +01:00
try {
2023-08-27 01:35:51 +02:00
syncEngineInstance . deleteByPath ( path ) ;
2019-02-21 09:51:15 +01:00
} catch ( CurlException e ) {
log . vlog ( "Offline, cannot delete item!" ) ;
2018-12-06 00:50:46 +01:00
} catch ( SyncException e ) {
if ( e . msg = = "The item to delete is not in the local database" ) {
2021-03-19 07:29:55 +01:00
log . vlog ( "Item cannot be deleted from OneDrive because it was not found in the local database" ) ;
2018-12-06 00:50:46 +01:00
} else {
2018-12-06 08:05:52 +01:00
log . logAndNotify ( "Cannot delete remote item: " , e . msg ) ;
2018-12-06 00:50:46 +01:00
}
2018-03-14 05:43:40 +01:00
} catch ( Exception e ) {
2018-12-06 08:05:52 +01:00
log . logAndNotify ( "Cannot delete remote item: " , e . msg ) ;
2018-03-14 05:43:40 +01:00
}
} ;
2023-08-27 01:35:51 +02:00
// Delegated function for when inotify detects a move event
filesystemMonitor . onMove = delegate ( string from , string to ) {
2022-04-21 00:19:24 +02:00
log . vlog ( "[M] Local item moved: " , from , " -> " , to ) ;
2018-03-14 05:43:40 +01:00
try {
2020-04-06 12:05:06 +02:00
// Handle .folder -> folder if skip_dotfiles is enabled
2023-08-27 01:35:51 +02:00
if ( ( appConfig . getValueBool ( "skip_dotfiles" ) ) & & ( isDotFile ( from ) ) ) {
2020-04-06 12:05:06 +02:00
// .folder -> folder handling - has to be handled as a new folder
2023-08-27 01:35:51 +02:00
syncEngineInstance . scanLocalFilesystemPathForNewData ( to ) ;
2020-04-06 12:05:06 +02:00
} else {
2023-08-27 01:35:51 +02:00
syncEngineInstance . uploadMoveItem ( from , to ) ;
2020-04-06 12:05:06 +02:00
}
2019-02-21 09:51:15 +01:00
} catch ( CurlException e ) {
log . vlog ( "Offline, cannot move item!" ) ;
2018-03-14 05:43:40 +01:00
} catch ( Exception e ) {
2020-03-19 19:35:21 +01:00
log . logAndNotify ( "Cannot move item: " , e . msg ) ;
2018-03-14 05:43:40 +01:00
}
} ;
2023-08-27 01:35:51 +02:00
// Handle SIGINT and SIGTERM
2018-12-28 03:19:20 +01:00
signal ( SIGINT , & exitHandler ) ;
signal ( SIGTERM , & exitHandler ) ;
2023-08-27 01:35:51 +02:00
// Initialise the filesystem monitor class
try {
log . log ( "Initialising filesystem inotify monitoring ..." ) ;
filesystemMonitor . initialise ( ) ;
log . log ( "Performing initial syncronisation to ensure consistent local state ..." ) ;
} catch ( MonitorException e ) {
// monitor class initialisation failed
log . error ( "ERROR: " , e . msg ) ;
return EXIT_FAILURE ;
2019-10-31 04:59:44 +01:00
}
2023-08-27 01:35:51 +02:00
// Filesystem monitor loop
2020-05-20 03:37:11 +02:00
bool performMonitor = true ;
ulong monitorLoopFullCount = 0 ;
2023-09-02 04:27:10 +02:00
ulong fullScanFrequencyLoopCount = 0 ;
ulong monitorLogOutputLoopCount = 0 ;
2023-08-27 01:35:51 +02:00
immutable auto checkOnlineInterval = dur ! "seconds" ( appConfig . getValueLong ( "monitor_interval" ) ) ;
2022-03-07 04:02:50 +01:00
immutable auto githubCheckInterval = dur ! "seconds" ( 86400 ) ;
2023-08-27 01:35:51 +02:00
immutable ulong logOutputSupressionInterval = appConfig . getValueLong ( "monitor_log_frequency" ) ;
immutable ulong fullScanFrequency = appConfig . getValueLong ( "monitor_fullscan_frequency" ) ;
2023-09-02 04:27:10 +02:00
immutable ulong monitorLogOutputFrequency = appConfig . getValueLong ( "monitor_log_frequency" ) ;
2020-05-20 03:37:11 +02:00
MonoTime lastCheckTime = MonoTime . currTime ( ) ;
2022-03-07 04:02:50 +01:00
MonoTime lastGitHubCheckTime = MonoTime . currTime ( ) ;
2023-08-27 01:35:51 +02:00
string loopStartOutputMessage = "################################################## NEW LOOP ##################################################" ;
string loopStopOutputMessage = "################################################ LOOP COMPLETE ###############################################" ;
2022-03-07 04:02:50 +01:00
2020-05-20 03:37:11 +02:00
while ( performMonitor ) {
2023-08-27 01:35:51 +02:00
try {
// Process any inotify events
2023-08-30 22:29:24 +02:00
filesystemMonitor . update ( true ) ;
2023-08-27 01:35:51 +02:00
} catch ( MonitorException e ) {
// Catch any exceptions thrown by inotify / monitor engine
log . error ( "ERROR: The following inotify error was generated: " , e . msg ) ;
2020-05-05 23:20:13 +02:00
}
2023-08-27 01:35:51 +02:00
2021-11-23 20:54:28 +01:00
// Check for notifications pushed from Microsoft to the webhook
bool notificationReceived = false ;
2023-08-27 01:35:51 +02:00
// Check here for a webhook notification
// Get the current time this loop is starting
auto currentTime = MonoTime . currTime ( ) ;
// Do we perform a sync with OneDrive?
if ( notificationReceived | | ( currentTime - lastCheckTime > checkOnlineInterval ) | | ( monitorLoopFullCount = = 0 ) ) {
2023-09-02 04:27:10 +02:00
// Increment relevant counters
2023-08-28 08:56:16 +02:00
monitorLoopFullCount + + ;
2023-09-02 04:27:10 +02:00
fullScanFrequencyLoopCount + + ;
monitorLogOutputLoopCount + + ;
// Do we flag to perform a full scan of the online data?
if ( fullScanFrequencyLoopCount > fullScanFrequency ) {
// set full scan trigger for true up
2023-09-08 22:34:52 +02:00
log . log ( "Enabling Full Scan True Up (fullScanFrequencyLoopCount > fullScanFrequency), resetting fullScanFrequencyLoopCount = 1" ) ;
2023-09-02 04:27:10 +02:00
fullScanFrequencyLoopCount = 1 ;
appConfig . fullScanTrueUpRequired = true ;
} else {
// unset full scan trigger for true up
log . vdebug ( "Disabling Full Scan True Up" ) ;
appConfig . fullScanTrueUpRequired = false ;
}
2023-09-08 22:34:52 +02:00
log . vdebug ( loopStartOutputMessage ) ;
log . log ( "Total Run-Time Loop Number: " , monitorLoopFullCount ) ;
log . log ( "Full Scan Freqency Loop Number: " , fullScanFrequencyLoopCount ) ;
SysTime startFunctionProcessingTime = Clock . currTime ( ) ;
log . vdebug ( "Start Monitor Loop Time: " , startFunctionProcessingTime ) ;
2023-09-02 04:27:10 +02:00
// Do we perform any monitor logging output surpression?
// 'monitor_log_frequency' controls how often, in a non-verbose application output mode, how often
// the full output of what is occuring is done. This is done to lessen the 'verbosity' of non-verbose
// logging, but only when running in --monitor
if ( monitorLogOutputLoopCount > monitorLogOutputFrequency ) {
// unsurpress the logging output
monitorLogOutputLoopCount = 1 ;
log . vdebug ( "Unsuppressing log output" ) ;
appConfig . surpressLoggingOutput = false ;
} else {
// do we surpress the logging output to absolute minimal
if ( monitorLoopFullCount = = 1 ) {
// application startup with --monitor
log . vdebug ( "Unsuppressing initial sync log output" ) ;
appConfig . surpressLoggingOutput = false ;
} else {
// only surpress if we are not doing --verbose or higher
if ( log . verbose = = 0 ) {
log . vdebug ( "Suppressing --monitor log output" ) ;
appConfig . surpressLoggingOutput = true ;
} else {
log . vdebug ( "Unsuppressing log output" ) ;
appConfig . surpressLoggingOutput = false ;
}
}
}
2023-08-29 01:56:55 +02:00
// How long has the application been running for?
auto elapsedTime = Clock . currTime ( ) - applicationStartTime ;
log . log ( "Application run-time thus far: " , elapsedTime ) ;
// Need to re-validate that the client is still online for this loop
if ( testInternetReachability ( appConfig ) ) {
// Starting a sync
log . log ( "Starting a sync with Microsoft OneDrive" ) ;
// Did the user specify --upload-only?
if ( appConfig . getValueBool ( "upload_only" ) ) {
// Perform the --upload-only sync process
2023-08-30 22:29:24 +02:00
performUploadOnlySyncProcess ( localPath , filesystemMonitor ) ;
2023-08-29 01:56:55 +02:00
} else {
// Perform the standard sync process
2023-08-30 22:29:24 +02:00
performStandardSyncProcess ( localPath , filesystemMonitor ) ;
2023-08-29 01:56:55 +02:00
}
2023-08-30 22:29:24 +02:00
// Discard any inotify events generated as part of any sync operation
filesystemMonitor . update ( false ) ;
2023-08-29 01:56:55 +02:00
// Detail the outcome of the sync process
displaySyncOutcome ( ) ;
// Write WAL and SHM data to file for this loop
log . vdebug ( "Merge contents of WAL and SHM files into main database file" ) ;
itemDB . performVacuum ( ) ;
2020-05-25 03:30:04 +02:00
} else {
2023-08-29 01:56:55 +02:00
// Not online
2023-08-30 22:29:24 +02:00
log . log ( "Microsoft OneDrive service is not reachable at this time. Will re-try on next loop attempt." ) ;
2019-10-31 04:59:44 +01:00
}
2022-06-15 01:16:06 +02:00
2023-08-27 01:35:51 +02:00
// Output end of loop processing times
SysTime endFunctionProcessingTime = Clock . currTime ( ) ;
log . vdebug ( "End Monitor Loop Time: " , endFunctionProcessingTime ) ;
log . vdebug ( "Elapsed Monitor Loop Processing Time: " , ( endFunctionProcessingTime - startFunctionProcessingTime ) ) ;
2022-06-15 01:16:06 +02:00
2023-08-27 01:35:51 +02:00
// Log that this loop is complete
log . vdebug ( loopStopOutputMessage ) ;
// performSync complete, set lastCheckTime to current time
lastCheckTime = MonoTime . currTime ( ) ;
2020-05-20 03:37:11 +02:00
}
2022-07-18 05:09:41 +02:00
// Sleep the monitor thread for 1 second, loop around and pick up any inotify changes
Thread . sleep ( dur ! "seconds" ( 1 ) ) ;
2015-09-17 00:16:23 +02:00
}
}
2023-08-27 01:35:51 +02:00
} else {
// Exit application as the sync engine could not be initialised
log . error ( "Application Sync Engine could not be initialised correctly" ) ;
// Use exit scope
return EXIT_FAILURE ;
2015-09-14 19:21:06 +02:00
}
2023-08-27 01:35:51 +02:00
// Before we exit, if we are using --dry-run, clean up the local syste,
if ( appConfig . getValueBool ( "dry_run" ) ) {
// Cleanup any existing dry-run elements ... these should never be left hanging around
cleanupDryRunDatabaseFiles ( runtimeDatabaseFile ) ;
}
// Exit application using exit scope
2023-09-12 22:15:17 +02:00
if ( ! syncEngineInstance . syncFailures ) {
return EXIT_SUCCESS ;
} else {
return EXIT_FAILURE ;
}
2015-09-01 20:45:34 +02:00
}
2015-09-20 21:21:51 +02:00
2023-08-30 22:29:24 +02:00
void performUploadOnlySyncProcess ( string localPath , Monitor filesystemMonitor = null ) {
2023-08-27 01:35:51 +02:00
// Perform the local database consistency check, picking up locally modified data and uploading this to OneDrive
syncEngineInstance . performDatabaseConsistencyAndIntegrityCheck ( ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Handle any inotify events whilst the DB was being scanned
filesystemMonitor . update ( true ) ;
}
2023-08-27 01:35:51 +02:00
// Scan the configured 'sync_dir' for new data to upload
syncEngineInstance . scanLocalFilesystemPathForNewData ( localPath ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Handle any new inotify events whilst the local filesystem was being scanned
filesystemMonitor . update ( true ) ;
}
2023-08-27 01:35:51 +02:00
}
2023-08-30 22:29:24 +02:00
void performStandardSyncProcess ( string localPath , Monitor filesystemMonitor = null ) {
2023-08-27 01:35:51 +02:00
2023-09-02 04:27:10 +02:00
// If we are performing log supression, output this message so the user knows what is happening
if ( appConfig . surpressLoggingOutput ) {
log . log ( "Syncing changes from OneDrive ..." ) ;
}
2023-09-08 22:34:52 +02:00
// Zero out these arrays
syncEngineInstance . fileDownloadFailures = [ ] ;
syncEngineInstance . fileUploadFailures = [ ] ;
2023-08-27 01:35:51 +02:00
// Which way do we sync first?
// OneDrive first then local changes (normal operational process that uses OneDrive as the source of truth)
// Local First then OneDrive changes (alternate operation process to use local files as source of truth)
if ( appConfig . getValueBool ( "local_first" ) ) {
// Local data first
// Perform the local database consistency check, picking up locally modified data and uploading this to OneDrive
syncEngineInstance . performDatabaseConsistencyAndIntegrityCheck ( ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Handle any inotify events whilst the DB was being scanned
filesystemMonitor . update ( true ) ;
}
2023-08-27 01:35:51 +02:00
// Scan the configured 'sync_dir' for new data to upload to OneDrive
syncEngineInstance . scanLocalFilesystemPathForNewData ( localPath ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Handle any new inotify events whilst the local filesystem was being scanned
filesystemMonitor . update ( true ) ;
}
2023-08-27 01:35:51 +02:00
// Download data from OneDrive last
syncEngineInstance . syncOneDriveAccountToLocalDisk ( ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Cancel out any inotify events from downloading data
filesystemMonitor . update ( false ) ;
}
2023-08-27 01:35:51 +02:00
} else {
// Normal sync
// Download data from OneDrive first
syncEngineInstance . syncOneDriveAccountToLocalDisk ( ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Cancel out any inotify events from downloading data
filesystemMonitor . update ( false ) ;
}
2023-08-27 01:35:51 +02:00
// Perform the local database consistency check, picking up locally modified data and uploading this to OneDrive
syncEngineInstance . performDatabaseConsistencyAndIntegrityCheck ( ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Handle any inotify events whilst the DB was being scanned
filesystemMonitor . update ( true ) ;
}
2023-08-27 01:35:51 +02:00
// Scan the configured 'sync_dir' for new data to upload to OneDrive
syncEngineInstance . scanLocalFilesystemPathForNewData ( localPath ) ;
2023-08-30 22:29:24 +02:00
if ( appConfig . getValueBool ( "monitor" ) ) {
// Handle any new inotify events whilst the local filesystem was being scanned
filesystemMonitor . update ( true ) ;
}
2023-09-02 04:27:10 +02:00
// Perform the final true up scan to ensure we have correctly replicated the current online state locally
if ( ! appConfig . surpressLoggingOutput ) {
log . log ( "Perfoming final true up scan of online data from OneDrive" ) ;
}
// We pass in the 'appConfig.fullScanTrueUpRequired' value which then flags do we use the configured 'deltaLink'
// If 'appConfig.fullScanTrueUpRequired' is true, we do not use the 'deltaLink' if we are in --monitor mode, thus forcing a full scan true up
syncEngineInstance . syncOneDriveAccountToLocalDisk ( appConfig . fullScanTrueUpRequired ) ;
if ( appConfig . getValueBool ( "monitor" ) ) {
// Cancel out any inotify events from downloading data
filesystemMonitor . update ( false ) ;
}
2022-08-05 01:16:54 +02:00
}
}
2023-08-27 01:35:51 +02:00
void displaySyncOutcome ( ) {
// Detail any download or upload transfer failures
syncEngineInstance . displaySyncFailures ( ) ;
// Sync is either complete or partially complete
if ( ! syncEngineInstance . syncFailures ) {
// No download or upload issues
if ( ! appConfig . getValueBool ( "monitor" ) ) writeln ( ) ; // Add an additional line break so that this is clear when using --sync
log . log ( "Sync with Microsoft OneDrive is complete" ) ;
} else {
log . log ( "\nSync with Microsoft OneDrive has completed, however there are items that failed to sync." ) ;
// Due to how the OneDrive API works 'changes' such as add new files online, rename files online, delete files online are only sent once when using the /delta API call.
// That we failed to download it, we need to track that, and then issue a --resync to download any of these failed files .. unfortunate, but there is no easy way here
if ( ! syncEngineInstance . fileDownloadFailures . empty ) {
log . log ( "To fix any download failures you may need to perform a --resync to ensure this system is correctly synced with your Microsoft OneDrive Account" ) ;
2018-12-04 01:15:44 +01:00
}
2023-08-27 01:35:51 +02:00
if ( ! syncEngineInstance . fileUploadFailures . empty ) {
2023-09-09 11:08:51 +02:00
log . log ( "To fix any upload failures you may need to perform a --resync to ensure this system is correctly synced with your Microsoft OneDrive Account" ) ;
2018-12-04 01:15:44 +01:00
}
2023-09-10 06:53:31 +02:00
// So that from a logging perspective these messages are clear, add a line break in
writeln ( ) ;
2018-12-04 01:15:44 +01:00
}
}
2023-09-08 23:36:24 +02:00
string updateTildeConfigDirectives ( string inputValue ) {
string outputValue ;
log . vdebug ( "sync_dir: Setting runtimeSyncDirectory from config value 'sync_dir'" ) ;
2023-08-27 01:35:51 +02:00
if ( ( environment . get ( "SHELL" ) = = "" ) & & ( environment . get ( "USER" ) = = "" ) ) {
log . vdebug ( "sync_dir: No SHELL or USER environment variable configuration detected" ) ;
// No shell or user set, so expandTilde() will fail - usually headless system running under init.d / systemd or potentially Docker
// Does the 'currently configured' sync_dir include a ~
2023-09-08 23:36:24 +02:00
if ( canFind ( inputValue , "~" ) ) {
2023-08-27 01:35:51 +02:00
// A ~ was found in sync_dir
2023-09-08 23:36:24 +02:00
log . vdebug ( "sync_dir: A '~' was found in 'sync_dir', using the calculated 'homePath' to replace '~' as no SHELL or USER environment variable set" ) ;
outputValue = appConfig . defaultHomePath ~ strip ( inputValue , "~" ) ;
2023-08-27 01:35:51 +02:00
} else {
// No ~ found in sync_dir, use as is
2023-09-08 23:36:24 +02:00
log . vdebug ( "sync_dir: Using configured 'sync_dir' path as-is as no SHELL or USER environment variable configuration detected" ) ;
outputValue = inputValue ;
2023-08-27 01:35:51 +02:00
}
} else {
2023-09-08 23:36:24 +02:00
// A shell and user environment variable is set, expand any ~ as this will be expanded correctly if present
if ( canFind ( inputValue , "~" ) ) {
log . vdebug ( "sync_dir: A '~' was found in configured 'sync_dir', automatically expanding as SHELL and USER environment variable is set" ) ;
outputValue = expandTilde ( inputValue ) ;
2023-08-27 01:35:51 +02:00
} else {
2023-09-08 23:36:24 +02:00
// No ~ found in sync_dir, does the path begin with a '/' ?
log . vdebug ( "sync_dir: Using configured 'sync_dir' path as-is as however SHELL or USER environment variable configuration detected - should be placed in USER home directory" ) ;
if ( ! startsWith ( inputValue , "/" ) ) {
log . log ( "Configured 'sync_dir' does not start with a '/' or '~/' - adjusting configured 'sync_dir' to use User Home Directory as base for 'sync_dir' path" ) ;
string updatedPathWithHome = "~/" ~ inputValue ;
outputValue = expandTilde ( updatedPathWithHome ) ;
} else {
log . vdebug ( "use 'sync_dir' as is - no touch" ) ;
outputValue = inputValue ;
}
2023-08-27 01:35:51 +02:00
}
2018-03-14 05:43:40 +01:00
}
2023-08-27 01:35:51 +02:00
2023-09-08 23:36:24 +02:00
// What will runtimeSyncDirectory be actually set to?
log . vdebug ( "runtimeSyncDirectory set to: " , outputValue ) ;
return outputValue ;
2023-08-27 01:35:51 +02:00
}
2021-11-23 20:54:28 +01:00
2023-08-27 01:35:51 +02:00
void processResyncDatabaseRemoval ( string databaseFilePathToRemove ) {
log . vdebug ( "Testing if we have exclusive access to local database file" ) ;
// Are we the only running instance? Test that we can open the database file path
itemDB = new ItemDatabase ( databaseFilePathToRemove ) ;
// did we successfully initialise the database class?
if ( ! itemDB . isDatabaseInitialised ( ) ) {
// no .. destroy class
itemDB = null ;
// exit application - void function, force exit this way
exit ( - 1 ) ;
}
// If we have exclusive access we will not have exited
// destroy access test
destroy ( itemDB ) ;
// delete application sync state
log . log ( "Deleting the saved application sync status ..." ) ;
if ( ! appConfig . getValueBool ( "dry_run" ) ) {
safeRemove ( databaseFilePathToRemove ) ;
} else {
// --dry-run scenario ... technically we should not be making any local file changes .......
log . log ( "DRY RUN: Not removing the saved application sync status" ) ;
}
}
2022-08-30 22:41:52 +02:00
2023-08-27 01:35:51 +02:00
void cleanupDryRunDatabaseFiles ( string dryRunDatabaseFile ) {
2022-08-30 22:41:52 +02:00
2023-08-27 01:35:51 +02:00
// Temp variables
string dryRunShmFile = dryRunDatabaseFile ~ "-shm" ;
string dryRunWalFile = dryRunDatabaseFile ~ "-wal" ;
2021-11-23 20:54:28 +01:00
2023-08-27 01:35:51 +02:00
// If the dry run database exists, clean this up
if ( exists ( dryRunDatabaseFile ) ) {
// remove the existing file
2023-09-12 22:15:17 +02:00
log . vdebug ( "DRY-RUN: Removing items-dryrun.sqlite3 as it still exists for some reason" ) ;
2023-08-27 01:35:51 +02:00
safeRemove ( dryRunDatabaseFile ) ;
}
// silent cleanup of shm files if it exists
if ( exists ( dryRunShmFile ) ) {
// remove items-dryrun.sqlite3-shm
2023-09-12 22:15:17 +02:00
log . vdebug ( "DRY-RUN: Removing items-dryrun.sqlite3-shm as it still exists for some reason" ) ;
2023-08-27 01:35:51 +02:00
safeRemove ( dryRunShmFile ) ;
}
// silent cleanup of wal files if it exists
if ( exists ( dryRunWalFile ) ) {
// remove items-dryrun.sqlite3-wal
2023-09-12 22:15:17 +02:00
log . vdebug ( "DRY-RUN: Removing items-dryrun.sqlite3-wal as it still exists for some reason" ) ;
2023-08-27 01:35:51 +02:00
safeRemove ( dryRunWalFile ) ;
}
2015-09-20 21:21:51 +02:00
}
2018-12-05 20:19:00 +01:00
2023-08-27 01:35:51 +02:00
// Getting around the @nogc problem
2018-12-28 03:19:20 +01:00
// https://p0nce.github.io/d-idioms/#Bypassing-@nogc
2023-08-27 01:35:51 +02:00
auto assumeNoGC ( T ) ( T t ) if ( isFunctionPointer ! T | | isDelegate ! T ) {
2018-12-28 03:19:20 +01:00
enum attrs = functionAttributes ! T | FunctionAttribute . nogc ;
return cast ( SetFunctionAttributes ! ( T , functionLinkage ! T , attrs ) ) t ;
}
2023-08-27 01:35:51 +02:00
// Catch CTRL-C
2018-12-28 03:19:20 +01:00
extern ( C ) nothrow @nogc @system void exitHandler ( int value ) {
try {
assumeNoGC ( ( ) {
2021-11-23 20:54:28 +01:00
log . log ( "Got termination signal, performing clean up" ) ;
2020-11-06 00:28:15 +01:00
// was itemDb initialised?
2023-08-27 01:35:51 +02:00
if ( itemDB . isDatabaseInitialised ( ) ) {
2020-11-06 00:28:15 +01:00
// Make sure the .wal file is incorporated into the main db before we exit
2023-08-27 01:35:51 +02:00
log . log ( "Shutting down DB connection and merging temporary data" ) ;
itemDB . performVacuum ( ) ;
destroy ( itemDB ) ;
2020-11-06 00:28:15 +01:00
}
2018-12-28 03:19:20 +01:00
} ) ( ) ;
} catch ( Exception e ) { }
exit ( 0 ) ;
2023-08-27 01:35:51 +02:00
}