diff --git a/Makefile b/Makefile index d0a4bfa4..6d8d5e5b 100644 --- a/Makefile +++ b/Makefile @@ -27,8 +27,10 @@ clean: install: onedrive onedrive.conf install onedrive $(DESTDIR) - install onedrive.conf $(CONFDIR) + install -m 644 onedrive.conf $(CONFDIR) + install -m 644 onedrive.service /usr/lib/systemd/user uninstall: rm -f $(DESTDIR)/onedrive rm -f $(CONFDIR)/onedrive.conf + rm -f /usr/lib/systemd/user/onedrive.service diff --git a/onedrive.service b/onedrive.service new file mode 100644 index 00000000..71db5ae6 --- /dev/null +++ b/onedrive.service @@ -0,0 +1,10 @@ +[Unit] +Description=OneDrive Free Client +Documentation=https://github.com/skilion/onedrive + +[Service] +ExecStart=/usr/local/bin/onedrive -m +Restart=no + +[Install] +WantedBy=default.target diff --git a/src/main.d b/src/main.d index 7de2f4fe..37f42905 100644 --- a/src/main.d +++ b/src/main.d @@ -1,6 +1,6 @@ import core.memory, core.time, core.thread; import std.getopt, std.file, std.path, std.process, std.stdio; -import config, itemdb, monitor, onedrive, sync; +import config, itemdb, monitor, onedrive, sync, util; void main(string[] args) @@ -50,6 +50,11 @@ void main(string[] args) auto cfg = config.Config(configFile1Path, configFile2Path, configFile3Path); if (verbose) writeln("Initializing the OneDrive API ..."); + bool online = testNetwork(); + if (!online && !monitor) { + writeln("No network connection"); + return; + } auto onedrive = new OneDriveApi(cfg, verbose); onedrive.onRefreshToken = (string refreshToken) { std.file.write(refreshTokenFilePath, refreshToken); @@ -60,7 +65,6 @@ void main(string[] args) } catch (FileException e) { onedrive.authorize(); } - // TODO check if the token is valid if (verbose) writeln("Opening the item database ..."); auto itemdb = new ItemDatabase(databaseFilePath); @@ -81,7 +85,7 @@ void main(string[] args) // swallow exception } sync.init(statusToken); - performSync(sync); + if (online) performSync(sync); if (monitor) { if (verbose) writeln("Initializing monitor ..."); @@ -123,13 +127,16 @@ void main(string[] args) immutable auto checkInterval = dur!"seconds"(45); auto lastCheckTime = MonoTime.currTime(); while (true) { - m.update(); + m.update(online); auto currTime = MonoTime.currTime(); if (currTime - lastCheckTime > checkInterval) { lastCheckTime = currTime; - performSync(sync); - // discard all events that may have been generated by the sync - m.update(false); + online = testNetwork(); + if (online) { + performSync(sync); + // discard all events that may have been generated by the sync + m.update(false); + } GC.collect(); } else { Thread.sleep(dur!"msecs"(100)); diff --git a/src/onedrive.d b/src/onedrive.d index cdcb604f..e68d3ca6 100644 --- a/src/onedrive.d +++ b/src/onedrive.d @@ -63,7 +63,6 @@ final class OneDriveApi void setRefreshToken(string refreshToken) { this.refreshToken = refreshToken; - newToken(); } // https://dev.onedrive.com/items/view_changes.htm diff --git a/src/util.d b/src/util.d index 1843bd8e..e9b31c9e 100644 --- a/src/util.d +++ b/src/util.d @@ -1,11 +1,16 @@ -import std.conv, std.digest.crc, std.digest.digest, std.file, std.path; -import std.regex, std.stdio, std.string: chomp; +import std.conv; +import std.digest.crc; +import std.file; +import std.path; +import std.regex; +import std.socket; +import std.stdio; +import std.string; private string deviceName; static this() { - import std.socket; deviceName = Socket.hostName; } @@ -66,3 +71,15 @@ Regex!char wild2regex(const(char)[] pattern) str ~= "$"; return regex(str, "i"); } + +// return true if the network connection is available +bool testNetwork() +{ + try { + auto addr = new InternetAddress("login.live.com", 443); + auto socket = new TcpSocket(addr); + return socket.isAlive(); + } catch (SocketException) { + return false; + } +}