Revert PR #1626 due to deprecation function that needs to be resolved before PR can be merged again
This commit is contained in:
abraunegg 2021-11-23 11:14:29 +11:00 committed by GitHub
parent 2901534171
commit ac2e07526a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 287 additions and 11046 deletions

View File

@ -78,8 +78,7 @@ SOURCES = \
src/sync.d \
src/upload.d \
src/util.d \
src/progress.d \
src/arsd/cgi.d
src/progress.d
ifeq ($(NOTIFICATIONS),yes)
SOURCES += src/notifications/notify.d src/notifications/dnotify.d
@ -124,7 +123,7 @@ else
ifeq ($(RHEL_VERSION),6)
install -D contrib/init.d/onedrive.init $(DESTDIR)/etc/init.d/onedrive
install -D contrib/init.d/onedrive_service.sh $(DESTDIR)$(bindir)/onedrive_service.sh
endif
endif
endif
ifeq ($(COMPLETIONS),yes)
$(INSTALL) -D -m 644 contrib/completions/complete.zsh $(DESTDIR)$(ZSH_COMPLETION_DIR)/_onedrive

View File

@ -13,8 +13,7 @@ This client is a 'fork' of the [skilion](https://github.com/skilion/onedrive) cl
## Features
* State caching
* Real-Time local file monitoring with inotify
* Real-Time syncing of remote updates via webhooks
* Real-Time file monitoring with Inotify
* File upload / download validation to ensure data integrity
* Resumable uploads
* Support OneDrive for Business (part of Office 365)
@ -28,6 +27,7 @@ This client is a 'fork' of the [skilion](https://github.com/skilion/onedrive) cl
* Supports rate limiting of traffic
## What's missing
* While local changes are uploaded right away, remote changes are delayed until next automated sync cycle when using --monitor
* Ability to encrypt/decrypt files on-the-fly when uploading/downloading files from OneDrive
* Support for Windows 'On-Demand' functionality so file is only downloaded when accessed locally
* A GUI for configuration management

8
config
View File

@ -41,10 +41,4 @@
# sync_business_shared_folders = "false"
# sync_dir_permissions = "700"
# sync_file_permissions = "600"
# rate_limit = "131072"
# webhook_enabled = "false"
# webhook_public_url = ""
# webhook_listening_host = ""
# webhook_listening_port = "8888"
# webhook_expiration_interval = "86400"
# webhook_renewal_interval = "43200"
# rate_limit = "131072"

View File

@ -36,10 +36,6 @@
* [Shared folders (OneDrive Business or Office 365)](#shared-folders-onedrive-business-or-office-365)
* [SharePoint / Office 365 Shared Libraries](#sharepoint--office-365-shared-libraries)
- [Running 'onedrive' in 'monitor' mode](#running-onedrive-in-monitor-mode)
* [Use webhook to subscribe to remote updates in 'monitor' mode](#use-webhook-to-subscribe-to-remote-updates-in-monitor-mode)
* [More webhook configuration options](#more-webhook-configuration-options)
+ [webhook_listening_host and webhook_listening_port](#webhook_listening_host-and-webhook_listening_port)
+ [webhook_expiration_interval and webhook_renewal_interval](#webhook_expiration_interval-and-webhook_renewal_interval)
- [Running 'onedrive' as a system service](#running-onedrive-as-a-system-service)
* [OneDrive service running as root user via init.d](#onedrive-service-running-as-root-user-via-initd)
* [OneDrive service running as root user via systemd (Arch, Ubuntu, Debian, OpenSuSE, Fedora)](#onedrive-service-running-as-root-user-via-systemd-arch-ubuntu-debian-opensuse-fedora)
@ -88,19 +84,19 @@ After installing the application you must authorize the application with your On
You will be asked to open a specific URL by using your web browser where you will have to login into your Microsoft Account and give the application the permission to access your files. After giving permission to the application, you will be redirected to a blank page. Copy the URI of the blank page into the application.
```text
[user@hostname ~]$ onedrive
[user@hostname ~]$ onedrive
Authorize this app visiting:
https://.....
Enter the response uri:
Enter the response uri:
```
**Example:**
```
[user@hostname ~]$ onedrive
[user@hostname ~]$ onedrive
Authorize this app visiting:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=22c49a0d-d21c-4792-aed1-8f163c982546&scope=Files.ReadWrite%20Files.ReadWrite.all%20Sites.ReadWrite.All%20offline_access&response_type=code&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient
@ -124,7 +120,7 @@ Config path = /home/alex/.config/onedrive
Config file found in config path = false
Config option 'check_nosync' = false
Config option 'sync_dir' = /home/alex/OneDrive
Config option 'skip_dir' =
Config option 'skip_dir' =
Config option 'skip_file' = ~*|.~*|*.tmp
Config option 'skip_dotfiles' = false
Config option 'skip_symlinks' = false
@ -192,7 +188,7 @@ Example: If the full path is `~/OneDrive/mydir`, the command would be `onedrive
### Performing a 'one-way' download sync
In some cases it may be desirable to 'download only' from OneDrive. To do this use the following command:
```text
onedrive --synchronize --download-only
onedrive --synchronize --download-only
```
### Performing a 'one-way' upload sync
@ -357,12 +353,6 @@ See the [config](https://raw.githubusercontent.com/abraunegg/onedrive/master/con
# sync_file_permissions = "600"
# rate_limit = "131072"
# operation_timeout = "3600"
# webhook_enabled = "false"
# webhook_public_url = ""
# webhook_listening_host = ""
# webhook_listening_port = "8888"
# webhook_expiration_interval = "86400"
# webhook_renewal_interval = "43200"
```
@ -411,7 +401,7 @@ sync_file_permissions = "600"
**Important:** Special permission bits (setuid, setgid, sticky bit) are not supported. Valid permission values are from `000` to `777` only.
#### skip_dir
Example:
Example:
```text
# When changing a config option below, remove the '#' from the start of the line
# For explanations of all config options below see docs/USAGE.md or the man page.
@ -538,7 +528,7 @@ Each line of the file represents a relative path from your `sync_dir`. All files
Here is an example of `sync_list`:
```text
# sync_list supports comments
#
#
# The ordering of entries is highly recommended - exclusions before inclusions
#
# Exclude temp folders under Documents
@ -572,7 +562,7 @@ The following are supported for pattern matching and exclusion rules:
To simplify 'exclusions' and 'inclusions', the following is also possible:
```text
# sync_list supports comments
#
#
# The ordering of entries is highly recommended - exclusions before inclusions
#
# Exclude temp folders under Documents
@ -593,7 +583,7 @@ sync_root_files = "true"
This will tell the application to sync any file that it finds in your 'sync_dir' root by default.
### Configuring the client for 'single tenant application' use
In some instances when using OneDrive Business Accounts, depending on the Azure organisational configuration, it will be necessary to configure the client as a 'single tenant application'.
In some instances when using OneDrive Business Accounts, depending on the Azure organisational configuration, it will be necessary to configure the client as a 'single tenant application'.
To configure this, after creating the application on your Azure tenant, update the 'config' file with the tenant name (not the GUID) and the newly created Application ID, then this will be used for the authentication process.
```text
# skip_dir_strict_match = "false"
@ -692,59 +682,6 @@ sudo sysctl fs.inotify.max_user_watches=<new_value>
To make these changes permanent, refer to your OS reference documentation.
### Use webhook to subscribe to remote updates in 'monitor' mode
A webhook can be optionally enabled in the monitor mode to allow the onedrive process to subscribe to remote updates. Remote changes can be synced to your local file system as soon as possible, without waiting for the next sync cycle.
To enable this feature, you need to configure the following options in the config file:
```
webhook_enabled = "true"
webhook_public_url = "<public-facing url to reach your webhook>"
```
Setting `webhook_enabled` to `true` enables the webhook in 'monitor' mode. The onedrive process will listen for incoming updates at a configurable endpoint, which defaults to `0.0.0.0:8888`. The `webhook_public_url` must be set to an public-facing url for Microsoft to send updates to your webhook. If your host is directly exposed to the Internet, the `webhook_public_url` can be set to `http://<your_host>:8888/` to match the default endpoint. However, the recommended approach is to configure a reverse proxy like nginx.
For example, below is a nginx config snippet to proxy traffic into the webhook:
```
http {
server {
listen 80;
location /webhooks/onedrive {
proxy_pass http://127.0.0.1:8888;
}
}
}
```
With nginx running, you can configure `webhook_public_url` to `http://<your_host>/webhooks/onedrive`.
### More webhook configuration options
Below options can be optionally configured. The default is usually good enough.
#### webhook_listening_host and webhook_listening_port
Set `webhook_listening_host` and `webhook_listening_port` to change the webhook listening endpoint. If `webhook_listening_host` is left empty, which is the default, the webhook will bind to `0.0.0.0`. The default `webhook_listening_port` is `8888`.
```
webhook_listening_host = ""
webhook_listening_port = "8888"
```
#### webhook_expiration_interval and webhook_renewal_interval
Set `webhook_expiration_interval` and `webhook_renewal_interval` to change the frequency of subscription renewal. By default, the webhook asks Microsoft to keep subscriptions alive for 24 hours, and it renews subscriptions when it is less than 12 hours before their expiration.
```
# Default expiration interval is 24 hours
webhook_expiration_interval = "86400"
# Default renewal interval is 12 hours
webhook_renewal_interval = "43200"
```
## Running 'onedrive' as a system service
There are a few ways to use onedrive as a service
* via init.d
@ -932,7 +869,7 @@ for extra details.
* Configuring the client for use in dual-boot (Windows / Linux) situations
* Configuring the client for use when 'sync_dir' is a mounted directory
* Upload data from the local ~/OneDrive folder to a specific location on OneDrive
Refer to [./advanced-usage.md](advanced-usage.md) for configuration assistance.
### Access OneDrive service through a proxy

View File

@ -1,8 +0,0 @@
The files in this directory have been obtained form the following places:
cgi.d
https://github.com/adamdruppe/arsd/blob/a870179988b8881b04126856105f0fad2cc0018d/cgi.d
License: Boost Software License - Version 1.0
Copyright 2008-2021, Adam D. Ruppe
see https://github.com/adamdruppe/arsd/blob/a870179988b8881b04126856105f0fad2cc0018d/LICENSE

File diff suppressed because it is too large Load Diff

View File

@ -43,7 +43,7 @@ final class Config
// Default file permission mode
public long defaultFilePermissionMode = 600;
public int configuredFilePermissionMode;
this(string confdirOption)
{
// default configuration - entries in config file ~/.config/onedrive/config
@ -123,14 +123,6 @@ final class Config
// This includes dns resolution, connecting, data transfer, etc.
longValues["operation_timeout"] = 3600;
// Webhook options
boolValues["webhook_enabled"] = false;
stringValues["webhook_public_url"] = "";
stringValues["webhook_listening_host"] = "";
longValues["webhook_listening_port"] = 8888;
longValues["webhook_expiration_interval"] = 3600 * 24;
longValues["webhook_renewal_interval"] = 3600 * 12;
// DEVELOPER OPTIONS
// display_memory = true | false
// - It may be desirable to display the memory usage of the application to assist with diagnosing memory issues with the application
@ -164,10 +156,10 @@ final class Config
homePath = "~";
}
}
// Output homePath calculation
log.vdebug("homePath: ", homePath);
// Determine the correct configuration directory to use
string configDirBase;
string systemConfigDirBase;
@ -193,7 +185,7 @@ final class Config
// Also set up a path to pre-shipped shared configs (which can be overridden by supplying a config file in userspace)
systemConfigDirBase = "/etc";
}
// Output configDirBase calculation
log.vdebug("configDirBase: ", configDirBase);
// Set the default application configuration directory
@ -203,7 +195,7 @@ final class Config
// systemConfigDirBase contains the correct path so we do not need to check for presence of '~'
systemConfigDirName = systemConfigDirBase ~ "/onedrive";
}
// Config directory options all determined
if (!exists(configDirName)) {
// create the directory
@ -211,11 +203,11 @@ final class Config
// Configure the applicable permissions for the folder
configDirName.setAttributes(returnRequiredDirectoryPermisions());
}
// configDirName has a trailing /
log.vlog("Using 'user' Config Dir: ", configDirName);
log.vlog("Using 'system' Config Dir: ", systemConfigDirName);
// Update application set variables based on configDirName
refreshTokenFilePath = buildNormalizedPath(configDirName ~ "/refresh_token");
deltaLinkFilePath = buildNormalizedPath(configDirName ~ "/delta_link");
@ -226,7 +218,7 @@ final class Config
syncListFilePath = buildNormalizedPath(configDirName ~ "/sync_list");
systemConfigFilePath = buildNormalizedPath(systemConfigDirName ~ "/config");
businessSharedFolderFilePath = buildNormalizedPath(configDirName ~ "/business_shared_folders");
// Debug Output for application set variables based on configDirName
log.vdebug("refreshTokenFilePath = ", refreshTokenFilePath);
log.vdebug("deltaLinkFilePath = ", deltaLinkFilePath);
@ -298,7 +290,7 @@ final class Config
boolValues["synchronize"] = false;
boolValues["force"] = false;
boolValues["list_business_shared_folders"] = false;
// Application Startup option validation
try {
string tmpStr;
@ -306,7 +298,7 @@ final class Config
long tmpVerb;
// duplicated from main.d to get full help output!
auto opt = getopt(
args,
std.getopt.config.bundling,
std.getopt.config.caseSensitive,
@ -317,7 +309,7 @@ final class Config
"Perform authentication not via interactive dialog but via providing the reponse url directly.",
&stringValues["auth_response"],
"check-for-nomount",
"Check for the presence of .nosync in the syncdir root. If found, do not perform sync.",
"Check for the presence of .nosync in the syncdir root. If found, do not perform sync.",
&boolValues["check_nomount"],
"check-for-nosync",
"Check for the presence of .nosync in each directory. If found, skip directory from sync.",
@ -331,8 +323,8 @@ final class Config
"create-share-link",
"Create a shareable link for an existing file on OneDrive",
&stringValues["create_share_link"],
"debug-https",
"Debug OneDrive HTTPS communication.",
"debug-https",
"Debug OneDrive HTTPS communication.",
&boolValues["debug_https"],
"destination-directory",
"Destination directory for renamed or move on OneDrive - no sync will be performed.",
@ -534,7 +526,7 @@ final class Config
// configure function variables
auto file = File(filename, "r");
string lineBuffer;
// configure scopes
// - failure
scope(failure) {
@ -553,7 +545,7 @@ final class Config
file.close();
}
}
// read file line by line
auto range = file.byLine();
foreach (line; range) {
@ -600,7 +592,7 @@ final class Config
setValueString("skip_dir", configFileSkipDir);
}
}
// Azure AD Configuration
if (key == "azure_ad_endpoint") {
string azureConfigValue = c.front.dup;
@ -620,7 +612,7 @@ final class Config
case "CN":
log.log("Using config option for Azure AD China operated by 21Vianet");
break;
// Default - all other entries
// Default - all other entries
default:
log.log("Unknown Azure AD Endpoint - using Global Azure AD Endpoints");
}
@ -643,10 +635,10 @@ final class Config
}
return true;
}
void configureRequiredDirectoryPermisions() {
// return the directory permission mode required
// - return octal!defaultDirectoryPermissionMode; ... cant be used .. which is odd
// - return octal!defaultDirectoryPermissionMode; ... cant be used .. which is odd
// Error: variable defaultDirectoryPermissionMode cannot be read at compile time
if (getValueLong("sync_dir_permissions") != defaultDirectoryPermissionMode) {
// return user configured permissions as octal integer
@ -660,10 +652,10 @@ final class Config
configuredDirectoryPermissionMode = to!int(convertedValue);
}
}
void configureRequiredFilePermisions() {
// return the file permission mode required
// - return octal!defaultFilePermissionMode; ... cant be used .. which is odd
// - return octal!defaultFilePermissionMode; ... cant be used .. which is odd
// Error: variable defaultFilePermissionMode cannot be read at compile time
if (getValueLong("sync_file_permissions") != defaultFilePermissionMode) {
// return user configured permissions as octal integer
@ -677,7 +669,7 @@ final class Config
configuredFilePermissionMode = to!int(convertedValue);
}
}
int returnRequiredDirectoryPermisions() {
// read the configuredDirectoryPermissionMode and return
if (configuredDirectoryPermissionMode == 0) {
@ -687,7 +679,7 @@ final class Config
}
return configuredDirectoryPermissionMode;
}
int returnRequiredFilePermisions() {
// read the configuredFilePermissionMode and return
if (configuredFilePermissionMode == 0) {

View File

@ -5,13 +5,11 @@ import config, itemdb, monitor, onedrive, selective, sync, util;
import std.net.curl: CurlException;
import core.stdc.signal;
import std.traits;
import std.concurrency: receiveTimeout;
static import log;
OneDriveApi oneDrive;
ItemDatabase itemDb;
bool onedriveInitialised = false;
const int EXIT_UNAUTHORIZED = 3;
enum MONITOR_LOG_SILENT = 2;
@ -22,7 +20,7 @@ int main(string[] args)
{
// Disable buffering on stdout
stdout.setvbuf(0, _IONBF);
// main function variables
string confdirOption;
string configFilePath;
@ -50,9 +48,10 @@ int main(string[] args)
bool skipDirDifferent = false;
bool online = false;
bool performSyncOK = false;
bool onedriveInitialised = false;
bool displayMemoryUsage = false;
bool displaySyncOptions = false;
// Define scopes
scope(exit) {
// Display memory details
@ -80,7 +79,7 @@ int main(string[] args)
log.displayMemoryUsagePostGC();
}
}
scope(failure) {
// Display memory details
if (displayMemoryUsage) {
@ -140,7 +139,7 @@ int main(string[] args)
log.error("Try 'onedrive -h' for more information");
return EXIT_FAILURE;
}
// load configuration file if available
auto cfg = new config.Config(confdirOption);
if (!cfg.initialize()) {
@ -148,22 +147,22 @@ int main(string[] args)
// Error message already printed
return EXIT_FAILURE;
}
// set memory display
displayMemoryUsage = cfg.getValueBool("display_memory");
// set display sync options
displaySyncOptions = cfg.getValueBool("display_sync_options");
// update configuration from command line args
cfg.update_from_args(args);
// Initialise normalised file paths
configFilePath = buildNormalizedPath(cfg.configDirName ~ "/config");
syncListFilePath = buildNormalizedPath(cfg.configDirName ~ "/sync_list");
databaseFilePath = buildNormalizedPath(cfg.configDirName ~ "/items.db");
businessSharedFolderFilePath = buildNormalizedPath(cfg.configDirName ~ "/business_shared_folders");
// Has any of our configuration that would require a --resync been changed?
// 1. sync_list file modification
// 2. config file modification - but only if sync_dir, skip_dir, skip_file or drive_id was modified
@ -172,25 +171,25 @@ int main(string[] args)
syncListHashFile = buildNormalizedPath(cfg.configDirName ~ "/.sync_list.hash");
configBackupFile = buildNormalizedPath(cfg.configDirName ~ "/.config.backup");
businessSharedFoldersHashFile = buildNormalizedPath(cfg.configDirName ~ "/.business_shared_folders.hash");
// Does a config file exist with a valid hash file
if ((exists(configFilePath)) && (!exists(configHashFile))) {
// Hash of config file needs to be created
std.file.write(configHashFile, computeQuickXorHash(configFilePath));
}
// Does a sync_list file exist with a valid hash file
if ((exists(syncListFilePath)) && (!exists(syncListHashFile))) {
// Hash of sync_list file needs to be created
std.file.write(syncListHashFile, computeQuickXorHash(syncListFilePath));
}
// check if business_shared_folders & business_shared_folders hash exists
if ((exists(businessSharedFolderFilePath)) && (!exists(businessSharedFoldersHashFile))) {
// Hash of business_shared_folders file needs to be created
std.file.write(businessSharedFoldersHashFile, computeQuickXorHash(businessSharedFolderFilePath));
}
// If hash files exist, but config files do not ... remove the hash, but only if --resync was issued as now the application will use 'defaults' which 'may' be different
if ((!exists(configFilePath)) && (exists(configHashFile))) {
// if --resync safe remove config.hash and config.backup
@ -199,18 +198,18 @@ int main(string[] args)
safeRemove(configBackupFile);
}
}
// If sync_list hash file exists, but sync_list file does not ... remove the hash, but only if --resync was issued as now the application will use 'defaults' which 'may' be different
if ((!exists(syncListFilePath)) && (exists(syncListHashFile))) {
// if --resync safe remove sync_list.hash
if (cfg.getValueBool("resync")) safeRemove(syncListHashFile);
}
if ((!exists(businessSharedFolderFilePath)) && (exists(businessSharedFoldersHashFile))) {
// if --resync safe remove business_shared_folders.hash
if (cfg.getValueBool("resync")) safeRemove(businessSharedFoldersHashFile);
}
// Read config hashes if they exist
if (exists(configFilePath)) currentConfigHash = computeQuickXorHash(configFilePath);
if (exists(syncListFilePath)) currentSyncListHash = computeQuickXorHash(syncListFilePath);
@ -218,21 +217,21 @@ int main(string[] args)
if (exists(configHashFile)) previousConfigHash = readText(configHashFile);
if (exists(syncListHashFile)) previousSyncListHash = readText(syncListHashFile);
if (exists(businessSharedFoldersHashFile)) previousBusinessSharedFoldersHash = readText(businessSharedFoldersHashFile);
// Was sync_list file updated?
if (currentSyncListHash != previousSyncListHash) {
// Debugging output to assist what changed
log.vdebug("sync_list file has been updated, --resync needed");
syncListDifferent = true;
}
// Was business_shared_folders updated?
if (currentBusinessSharedFoldersHash != previousBusinessSharedFoldersHash) {
// Debugging output to assist what changed
log.vdebug("business_shared_folders file has been updated, --resync needed");
businessSharedFoldersDifferent = true;
}
// Was config file updated between last execution ang this execution?
if (currentConfigHash != previousConfigHash) {
// config file was updated, however we only want to trigger a --resync requirement if sync_dir, skip_dir, skip_file or drive_id was modified
@ -267,7 +266,7 @@ int main(string[] args)
log.vdebug(key, " was modified since the last time the application was successfully run, --resync needed");
configOptionsDifferent = true;
}
if ((key == "skip_file") && (c.front.dup != cfg.getValueString("skip_file"))){
log.vdebug(key, " was modified since the last time the application was successfully run, --resync needed");
configOptionsDifferent = true;
@ -292,7 +291,7 @@ int main(string[] args)
// no backup to check
log.vdebug("WARNING: no backup config file was found, unable to validate if any changes made");
}
// If there was a backup, any modified values we need to worry about would been detected
if (!cfg.getValueBool("display_config")) {
// we are not testing the configuration
@ -310,13 +309,13 @@ int main(string[] args)
}
}
}
// Is there a backup of the config file if the config file exists?
if ((exists(configFilePath)) && (!exists(configBackupFile))) {
// create backup copy of current config file
std.file.copy(configFilePath, configBackupFile);
}
// config file set options can be changed via CLI input, specifically these will impact sync and --resync will be needed:
// --syncdir ARG
// --skip-file ARG
@ -332,7 +331,7 @@ int main(string[] args)
syncDirDifferent = true;
}
}
// was the skip_file updated by CLI?
if (cfg.configFileSkipFile != "") {
// skip_file was set in config file
@ -341,8 +340,8 @@ int main(string[] args)
log.vdebug("skip_file: CLI override of config file option, --resync needed");
skipFileDifferent = true;
}
}
}
// was the skip_dir updated by CLI?
if (cfg.configFileSkipDir != "") {
// skip_dir was set in config file
@ -353,7 +352,7 @@ int main(string[] args)
}
}
}
// Has anything triggered a --resync requirement?
if (configOptionsDifferent || syncListDifferent || syncDirDifferent || skipFileDifferent || skipDirDifferent || businessSharedFoldersDifferent) {
// --resync needed, is the user just testing configuration changes?
@ -389,7 +388,7 @@ int main(string[] args)
}
}
}
// dry-run notification and database setup
if (cfg.getValueBool("dry_run")) {
log.log("DRY-RUN Configured. Output below shows what 'would' have occurred.");
@ -399,18 +398,18 @@ int main(string[] args)
if (exists(cfg.databaseFilePathDryRun)) {
// remove the existing file
log.vdebug("Removing items-dryrun.sqlite3 as it still exists for some reason");
safeRemove(cfg.databaseFilePathDryRun);
safeRemove(cfg.databaseFilePathDryRun);
}
// silent cleanup of shm and wal files if they exist
if (exists(dryRunShmFile)) {
// remove items-dryrun.sqlite3-shm
safeRemove(dryRunShmFile);
safeRemove(dryRunShmFile);
}
if (exists(dryRunWalFile)) {
// remove items-dryrun.sqlite3-wal
safeRemove(dryRunWalFile);
safeRemove(dryRunWalFile);
}
// Make a copy of the original items.sqlite3 for use as the dry run copy if it exists
if (exists(cfg.databaseFilePath)) {
// in a --dry-run --resync scenario, we should not copy the existing database file
@ -424,7 +423,7 @@ int main(string[] args)
}
}
}
// sync_dir environment handling to handle ~ expansion properly
bool shellEnvSet = false;
if ((environment.get("SHELL") == "") && (environment.get("USER") == "")){
@ -451,10 +450,10 @@ int main(string[] args)
syncDir = cfg.getValueString("sync_dir");
}
}
// vdebug syncDir as set and calculated
log.vdebug("syncDir: ", syncDir);
// Configure the logging directory if different from application default
// log_dir environment handling to handle ~ expansion properly
string logDir = cfg.getValueString("log_dir");
@ -479,7 +478,7 @@ int main(string[] args)
// update log_dir with normalised path, with '~' expanded correctly
cfg.setValueString("log_dir", logDir);
}
// Configure logging only if enabled
if (cfg.getValueBool("enable_logging")){
// Initialise using the configured logging directory
@ -489,7 +488,7 @@ int main(string[] args)
// Configure whether notifications are used
log.setNotifications(cfg.getValueBool("monitor") && !cfg.getValueBool("disable_notifications"));
// Application upgrades - skilion version etc
if (exists(databaseFilePath)) {
if (!cfg.getValueBool("dry_run")) {
@ -498,7 +497,7 @@ int main(string[] args)
log.logAndNotify("Database schema changed, resync needed");
cfg.setValueBool("resync", true);
}
// Handle --logout as separate item, do not 'resync' on a --logout / reauth
if (cfg.getValueBool("logout")) {
log.vdebug("--logout requested");
@ -509,7 +508,7 @@ int main(string[] args)
// Exit
return EXIT_SUCCESS;
}
// Handle --resync to remove local files
if (cfg.getValueBool("resync")) {
if (cfg.getValueBool("resync")) log.vdebug("--resync requested");
@ -520,7 +519,7 @@ int main(string[] args)
safeRemove(cfg.uploadStateFilePath);
}
}
// Display current application configuration, no application initialisation
if (cfg.getValueBool("display_config")){
// Display application version
@ -529,7 +528,7 @@ int main(string[] args)
writeln("Config path = ", cfg.configDirName);
// Does a config file exist or are we using application defaults
writeln("Config file found in config path = ", exists(configFilePath));
// Config Options
writeln("Config option 'check_nosync' = ", cfg.getValueBool("check_nosync"));
writeln("Config option 'sync_dir' = ", syncDir);
@ -544,12 +543,12 @@ int main(string[] args)
writeln("Config option 'upload_only' = ", cfg.getValueBool("upload_only"));
writeln("Config option 'no_remote_delete' = ", cfg.getValueBool("no_remote_delete"));
writeln("Config option 'remove_source_files' = ", cfg.getValueBool("remove_source_files"));
// Is config option drive_id configured?
if (cfg.getValueString("drive_id") != ""){
writeln("Config option 'drive_id' = ", cfg.getValueString("drive_id"));
}
// Is sync_list configured?
if (exists(syncListFilePath)){
writeln("Config option 'sync_root_files' = ", cfg.getValueBool("sync_root_files"));
@ -566,7 +565,7 @@ int main(string[] args)
writeln("Config option 'sync_root_files' = ", cfg.getValueBool("sync_root_files"));
writeln("Selective sync 'sync_list' configured = false");
}
// Is business_shared_folders configured
if (exists(businessSharedFolderFilePath)){
writeln("Business Shared Folders configured = true");
@ -581,11 +580,11 @@ int main(string[] args)
} else {
writeln("Business Shared Folders configured = false");
}
// Exit
return EXIT_SUCCESS;
}
// Test if OneDrive service can be reached, exit if it cant be reached
log.vdebug("Testing network to ensure network connectivity to Microsoft OneDrive Service");
online = testNetwork();
@ -598,7 +597,7 @@ int main(string[] args)
} else {
// Running as --monitor
log.error("Unable to reach Microsoft OneDrive API service at this point in time, re-trying network tests\n");
// re-try network connection to OneDrive
// https://github.com/abraunegg/onedrive/issues/1184
// Back off & retry with incremental delay
@ -606,13 +605,13 @@ int main(string[] args)
int retryAttempts = 1;
int backoffInterval = 1;
int maxBackoffInterval = 3600;
bool retrySuccess = false;
while (!retrySuccess){
// retry to access OneDrive API
backoffInterval++;
int thisBackOffInterval = retryAttempts*backoffInterval;
log.vdebug(" Retry Attempt: ", retryAttempts);
log.vdebug(" Retry Attempt: ", retryAttempts);
if (thisBackOffInterval <= maxBackoffInterval) {
log.vdebug(" Retry In (seconds): ", thisBackOffInterval);
Thread.sleep(dur!"seconds"(thisBackOffInterval));
@ -632,7 +631,7 @@ int main(string[] args)
// we have attempted to re-connect X number of times
// false set this to true to break out of while loop
retrySuccess = true;
}
}
}
// Increment & loop around
retryAttempts++;
@ -644,7 +643,7 @@ int main(string[] args)
}
}
}
// Initialize OneDrive, check for authorization
if (online) {
// we can only initialise if we are online
@ -653,24 +652,24 @@ int main(string[] args)
onedriveInitialised = oneDrive.init();
oneDrive.printAccessToken = cfg.getValueBool("print_token");
}
if (!onedriveInitialised) {
log.error("Could not initialize the OneDrive API");
// Use exit scopes to shutdown API
return EXIT_UNAUTHORIZED;
}
// if --synchronize or --monitor not passed in, configure the flag to display help & exit
if (cfg.getValueBool("synchronize") || cfg.getValueBool("monitor")) {
performSyncOK = true;
}
// create-directory, remove-directory, source-directory, destination-directory
// these are activities that dont perform a sync, so to not generate an error message for these items either
if (((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) || ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) || (cfg.getValueString("get_file_link") != "") || (cfg.getValueString("create_share_link") != "") || (cfg.getValueString("get_o365_drive_id") != "") || cfg.getValueBool("display_sync_status") || cfg.getValueBool("list_business_shared_folders")) {
performSyncOK = true;
}
// Were acceptable sync operations provided? Was --synchronize or --monitor passed in
if (!performSyncOK) {
// was the application just authorised?
@ -695,7 +694,7 @@ int main(string[] args)
return EXIT_FAILURE;
}
}
// if --synchronize && --monitor passed in, exit & display help as these conflict with each other
if (cfg.getValueBool("synchronize") && cfg.getValueBool("monitor")) {
writeln("\nERROR: --synchronize and --monitor cannot be used together\n");
@ -703,7 +702,7 @@ int main(string[] args)
// Use exit scopes to shutdown API
return EXIT_FAILURE;
}
// Initialize the item database
log.vlog("Opening the item database ...");
if (!cfg.getValueBool("dry_run")) {
@ -715,7 +714,7 @@ int main(string[] args)
log.vdebug("Using database file: ", asNormalizedPath(cfg.databaseFilePathDryRun));
itemDb = new ItemDatabase(cfg.databaseFilePathDryRun);
}
// What are the permission that have been set for the application?
// These are relevant for:
// - The ~/OneDrive parent folder or 'sync_dir' configured item
@ -732,7 +731,7 @@ int main(string[] args)
log.vdebug("Configuring default new file permissions as: ", cfg.getValueLong("sync_file_permissions"));
cfg.configureRequiredFilePermisions();
}
// configure the sync direcory based on syncDir config option
log.vlog("All operations will be performed in: ", syncDir);
if (!exists(syncDir)) {
@ -750,13 +749,13 @@ int main(string[] args)
return EXIT_FAILURE;
}
}
// Change the working directory to the 'sync_dir' configured item
chdir(syncDir);
// Configure selective sync by parsing and getting a regex for skip_file config component
auto selectiveSync = new SelectiveSync();
// load sync_list if it exists
if (exists(syncListFilePath)){
log.vdebug("Loading user configured sync_list file ...");
@ -775,7 +774,7 @@ int main(string[] args)
}
}
selectiveSync.load(syncListFilePath);
// load business_shared_folders if it exists
if (exists(businessSharedFolderFilePath)){
log.vdebug("Loading user configured business_shared_folders file ...");
@ -788,27 +787,27 @@ int main(string[] args)
}
}
selectiveSync.loadSharedFolders(businessSharedFolderFilePath);
// Configure skip_dir, skip_file, skip-dir-strict-match & skip_dotfiles from config entries
// Handle skip_dir configuration in config file
log.vdebug("Configuring skip_dir ...");
log.vdebug("skip_dir: ", cfg.getValueString("skip_dir"));
selectiveSync.setDirMask(cfg.getValueString("skip_dir"));
// Was --skip-dir-strict-match configured?
log.vdebug("Configuring skip_dir_strict_match ...");
log.vdebug("skip_dir_strict_match: ", cfg.getValueBool("skip_dir_strict_match"));
if (cfg.getValueBool("skip_dir_strict_match")) {
selectiveSync.setSkipDirStrictMatch();
}
// Was --skip-dot-files configured?
log.vdebug("Configuring skip_dotfiles ...");
log.vdebug("skip_dotfiles: ", cfg.getValueBool("skip_dotfiles"));
if (cfg.getValueBool("skip_dotfiles")) {
selectiveSync.setSkipDotfiles();
}
// Handle skip_file configuration in config file
log.vdebug("Configuring skip_file ...");
// Validate skip_file to ensure that this does not contain an invalid configuration
@ -823,7 +822,7 @@ int main(string[] args)
// All skip_file entries are valid
log.vdebug("skip_file: ", cfg.getValueString("skip_file"));
selectiveSync.setFileMask(cfg.getValueString("skip_file"));
// Initialize the sync engine
auto sync = new SyncEngine(cfg, oneDrive, itemDb, selectiveSync);
try {
@ -848,7 +847,7 @@ int main(string[] args)
if (syncListConfigured) {
sync.setSyncListConfigured();
}
// Do we need to configure specific --upload-only options?
if (cfg.getValueBool("upload_only")) {
// --upload-only was passed in or configured
@ -867,7 +866,7 @@ int main(string[] args)
sync.setLocalDeleteAfterUpload();
}
}
// Do we configure to disable the upload validation routine
if (cfg.getValueBool("disable_upload_validation")) sync.setDisableUploadValidation();
@ -880,7 +879,7 @@ int main(string[] args)
log.log("WARNING: Local data loss MAY occur in this scenario.");
sync.setBypassDataPreservation();
}
// Are we configured to use a National Cloud Deployment
if (cfg.getValueString("azure_ad_endpoint") != "") {
// value is configured, is it a valid value?
@ -889,7 +888,7 @@ int main(string[] args)
sync.setNationalCloudDeployment();
}
}
// Do we need to validate the syncDir to check for the presence of a '.nosync' file
if (cfg.getValueBool("check_nomount")) {
// we were asked to check the mounts
@ -899,53 +898,53 @@ int main(string[] args)
return EXIT_FAILURE;
}
}
// Do we need to create or remove a directory?
if ((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) {
if (cfg.getValueString("create_directory") != "") {
// create a directory on OneDrive
sync.createDirectoryNoSync(cfg.getValueString("create_directory"));
}
if (cfg.getValueString("remove_directory") != "") {
// remove a directory on OneDrive
sync.deleteDirectoryNoSync(cfg.getValueString("remove_directory"));
}
}
// Are we renaming or moving a directory?
if ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) {
// We are renaming or moving a directory
sync.renameDirectoryNoSync(cfg.getValueString("source_directory"), cfg.getValueString("destination_directory"));
}
// Are we obtaining the Office 365 Drive ID for a given Office 365 SharePoint Shared Library?
if (cfg.getValueString("get_o365_drive_id") != "") {
sync.querySiteCollectionForDriveID(cfg.getValueString("get_o365_drive_id"));
// Exit application
// Exit application
// Use exit scopes to shutdown API
return EXIT_SUCCESS;
}
// Are we createing an anonymous read-only shareable link for an existing file on OneDrive?
if (cfg.getValueString("create_share_link") != "") {
// Query OneDrive for the file, and if valid, create a shareable link for the file
sync.createShareableLinkForFile(cfg.getValueString("create_share_link"));
// Exit application
// Exit application
// Use exit scopes to shutdown API
return EXIT_SUCCESS;
}
// Are we obtaining the URL path for a synced file?
if (cfg.getValueString("get_file_link") != "") {
// Query OneDrive for the file link
sync.queryOneDriveForFileURL(cfg.getValueString("get_file_link"), syncDir);
// Exit application
// Exit application
// Use exit scopes to shutdown API
return EXIT_SUCCESS;
}
// Are we listing OneDrive Business Shared Folders
if (cfg.getValueBool("list_business_shared_folders")) {
// Is this a business account type?
@ -955,11 +954,11 @@ int main(string[] args)
} else {
log.error("ERROR: Unsupported account type for listing OneDrive Business Shared Folders");
}
// Exit application
// Exit application
// Use exit scopes to shutdown API
return EXIT_SUCCESS;
}
// Are we going to sync OneDrive Business Shared Folders
if (cfg.getValueBool("sync_business_shared_folders")) {
// Is this a business account type?
@ -970,7 +969,7 @@ int main(string[] args)
log.error("ERROR: Unsupported account type for syncing OneDrive Business Shared Folders");
}
}
// Are we displaying the sync status of the client?
if (cfg.getValueBool("display_sync_status")) {
string remotePath = "/";
@ -981,7 +980,7 @@ int main(string[] args)
}
sync.queryDriveForChanges(remotePath);
}
// Are we performing a sync, or monitor operation?
if ((cfg.getValueBool("synchronize")) || (cfg.getValueBool("monitor"))) {
// Initialise the monitor class, so that we can do more granular inotify handling when performing the actual sync
@ -1008,17 +1007,17 @@ int main(string[] args)
// fullScanRequired = false, for final true-up
// but if we have sync_list configured, use syncListConfigured which = true
performSync(sync, cfg.getValueString("single_directory"), cfg.getValueBool("download_only"), cfg.getValueBool("local_first"), cfg.getValueBool("upload_only"), LOG_NORMAL, false, syncListConfigured, displaySyncOptions, cfg.getValueBool("monitor"), m);
// Write WAL and SHM data to file for this sync
log.vdebug("Merge contents of WAL and SHM files into main database file");
itemDb.performVacuum();
}
}
if (cfg.getValueBool("monitor")) {
log.logAndNotify("Initializing monitor ...");
log.log("OneDrive monitor interval (seconds): ", cfg.getValueLong("monitor_interval"));
m.onDirCreated = delegate(string path) {
// Handle .folder creation if skip_dotfiles is enabled
if ((cfg.getValueBool("skip_dotfiles")) && (selectiveSync.isDotFile(path))) {
@ -1107,8 +1106,7 @@ int main(string[] args)
// sync list is configured
syncListConfiguredFullScanOverride = true;
}
immutable bool webhookEnabled = cfg.getValueBool("webhook_enabled");
while (performMonitor) {
if (!cfg.getValueBool("download_only")) {
try {
@ -1118,35 +1116,7 @@ int main(string[] args)
log.error("ERROR: The following inotify error was generated: ", e.msg);
}
}
// Check for notifications pushed from Microsoft to the webhook
bool notificationReceived = false;
if (webhookEnabled) {
// Create a subscription on the first run, or renew the subscription
// on subsequent runs when it is about to expire.
oneDrive.createOrRenewSubscription();
// Process incoming notifications if any.
// Empirical evidence shows that Microsoft often sends multiple
// notifications for one single change, so we need a loop to exhaust
// all signals that were queued up by the webhook. The notifications
// do not contain any actual changes, and we will always rely do the
// delta endpoint to sync to latest. Therefore, only one sync run is
// good enough to catch up for multiple notifications.
for (int signalCount = 0;; signalCount++) {
const auto signalExists = receiveTimeout(dur!"seconds"(-1), (ulong _) => {});
if (signalExists) {
notificationReceived = true;
} else {
if (notificationReceived) {
log.log("Received ", signalCount," refresh signals from the webhook");
}
break;
}
}
}
auto currTime = MonoTime.currTime();
// has monitor_interval elapsed or are we at application startup / monitor startup?
// in a --resync scenario, if we have not 're-populated' the database, valid changes will get skipped:
@ -1156,7 +1126,7 @@ int main(string[] args)
// Moving random_files/2eVPInOMTFNXzRXeNMEoJch5OR9XpGby to target/2eVPInOMTFNXzRXeNMEoJch5OR9XpGby
// Skipping uploading this new file as parent path is not in the database: target/2eVPInOMTFNXzRXeNMEoJch5OR9XpGby
// 'target' should be in the DB, it should also exist online, but because of --resync, it does not exist in the database thus parent check fails
if (notificationReceived || (currTime - lastCheckTime > checkInterval) || (monitorLoopFullCount == 0)) {
if ((currTime - lastCheckTime > checkInterval) || (monitorLoopFullCount == 0)) {
// monitor sync loop
logOutputMessage = "################################################## NEW LOOP ##################################################";
if (displaySyncOptions) {
@ -1170,7 +1140,7 @@ int main(string[] args)
if (displayMemoryUsage) {
log.displayMemoryUsagePreGC();
}
// log monitor output suppression
logMonitorCounter += 1;
if (logMonitorCounter > logInterval) {
@ -1192,7 +1162,7 @@ int main(string[] args)
fullScanRequired = false;
}
}
if (displaySyncOptions) {
// sync option handling per sync loop
log.log("fullScanCounter = ", fullScanCounter);
@ -1252,14 +1222,14 @@ int main(string[] args)
if (displayMemoryUsage) {
log.displayMemoryUsagePostGC();
}
// 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();
// monitor loop complete
logOutputMessage = "################################################ LOOP COMPLETE ###############################################";
// Handle display options
if (displaySyncOptions) {
log.log(logOutputMessage);
@ -1288,20 +1258,20 @@ int main(string[] args)
// remove the file
log.vdebug("Removing items-dryrun.sqlite3 as dry run operations complete");
// remove items-dryrun.sqlite3
safeRemove(cfg.databaseFilePathDryRun);
safeRemove(cfg.databaseFilePathDryRun);
}
// silent cleanup of shm and wal files if they exist
if (exists(dryRunShmFile)) {
// remove items-dryrun.sqlite3-shm
safeRemove(dryRunShmFile);
safeRemove(dryRunShmFile);
}
if (exists(dryRunWalFile)) {
// remove items-dryrun.sqlite3-wal
safeRemove(dryRunWalFile);
safeRemove(dryRunWalFile);
}
}
// Exit application
// Exit application
// Use exit scopes to shutdown API
return EXIT_SUCCESS;
}
@ -1331,14 +1301,14 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
string remotePath = "/";
string localPath = ".";
string logOutputMessage;
// performSync API scan triggers
log.vdebug("performSync API scan triggers");
log.vdebug("-----------------------------");
log.vdebug("fullScanRequired = ", fullScanRequired);
log.vdebug("syncListConfiguredFullScanOverride = ", syncListConfiguredFullScanOverride);
log.vdebug("-----------------------------");
// Are we doing a single directory sync?
if (singleDirectory != ""){
// Need two different path strings here
@ -1347,12 +1317,12 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
// Set flag for singleDirectoryScope for change handling
sync.setSingleDirectoryScope();
}
// Due to Microsoft Sharepoint 'enrichment' of files, we try to download the Microsoft modified file automatically
// Set flag if we are in upload only state to handle this differently
// See: https://github.com/OneDrive/onedrive-api-docs/issues/935 for further details
// See: https://github.com/OneDrive/onedrive-api-docs/issues/935 for further details
if (uploadOnly) sync.setUploadOnly();
do {
try {
// starting a sync
@ -1413,7 +1383,7 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
} else {
// sync from OneDrive first before uploading files to OneDrive
if (logLevel < MONITOR_LOG_SILENT) log.log("Syncing changes from OneDrive ...");
// For the initial sync, always use the delta link so that we capture all the right delta changes including adds, moves & deletes
logOutputMessage = "Initial Scan: Call OneDrive Delta API for delta changes as compared to last successful sync.";
syncCallLogOutput = "Calling sync.applyDifferences(false);";
@ -1425,8 +1395,8 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
log.vdebug(syncCallLogOutput);
}
sync.applyDifferences(false);
// is this a download only request?
// is this a download only request?
if (!downloadOnly) {
// process local changes walking the entire path checking for changes
// in monitor mode all local changes are captured via inotify
@ -1440,7 +1410,7 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
log.vdebug(logOutputMessage);
log.vdebug(syncCallLogOutput);
}
// What sort of local scan do we want to do?
// In --monitor mode, when performing the DB scan, a race condition occurs where by if a file or folder is moved during this process
// the inotify event is discarded once performSync() is finished (see m.update(false) above), so these events need to be handled
@ -1461,20 +1431,20 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
// handle any inotify events that occured 'whilst' we were scanning the local filesystem
m.update(true);
}
// At this point, all OneDrive changes / local changes should be uploaded and in sync
// This MAY not be the case when using sync_list, thus a full walk of OneDrive ojects is required
// --synchronize & no sync_list : fullScanRequired = false, syncListConfiguredFullScanOverride = false
// --synchronize & sync_list in use : fullScanRequired = false, syncListConfiguredFullScanOverride = true
// --monitor loops around 10 iterations. On the 1st loop, sets fullScanRequired = false, syncListConfiguredFullScanOverride = true if requried
// --monitor & no sync_list (loop #1) : fullScanRequired = true, syncListConfiguredFullScanOverride = false
// --monitor & no sync_list (loop #2 - #10) : fullScanRequired = false, syncListConfiguredFullScanOverride = false
// --monitor & sync_list in use (loop #1) : fullScanRequired = true, syncListConfiguredFullScanOverride = true
// --monitor & sync_list in use (loop #2 - #10) : fullScanRequired = false, syncListConfiguredFullScanOverride = false
// Do not perform a full walk of the OneDrive objects
if ((!fullScanRequired) && (!syncListConfiguredFullScanOverride)){
logOutputMessage = "Final True-Up: Do not perform a full walk of the OneDrive objects - not required";
@ -1488,7 +1458,7 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
}
sync.applyDifferences(false);
}
// Perform a full walk of OneDrive objects because sync_list is in use / or trigger was set in --monitor loop
if ((!fullScanRequired) && (syncListConfiguredFullScanOverride)){
logOutputMessage = "Final True-Up: Perform a full walk of OneDrive objects because sync_list is in use / or trigger was set in --monitor loop";
@ -1502,7 +1472,7 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
}
sync.applyDifferences(true);
}
// Perform a full walk of OneDrive objects because a full scan was required
if ((fullScanRequired) && (!syncListConfiguredFullScanOverride)){
logOutputMessage = "Final True-Up: Perform a full walk of OneDrive objects because a full scan was required";
@ -1513,10 +1483,10 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
} else {
log.vdebug(logOutputMessage);
log.vdebug(syncCallLogOutput);
}
}
sync.applyDifferences(true);
}
// Perform a full walk of OneDrive objects because a full scan was required and sync_list is in use and trigger was set in --monitor loop
if ((fullScanRequired) && (syncListConfiguredFullScanOverride)){
logOutputMessage = "Final True-Up: Perform a full walk of OneDrive objects because a full scan was required and sync_list is in use and trigger was set in --monitor loop";
@ -1534,7 +1504,7 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
}
}
}
// sync is complete
logOutputMessage = "################################################ SYNC COMPLETE ###############################################";
if (displaySyncOptions) {
@ -1542,13 +1512,13 @@ void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, boo
} else {
log.vdebug(logOutputMessage);
}
count = -1;
} catch (Exception e) {
if (++count == 3) {
log.log("Giving up on sync after three attempts: ", e.msg);
throw e;
} else
} else
log.log("Retry sync count: ", count, ": ", e.msg);
}
} while (count != -1);
@ -1565,19 +1535,14 @@ auto assumeNoGC(T) (T t) if (isFunctionPointer!T || isDelegate!T)
extern(C) nothrow @nogc @system void exitHandler(int value) {
try {
assumeNoGC ( () {
log.log("Got termination signal, performing clean up");
// if initialised, shut down the HTTP instance
if (onedriveInitialised) {
log.log("Shutting down the HTTP instance");
oneDrive.shutdown();
}
log.log("Got termination signal, shutting down db connection");
// was itemDb initialised?
if (itemDb !is null) {
// Make sure the .wal file is incorporated into the main db before we exit
log.log("Shutting down db connection");
itemDb.performVacuum();
destroy(itemDb);
}
// Use exit scopes to shutdown OneDrive API
})();
} catch(Exception e) {}
exit(0);

View File

@ -1,17 +1,14 @@
import std.net.curl;
import etc.c.curl: CurlOption;
import std.datetime, std.datetime.systime, std.exception, std.file, std.json, std.path;
import std.stdio, std.string, std.uni, std.uri, std.file, std.uuid;
import std.datetime, std.exception, std.file, std.json, std.path;
import std.stdio, std.string, std.uni, std.uri, std.file;
import std.array: split;
import core.atomic : atomicOp;
import core.stdc.stdlib;
import core.thread, std.conv, std.math;
import std.algorithm.searching;
import std.concurrency;
import progress;
import config;
import util;
import arsd.cgi;
static import log;
shared bool debugResponse = false;
private bool dryRun = false;
@ -26,22 +23,22 @@ private immutable {
// Global & Defaults
string globalAuthEndpoint = "https://login.microsoftonline.com";
string globalGraphEndpoint = "https://graph.microsoft.com";
// US Government L4
string usl4AuthEndpoint = "https://login.microsoftonline.us";
string usl4GraphEndpoint = "https://graph.microsoft.us";
// US Government L5
string usl5AuthEndpoint = "https://login.microsoftonline.us";
string usl5GraphEndpoint = "https://dod-graph.microsoft.us";
// Germany
string deAuthEndpoint = "https://login.microsoftonline.de";
string deGraphEndpoint = "https://graph.microsoft.de";
// China
string cnAuthEndpoint = "https://login.chinacloudapi.cn";
string cnGraphEndpoint = "https://microsoftgraph.chinacloudapi.cn";
string cnGraphEndpoint = "https://microsoftgraph.chinacloudapi.cn";
}
private {
@ -56,30 +53,27 @@ private {
// Default Drive ID
string driveId = "";
// API Query URL's, based on using defaults, but can be updated by config option 'azure_ad_endpoint'
// Authentication
string authUrl = globalAuthEndpoint ~ "/common/oauth2/v2.0/authorize";
string redirectUrl = globalAuthEndpoint ~ "/common/oauth2/nativeclient";
string tokenUrl = globalAuthEndpoint ~ "/common/oauth2/v2.0/token";
// Drive Queries
string driveUrl = globalGraphEndpoint ~ "/v1.0/me/drive";
string driveByIdUrl = globalGraphEndpoint ~ "/v1.0/drives/";
// What is 'shared with me' Query
string sharedWithMeUrl = globalGraphEndpoint ~ "/v1.0/me/drive/sharedWithMe";
// Item Queries
string itemByIdUrl = globalGraphEndpoint ~ "/v1.0/me/drive/items/";
string itemByPathUrl = globalGraphEndpoint ~ "/v1.0/me/drive/root:/";
// Office 365 / SharePoint Queries
string siteSearchUrl = globalGraphEndpoint ~ "/v1.0/sites?search";
string siteDriveUrl = globalGraphEndpoint ~ "/v1.0/sites/";
// Subscriptions
string subscriptionUrl = globalGraphEndpoint ~ "/v1.0/subscriptions";
}
class OneDriveException: Exception
@ -105,64 +99,12 @@ class OneDriveException: Exception
}
}
shared class OneDriveWebhook {
private string host;
private ushort port;
private uint count;
this(string host, ushort port) {
this.host = host;
this.port = port;
this.count = 0;
}
// The static serve() is necessary because spawn() does not like instance methods
static serve(shared OneDriveWebhook webhook, Tid parentTid) {
webhook.serve(parentTid);
}
private void serve(Tid parentTid) {
auto server = new RequestServer(host, port);
server.serveEmbeddedHttp!(cgi => handle(cgi, parentTid))();
}
private void handle(Cgi cgi, Tid parentTid) {
if (.debugResponse) {
log.log("Webhook request: ", cgi.requestMethod, " ", cgi.requestUri);
if (!cgi.postBody.empty) {
log.log("Webhook post body: ", cgi.postBody);
}
}
cgi.setResponseContentType("text/plain");
if ("validationToken" in cgi.get) {
// For validation requests, respond with the validation token passed in the query string
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/webhook-receiver-validation-request
cgi.write(cgi.get["validationToken"]);
log.log("Webhook: handled validation request");
} else {
// Notifications don't include any information about the changes that triggered them.
// Put a refresh signal in the queue and let the main monitor loop process it.
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/using-webhooks
count.atomicOp!"+="(1);
send(parentTid, to!ulong(count));
cgi.write("OK");
log.log("Webhook: sent refresh signal #", count);
}
}
}
final class OneDriveApi
{
private Config cfg;
private string refreshToken, accessToken, subscriptionId;
private string refreshToken, accessToken;
private SysTime accessTokenExpiration;
private HTTP http;
private shared OneDriveWebhook webhook;
private SysTime subscriptionExpiration;
private Duration subscriptionExpirationInterval, subscriptionRenewalInterval;
private string notificationUrl;
// if true, every new access token is printed
bool printAccessToken;
@ -190,16 +132,16 @@ final class OneDriveApi
// maximum time an operation is allowed to take
// This includes dns resolution, connecting, data transfer, etc.
http.operationTimeout = (dur!"seconds"(cfg.getValueLong("operation_timeout")));
// Specify how many redirects should be allowed
http.maxRedirects(5);
// Do we enable curl debugging?
if (cfg.getValueBool("debug_https")) {
http.verbose = true;
.debugResponse = true;
}
// Update clientId if application_id is set in config file
if (cfg.getValueString("application_id") != "") {
// an application_id is set in config file
@ -207,7 +149,7 @@ final class OneDriveApi
clientId = cfg.getValueString("application_id");
companyName = "custom_application";
}
// Configure tenant id value, if 'azure_tenant_id' is configured,
// otherwise use the "common" multiplexer
string tenantId = "common";
@ -215,7 +157,7 @@ final class OneDriveApi
// Use the value entered by the user
tenantId = cfg.getValueString("azure_tenant_id");
}
// Configure Azure AD endpoints if 'azure_ad_endpoint' is configured
string azureConfigValue = cfg.getValueString("azure_ad_endpoint");
switch(azureConfigValue) {
@ -243,10 +185,10 @@ final class OneDriveApi
// custom application_id
redirectUrl = usl4AuthEndpoint ~ "/" ~ tenantId ~ "/oauth2/nativeclient";
}
// Drive Queries
driveUrl = usl4GraphEndpoint ~ "/v1.0/me/drive";
driveByIdUrl = usl4GraphEndpoint ~ "/v1.0/drives/";
driveByIdUrl = usl4GraphEndpoint ~ "/v1.0/drives/";
// Item Queries
itemByIdUrl = usl4GraphEndpoint ~ "/v1.0/me/drive/items/";
itemByPathUrl = usl4GraphEndpoint ~ "/v1.0/me/drive/root:/";
@ -255,8 +197,6 @@ final class OneDriveApi
siteDriveUrl = usl4GraphEndpoint ~ "/v1.0/sites/";
// Shared With Me
sharedWithMeUrl = usl4GraphEndpoint ~ "/v1.0/me/drive/sharedWithMe";
// Subscriptions
subscriptionUrl = usl4GraphEndpoint ~ "/v1.0/subscriptions";
break;
case "USL5":
log.log("Configuring Azure AD for US Government Endpoints (DOD)");
@ -271,10 +211,10 @@ final class OneDriveApi
// custom application_id
redirectUrl = usl5AuthEndpoint ~ "/" ~ tenantId ~ "/oauth2/nativeclient";
}
// Drive Queries
driveUrl = usl5GraphEndpoint ~ "/v1.0/me/drive";
driveByIdUrl = usl5GraphEndpoint ~ "/v1.0/drives/";
driveByIdUrl = usl5GraphEndpoint ~ "/v1.0/drives/";
// Item Queries
itemByIdUrl = usl5GraphEndpoint ~ "/v1.0/me/drive/items/";
itemByPathUrl = usl5GraphEndpoint ~ "/v1.0/me/drive/root:/";
@ -283,8 +223,6 @@ final class OneDriveApi
siteDriveUrl = usl5GraphEndpoint ~ "/v1.0/sites/";
// Shared With Me
sharedWithMeUrl = usl5GraphEndpoint ~ "/v1.0/me/drive/sharedWithMe";
// Subscriptions
subscriptionUrl = usl5GraphEndpoint ~ "/v1.0/subscriptions";
break;
case "DE":
log.log("Configuring Azure AD Germany");
@ -299,10 +237,10 @@ final class OneDriveApi
// custom application_id
redirectUrl = deAuthEndpoint ~ "/" ~ tenantId ~ "/oauth2/nativeclient";
}
// Drive Queries
driveUrl = deGraphEndpoint ~ "/v1.0/me/drive";
driveByIdUrl = deGraphEndpoint ~ "/v1.0/drives/";
driveByIdUrl = deGraphEndpoint ~ "/v1.0/drives/";
// Item Queries
itemByIdUrl = deGraphEndpoint ~ "/v1.0/me/drive/items/";
itemByPathUrl = deGraphEndpoint ~ "/v1.0/me/drive/root:/";
@ -311,8 +249,6 @@ final class OneDriveApi
siteDriveUrl = deGraphEndpoint ~ "/v1.0/sites/";
// Shared With Me
sharedWithMeUrl = deGraphEndpoint ~ "/v1.0/me/drive/sharedWithMe";
// Subscriptions
subscriptionUrl = deGraphEndpoint ~ "/v1.0/subscriptions";
break;
case "CN":
log.log("Configuring AD China operated by 21Vianet");
@ -327,10 +263,10 @@ final class OneDriveApi
// custom application_id
redirectUrl = cnAuthEndpoint ~ "/" ~ tenantId ~ "/oauth2/nativeclient";
}
// Drive Queries
driveUrl = cnGraphEndpoint ~ "/v1.0/me/drive";
driveByIdUrl = cnGraphEndpoint ~ "/v1.0/drives/";
driveByIdUrl = cnGraphEndpoint ~ "/v1.0/drives/";
// Item Queries
itemByIdUrl = cnGraphEndpoint ~ "/v1.0/me/drive/items/";
itemByPathUrl = cnGraphEndpoint ~ "/v1.0/me/drive/root:/";
@ -339,35 +275,33 @@ final class OneDriveApi
siteDriveUrl = cnGraphEndpoint ~ "/v1.0/sites/";
// Shared With Me
sharedWithMeUrl = cnGraphEndpoint ~ "/v1.0/me/drive/sharedWithMe";
// Subscriptions
subscriptionUrl = cnGraphEndpoint ~ "/v1.0/subscriptions";
break;
// Default - all other entries
// Default - all other entries
default:
log.log("Unknown Azure AD Endpoint request - using Global Azure AD Endpoints");
}
// Debug output of configured URL's
// Authentication
log.vdebug("Configured authUrl: ", authUrl);
log.vdebug("Configured redirectUrl: ", redirectUrl);
log.vdebug("Configured tokenUrl: ", tokenUrl);
// Drive Queries
log.vdebug("Configured driveUrl: ", driveUrl);
log.vdebug("Configured driveByIdUrl: ", driveByIdUrl);
// Shared With Me
log.vdebug("Configured sharedWithMeUrl: ", sharedWithMeUrl);
// Item Queries
log.vdebug("Configured itemByIdUrl: ", itemByIdUrl);
log.vdebug("Configured itemByPathUrl: ", itemByPathUrl);
// SharePoint Queries
log.vdebug("Configured siteSearchUrl: ", siteSearchUrl);
log.vdebug("Configured siteDriveUrl: ", siteDriveUrl);
// Configure the User Agent string
if (cfg.getValueString("user_agent") == "") {
// Application User Agent string defaults
@ -381,7 +315,7 @@ final class OneDriveApi
// Use the value entered by the user
http.setUserAgent = cfg.getValueString("user_agent");
}
// What version of HTTP protocol do we use?
// Curl >= 7.62.0 defaults to http2 for a significant number of operations
if (cfg.getValueBool("force_http_2")) {
@ -394,28 +328,28 @@ final class OneDriveApi
// Downgrade to HTTP 1.1 - yes version = 2 is HTTP 1.1
http.handle.set(CurlOption.http_version,2);
}
// Configure upload / download rate limits if configured
long userRateLimit = cfg.getValueLong("rate_limit");
// 131072 = 128 KB/s - minimum for basic application operations to prevent timeouts
// A 0 value means rate is unlimited, and is the curl default
if (userRateLimit > 0) {
// User configured rate limit
writeln("User Configured Rate Limit: ", userRateLimit);
// If user provided rate limit is < 131072, flag that this is too low, setting to the minimum of 131072
if (userRateLimit < 131072) {
// user provided limit too low
log.log("WARNING: User configured rate limit too low for normal application processing and preventing application timeouts. Overriding to default minimum of 131072 (128KB/s)");
userRateLimit = 131072;
}
// set rate limit
http.handle.set(CurlOption.max_send_speed_large,userRateLimit);
http.handle.set(CurlOption.max_send_speed_large,userRateLimit);
http.handle.set(CurlOption.max_recv_speed_large,userRateLimit);
}
// Do we set the dryRun handlers?
if (cfg.getValueBool("dry_run")) {
.dryRun = true;
@ -423,19 +357,11 @@ final class OneDriveApi
.simulateNoRefreshTokenFile = true;
}
}
subscriptionExpiration = Clock.currTime(UTC());
subscriptionExpirationInterval = dur!"seconds"(cfg.getValueLong("webhook_expiration_interval"));
subscriptionRenewalInterval = dur!"seconds"(cfg.getValueLong("webhook_renewal_interval"));
notificationUrl = cfg.getValueString("webhook_public_url");
}
// Shutdown OneDrive HTTP construct
void shutdown()
{
// delete subscription if there exists any
deleteSubscription();
// reset any values to defaults, freeing any set objects
http.clearRequestHeaders();
http.onSend = null;
@ -454,7 +380,7 @@ final class OneDriveApi
log.vdebug("clientId = ", clientId);
log.vdebug("companyName = ", companyName);
log.vdebug("appTitle = ", appTitle);
try {
driveId = cfg.getValueString("drive_id");
if (driveId.length) {
@ -463,7 +389,7 @@ final class OneDriveApi
itemByPathUrl = driveUrl ~ "/root:/";
}
} catch (Exception e) {}
if (!.dryRun) {
// original code
try {
@ -522,7 +448,7 @@ final class OneDriveApi
while (!exists(responseUrl)) {
Thread.sleep(dur!("msecs")(100));
}
// read response from OneDrive
try {
response = cast(char[]) read(responseUrl);
@ -530,8 +456,8 @@ final class OneDriveApi
// exception generated
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
return false;
}
}
// try to remove old files
try {
std.file.remove(authUrl);
@ -562,7 +488,7 @@ final class OneDriveApi
// Return the current value of retryAfterValue if it has been set to something other than 0
return .retryAfterValue;
}
void resetRetryAfterValue()
{
// Reset the current value of retryAfterValue to 0 after it has been used
@ -586,7 +512,7 @@ final class OneDriveApi
url = driveUrl ~ "/root";
return get(url);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get
JSONValue getDriveIdRoot(const(char)[] driveId)
{
@ -602,7 +528,7 @@ final class OneDriveApi
checkAccessTokenExpired();
return get(sharedWithMeUrl);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_get
JSONValue getDriveQuota(const(char)[] driveId)
{
@ -612,7 +538,7 @@ final class OneDriveApi
url ~= "?select=quota";
return get(url);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_delta
JSONValue viewChangesByItemId(const(char)[] driveId, const(char)[] id, const(char)[] deltaLink)
{
@ -627,7 +553,7 @@ final class OneDriveApi
}
return get(url);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_delta
JSONValue viewChangesByDriveId(const(char)[] driveId, const(char)[] deltaLink)
{
@ -639,7 +565,7 @@ final class OneDriveApi
}
return get(url);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_list_children
JSONValue listChildren(const(char)[] driveId, const(char)[] id, const(char)[] nextLink)
{
@ -667,13 +593,13 @@ final class OneDriveApi
} catch (FileException e) {
// display the error message
displayFileSystemErrorMessage(e.msg, getFunctionName!({}));
}
}
}
}
}
// Create the required local directory
string newPath = dirName(saveToPath);
// Does the path exist locally?
if (!exists(newPath)) {
try {
@ -687,7 +613,7 @@ final class OneDriveApi
displayFileSystemErrorMessage(e.msg, getFunctionName!({}));
}
}
const(char)[] url = driveByIdUrl ~ driveId ~ "/items/" ~ id ~ "/content?AVOverride=1";
// Download file
download(url, saveToPath, fileSize);
@ -744,7 +670,7 @@ final class OneDriveApi
{
checkAccessTokenExpired();
const(char)[] url = driveByIdUrl ~ parentDriveId ~ "/items/" ~ parentId ~ "/children";
http.addRequestHeader("Content-Type", "application/json");
http.addRequestHeader("Content-Type", "application/json");
return post(url, item.toString());
}
@ -758,7 +684,7 @@ final class OneDriveApi
url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference,size";
return get(url);
}
// Return the details of the specified id
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get
JSONValue getPathDetailsById(const(char)[] driveId, const(char)[] id)
@ -769,7 +695,7 @@ final class OneDriveApi
url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference,size";
return get(url);
}
// Return the requested details of the specified path on the specified drive id and path
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get?view=odsp-graph-online
JSONValue getPathDetailsByDriveId(const(char)[] driveId, const(string) path)
@ -782,7 +708,7 @@ final class OneDriveApi
url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference,size";
return get(url);
}
// Return the requested details of the specified path on the specified drive id and item id
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get?view=odsp-graph-online
JSONValue getPathDetailsByDriveIdAndItemId(const(char)[] driveId, const(char)[] itemId)
@ -795,7 +721,7 @@ final class OneDriveApi
url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference,size";
return get(url);
}
// Return the requested details of the specified id
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get
JSONValue getFileDetails(const(char)[] driveId, const(char)[] id)
@ -806,7 +732,7 @@ final class OneDriveApi
url ~= "?select=size,malware,file,webUrl";
return get(url);
}
// Create an anonymous read-only shareable link for an existing file on OneDrive
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createlink
JSONValue createShareableLink(const(char)[] driveId, const(char)[] id, JSONValue accessScope)
@ -814,10 +740,10 @@ final class OneDriveApi
checkAccessTokenExpired();
const(char)[] url;
url = driveByIdUrl ~ driveId ~ "/items/" ~ id ~ "/createLink";
http.addRequestHeader("Content-Type", "application/json");
http.addRequestHeader("Content-Type", "application/json");
return post(url, accessScope.toString());
}
// https://dev.onedrive.com/items/move.htm
JSONValue moveByPath(const(char)[] sourcePath, JSONValue moveData)
{
@ -827,7 +753,7 @@ final class OneDriveApi
http.addRequestHeader("Content-Type", "application/json");
return move(url, moveData.toString());
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession
JSONValue createUploadSession(const(char)[] parentDriveId, const(char)[] parentId, const(char)[] filename, const(char)[] eTag = null, JSONValue item = null)
{
@ -847,7 +773,7 @@ final class OneDriveApi
file.seek(offset);
string contentRange = "bytes " ~ to!string(offset) ~ "-" ~ to!string(offset + offsetSize - 1) ~ "/" ~ to!string(fileSize);
log.vdebugNewLine("contentRange: ", contentRange);
// function scopes
scope(exit) {
http.clearRequestHeaders();
@ -862,7 +788,7 @@ final class OneDriveApi
file.close();
}
}
http.method = HTTP.Method.put;
http.url = uploadUrl;
http.addRequestHeader("Content-Range", contentRange);
@ -895,7 +821,7 @@ final class OneDriveApi
}
return get(url);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_list?view=odsp-graph-online
JSONValue o365SiteDrives(string site_id){
checkAccessTokenExpired();
@ -904,89 +830,6 @@ final class OneDriveApi
return get(url);
}
// Create a new subscription or renew the existing subscription
void createOrRenewSubscription() {
checkAccessTokenExpired();
// Kick off the webhook server first
if (webhook is null) {
webhook = new shared OneDriveWebhook(
cfg.getValueString("webhook_listening_host"),
to!ushort(cfg.getValueLong("webhook_listening_port"))
);
spawn(&OneDriveWebhook.serve, webhook, thisTid);
}
if (!hasValidSubscription()) {
createSubscription();
} else if (isSubscriptionUpForRenewal()) {
try {
renewSubscription();
} catch (OneDriveException e) {
if (e.httpStatusCode == 404) {
log.log("The subscription is not found on the server. Recreating subscription ...");
createSubscription();
}
}
}
}
private bool hasValidSubscription() {
return !subscriptionId.empty && subscriptionExpiration > Clock.currTime(UTC());
}
private bool isSubscriptionUpForRenewal() {
return subscriptionExpiration < Clock.currTime(UTC()) + subscriptionRenewalInterval;
}
private void createSubscription() {
log.log("Initializing subscription for updates ...");
auto expirationDateTime = Clock.currTime(UTC()) + subscriptionExpirationInterval;
const(char)[] url;
url = subscriptionUrl;
const JSONValue request = [
"changeType": "updated",
"notificationUrl": notificationUrl,
"resource": "/me/drive/root",
"expirationDateTime": expirationDateTime.toISOExtString(),
"clientState": randomUUID().toString()
];
http.addRequestHeader("Content-Type", "application/json");
JSONValue response = post(url, request.toString());
// Save important subscription metadata including id and expiration
subscriptionId = response["id"].str;
subscriptionExpiration = SysTime.fromISOExtString(response["expirationDateTime"].str);
}
private void renewSubscription() {
log.log("Renewing subscription for updates ...");
auto expirationDateTime = Clock.currTime(UTC()) + subscriptionExpirationInterval;
const(char)[] url;
url = subscriptionUrl ~ "/" ~ subscriptionId;
const JSONValue request = [
"expirationDateTime": expirationDateTime.toISOExtString()
];
http.addRequestHeader("Content-Type", "application/json");
JSONValue response = patch(url, request.toString());
// Update subscription expiration from the response
subscriptionExpiration = SysTime.fromISOExtString(response["expirationDateTime"].str);
}
private void deleteSubscription() {
if (!hasValidSubscription()) {
return;
}
const(char)[] url;
url = subscriptionUrl ~ "/" ~ subscriptionId;
del(url);
log.log("Deleted subscription");
}
private void redeemToken(const(char)[] authCode)
{
const(char)[] postData =
@ -1010,14 +853,14 @@ final class OneDriveApi
private void acquireToken(const(char)[] postData)
{
JSONValue response;
try {
response = post(tokenUrl, postData);
} catch (OneDriveException e) {
// an error was generated
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
}
if (response.type() == JSONType.object) {
if ("access_token" in response){
accessToken = "bearer " ~ response["access_token"].str();
@ -1099,7 +942,7 @@ final class OneDriveApi
long thresholdFileSize = 4 * 2^^20; // 4 MiB
// open file as write in binary mode
auto file = File(filename, "wb");
// function scopes
scope(exit) {
http.clearRequestHeaders();
@ -1119,16 +962,16 @@ final class OneDriveApi
file.close();
}
}
http.method = HTTP.Method.get;
http.url = url;
addAccessTokenHeader();
http.onReceive = (ubyte[] data) {
file.rawWrite(data);
return data.length;
};
if (fileSize >= thresholdFileSize){
// Download Progress Bar
size_t iteration = 20;
@ -1166,7 +1009,7 @@ final class OneDriveApi
}
return 0;
};
// Perform download & display progress bar
try {
// try and catch any curl error
@ -1191,7 +1034,7 @@ final class OneDriveApi
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
}
}
// Check the HTTP response code, which, if a 429, will also check response headers
checkHttpCode();
}
@ -1229,13 +1072,13 @@ final class OneDriveApi
checkHttpCode();
return response;
}
private JSONValue upload(string filepath, string url)
{
checkAccessTokenExpired();
// open file as read-only in binary mode
auto file = File(filepath, "rb");
// function scopes
scope(exit) {
http.clearRequestHeaders();
@ -1250,7 +1093,7 @@ final class OneDriveApi
file.close();
}
}
http.method = HTTP.Method.put;
http.url = url;
addAccessTokenHeader();
@ -1299,7 +1142,7 @@ final class OneDriveApi
}
return data.length;
};
try {
http.perform();
// Check the HTTP Response headers - needed for correct 429 handling
@ -1311,7 +1154,7 @@ final class OneDriveApi
auto errorArray = splitLines(e.msg);
string errorMessage = errorArray[0];
string defaultTimeoutErrorMessage = " Error Message: There was a timeout in accessing the Microsoft OneDrive service - Internet connectivity issue?";
if (canFind(errorMessage, "Couldn't connect to server on handle") || canFind(errorMessage, "Couldn't resolve host name on handle") || canFind(errorMessage, "Timeout was reached on handle")) {
// This is a curl timeout
log.error(defaultTimeoutErrorMessage);
@ -1326,7 +1169,7 @@ final class OneDriveApi
while (!retrySuccess){
backoffInterval++;
int thisBackOffInterval = retryAttempts*backoffInterval;
log.vdebug(" Retry Attempt: ", retryAttempts);
log.vdebug(" Retry Attempt: ", retryAttempts);
if (thisBackOffInterval <= maxBackoffInterval) {
log.vdebug(" Retry In (seconds): ", thisBackOffInterval);
Thread.sleep(dur!"seconds"(thisBackOffInterval));
@ -1366,7 +1209,7 @@ final class OneDriveApi
// return an empty JSON for handling
return json;
}
try {
json = content.parseJSON();
} catch (JSONException e) {
@ -1375,7 +1218,7 @@ final class OneDriveApi
}
return json;
}
private void checkHTTPResponseHeaders()
{
// Get the HTTP Response headers - needed for correct 429 handling
@ -1383,7 +1226,7 @@ final class OneDriveApi
if (.debugResponse){
log.vdebug("http.perform() => HTTP Response Headers: ", responseHeaders);
}
// is retry-after in the response headers
if ("retry-after" in http.responseHeaders) {
// Set the retry-after value
@ -1397,18 +1240,18 @@ final class OneDriveApi
{
// https://dev.onedrive.com/misc/errors.htm
// https://developer.overdrive.com/docs/reference-guide
/*
HTTP/1.1 Response handling
Errors in the OneDrive API are returned using standard HTTP status codes, as well as a JSON error response object. The following HTTP status codes should be expected.
Status code Status message Description
100 Continue Continue
100 Continue Continue
200 OK Request was handled OK
201 Created This means you've made a successful POST to checkout, lock in a format, or place a hold
204 No Content This means you've made a successful DELETE to remove a hold or return a title
400 Bad Request Cannot process the request because it is malformed or incorrect.
401 Unauthorized Required authentication information is either missing or not valid for the resource.
403 Forbidden Access is denied to the requested resource. The user might not have enough permission.
@ -1425,20 +1268,20 @@ final class OneDriveApi
416 Requested Range Not Satisfiable The specified byte range is invalid or unavailable.
422 Unprocessable Entity Cannot process the request because it is semantically incorrect.
429 Too Many Requests Client application has been throttled and should not attempt to repeat the request until an amount of time has elapsed.
500 Internal Server Error There was an internal server error while processing the request.
501 Not Implemented The requested feature isnt implemented.
502 Bad Gateway The service was unreachable
503 Service Unavailable The service is temporarily unavailable. You may repeat the request after a delay. There may be a Retry-After header.
507 Insufficient Storage The maximum storage quota has been reached.
509 Bandwidth Limit Exceeded Your app has been throttled for exceeding the maximum bandwidth cap. Your app can retry the request again after more time has elapsed.
HTTP/2 Response handling
HTTP/2 Response handling
0 OK
*/
switch(http.statusLine.code)
{
// 0 - OK ... HTTP2 version of 200 OK
@ -1449,7 +1292,7 @@ final class OneDriveApi
break;
// 200 - OK
case 200:
// No Log ..
// No Log ..
break;
// 201 - Created OK
// 202 - Accepted
@ -1458,53 +1301,53 @@ final class OneDriveApi
// No actions, but log if verbose logging
//log.vlog("OneDrive Response: '", http.statusLine.code, " - ", http.statusLine.reason, "'");
break;
// 302 - resource found and available at another location, redirect
case 302:
break;
// 400 - Bad Request
case 400:
// Bad Request .. how should we act?
log.vlog("OneDrive returned a 'HTTP 400 - Bad Request' - gracefully handling error");
break;
// 403 - Forbidden
case 403:
// OneDrive responded that the user is forbidden
log.vlog("OneDrive returned a 'HTTP 403 - Forbidden' - gracefully handling error");
break;
// 404 - Item not found
case 404:
// Item was not found - do not throw an exception
log.vlog("OneDrive returned a 'HTTP 404 - Item not found' - gracefully handling error");
break;
// 408 - Request Timeout
case 408:
// Request to connect to OneDrive service timed out
log.vlog("Request Timeout - gracefully handling error");
throw new OneDriveException(408, "Request Timeout - HTTP 408 or Internet down?");
throw new OneDriveException(408, "Request Timeout - HTTP 408 or Internet down?");
// 409 - Conflict
case 409:
// Conflict handling .. how should we act? This only really gets triggered if we are using --local-first & we remove items.db as the DB thinks the file is not uploaded but it is
log.vlog("OneDrive returned a 'HTTP 409 - Conflict' - gracefully handling error");
break;
break;
// 412 - Precondition Failed
case 412:
// A precondition provided in the request (such as an if-match header) does not match the resource's current state.
log.vlog("OneDrive returned a 'HTTP 412 - Precondition Failed' - gracefully handling error");
break;
break;
// 415 - Unsupported Media Type
case 415:
// Unsupported Media Type ... sometimes triggered on image files, especially PNG
log.vlog("OneDrive returned a 'HTTP 415 - Unsupported Media Type' - gracefully handling error");
break;
// 429 - Too Many Requests
case 429:
// Too many requests in a certain time window
@ -1513,7 +1356,7 @@ final class OneDriveApi
// https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-avoid-getting-throttled-or-blocked-in-sharepoint-online
log.vlog("OneDrive returned a 'HTTP 429 - Too Many Requests' - gracefully handling error");
throw new OneDriveException(http.statusLine.code, http.statusLine.reason);
// Server side (OneDrive) Errors
// 500 - Internal Server Error
// 502 - Bad Gateway
@ -1523,17 +1366,17 @@ final class OneDriveApi
// No actions
log.vlog("OneDrive returned a 'HTTP 500 Internal Server Error' - gracefully handling error");
break;
case 502:
// No actions
log.vlog("OneDrive returned a 'HTTP 502 Bad Gateway Error' - gracefully handling error");
break;
case 503:
// No actions
log.vlog("OneDrive returned a 'HTTP 503 Service Unavailable Error' - gracefully handling error");
break;
case 504:
// No actions
log.vlog("OneDrive returned a 'HTTP 504 Gateway Timeout Error' - gracefully handling error");
@ -1541,7 +1384,7 @@ final class OneDriveApi
// "else"
default:
throw new OneDriveException(http.statusLine.code, http.statusLine.reason);
throw new OneDriveException(http.statusLine.code, http.statusLine.reason);
}
}
@ -1557,7 +1400,7 @@ final class OneDriveApi
break;
// 200 - OK
case 200:
// No Log ..
// No Log ..
break;
// 201 - Created OK
// 202 - Accepted
@ -1566,29 +1409,29 @@ final class OneDriveApi
// No actions, but log if verbose logging
//log.vlog("OneDrive Response: '", http.statusLine.code, " - ", http.statusLine.reason, "'");
break;
// 302 - resource found and available at another location, redirect
case 302:
break;
// 400 - Bad Request
case 400:
// Bad Request .. how should we act?
// make sure this is thrown so that it is caught
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
// 403 - Forbidden
case 403:
// OneDrive responded that the user is forbidden
log.vlog("OneDrive returned a 'HTTP 403 - Forbidden' - gracefully handling error");
// Throw this as a specific exception so this is caught when performing sync.o365SiteSearch
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
// 412 - Precondition Failed
case 412:
// Throw this as a specific exception so this is caught when performing sync.uploadLastModifiedTime
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
// Server side (OneDrive) Errors
// 500 - Internal Server Error
// 502 - Bad Gateway
@ -1597,19 +1440,19 @@ final class OneDriveApi
case 500:
// Throw this as a specific exception so this is caught
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
case 502:
// Throw this as a specific exception so this is caught
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
case 503:
// Throw this as a specific exception so this is caught
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
case 504:
// Throw this as a specific exception so this is caught
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
// Default - all other errors that are not a 2xx or a 302
default:
if (http.statusLine.code / 100 != 2 && http.statusLine.code != 302) {