You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kiplatform/io.h>
  20. #include <wx/string.h>
  21. #include <wx/wxcrt.h>
  22. #include <wx/filename.h>
  23. #include <windows.h>
  24. // Define USE_MSYS2_FALlBACK if the code for _MSC_VER does not compile on msys2
  25. //#define USE_MSYS2_FALLBACK
  26. FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode )
  27. {
  28. #if defined( _MSC_VER ) || !defined( USE_MSYS2_FALLBACK )
  29. // We need to use the win32 api to setup a file handle with sequential scan flagged
  30. // and pass it up the chain to create a normal FILE stream
  31. HANDLE hFile = INVALID_HANDLE_VALUE;
  32. hFile = CreateFileW( aPath.wc_str(),
  33. GENERIC_READ,
  34. FILE_SHARE_READ,
  35. NULL,
  36. OPEN_EXISTING,
  37. FILE_FLAG_SEQUENTIAL_SCAN,
  38. NULL );
  39. if (hFile == INVALID_HANDLE_VALUE)
  40. {
  41. return NULL;
  42. }
  43. int fd = _open_osfhandle( reinterpret_cast<intptr_t>( hFile ), 0 );
  44. if( fd == -1 )
  45. {
  46. // close the handle manually as the ownership didnt transfer
  47. CloseHandle( hFile );
  48. return NULL;
  49. }
  50. FILE* fp = _fdopen( fd, aMode.c_str() );
  51. if( !fp )
  52. {
  53. // close the file descriptor manually as the ownership didnt transfer
  54. _close( fd );
  55. }
  56. return fp;
  57. #else
  58. // Fallback for MSYS2
  59. return wxFopen( aPath, aMode );
  60. #endif
  61. }
  62. bool KIPLATFORM::IO::DuplicatePermissions( const wxString &aSrc, const wxString &aDest )
  63. {
  64. bool retval = false;
  65. DWORD dwSize = 0;
  66. // Retrieve the security descriptor from the source file
  67. if( GetFileSecurity( aSrc.wc_str(),
  68. OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
  69. NULL, 0, &dwSize ) )
  70. {
  71. #ifdef __MINGW32__
  72. // pSD is used as PSECURITY_DESCRIPTOR, aka void* pointer
  73. // it create an annoying warning on gcc with "delete[] pSD;" :
  74. // "warning: deleting 'PSECURITY_DESCRIPTOR' {aka 'void*'} is undefined"
  75. // so use a BYTE* pointer (do not cast it to a void pointer)
  76. BYTE* pSD = new BYTE[dwSize];
  77. #else
  78. PSECURITY_DESCRIPTOR pSD = static_cast<PSECURITY_DESCRIPTOR>( new BYTE[dwSize] );
  79. #endif
  80. if( !pSD )
  81. return false;
  82. if( !GetFileSecurity( aSrc.wc_str(),
  83. OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
  84. | DACL_SECURITY_INFORMATION, pSD, dwSize, &dwSize ) )
  85. {
  86. delete[] pSD;
  87. return false;
  88. }
  89. // Assign the retrieved security descriptor to the destination file
  90. if( !SetFileSecurity( aDest.wc_str(),
  91. OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
  92. | DACL_SECURITY_INFORMATION, pSD ) )
  93. {
  94. retval = false;
  95. }
  96. delete[] pSD;
  97. }
  98. return retval;
  99. }
  100. bool KIPLATFORM::IO::IsFileHidden( const wxString& aFileName )
  101. {
  102. bool result = false;
  103. if( ( GetFileAttributesW( aFileName.fn_str() ) & FILE_ATTRIBUTE_HIDDEN ) )
  104. result = true;
  105. return result;
  106. }
  107. void KIPLATFORM::IO::LongPathAdjustment( wxFileName& aFilename )
  108. {
  109. // dont shortcut this for shorter lengths as there are uses like directory
  110. // paths that exceed the path length when you start traversing their subdirectories
  111. // so we want to start with the long path prefix all the time
  112. if( aFilename.GetVolume().Length() == 1 )
  113. // assume single letter == drive volume
  114. aFilename.SetVolume( "\\\\?\\" + aFilename.GetVolume() + ":" );
  115. else if( aFilename.GetVolume().Length() > 1
  116. && aFilename.GetVolume().StartsWith( wxT( "\\\\" ) )
  117. && !aFilename.GetVolume().StartsWith( wxT( "\\\\?\\" ) ) )
  118. // unc path aka network share, wx returns with \\ already
  119. // so skip the first slash and combine with the prefix
  120. // which in the case of UNCs is actually \\?\UNC\<server>\<share>
  121. // where UNC is literally the text UNC
  122. aFilename.SetVolume( "\\\\?\\UNC" + aFilename.GetVolume().Mid(1) );
  123. }