remove conflict
[c11concurrency-benchmarks.git] / mabain / src / file_io.cpp
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #include <sys/mman.h>
20 #include <errno.h>
21
22 #include "file_io.h"
23
24 namespace mabain {
25
26 FileIO::FileIO(const std::string &fpath, int oflags, int fmode, bool sync)
27         : path(fpath),
28           options(oflags),
29           sync_on_write(sync),
30           mode(fmode)
31 {
32     fd = -1;
33 }
34
35 FileIO::~FileIO()
36 {
37     if(fd > 0)
38         close(fd);
39 }
40
41 int FileIO::Open()
42 {
43     mode_t prev_mask = umask(0);
44     fd = open(path.c_str(), options, mode);
45     umask(prev_mask);
46
47     return fd;
48 }
49
50 size_t FileIO::Write(const void *data, size_t size)
51 {
52     if(options & MMAP_ANONYMOUS_MODE)
53         return 0;
54
55     size_t bytes_written;
56
57     if(fd > 0)
58     {
59         bytes_written = write(fd, data, size);
60         if(sync_on_write) fsync(fd);
61     }
62     else
63     {
64         bytes_written = 0;
65     }
66
67     return bytes_written;
68 }
69
70 size_t FileIO::Read(void *buff, size_t size)
71 {
72     if(options & MMAP_ANONYMOUS_MODE)
73         return 0;
74
75     size_t bytes_read;
76
77     if(fd > 0)
78     {
79         bytes_read = read(fd, buff, size);
80     }
81     else
82     {
83         bytes_read = 0;
84     }
85
86     return bytes_read;
87 }
88
89 size_t FileIO::RandomWrite(const void *data, size_t size, off_t offset)
90 {
91     if(options & MMAP_ANONYMOUS_MODE)
92         return 0;
93
94     size_t bytes_written;
95
96     if(fd > 0)
97     {
98         bytes_written = pwrite(fd, data, size, offset);
99         if(sync_on_write) fsync(fd);
100     }
101     else
102     {
103         bytes_written = 0;
104     }
105
106     return bytes_written;
107 }
108
109 size_t FileIO::RandomRead(void *buff, size_t size, off_t offset)
110 {
111     if(options & MMAP_ANONYMOUS_MODE)
112         return 0;
113
114     size_t bytes_read;
115
116     if(fd > 0)
117     {
118         bytes_read = pread(fd, buff, size, offset);
119     }
120     else
121     {
122         bytes_read = 0;
123     }
124
125     return bytes_read;
126 }
127
128 void* FileIO::MapFile(size_t size, int prot, int flags, off_t offset)
129 {
130     return mmap(NULL, size, prot, flags, fd, offset);
131 }
132
133 off_t FileIO::SetOffset(off_t offset)
134 {
135     return lseek(fd, offset, SEEK_SET);
136 }
137
138 void FileIO::Close()
139 {
140     if(fd > 0)
141     {
142         close(fd);
143         fd = -1;
144     }
145 }
146
147 bool FileIO::IsOpen() const
148 {
149     return fd > 0;
150 }
151
152 int FileIO::TruncateFile(off_t filesize)
153 {
154     if(fd > 0)
155         return ftruncate(fd, filesize);
156
157     return 1;
158 }
159
160 void FileIO::Flush()
161 {
162     if(fd > 0)
163         fsync(fd);
164 }
165
166 const std::string& FileIO::GetFilePath() const
167 {
168     return path;
169 }
170
171 }