Browse Source

Add shutdownIfUnreadable file feature: shut down if shutdownIfUnreadable in home folder is in fact existent but unreadable (e.g. broken link). This enables nifty shutdown on .app trashing feature for OSX.

Adam Ierymenko 11 years ago
parent
commit
b699bdefbd
3 changed files with 21 additions and 5 deletions
  1. 8 0
      node/Node.cpp
  2. 11 1
      node/Utils.cpp
  3. 2 4
      node/Utils.hpp

+ 8 - 0
node/Node.cpp

@@ -467,6 +467,7 @@ Node::ReasonForTermination Node::run()
 
 
 	// Core I/O loop
 	// Core I/O loop
 	try {
 	try {
+		std::string shutdownIfUnreadablePath(_r->homePath + ZT_PATH_SEPARATOR_S + "shutdownIfUnreadable");
 		uint64_t lastNetworkAutoconfCheck = Utils::now() - 5000; // check autoconf again after 5s for startup
 		uint64_t lastNetworkAutoconfCheck = Utils::now() - 5000; // check autoconf again after 5s for startup
 		uint64_t lastPingCheck = 0;
 		uint64_t lastPingCheck = 0;
 		uint64_t lastClean = Utils::now(); // don't need to do this immediately
 		uint64_t lastClean = Utils::now(); // don't need to do this immediately
@@ -476,6 +477,13 @@ Node::ReasonForTermination Node::run()
 		long lastDelayDelta = 0;
 		long lastDelayDelta = 0;
 
 
 		while (impl->reasonForTermination == NODE_RUNNING) {
 		while (impl->reasonForTermination == NODE_RUNNING) {
+			if (Utils::fileExists(shutdownIfUnreadablePath.c_str(),false)) {
+				FILE *tmpf = fopen(shutdownIfUnreadablePath.c_str(),"r");
+				if (!tmpf)
+					return impl->terminateBecause(Node::NODE_NORMAL_TERMINATION,"shutdownIfUnreadable was not readable");
+				fclose(tmpf);
+			}
+
 			uint64_t now = Utils::now();
 			uint64_t now = Utils::now();
 			bool resynchronize = false;
 			bool resynchronize = false;
 
 

+ 11 - 1
node/Utils.cpp

@@ -246,7 +246,7 @@ no getSecureRandom() implementation;
 
 
 void Utils::lockDownFile(const char *path,bool isDir)
 void Utils::lockDownFile(const char *path,bool isDir)
 {
 {
-#if defined(__APPLE__) || defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
+#ifdef __UNIX_LIKE__
 	chmod(path,isDir ? 0700 : 0600);
 	chmod(path,isDir ? 0700 : 0600);
 #else
 #else
 #ifdef _WIN32
 #ifdef _WIN32
@@ -263,6 +263,16 @@ uint64_t Utils::getLastModified(const char *path)
 	return (((uint64_t)s.st_mtime) * 1000ULL);
 	return (((uint64_t)s.st_mtime) * 1000ULL);
 }
 }
 
 
+bool Utils::fileExists(const char *path,bool followLinks)
+{
+	struct stat s;
+#ifdef __UNIX_LIKE__
+	if (!followLinks)
+		return (lstat(path,&s) == 0);
+#endif
+	return (stat(path,&s) == 0);
+}
+
 int64_t Utils::getFileSize(const char *path)
 int64_t Utils::getFileSize(const char *path)
 {
 {
 	struct stat s;
 	struct stat s;

+ 2 - 4
node/Utils.hpp

@@ -177,12 +177,10 @@ public:
 
 
 	/**
 	/**
 	 * @param path Path to check
 	 * @param path Path to check
+	 * @param followLinks Follow links (on platforms with that concept)
 	 * @return True if file or directory exists at path location
 	 * @return True if file or directory exists at path location
 	 */
 	 */
-	static inline bool fileExists(const char *path)
-	{
-		return (getLastModified(path) != 0);
-	}
+	static bool fileExists(const char *path,bool followLinks = true);
 
 
 	/**
 	/**
 	 * @param path Path to file
 	 * @param path Path to file