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/stat.h>
33#include <unistd.h>
34
35FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode )
36{
37 FILE* fp = wxFopen( aPath, aMode );
38
39 if( fp )
40 {
41 if( posix_fadvise( fileno( fp ), 0, 0, POSIX_FADV_SEQUENTIAL ) != 0 )
42 {
43 fclose( fp );
44 fp = nullptr;
45 }
46 }
47
48 return fp;
49}
50
51bool KIPLATFORM::IO::DuplicatePermissions( const wxString &aSrc, const wxString &aDest )
52{
53 struct stat sourceStat;
54 if( stat( aSrc.fn_str(), &sourceStat ) == 0 )
55 {
56 mode_t permissions = sourceStat.st_mode & ( S_IRWXU | S_IRWXG | S_IRWXO );
57 if( chmod( aDest.fn_str(), permissions ) == 0 )
58 {
59 return true;
60 }
61 else
62 {
63 // Handle error
64 return false;
65 }
66 }
67 else
68 {
69 // Handle error
70 return false;
71 }
72}
73
74bool KIPLATFORM::IO::MakeWriteable( const wxString& aFilePath )
75{
76 struct stat fileStat;
77 if( stat( aFilePath.fn_str(), &fileStat ) == 0 )
78 {
79 // Add user write permission to existing permissions
80 mode_t newPermissions = fileStat.st_mode | S_IWUSR;
81 if( chmod( aFilePath.fn_str(), newPermissions ) == 0 )
82 {
83 return true;
84 }
85 }
86 return false;
87}
88
89bool KIPLATFORM::IO::IsFileHidden( const wxString& aFileName )
90{
91 wxFileName fn( aFileName );
92
93 return fn.GetName().StartsWith( wxT( "." ) );
94}
95
96
97void KIPLATFORM::IO::LongPathAdjustment( wxFileName& aFilename )
98{
99 // no-op
100}
101
102
103long long KIPLATFORM::IO::TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
104{
105 long long timestamp = 0;
106
107 std::string pattern( aFilespec.fn_str() );
108 std::string dir_path( aDirPath.fn_str() );
109
110 DIR* dir = opendir( dir_path.c_str() );
111
112 if( dir )
113 {
114 for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
115 {
116 // FNM_PERIOD skips dotfiles (hidden files), FNM_CASEFOLD for case-insensitive match
117 if( fnmatch( pattern.c_str(), dir_entry->d_name, FNM_CASEFOLD | FNM_PERIOD ) != 0 )
118 continue;
119
120 std::string entry_path = dir_path + '/' + dir_entry->d_name;
121 struct stat entry_stat;
122
123 if( lstat( entry_path.c_str(), &entry_stat ) == 0 )
124 {
125 // Follow symlinks to get the actual file's timestamp
126 if( S_ISLNK( entry_stat.st_mode ) )
127 {
128 char buffer[PATH_MAX + 1];
129 ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
130
131 if( pathLen > 0 )
132 {
133 struct stat linked_stat;
134 buffer[pathLen] = '\0';
135 std::string linked_path = dir_path + '/' + buffer;
136
137 if( lstat( linked_path.c_str(), &linked_stat ) == 0 )
138 entry_stat = linked_stat;
139 }
140 }
141
142 if( S_ISREG( entry_stat.st_mode ) )
143 {
144 timestamp += entry_stat.st_mtime * 1000;
145 timestamp += entry_stat.st_size;
146 }
147 }
148 else
149 {
150 // If we couldn't stat the file, use the name hash
151 timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
152 }
153 }
154
155 closedir( dir );
156 }
157
158 return timestamp;
159}
void LongPathAdjustment(wxFileName &aFilename)
Adjusts a filename to be a long path compatible.
Definition unix/io.cpp:97
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:35
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:51
bool IsFileHidden(const wxString &aFileName)
Helper function to determine the status of the 'Hidden' file attribute.
Definition unix/io.cpp:89
bool MakeWriteable(const wxString &aFilePath)
Ensures that a file has write permissions.
Definition unix/io.cpp:74
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:103