Просмотр исходного кода

core: be more fault-tolerant when file opening fails

Daniele Bartolini 8 лет назад
Родитель
Сommit
29d60e50c4

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

@@ -24,6 +24,9 @@ struct File
 	/// Closes the file.
 	virtual void close() = 0;
 
+	/// Returns whether the file is open.
+	virtual bool is_open() = 0;
+
 	/// Returns the size of file in bytes.
 	virtual u32 size() = 0;
 

+ 13 - 1
src/core/filesystem/filesystem_apk.cpp

@@ -35,7 +35,6 @@ struct FileApk : public File
 	void open(const char* path, FileOpenMode::Enum /*mode*/)
 	{
 		_asset = AAssetManager_open(_asset_manager, path, AASSET_MODE_RANDOM);
-		CE_ASSERT(_asset != NULL, "AAssetManager_open: failed to open %s", path);
 	}
 
 	void close()
@@ -47,23 +46,32 @@ struct FileApk : public File
 		}
 	}
 
+	bool is_open()
+	{
+		return _asset != NULL;
+	}
+
 	u32 size()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		return AAsset_getLength(_asset);
 	}
 
 	u32 position()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		return u32(AAsset_getLength(_asset) - AAsset_getRemainingLength(_asset));
 	}
 
 	bool end_of_file()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		return AAsset_getRemainingLength(_asset) == 0;
 	}
 
 	void seek(u32 position)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		off_t seek_result = AAsset_seek(_asset, (off_t)position, SEEK_SET);
 		CE_ASSERT(seek_result != (off_t)-1, "AAsset_seek: error");
 		CE_UNUSED(seek_result);
@@ -71,6 +79,7 @@ struct FileApk : public File
 
 	void seek_to_end()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		off_t seek_result = AAsset_seek(_asset, 0, SEEK_END);
 		CE_ASSERT(seek_result != (off_t)-1, "AAsset_seek: error");
 		CE_UNUSED(seek_result);
@@ -78,6 +87,7 @@ struct FileApk : public File
 
 	void skip(u32 bytes)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		off_t seek_result = AAsset_seek(_asset, (off_t)bytes, SEEK_CUR);
 		CE_ASSERT(seek_result != (off_t)-1, "AAsset_seek: error");
 		CE_UNUSED(seek_result);
@@ -85,12 +95,14 @@ struct FileApk : public File
 
 	u32 read(void* data, u32 size)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		CE_ENSURE(NULL != data);
 		return (u32)AAsset_read(_asset, data, size);
 	}
 
 	u32 write(const void* /*data*/, u32 /*size*/)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		CE_FATAL("Apk files are read only!");
 		return 0;
 	}

+ 10 - 7
src/core/filesystem/filesystem_disk.cpp

@@ -50,7 +50,6 @@ struct FileDisk : public File
 	{
 #if CROWN_PLATFORM_POSIX
 		_file = fopen(path, (mode == FileOpenMode::READ) ? "rb" : "wb");
-		CE_ASSERT(_file != NULL, "fopen: errno = %d, path = '%s'", errno, path);
 #elif CROWN_PLATFORM_WINDOWS
 		_file = CreateFile(path
 			, (mode == FileOpenMode::READ) ? GENERIC_READ : GENERIC_WRITE
@@ -60,11 +59,6 @@ struct FileDisk : public File
 			, FILE_ATTRIBUTE_NORMAL
 			, NULL
 			);
-		CE_ASSERT(_file != INVALID_HANDLE_VALUE
-			, "CreateFile: GetLastError = %d, path = '%s'"
-			, GetLastError()
-			, path
-			);
 #endif
 	}
 
@@ -82,7 +76,7 @@ struct FileDisk : public File
 		}
 	}
 
-	bool is_open() const
+	bool is_open()
 	{
 #if CROWN_PLATFORM_POSIX
 		return _file != NULL;
@@ -93,6 +87,7 @@ struct FileDisk : public File
 
 	u32 size()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_POSIX
 		long pos = ftell(_file);
 		CE_ASSERT(pos != -1, "ftell: errno = %d", errno);
@@ -111,6 +106,7 @@ struct FileDisk : public File
 
 	u32 position()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_POSIX
 		long pos = ftell(_file);
 		CE_ASSERT(pos != -1, "ftell: errno = %d", errno);
@@ -127,6 +123,7 @@ struct FileDisk : public File
 
 	bool end_of_file()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_POSIX
 		return feof(_file) != 0;
 #elif CROWN_PLATFORM_WINDOWS
@@ -136,6 +133,7 @@ struct FileDisk : public File
 
 	void seek(u32 position)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_POSIX
 		int err = fseek(_file, (long)position, SEEK_SET);
 		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
@@ -151,6 +149,7 @@ struct FileDisk : public File
 
 	void seek_to_end()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_POSIX
 		int err = fseek(_file, 0, SEEK_END);
 		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
@@ -166,6 +165,7 @@ struct FileDisk : public File
 
 	void skip(u32 bytes)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_POSIX
 		int err = fseek(_file, bytes, SEEK_CUR);
 		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
@@ -181,6 +181,7 @@ struct FileDisk : public File
 
 	u32 read(void* data, u32 size)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		CE_ASSERT(data != NULL, "Data must be != NULL");
 #if CROWN_PLATFORM_POSIX
 		size_t bytes_read = fread(data, 1, size, _file);
@@ -197,6 +198,7 @@ struct FileDisk : public File
 
 	u32 write(const void* data, u32 size)
 	{
+		CE_ASSERT(is_open(), "File is not open");
 		CE_ASSERT(data != NULL, "Data must be != NULL");
 #if CROWN_PLATFORM_POSIX
 		size_t bytes_written = fwrite(data, 1, size, _file);
@@ -215,6 +217,7 @@ struct FileDisk : public File
 
 	void flush()
 	{
+		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_POSIX
 		int err = fflush(_file);
 		CE_ASSERT(err == 0, "fflush: errno = %d", errno);