KiCad PCB EDA Suite
Loading...
Searching...
No Matches
windows/io.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3*
4* Copyright The 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
20#include <kiplatform/io.h>
21
22#include <wx/string.h>
23#include <wx/wxcrt.h>
24#include <wx/filename.h>
25#include <windows.h>
26
27// Define USE_MSYS2_FALlBACK if the code for _MSC_VER does not compile on msys2
28//#define USE_MSYS2_FALLBACK
29
30FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode )
31{
32#if defined( _MSC_VER ) || !defined( USE_MSYS2_FALLBACK )
33 // We need to use the win32 api to setup a file handle with sequential scan flagged
34 // and pass it up the chain to create a normal FILE stream
35 HANDLE hFile = INVALID_HANDLE_VALUE;
36 hFile = CreateFileW( aPath.wc_str(),
37 GENERIC_READ,
38 FILE_SHARE_READ,
39 NULL,
40 OPEN_EXISTING,
41 FILE_FLAG_SEQUENTIAL_SCAN,
42 NULL );
43
44 if (hFile == INVALID_HANDLE_VALUE)
45 {
46 return NULL;
47 }
48
49 int fd = _open_osfhandle( reinterpret_cast<intptr_t>( hFile ), 0 );
50
51 if( fd == -1 )
52 {
53 // close the handle manually as the ownership didnt transfer
54 CloseHandle( hFile );
55 return NULL;
56 }
57
58 FILE* fp = _fdopen( fd, aMode.c_str() );
59
60 if( !fp )
61 {
62 // close the file descriptor manually as the ownership didnt transfer
63 _close( fd );
64 }
65
66 return fp;
67#else
68 // Fallback for MSYS2
69 return wxFopen( aPath, aMode );
70#endif
71}
72
73bool KIPLATFORM::IO::DuplicatePermissions( const wxString &aSrc, const wxString &aDest )
74{
75 bool retval = false;
76 DWORD dwSize = 0;
77
78 // Retrieve the security descriptor from the source file
79 if( GetFileSecurity( aSrc.wc_str(),
80 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
81 NULL, 0, &dwSize ) )
82 {
83 #ifdef __MINGW32__
84 // pSD is used as PSECURITY_DESCRIPTOR, aka void* pointer
85 // it create an annoying warning on gcc with "delete[] pSD;" :
86 // "warning: deleting 'PSECURITY_DESCRIPTOR' {aka 'void*'} is undefined"
87 // so use a BYTE* pointer (do not cast it to a void pointer)
88 BYTE* pSD = new BYTE[dwSize];
89 #else
90 PSECURITY_DESCRIPTOR pSD = static_cast<PSECURITY_DESCRIPTOR>( new BYTE[dwSize] );
91 #endif
92
93 if( !pSD )
94 return false;
95
96 if( !GetFileSecurity( aSrc.wc_str(),
97 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
98 | DACL_SECURITY_INFORMATION, pSD, dwSize, &dwSize ) )
99 {
100 delete[] pSD;
101 return false;
102 }
103
104 // Assign the retrieved security descriptor to the destination file
105 if( !SetFileSecurity( aDest.wc_str(),
106 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
107 | DACL_SECURITY_INFORMATION, pSD ) )
108 {
109 retval = false;
110 }
111
112 delete[] pSD;
113 }
114
115 return retval;
116}
117
118bool KIPLATFORM::IO::MakeWriteable( const wxString& aFilePath )
119{
120 DWORD attrs = GetFileAttributesW( aFilePath.wc_str() );
121
122 if( attrs == INVALID_FILE_ATTRIBUTES )
123 return false;
124
125 // Remove read-only attribute if present
126 if( attrs & FILE_ATTRIBUTE_READONLY )
127 {
128 attrs &= ~FILE_ATTRIBUTE_READONLY;
129 return SetFileAttributesW( aFilePath.wc_str(), attrs ) != 0;
130 }
131
132 return true; // Already writeable
133}
134
135bool KIPLATFORM::IO::IsFileHidden( const wxString& aFileName )
136{
137 bool result = false;
138
139 if( ( GetFileAttributesW( aFileName.fn_str() ) & FILE_ATTRIBUTE_HIDDEN ) )
140 result = true;
141
142 return result;
143}
144
145
146void KIPLATFORM::IO::LongPathAdjustment( wxFileName& aFilename )
147{
148 // dont shortcut this for shorter lengths as there are uses like directory
149 // paths that exceed the path length when you start traversing their subdirectories
150 // so we want to start with the long path prefix all the time
151
152 if( aFilename.GetVolume().Length() == 1 )
153 // assume single letter == drive volume
154 aFilename.SetVolume( "\\\\?\\" + aFilename.GetVolume() + ":" );
155 else if( aFilename.GetVolume().Length() > 1
156 && aFilename.GetVolume().StartsWith( wxT( "\\\\" ) )
157 && !aFilename.GetVolume().StartsWith( wxT( "\\\\?" ) ) )
158 // unc path aka network share, wx returns with \\ already
159 // so skip the first slash and combine with the prefix
160 // which in the case of UNCs is actually \\?\UNC<server><share>
161 // where UNC is literally the text UNC
162 aFilename.SetVolume( "\\\\?\\UNC" + aFilename.GetVolume().Mid( 1 ) );
163 else if( aFilename.GetVolume().StartsWith( wxT( "\\\\?" ) )
164 && aFilename.GetDirs().size() >= 2
165 && aFilename.GetDirs()[0] == "UNC" )
166 {
167 // wxWidgets can parse \\?\UNC<server> into a mess
168 // UNC gets stored into a directory
169 // volume gets reduced to just \\?
170 // so we need to repair it
171 aFilename.SetVolume( "\\\\?\\UNC\\" + aFilename.GetDirs()[1] );
172 aFilename.RemoveDir( 0 );
173 aFilename.RemoveDir( 0 );
174 }
175}
void LongPathAdjustment(wxFileName &aFilename)
Adjusts a filename to be a long path compatible.
Definition unix/io.cpp:93
FILE * SeqFOpen(const wxString &aPath, const wxString &mode)
Opens the file like fopen but sets flags (if available) for sequential read hinting.
Definition unix/io.cpp:31
bool DuplicatePermissions(const wxString &aSrc, const wxString &aDest)
Duplicates the file security data from one file to another ensuring that they are the same between bo...
Definition unix/io.cpp:47
bool IsFileHidden(const wxString &aFileName)
Helper function to determine the status of the 'Hidden' file attribute.
Definition unix/io.cpp:85
bool MakeWriteable(const wxString &aFilePath)
Ensures that a file has write permissions.
Definition unix/io.cpp:70
wxString result
Test unit parsing edge cases and error handling.