summaryrefslogtreecommitdiff
path: root/zen/file_io.h
blob: 838b50217e9c6aef500a3f5df3f77d81c712314a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************

#ifndef FILE_IO_H_89578342758342572345
#define FILE_IO_H_89578342758342572345

#include "file_access.h"
#include "serialize.h"
#include "crc.h"
#include "guid.h"


namespace zen
{
    const char LINE_BREAK[] = "\n"; //since OS X Apple uses newline, too

/*  OS-buffered file I/O:
    - sequential read/write accesses
    - better error reporting
    - long path support
    - follows symlinks                     */
class FileBase
{
public:
    using FileHandle = int;
    static const int invalidFileHandle = -1;

    FileHandle getHandle() { return hFile_; }

    const Zstring& getFilePath() const { return filePath_; }

    size_t getBlockSize(); //throw FileError

    static constexpr size_t defaultBlockSize = 256 * 1024;

    void close(); //throw FileError -> good place to catch errors when closing stream, otherwise called in ~FileBase()!

    const struct stat& getStatBuffered(); //throw FileError

protected:
    FileBase(FileHandle handle, const Zstring& filePath) : hFile_(handle), filePath_(filePath) {}
    ~FileBase();

    void setStatBuffered(const struct stat& fileInfo) { statBuf_ = fileInfo; }

private:
    FileBase           (const FileBase&) = delete;
    FileBase& operator=(const FileBase&) = delete;

    FileHandle hFile_ = invalidFileHandle;
    const Zstring filePath_;
    size_t blockSizeBuf_ = 0;
    std::optional<struct stat> statBuf_;
};

//-----------------------------------------------------------------------------------------------

class FileInputPlain : public FileBase
{
public:
    FileInputPlain(                   const Zstring& filePath); //throw FileError, ErrorFileLocked
    FileInputPlain(FileHandle handle, const Zstring& filePath); //takes ownership!

    //may return short, only 0 means EOF! CONTRACT: bytesToRead > 0!
    size_t tryRead(void* buffer, size_t bytesToRead); //throw FileError, ErrorFileLocked

private:
    FileInputPlain(const std::pair<FileBase::FileHandle, struct stat>& fileDetails, const Zstring& filePath);
};


class FileOutputPlain : public FileBase
{
public:
    FileOutputPlain(                   const Zstring& filePath); //throw FileError, ErrorTargetExisting
    FileOutputPlain(FileHandle handle, const Zstring& filePath); //takes ownership!
    ~FileOutputPlain();

    //preallocate disk space & reduce fragmentation
    void reserveSpace(uint64_t expectedSize); //throw FileError

    //may return short! CONTRACT: bytesToWrite > 0
    size_t tryWrite(const void* buffer, size_t bytesToWrite); //throw FileError

    //close() when done, or else file is considered incomplete and will be deleted!

private:
};

//--------------------------------------------------------------------

namespace impl
{
inline
auto makeTryRead(FileInputPlain& fip, const IoCallback& notifyUnbufferedIO /*throw X*/)
{
    return [&](void* buffer, size_t bytesToRead)
    {
        const size_t bytesRead = fip.tryRead(buffer, bytesToRead); //throw FileError, ErrorFileLocked; may return short, only 0 means EOF! =>  CONTRACT: bytesToRead > 0!
        if (notifyUnbufferedIO) notifyUnbufferedIO(bytesRead); //throw X
        return bytesRead;
    };
}


inline
auto makeTryWrite(FileOutputPlain& fop, const IoCallback& notifyUnbufferedIO /*throw X*/)
{
    return [&](const void* buffer, size_t bytesToWrite)
    {
        const size_t bytesWritten = fop.tryWrite(buffer, bytesToWrite); //throw FileError
        if (notifyUnbufferedIO) notifyUnbufferedIO(bytesWritten); //throw X
        return bytesWritten;
    };
}
}

//--------------------------------------------------------------------

class FileInputBuffered
{
public:
    FileInputBuffered(const Zstring& filePath, const IoCallback& notifyUnbufferedIO /*throw X*/) : //throw FileError, ErrorFileLocked
        fileIn_(filePath), //throw FileError, ErrorFileLocked
        notifyUnbufferedIO_(notifyUnbufferedIO) {}

    //return "bytesToRead" bytes unless end of stream!
    size_t read(void* buffer, size_t bytesToRead) { return streamIn_.read(buffer, bytesToRead); } //throw FileError, ErrorFileLocked, X

private:
    FileInputPlain fileIn_;
    const IoCallback notifyUnbufferedIO_; //throw X

    BufferedInputStream<FunctionReturnTypeT<decltype(&impl::makeTryRead)>>
    streamIn_{impl::makeTryRead(fileIn_, notifyUnbufferedIO_), fileIn_.getBlockSize()}; //throw FileError
};


class FileOutputBuffered
{
public:
    FileOutputBuffered(const Zstring& filePath, const IoCallback& notifyUnbufferedIO /*throw X*/) : //throw FileError, ErrorTargetExisting
        fileOut_(filePath), //throw FileError, ErrorTargetExisting
        notifyUnbufferedIO_(notifyUnbufferedIO) {}

    void write(const void* buffer, size_t bytesToWrite) { streamOut_.write(buffer, bytesToWrite); } //throw FileError, X

    void finalize() //throw FileError, X
    {
        streamOut_.flushBuffer(); //throw FileError, X
        fileOut_.close(); //throw FileError
    }

private:
    FileOutputPlain fileOut_;
    const IoCallback notifyUnbufferedIO_; //throw X

    BufferedOutputStream<FunctionReturnTypeT<decltype(&impl::makeTryWrite)>>
    streamOut_{impl::makeTryWrite(fileOut_, notifyUnbufferedIO_), fileOut_.getBlockSize()}; //throw FileError
};
//-----------------------------------------------------------------------------------------------

//stream I/O convenience functions:

inline
Zstring getPathWithTempName(const Zstring& filePath) //generate (hopefully) unique file name
{
    const Zstring shortGuid_ = printNumber<Zstring>(Zstr("%04x"), static_cast<unsigned int>(getCrc16(generateGUID())));
    return filePath + Zstr('.') + shortGuid_ + Zstr(".tmp");
}

[[nodiscard]] std::string getFileContent(const Zstring& filePath, const IoCallback& notifyUnbufferedIO /*throw X*/); //throw FileError, X

//overwrites if existing + transactional! :)
void setFileContent(const Zstring& filePath, const std::string_view bytes, const IoCallback& notifyUnbufferedIO /*throw X*/); //throw FileError, X
}

#endif //FILE_IO_H_89578342758342572345
bgstack15