summaryrefslogtreecommitdiff
path: root/src/syncevo/TmpFile.h
blob: 9b4f676bf79de08b23a611893519a0fe5d3b4ba1 (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
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef INCL_SYNCEVOLUTION_TMPFILE
#define INCL_SYNCEVOLUTION_TMPFILE

#include <stdexcept>
#include <string>

#include <pcrecpp.h>


/**
 * Exception class for TmpFile.
 */
class TmpFileException : public std::runtime_error
{
    public:
        TmpFileException(const std::string &what)
            : std::runtime_error(what)
            { }
};


/**
 * Class for handling temporary files, either read/write access
 * or memory mapped.
 *
 * Closing and removing a mapped file is supported by calling close()
 * after map().
 */
class TmpFile
{
    protected:
        int m_fd;
        void *m_mapptr;
        size_t m_mapsize;
        std::string m_filename;

    public:
        TmpFile();
        virtual ~TmpFile();

        /**
         * Create a temporary file.
         */
        void create();
        /**
         * Map a view of file and optionally return pointer and/or size.
         *
         * File should already have a correct size.
         *
         * @param mapptr Pointer to variable for mapped pointer. (can be NULL)
         * @param mapsize Pointer to variable for mapped size. (can be NULL)
         */
        void map(void **mapptr = 0, size_t *mapsize = 0);
        /**
         * Unmap a view of file.
         */
        void unmap();

        /**
         * Returns amount of bytes not mapped into memory yet, zero if none.
         */
        size_t moreData() const;

        /**
         * Remove the file. If the process crashes, the file will be removed,
         * but the process itself can still map and use the file content.
         */
        void remove();
        /**
         * Remove and close the file.
         *
         * Calling this after map() will make the file disappear from
         * filesystem but the mapping will be valid until unmapped or
         * instance of this class is destroyed.
         */
        void close();

        /**
         * Retrieve file name of the file.
         *
         * @return file name
         */
        const std::string & filename() const
            { return m_filename; }
        /**
         * Retrieve descriptor of the file.
         *
         * @return descriptor
         */
        int fd()
            { return m_fd; }
        /**
         * Size of the mapping.
         *
         * @return mapped size
         */
        size_t size() const
            { return m_mapsize; }
        /**
         * Pointer to the mapping.
         *
         * @return pointer to the mapping
         */
        operator void *()
            { return m_mapptr; }
        /**
         * @overload
         */
        operator const void *() const
            { return m_mapptr; }

        /**
         * Retrieve pcrecpp::StringPiece object for the mapped view.
         *
         * @return pcrecpp::StringPiece of the mapped view
         */
        pcrecpp::StringPiece stringPiece();
};

#endif  // INCL_SYNCEVOLUTION_TMPFILE