KiCad PCB EDA Suite
Loading...
Searching...
No Matches
io.h
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 <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef KIPLATFORM_IO_H_
21#define KIPLATFORM_IO_H_
22
23#include <stdio.h>
24#include <cstddef>
25#include <cstdint>
26#include <vector>
27
28class wxString;
29class wxFileName;
30
31namespace KIPLATFORM
32{
33namespace IO
34{
42 {
43 public:
44 explicit MAPPED_FILE( const wxString& aFileName );
46
47 MAPPED_FILE( const MAPPED_FILE& ) = delete;
48 MAPPED_FILE& operator=( const MAPPED_FILE& ) = delete;
49
50 const uint8_t* Data() const { return m_data; }
51 size_t Size() const { return m_size; }
52
53 private:
54 void readIntoBuffer( const wxString& aFileName );
55
56 const uint8_t* m_data = nullptr;
57 size_t m_size = 0;
58
59#ifdef _WIN32
60 void* m_fileHandle = nullptr;
61 void* m_mapHandle = nullptr;
62#else
63 bool m_isMapped = false;
64#endif
65
66 std::vector<uint8_t> m_fallbackBuffer;
67 };
68
77 static constexpr size_t CLOUD_SYNC_BUFFER_SIZE = 512 * 1024;
86 FILE* SeqFOpen( const wxString& aPath, const wxString& mode );
87
93 bool DuplicatePermissions( const wxString& aSrc, const wxString& aDest );
94
101 bool MakeWriteable( const wxString& aFilePath );
102
107 bool IsFileHidden( const wxString& aFileName );
108
113 void LongPathAdjustment( wxFileName& aFilename );
114
123 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec );
124
136 bool FlushToDisk( FILE* aFp );
137
149 bool FlushDirectory( const wxString& aDirPath );
150
169 bool AtomicRename( const wxString& aSrc, const wxString& aDst, wxString* aError = nullptr );
170
176 wxString MakeSiblingTempPath( const wxString& aTargetPath );
177
190 FILE* OpenUniqueSiblingTempFile( const wxString& aTargetPath, const wxString& aMode,
191 wxString* aTempPathOut, wxString* aError = nullptr );
192
199 wxString ResolveSymlinkTarget( const wxString& aPath );
200
209 {
210 std::uint32_t value = 0;
211 bool captured = false;
212 };
213
219 TARGET_ATTRS CaptureTargetAttributes( const wxString& aPath );
220
231 bool ApplyTargetAttributes( const wxString& aPath, const TARGET_ATTRS& aAttrs );
232
247 bool CommitTempFile( const wxString& aTempPath, const wxString& aTargetPath,
248 wxString* aError = nullptr );
249
267 bool AtomicWriteFile( const wxString& aTargetPath, const void* aData, size_t aSize,
268 wxString* aError = nullptr );
269} // namespace IO
270} // namespace KIPLATFORM
271
272#endif // KIPLATFORM_IO_H_
MAPPED_FILE(const MAPPED_FILE &)=delete
size_t Size() const
Definition io.h:51
MAPPED_FILE & operator=(const MAPPED_FILE &)=delete
MAPPED_FILE(const wxString &aFileName)
Definition unix/io.cpp:199
void readIntoBuffer(const wxString &aFileName)
const uint8_t * Data() const
Definition io.h:50
const uint8_t * m_data
Definition io.h:56
std::vector< uint8_t > m_fallbackBuffer
Definition io.h:66
wxString MakeSiblingTempPath(const wxString &aTargetPath)
Returns a unique sibling path of aTargetPath suitable as an atomic-save temp file.
Definition common/io.cpp:45
bool FlushDirectory(const wxString &aDirPath)
Forces a directory entry's metadata to stable storage.
void LongPathAdjustment(wxFileName &aFilename)
Adjusts a filename to be a long path compatible.
Definition unix/io.cpp:117
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:39
TARGET_ATTRS CaptureTargetAttributes(const wxString &aPath)
Captures attributes of an existing aPath that must survive an atomic rename.
Definition unix/io.cpp:94
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:55
wxString ResolveSymlinkTarget(const wxString &aPath)
If aPath is a symlink on POSIX, returns the canonical path of its referent so atomic-save operations ...
bool IsFileHidden(const wxString &aFileName)
Helper function to determine the status of the 'Hidden' file attribute.
Definition unix/io.cpp:109
bool AtomicRename(const wxString &aSrc, const wxString &aDst, wxString *aError=nullptr)
Atomically replaces aDst with aSrc.
FILE * OpenUniqueSiblingTempFile(const wxString &aTargetPath, const wxString &aMode, wxString *aTempPathOut, wxString *aError=nullptr)
Opens a fresh sibling temp file next to aTargetPath with exclusive-create semantics (POSIX O_CREAT|O_...
Definition common/io.cpp:66
bool CommitTempFile(const wxString &aTempPath, const wxString &aTargetPath, wxString *aError=nullptr)
Completes an atomic save.
bool AtomicWriteFile(const wxString &aTargetPath, const void *aData, size_t aSize, wxString *aError=nullptr)
Writes aData to aTargetPath via a sibling temp file, fsyncs the data and directory,...
bool MakeWriteable(const wxString &aFilePath)
Ensures that a file has write permissions.
Definition unix/io.cpp:78
bool ApplyTargetAttributes(const wxString &aPath, const TARGET_ATTRS &aAttrs)
Re-applies attributes previously captured by CaptureTargetAttributes.
Definition unix/io.cpp:103
static constexpr size_t CLOUD_SYNC_BUFFER_SIZE
Buffer size for file I/O operations on cloud-synced folders.
Definition io.h:77
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:123
bool FlushToDisk(FILE *aFp)
Flushes user-space buffers for aFp and forces the kernel/filesystem to commit the file's data blocks ...
Definition unix/io.cpp:182
Opaque snapshot of filesystem attributes that MakeWriteable may alter and that the atomic rename sequ...
Definition io.h:209
std::uint32_t value
Definition io.h:210