Daniele Bartolini 8 gadi atpakaļ
vecāks
revīzija
72191f282a

+ 0 - 3
src/core/filesystem/filesystem.h

@@ -46,9 +46,6 @@ public:
 	/// Deletes the directory at the given @a path.
 	virtual void delete_directory(const char* path) = 0;
 
-	/// Creates the file at the given @a path.
-	virtual void create_file(const char* path) = 0;
-
 	/// Deletes the file at the given @a path.
 	virtual void delete_file(const char* path) = 0;
 

+ 0 - 5
src/core/filesystem/filesystem_apk.cpp

@@ -153,11 +153,6 @@ void FilesystemApk::delete_directory(const char* /*path*/)
 	CE_FATAL("Cannot delete directory in Android assets folder");
 }
 
-void FilesystemApk::create_file(const char* /*path*/)
-{
-	CE_FATAL("Cannot create file in Android assets folder");
-}
-
 void FilesystemApk::delete_file(const char* /*path*/)
 {
 	CE_FATAL("Cannot delete file in Android assets folder");

+ 0 - 3
src/core/filesystem/filesystem_apk.h

@@ -52,9 +52,6 @@ public:
 	/// @copydoc Filesystem::delete_directory()
 	void delete_directory(const char* path);
 
-	/// @copydoc Filesystem::create_file()
-	void create_file(const char* path);
-
 	/// @copydoc Filesystem::delete_file()
 	void delete_file(const char* path);
 

+ 18 - 17
src/core/filesystem/filesystem_disk.cpp

@@ -268,7 +268,9 @@ bool FilesystemDisk::exists(const char* path)
 	DynamicString abs_path(ta);
 	get_absolute_path(path, abs_path);
 
-	return os::exists(abs_path.c_str());
+	Stat info;
+	os::stat(info, abs_path.c_str());
+	return info.file_type != Stat::NO_ENTRY;
 }
 
 bool FilesystemDisk::is_directory(const char* path)
@@ -279,7 +281,9 @@ bool FilesystemDisk::is_directory(const char* path)
 	DynamicString abs_path(ta);
 	get_absolute_path(path, abs_path);
 
-	return os::is_directory(abs_path.c_str());
+	Stat info;
+	os::stat(info, abs_path.c_str());
+	return info.file_type == Stat::DIRECTORY;
 }
 
 bool FilesystemDisk::is_file(const char* path)
@@ -290,7 +294,9 @@ bool FilesystemDisk::is_file(const char* path)
 	DynamicString abs_path(ta);
 	get_absolute_path(path, abs_path);
 
-	return os::is_file(abs_path.c_str());
+	Stat info;
+	os::stat(info, abs_path.c_str());
+	return info.file_type == Stat::REGULAR;
 }
 
 u64 FilesystemDisk::last_modified_time(const char* path)
@@ -301,7 +307,9 @@ u64 FilesystemDisk::last_modified_time(const char* path)
 	DynamicString abs_path(ta);
 	get_absolute_path(path, abs_path);
 
-	return os::mtime(abs_path.c_str());
+	Stat info;
+	os::stat(info, abs_path.c_str());
+	return info.mtime;
 }
 
 void FilesystemDisk::create_directory(const char* path)
@@ -312,8 +320,12 @@ void FilesystemDisk::create_directory(const char* path)
 	DynamicString abs_path(ta);
 	get_absolute_path(path, abs_path);
 
-	if (!os::exists(abs_path.c_str()))
-		os::create_directory(abs_path.c_str());
+	Stat info;
+	os::stat(info, abs_path.c_str());
+	if (info.file_type != Stat::NO_ENTRY)
+		return;
+
+	os::create_directory(abs_path.c_str());
 }
 
 void FilesystemDisk::delete_directory(const char* path)
@@ -327,17 +339,6 @@ void FilesystemDisk::delete_directory(const char* path)
 	os::delete_directory(abs_path.c_str());
 }
 
-void FilesystemDisk::create_file(const char* path)
-{
-	CE_ENSURE(NULL != path);
-
-	TempAllocator256 ta;
-	DynamicString abs_path(ta);
-	get_absolute_path(path, abs_path);
-
-	os::create_file(abs_path.c_str());
-}
-
 void FilesystemDisk::delete_file(const char* path)
 {
 	CE_ENSURE(NULL != path);

+ 0 - 3
src/core/filesystem/filesystem_disk.h

@@ -56,9 +56,6 @@ public:
 	/// @copydoc Filesystem::delete_directory()
 	void delete_directory(const char* path);
 
-	/// @copydoc Filesystem::create_file()
-	void create_file(const char* path);
-
 	/// @copydoc Filesystem::delete_file()
 	void delete_file(const char* path);
 

+ 25 - 84
src/core/os.cpp

@@ -19,10 +19,10 @@
 	#include <stdio.h>    // fputs
 	#include <stdlib.h>   // getenv
 	#include <string.h>   // memset
-	#include <sys/stat.h> // lstat, mknod, mkdir
+	#include <sys/stat.h> // stat, mkdir
 	#include <sys/wait.h> // wait
 	#include <time.h>     // clock_gettime
-	#include <unistd.h>   // access, unlink, rmdir, getcwd, fork, execv
+	#include <unistd.h>   // unlink, rmdir, getcwd, fork, execv
 #elif CROWN_PLATFORM_WINDOWS
 	#include <io.h>
 	#include <stdio.h>
@@ -119,96 +119,37 @@ namespace os
 #endif
 	}
 
-	/// Returns whether the @a path exists.
-	bool exists(const char* path)
+	void stat(Stat& info, const char* path)
 	{
-#if CROWN_PLATFORM_POSIX
-		return access(path, F_OK) != -1;
-#elif CROWN_PLATFORM_WINDOWS
-		return _access(path, 0) != -1;
-#endif
-	}
+		info.file_type = Stat::NO_ENTRY;
+		info.size = 0;
+		info.mtime = 0;
 
-	/// Returns whether @a path is a directory.
-	bool is_directory(const char* path)
-	{
 #if CROWN_PLATFORM_POSIX
-		struct stat info;
-		memset(&info, 0, sizeof(info));
-		int err = lstat(path, &info);
-		CE_ASSERT(err == 0, "lstat: errno = %d", errno);
-		CE_UNUSED(err);
-		return ((S_ISDIR(info.st_mode) == 1) && (S_ISLNK(info.st_mode) == 0));
-#elif CROWN_PLATFORM_WINDOWS
-		DWORD fattr = GetFileAttributes(path);
-		return (fattr != INVALID_FILE_ATTRIBUTES && (fattr & FILE_ATTRIBUTE_DIRECTORY) != 0);
-#endif
-	}
+		struct stat buf;
+		memset(&buf, 0, sizeof(buf));
+		int err = ::stat(path, &buf);
+		if (err != 0)
+			return;
 
-	/// Returns whether @a path is a regular file.
-	bool is_file(const char* path)
-	{
-#if CROWN_PLATFORM_POSIX
-		struct stat info;
-		memset(&info, 0, sizeof(info));
-		int err = lstat(path, &info);
-		CE_ASSERT(err == 0, "lstat: errno = %d", errno);
-		CE_UNUSED(err);
-		return ((S_ISREG(info.st_mode) == 1) && (S_ISLNK(info.st_mode) == 0));
+		if (S_ISREG(buf.st_mode) == 1)
+			info.file_type = Stat::REGULAR;
+		else if (S_ISDIR(buf.st_mode) == 1)
+			info.file_type = Stat::DIRECTORY;
 #elif CROWN_PLATFORM_WINDOWS
-		DWORD fattr = GetFileAttributes(path);
-		return (fattr != INVALID_FILE_ATTRIBUTES && (fattr & FILE_ATTRIBUTE_DIRECTORY) == 0);
-#endif
-	}
+		struct _stat64 buf;
+		int err = ::_stat64(path, &buf);
+		if (err != 0)
+			return;
 
-	/// Returns the last modification time of @a path.
-	u64 mtime(const char* path)
-	{
-#if CROWN_PLATFORM_POSIX
-		struct stat info;
-		memset(&info, 0, sizeof(info));
-		int err = lstat(path, &info);
-		CE_ASSERT(err == 0, "lstat: errno = %d", errno);
-		CE_UNUSED(err);
-		return info.st_mtime;
-#elif CROWN_PLATFORM_WINDOWS
-		HANDLE hfile = CreateFile(path
-			, GENERIC_READ
-			, FILE_SHARE_READ
-			, NULL
-			, OPEN_EXISTING
-			, 0
-			, NULL
-			);
-		CE_ASSERT(hfile != INVALID_HANDLE_VALUE, "CreateFile: GetLastError = %d", GetLastError());
-		FILETIME ftwrite;
-		BOOL err = GetFileTime(hfile, NULL, NULL, &ftwrite);
-		CE_ASSERT(err != 0, "GetFileTime: GetLastError = %d", GetLastError());
-		CE_UNUSED(err);
-		CloseHandle(hfile);
-		return (u64)((u64(ftwrite.dwHighDateTime) << 32) | ftwrite.dwLowDateTime);
+		if ((buf.st_mode & _S_IFREG) != 0)
+			info.file_type = Stat::REGULAR;
+		else if ((buf.st_mode & _S_IFDIR) != 0)
+			info.file_type = Stat::DIRECTORY;
 #endif
-	}
 
-	/// Creates a regular file named @a path.
-	void create_file(const char* path)
-	{
-#if CROWN_PLATFORM_POSIX
-		int err = ::mknod(path, 0644 | S_IFREG , 0);
-		CE_ASSERT(err == 0, "mknod: errno = %d", errno);
-		CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-		HANDLE hfile = CreateFile(path
-			, GENERIC_READ | GENERIC_WRITE
-			, 0
-			, NULL
-			, CREATE_ALWAYS
-			, FILE_ATTRIBUTE_NORMAL
-			, NULL
-			);
-		CE_ASSERT(hfile != INVALID_HANDLE_VALUE, "CreateFile: GetLastError = %d", GetLastError());
-		CloseHandle(hfile);
-#endif
+		info.size = buf.st_size;
+		info.mtime = buf.st_mtime;
 	}
 
 	/// Deletes the file at @a path.

+ 18 - 14
src/core/os.h

@@ -12,6 +12,22 @@
 /// @ingroup Core
 namespace crown
 {
+/// Holds information about a file.
+///
+/// @ingroup OS
+struct Stat
+{
+	enum FileType
+	{
+		REGULAR,
+		DIRECTORY,
+		NO_ENTRY
+	} file_type;
+
+	u64 size;  ///< Size in bytes.
+	u64 mtime; ///< Last modified time.
+};
+
 /// Operating system functions.
 ///
 /// @ingroup OS
@@ -38,20 +54,8 @@ namespace os
 	/// Logs the message @a msg.
 	void log(const char* msg);
 
-	/// Returns whether the @a path exists.
-	bool exists(const char* path);
-
-	/// Returns whether @a path is a directory.
-	bool is_directory(const char* path);
-
-	/// Returns whether @a path is a regular file.
-	bool is_file(const char* path);
-
-	/// Returns the last modification time of @a path.
-	u64 mtime(const char* path);
-
-	/// Creates a regular file named @a path.
-	void create_file(const char* path);
+	/// Returns information about @a path.
+	void stat(Stat& info, const char* path);
 
 	/// Deletes the file at @a path.
 	void delete_file(const char* path);