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
17* along with this program. If not, see <https://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 <string>
27#include <vector>
28
29class wxString;
30class wxFileName;
31
32namespace KIPLATFORM
33{
34namespace IO
35{
43 {
44 public:
45 explicit MAPPED_FILE( const wxString& aFileName );
47
48 MAPPED_FILE( const MAPPED_FILE& ) = delete;
49 MAPPED_FILE& operator=( const MAPPED_FILE& ) = delete;
50
51 const uint8_t* Data() const { return m_data; }
52 size_t Size() const { return m_size; }
53
54 private:
55 void readIntoBuffer( const wxString& aFileName );
56
57 const uint8_t* m_data = nullptr;
58 size_t m_size = 0;
59
60#ifdef _WIN32
61 void* m_fileHandle = nullptr;
62 void* m_mapHandle = nullptr;
63#else
64 bool m_isMapped = false;
65#endif
66
67 std::vector<uint8_t> m_fallbackBuffer;
68 };
69
91 {
92 public:
93 enum class STATE
94 {
99 };
100
101 FILE_LOCK() = default;
102 ~FILE_LOCK();
103
104 FILE_LOCK( FILE_LOCK&& aOther ) noexcept;
105 FILE_LOCK& operator=( FILE_LOCK&& aOther ) noexcept;
106
107 FILE_LOCK( const FILE_LOCK& ) = delete;
108 FILE_LOCK& operator=( const FILE_LOCK& ) = delete;
109
121 STATE Acquire( const wxString& aPath, bool& aCreated );
122
133 bool OpenForInspect( const wxString& aPath, bool& aHeldByAnother );
134
135 bool IsOpen() const;
136
140 bool ReadAll( std::string& aContents ) const;
141
145 bool Rewrite( const std::string& aContents );
146
150 void Release();
151
152 private:
153#ifdef _WIN32
154 void* m_handle = nullptr;
155#else
156 int m_fd = -1;
157#endif
159 };
160
169 static constexpr size_t CLOUD_SYNC_BUFFER_SIZE = 512 * 1024;
178 FILE* SeqFOpen( const wxString& aPath, const wxString& mode );
179
185 bool DuplicatePermissions( const wxString& aSrc, const wxString& aDest );
186
193 bool MakeWriteable( const wxString& aFilePath );
194
199 bool IsFileHidden( const wxString& aFileName );
200
205 void LongPathAdjustment( wxFileName& aFilename );
206
215 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec );
216
228 bool FlushToDisk( FILE* aFp );
229
241 bool FlushDirectory( const wxString& aDirPath );
242
261 bool AtomicRename( const wxString& aSrc, const wxString& aDst, wxString* aError = nullptr );
262
268 wxString MakeSiblingTempPath( const wxString& aTargetPath );
269
282 FILE* OpenUniqueSiblingTempFile( const wxString& aTargetPath, const wxString& aMode,
283 wxString* aTempPathOut, wxString* aError = nullptr );
284
291 wxString ResolveSymlinkTarget( const wxString& aPath );
292
301 {
302 std::uint32_t value = 0;
303 bool captured = false;
304 };
305
311 TARGET_ATTRS CaptureTargetAttributes( const wxString& aPath );
312
323 bool ApplyTargetAttributes( const wxString& aPath, const TARGET_ATTRS& aAttrs );
324
339 bool CommitTempFile( const wxString& aTempPath, const wxString& aTargetPath,
340 wxString* aError = nullptr );
341
359 bool AtomicWriteFile( const wxString& aTargetPath, const void* aData, size_t aSize,
360 wxString* aError = nullptr );
361} // namespace IO
362} // namespace KIPLATFORM
363
364#endif // KIPLATFORM_IO_H_
bool ReadAll(std::string &aContents) const
Read the whole file through the descriptor we hold.
FILE_LOCK & operator=(const FILE_LOCK &)=delete
void Release()
Release the lock and close the file.
bool Rewrite(const std::string &aContents)
Replace the file contents through the descriptor we hold, keeping the same inode.
@ UNSUPPORTED
The file is open but the filesystem cannot answer.
Definition io.h:98
@ BUSY
Another process holds the lock.
Definition io.h:97
@ HELD
We hold the lock.
Definition io.h:96
@ NONE
No file is open.
Definition io.h:95
STATE Acquire(const wxString &aPath, bool &aCreated)
Open aPath, creating it if it does not exist, and try to take the lock without ever blocking on it.
FILE_LOCK(const FILE_LOCK &)=delete
FILE_LOCK & operator=(FILE_LOCK &&aOther) noexcept
bool OpenForInspect(const wxString &aPath, bool &aHeldByAnother)
Open an existing file and report whether another process holds its lock, creating nothing and keeping...
MAPPED_FILE(const MAPPED_FILE &)=delete
size_t Size() const
Definition io.h:52
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:51
const uint8_t * m_data
Definition io.h:57
std::vector< uint8_t > m_fallbackBuffer
Definition io.h:67
wxString MakeSiblingTempPath(const wxString &aTargetPath)
Returns a unique sibling path of aTargetPath suitable as an atomic-save temp file.
Definition common/io.cpp:46
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:67
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:169
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:301
std::uint32_t value
Definition io.h:302