Browse Source

WinRT: Avoid the use of IsAvailable function which is unavailable under WP8

Lou Yihua 6 years ago
parent
commit
6489ebaf44
1 changed files with 24 additions and 10 deletions
  1. 24 10
      winrt/SDLPal.Common/WinRTIO.cpp

+ 24 - 10
winrt/SDLPal.Common/WinRTIO.cpp

@@ -424,20 +424,34 @@ int WRT_access(const char * const _FileName, int _AccessMode)
 		return -1;
 	}
 
-	auto file = AWait(Windows::Storage::StorageFile::GetFileFromPathAsync(ConvertString(_FileName)));
-	if (!file->IsAvailable)
+	try
 	{
-		_set_errno(ENOENT);
-		return -1;
-	}
+		auto file = AWait(Windows::Storage::StorageFile::GetFileFromPathAsync(ConvertString(_FileName)));
 
-	if ((file->Attributes & Windows::Storage::FileAttributes::Directory) != Windows::Storage::FileAttributes::Directory &&
-		(file->Attributes & Windows::Storage::FileAttributes::ReadOnly) == Windows::Storage::FileAttributes::ReadOnly &&
-		(_AccessMode & 0x2) != 0)
+		if ((file->Attributes & Windows::Storage::FileAttributes::Directory) != Windows::Storage::FileAttributes::Directory &&
+			(file->Attributes & Windows::Storage::FileAttributes::ReadOnly) == Windows::Storage::FileAttributes::ReadOnly &&
+			(_AccessMode & 0x2) != 0)
+		{
+			throw ref new Platform::AccessDeniedException();
+		}
+
+		return 0;
+	}
+	catch (AccessDeniedException^ e)
 	{
 		_set_errno(EACCES);
 		return -1;
 	}
-
-	return 0;
+	catch (Exception^ e)
+	{
+		if (e->HResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
+		{
+			_set_errno(ENOENT);
+		}
+		else
+		{
+			_set_errno(EINVAL);
+		}
+		return -1;
+	}
 }