-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCExclusiveFile.h
More file actions
executable file
·121 lines (98 loc) · 3.29 KB
/
CExclusiveFile.h
File metadata and controls
executable file
·121 lines (98 loc) · 3.29 KB
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
/*************************************************************************************
cpl - cross-platform library - v. 0.1.0.
Copyright (C) 2016 Janus Lynggaard Thorborg (www.jthorborg.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
See \licenses\ for additional details on licenses associated with this program.
**************************************************************************************
file:CExclusiveFile.h
A file class that marks owned file as exclusive. This implies that other instances
of this class cannot open the same file. This lock is obtained atomically and can
be used to allow sequental access to files.
Whether a file is exclusive can be tested with the static method isFileExclusive().
Notice that isFileExclusive() is only predictable on existing files.
Class is RAII safe (file is closed on destruction).
options:
#define CPL_CEF_USE_CSTDLIB
causes the header to use the cstdlib, losing all previous properties but provided
for compability
*************************************************************************************/
#ifndef CPL_CEXCLUSIVEFILE_H
#define CPL_CEXCLUSIVEFILE_H
#include "MacroConstants.h"
#include "Utility.h"
#include <cstring>
#include <string>
#include <cstdint>
#if defined(CPL_CEF_USE_CSTDLIB)
#include <cstdio>
typedef FILE * FileHandle;
#elif defined(CPL_WINDOWS)
#include <Windows.h>
typedef HANDLE FileHandle;
#elif defined(CPL_MAC)
#include <unistd.h>
#include <fcntl.h>
typedef int FileHandle;
#elif defined(CPL_UNIXC)
#include <sys/file.h>
#include <unistd.h>
#include <fcntl.h>
typedef int FileHandle;
#endif
namespace cpl
{
class CExclusiveFile
:
public Utility::CNoncopyable
{
public:
enum mode : std::uint32_t
{
#ifdef CPL_WINDOWS
readMode = GENERIC_READ,
writeMode = GENERIC_WRITE,
readWriteMode = readMode | writeMode,
append = FILE_APPEND_DATA,
clear = 0
#elif defined(CPL_MAC) || defined(CPL_UNIXC)
clear = 0,
readMode = 2,
writeMode = 4,
append = 8
#endif
};
private:
bool isOpen;
FileHandle handle;
std::string fileName;
mode fileMode;
public:
CExclusiveFile();
~CExclusiveFile();
// TODO: fs::path
static bool isFileExclusive(const std::string & path);
// TODO: fs::path
bool open(const std::string & path, std::uint32_t m = writeMode, bool waitForLock = false);
bool read(void * src, std::int64_t bufsiz);
std::int64_t getFileSize();
bool newline();
bool write(const void * src, std::int64_t bufsiz);
bool reset();
bool remove();
bool write(const char * src);
bool isOpened();
const std::string & getName();
bool flush();
bool close();
};
}
#endif