2 * Copyright (C) 2017 Cisco Inc.
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.
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.
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/>.
17 // @author Changxue Deng <chadeng@cisco.com>
26 FileIO::FileIO(const std::string &fpath, int oflags, int fmode, bool sync)
43 mode_t prev_mask = umask(0);
44 fd = open(path.c_str(), options, mode);
50 size_t FileIO::Write(const void *data, size_t size)
52 if(options & MMAP_ANONYMOUS_MODE)
59 bytes_written = write(fd, data, size);
60 if(sync_on_write) fsync(fd);
70 size_t FileIO::Read(void *buff, size_t size)
72 if(options & MMAP_ANONYMOUS_MODE)
79 bytes_read = read(fd, buff, size);
89 size_t FileIO::RandomWrite(const void *data, size_t size, off_t offset)
91 if(options & MMAP_ANONYMOUS_MODE)
98 bytes_written = pwrite(fd, data, size, offset);
99 if(sync_on_write) fsync(fd);
106 return bytes_written;
109 size_t FileIO::RandomRead(void *buff, size_t size, off_t offset)
111 if(options & MMAP_ANONYMOUS_MODE)
118 bytes_read = pread(fd, buff, size, offset);
128 void* FileIO::MapFile(size_t size, int prot, int flags, off_t offset)
130 return mmap(NULL, size, prot, flags, fd, offset);
133 off_t FileIO::SetOffset(off_t offset)
135 return lseek(fd, offset, SEEK_SET);
147 bool FileIO::IsOpen() const
152 int FileIO::TruncateFile(off_t filesize)
155 return ftruncate(fd, filesize);
166 const std::string& FileIO::GetFilePath() const