KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unix/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 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6*
7* This program is free software: you can redistribute it and/or modify it
8* under the terms of the GNU General Public License as published by the
9* Free Software Foundation, either version 3 of the License, or (at your
10* option) any later version.
11*
12* This program is distributed in the hope that it will be useful, but
13* WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License along
18* with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <kiplatform/io.h>
22
23#include <wx/crt.h>
24#include <wx/string.h>
25#include <wx/filename.h>
26
27#include <climits>
28#include <dirent.h>
29#include <fcntl.h>
30#include <fnmatch.h>
31#include <string>
32#include <sys/mman.h>
33#include <sys/stat.h>
34#include <unistd.h>
35
36FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode )
37{
38 FILE* fp = wxFopen( aPath, aMode );
39
40 if( fp )
41 {
42 if( posix_fadvise( fileno( fp ), 0, 0, POSIX_FADV_SEQUENTIAL ) != 0 )
43 {
44 fclose( fp );
45 fp = nullptr;
46 }
47 }
48
49 return fp;
50}
51
52bool KIPLATFORM::IO::DuplicatePermissions( const wxString &aSrc, const wxString &aDest )
53{
54 struct stat sourceStat;
55 if( stat( aSrc.fn_str(), &sourceStat ) == 0 )
56 {
57 mode_t permissions = sourceStat.st_mode & ( S_IRWXU | S_IRWXG | S_IRWXO );
58 if( chmod( aDest.fn_str(), permissions ) == 0 )
59 {
60 return true;
61 }
62 else
63 {
64 // Handle error
65 return false;
66 }
67 }
68 else
69 {
70 // Handle error
71 return false;
72 }
73}
74
75bool KIPLATFORM::IO::MakeWriteable( const wxString& aFilePath )
76{
77 struct stat fileStat;
78 if( stat( aFilePath.fn_str(), &fileStat ) == 0 )
79 {
80 // Add user write permission to existing permissions
81 mode_t newPermissions = fileStat.st_mode | S_IWUSR;
82 if( chmod( aFilePath.fn_str(), newPermissions ) == 0 )
83 {
84 return true;
85 }
86 }
87 return false;
88}
89
90bool KIPLATFORM::IO::IsFileHidden( const wxString& aFileName )
91{
92 wxFileName fn( aFileName );
93
94 return fn.GetName().StartsWith( wxT( "." ) );
95}
96
97
98void KIPLATFORM::IO::LongPathAdjustment( wxFileName& aFilename )
99{
100 // no-op
101}
102
103
104long long KIPLATFORM::IO::TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
105{
106 long long timestamp = 0;
107
108 std::string pattern( aFilespec.fn_str() );
109 std::string dir_path( aDirPath.fn_str() );
110
111 DIR* dir = opendir( dir_path.c_str() );
112
113 if( dir )
114 {
115 for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
116 {
117 // FNM_PERIOD skips dotfiles (hidden files), FNM_CASEFOLD for case-insensitive match
118 if( fnmatch( pattern.c_str(), dir_entry->d_name, FNM_CASEFOLD | FNM_PERIOD ) != 0 )
119 continue;
120
121 std::string entry_path = dir_path + '/' + dir_entry->d_name;
122 struct stat entry_stat;
123
124 if( lstat( entry_path.c_str(), &entry_stat ) == 0 )
125 {
126 // Follow symlinks to get the actual file's timestamp
127 if( S_ISLNK( entry_stat.st_mode ) )
128 {
129 char buffer[PATH_MAX + 1];
130 ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
131
132 if( pathLen > 0 )
133 {
134 struct stat linked_stat;
135 buffer[pathLen] = '\0';
136 std::string linked_path = dir_path + '/' + buffer;
137
138 if( lstat( linked_path.c_str(), &linked_stat ) == 0 )
139 entry_stat = linked_stat;
140 }
141 }
142
143 if( S_ISREG( entry_stat.st_mode ) )
144 {
145 timestamp += entry_stat.st_mtime * 1000;
146 timestamp += entry_stat.st_size;
147 }
148 }
149 else
150 {
151 // If we couldn't stat the file, use the name hash
152 timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
153 }
154 }
155
156 closedir( dir );
157 }
158
159 return timestamp;
160}
161
162
164{
165 int fd = open( aFileName.fn_str(), O_RDONLY );
166
167 if( fd < 0 )
168 {
169 throw std::runtime_error( std::string( "Cannot open file: " )
170 + aFileName.ToStdString() );
171 }
172
173 struct stat st;
174
175 if( fstat( fd, &st ) != 0 )
176 {
177 close( fd );
178 throw std::runtime_error( std::string( "Cannot stat file: " )
179 + aFileName.ToStdString() );
180 }
181
182 m_size = static_cast<size_t>( st.st_size );
183
184 if( m_size == 0 )
185 {
186 close( fd );
187 return;
188 }
189
190 void* ptr = mmap( nullptr, m_size, PROT_READ, MAP_PRIVATE, fd, 0 );
191 close( fd );
192
193 if( ptr == MAP_FAILED )
194 {
195 readIntoBuffer( aFileName );
196 return;
197 }
198
199 madvise( ptr, m_size, MADV_SEQUENTIAL );
200 m_data = static_cast<const uint8_t*>( ptr );
201 m_isMapped = true;
202}
203
204
206{
207 if( m_isMapped && m_data )
208 munmap( const_cast<uint8_t*>( m_data ), m_size );
209}
MAPPED_FILE(const wxString &aFileName)
Definition unix/io.cpp:163
void readIntoBuffer(const wxString &aFileName)
Definition common/io.cpp:29
const uint8_t * m_data
Definition io.h:56
void LongPathAdjustment(wxFileName &aFilename)
Adjusts a filename to be a long path compatible.
Definition unix/io.cpp:98
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:36
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:52
bool IsFileHidden(const wxString &aFileName)
Helper function to determine the status of the 'Hidden' file attribute.
Definition unix/io.cpp:90
bool MakeWriteable(const wxString &aFilePath)
Ensures that a file has write permissions.
Definition unix/io.cpp:75
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
Computes a hash of modification times and sizes for files matching a pattern.
Definition unix/io.cpp:104