mirror of
				https://github.com/c64scene-ar/llvm-6502.git
				synced 2025-11-03 14:21:30 +00:00 
			
		
		
		
	Split openFileForRead into Windows and Unix versions.
This has some advantages: * Lets us use native, utf16 windows functions. * Easy to produce good errors on windows about trying to use a directory when we want a file. * Simplifies the unix version a bit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186511 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
		@@ -722,21 +722,6 @@ error_code openFileForWrite(const Twine &Name, int &ResultFD,
 | 
			
		||||
  return error_code::success();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
error_code openFileForRead(const Twine &Name, int &ResultFD) {
 | 
			
		||||
  int OpenFlags = O_RDONLY;
 | 
			
		||||
#ifdef O_BINARY
 | 
			
		||||
  OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  SmallString<128> Storage;
 | 
			
		||||
  StringRef P = Name.toNullTerminatedStringRef(Storage);
 | 
			
		||||
  while ((ResultFD = open(P.begin(), OpenFlags)) < 0) {
 | 
			
		||||
    if (errno != EINTR)
 | 
			
		||||
      return error_code(errno, system_category());
 | 
			
		||||
  }
 | 
			
		||||
  return error_code::success();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
error_code make_absolute(SmallVectorImpl<char> &path) {
 | 
			
		||||
  StringRef p(path.data(), path.size());
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -815,6 +815,15 @@ error_code unmap_file_pages(void *base, size_t size) {
 | 
			
		||||
  return error_code::success();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
error_code openFileForRead(const Twine &Name, int &ResultFD) {
 | 
			
		||||
  SmallString<128> Storage;
 | 
			
		||||
  StringRef P = Name.toNullTerminatedStringRef(Storage);
 | 
			
		||||
  while ((ResultFD = open(P.begin(), O_RDONLY)) < 0) {
 | 
			
		||||
    if (errno != EINTR)
 | 
			
		||||
      return error_code(errno, system_category());
 | 
			
		||||
  }
 | 
			
		||||
  return error_code::success();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // end namespace fs
 | 
			
		||||
} // end namespace sys
 | 
			
		||||
 
 | 
			
		||||
@@ -1041,7 +1041,38 @@ error_code unmap_file_pages(void *base, size_t size) {
 | 
			
		||||
  return windows_error::invalid_function;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
error_code openFileForRead(const Twine &Name, int &ResultFD) {
 | 
			
		||||
  SmallString<128> PathStorage;
 | 
			
		||||
  SmallVector<wchar_t, 128> PathUTF16;
 | 
			
		||||
 | 
			
		||||
  if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
 | 
			
		||||
                                  PathUTF16))
 | 
			
		||||
    return EC;
 | 
			
		||||
 | 
			
		||||
  HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ,
 | 
			
		||||
                           FILE_SHARE_READ, NULL,
 | 
			
		||||
                           OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 | 
			
		||||
  if (H == INVALID_HANDLE_VALUE) {
 | 
			
		||||
    error_code EC = windows_error(::GetLastError());
 | 
			
		||||
    // Provide a better error massage when trying to open directories.
 | 
			
		||||
    // This only runs if we failed to open the file, so there is probably
 | 
			
		||||
    // no performances issues.
 | 
			
		||||
    if (EC != windows_error::access_denied)
 | 
			
		||||
      return EC;
 | 
			
		||||
    if (is_directory(Name))
 | 
			
		||||
      return error_code(errc::is_a_directory, posix_category());
 | 
			
		||||
    return EC;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int FD = ::_open_osfhandle(intptr_t(H), 0);
 | 
			
		||||
  if (FD == -1) {
 | 
			
		||||
    ::CloseHandle(H);
 | 
			
		||||
    return windows_error::invalid_handle;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ResultFD = FD;
 | 
			
		||||
  return error_code::success();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // end namespace fs
 | 
			
		||||
} // end namespace sys
 | 
			
		||||
 
 | 
			
		||||
@@ -3,8 +3,7 @@
 | 
			
		||||
;CHECK: .: Is a directory
 | 
			
		||||
 | 
			
		||||
; Opening a directory works on cygwin and freebsd.
 | 
			
		||||
; On windows we just get a "Permission denied."
 | 
			
		||||
;XFAIL: freebsd, win32, mingw32, cygwin
 | 
			
		||||
;XFAIL: freebsd, cygwin
 | 
			
		||||
 | 
			
		||||
;RUN: rm -f %T/test.a
 | 
			
		||||
;RUN: touch %T/a-very-long-file-name
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user