KiCad PCB EDA Suite
Loading...
Searching...
No Matches
msw/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 (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
20#include <kiplatform/io.h>
21
22#include <wx/string.h>
23#include <wx/wxcrt.h>
24
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::IsFileHidden( const wxString& aFileName )
119{
120 bool result = false;
121
122 if( ( GetFileAttributesW( aFileName.fn_str() ) & FILE_ATTRIBUTE_HIDDEN ) )
123 result = true;
124
125 return result;
126}
FILE * SeqFOpen(const wxString &aPath, const wxString &mode)
Opens the file like fopen but sets flags (if available) for sequential read hinting.
Definition: gtk/io.cpp:30
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: gtk/io.cpp:40
bool IsFileHidden(const wxString &aFileName)
Helper function to determine the status of the 'Hidden' file attribute.
Definition: gtk/io.cpp:63