Browse Source

[BUGFIX] Fix some INotify and removeDirectory()

Panagiotis Christopoulos Charitos 8 years ago
parent
commit
6aa4687e65

+ 2 - 0
src/anki/Config.h.cmake

@@ -154,6 +154,7 @@
 #	define ANKI_FORCE_INLINE __attribute__((always_inline))
 #	define ANKI_FORCE_INLINE __attribute__((always_inline))
 #	define ANKI_DONT_INLINE __attribute__((noinline))
 #	define ANKI_DONT_INLINE __attribute__((noinline))
 #	define ANKI_UNUSED __attribute__((__unused__))
 #	define ANKI_UNUSED __attribute__((__unused__))
+#	define ANKI_COLD __attribute__((cold, optimize("Os")))
 #else
 #else
 #	define ANKI_LIKELY(x) ((x) == 1)
 #	define ANKI_LIKELY(x) ((x) == 1)
 #	define ANKI_UNLIKELY(x) ((x) == 1)
 #	define ANKI_UNLIKELY(x) ((x) == 1)
@@ -162,6 +163,7 @@
 #	define ANKI_FORCE_INLINE
 #	define ANKI_FORCE_INLINE
 #	define ANKI_DONT_INLINE
 #	define ANKI_DONT_INLINE
 #	define ANKI_UNUSED
 #	define ANKI_UNUSED
+#	define ANKI_COLD
 #endif
 #endif
 
 
 // Pack structs
 // Pack structs

+ 2 - 3
src/anki/resource/ResourceManager.h

@@ -30,9 +30,6 @@ template<typename Type>
 class TypeResourceManager
 class TypeResourceManager
 {
 {
 protected:
 protected:
-	/// @privatesection
-	using Container = List<Type*>;
-
 	TypeResourceManager()
 	TypeResourceManager()
 	{
 	{
 	}
 	}
@@ -69,6 +66,8 @@ protected:
 	}
 	}
 
 
 private:
 private:
+	using Container = List<Type*>;
+
 	ResourceAllocator<U8> m_alloc;
 	ResourceAllocator<U8> m_alloc;
 	Container m_ptrs;
 	Container m_ptrs;
 
 

+ 1 - 1
src/anki/resource/ResourceManager.inl.h

@@ -16,7 +16,7 @@ Error ResourceManager::loadResource(const CString& filename, ResourcePtr<T>& out
 	Error err = Error::NONE;
 	Error err = Error::NONE;
 	++m_loadRequestCount;
 	++m_loadRequestCount;
 
 
-	T* other = findLoadedResource<T>(filename);
+	T* const other = findLoadedResource<T>(filename);
 
 
 	if(other)
 	if(other)
 	{
 	{

+ 9 - 4
src/anki/util/FilesystemPosix.cpp

@@ -111,9 +111,9 @@ Error walkDirectoryTree(const CString& dir, void* userData, WalkDirectoryTreeCal
 
 
 // To avoid having the g_removeDirectoryPath in stack or having to allocate it, make it global.
 // To avoid having the g_removeDirectoryPath in stack or having to allocate it, make it global.
 static char g_removeDirectoryPath[PATH_MAX];
 static char g_removeDirectoryPath[PATH_MAX];
-Mutex g_removeDirectoryLock;
+static Mutex g_removeDirectoryLock;
 
 
-Error removeDirectory(const CString& dirname)
+static Error removeDirectoryInternal(const CString& dirname)
 {
 {
 	DIR* dir;
 	DIR* dir;
 	struct dirent* entry;
 	struct dirent* entry;
@@ -125,7 +125,6 @@ Error removeDirectory(const CString& dirname)
 		return Error::FUNCTION_FAILED;
 		return Error::FUNCTION_FAILED;
 	}
 	}
 
 
-	LockGuard<Mutex> lock(g_removeDirectoryLock);
 	while((entry = readdir(dir)) != nullptr)
 	while((entry = readdir(dir)) != nullptr)
 	{
 	{
 		if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
 		if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
@@ -134,7 +133,7 @@ Error removeDirectory(const CString& dirname)
 
 
 			if(entry->d_type == DT_DIR)
 			if(entry->d_type == DT_DIR)
 			{
 			{
-				Error err = removeDirectory(CString(g_removeDirectoryPath));
+				Error err = removeDirectoryInternal(CString(g_removeDirectoryPath));
 				if(err)
 				if(err)
 				{
 				{
 					return err;
 					return err;
@@ -153,6 +152,12 @@ Error removeDirectory(const CString& dirname)
 	return Error::NONE;
 	return Error::NONE;
 }
 }
 
 
+Error removeDirectory(const CString& dirname)
+{
+	LockGuard<Mutex> lock(g_removeDirectoryLock);
+	return removeDirectoryInternal(dirname);
+}
+
 Error createDirectory(const CString& dir)
 Error createDirectory(const CString& dir)
 {
 {
 	if(directoryExists(dir))
 	if(directoryExists(dir))

+ 7 - 5
src/anki/util/INotifyLinux.cpp

@@ -93,11 +93,13 @@ Error INotify::pollEvents(Bool& modified)
 		{
 		{
 			// Process the new event
 			// Process the new event
 
 
-			inotify_event event;
-			int nbytes = read(m_fd, &event, sizeof(event));
-			if(nbytes == sizeof(event))
+			Array<U8, 2_KB> readBuff;
+			int nbytes = read(m_fd, &readBuff[0], sizeof(readBuff));
+			if(nbytes > 0)
 			{
 			{
-				if(event.mask & IN_IGNORED)
+				inotify_event* event = reinterpret_cast<inotify_event*>(&readBuff[0]);
+
+				if(event->mask & IN_IGNORED)
 				{
 				{
 					// File was moved or deleted. Some editors on save they delete the file and move another file to
 					// File was moved or deleted. Some editors on save they delete the file and move another file to
 					// its place. In that case the m_fd and the m_watch need to be re-created.
 					// its place. In that case the m_fd and the m_watch need to be re-created.
@@ -117,7 +119,7 @@ Error INotify::pollEvents(Bool& modified)
 			}
 			}
 			else
 			else
 			{
 			{
-				ANKI_UTIL_LOGE("read() failed to read the expected size of data");
+				ANKI_UTIL_LOGE("read() failed to read the expected size of data: %s", strerror(errno));
 				err = Error::FUNCTION_FAILED;
 				err = Error::FUNCTION_FAILED;
 				break;
 				break;
 			}
 			}

+ 39 - 0
tests/util/INotify.cpp

@@ -0,0 +1,39 @@
+// Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include <tests/framework/Framework.h>
+#include <anki/util/INotify.h>
+#include <anki/util/Filesystem.h>
+
+ANKI_TEST(Util, INotify)
+{
+	HeapAllocator<U8> alloc(allocAligned, nullptr);
+
+	// Monitor a dir
+	{
+		CString dir = "in_test_dir";
+
+		ANKI_TEST_EXPECT_NO_ERR(createDirectory(dir));
+
+		{
+			INotify in;
+			ANKI_TEST_EXPECT_NO_ERR(in.init(alloc, dir));
+
+			Bool modified;
+			ANKI_TEST_EXPECT_NO_ERR(in.pollEvents(modified));
+			ANKI_TEST_EXPECT_EQ(modified, false);
+
+			File file;
+			ANKI_TEST_EXPECT_NO_ERR(
+				file.open(StringAuto(alloc).sprintf("%s/file.txt", dir.cstr()).toCString(), FileOpenFlag::WRITE));
+			file.close();
+
+			ANKI_TEST_EXPECT_NO_ERR(in.pollEvents(modified));
+			ANKI_TEST_EXPECT_EQ(modified, true);
+		}
+
+		ANKI_TEST_EXPECT_NO_ERR(removeDirectory(dir));
+	}
+}