FreeNOS
BufferedFile.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Niek Linnenbank
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 as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <Log.h>
19 #include <Assert.h>
20 #include <Macros.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include "BufferedFile.h"
27 
28 BufferedFile::BufferedFile(const char *path)
29  : m_path(path)
30  , m_buffer(ZERO)
31  , m_size(0)
32 {
33 }
34 
36 {
37  if (m_buffer != ZERO)
38  {
39  delete[] m_buffer;
40  }
41 }
42 
43 const char * BufferedFile::path() const
44 {
45  return m_path;
46 }
47 
48 const void * BufferedFile::buffer() const
49 {
50  return m_buffer;
51 }
52 
53 const Size BufferedFile::size() const
54 {
55  return m_size;
56 }
57 
59 {
60  struct stat st;
61  int fp;
62 
63  // Retrieve file information
64  if (::stat(m_path, &st) != 0)
65  {
66  ERROR("failed to stat input file " << m_path << ": " << strerror(errno));
67  return NotFound;
68  }
69  m_size = st.st_size;
70 
71  // Open the file
72  if ((fp = ::open(m_path, O_RDONLY)) == -1)
73  {
74  ERROR("failed to open input file " << m_path << ": " << strerror(errno));
75  return IOError;
76  }
77 
78  // (Re)allocate the internal buffer
79  if (m_buffer != ZERO)
80  {
81  delete[] m_buffer;
82  }
83  m_buffer = new u8[m_size];
84  assert(m_buffer != ZERO);
85 
86  // Read input file
87  if (::read(fp, m_buffer, st.st_size) <= 0)
88  {
89  ERROR("failed to read input file " << m_path << ": " << strerror(errno));
90  ::close(fp);
91  return IOError;
92  }
93 
94  // Done
95  ::close(fp);
96  return Success;
97 }
98 
99 BufferedFile::Result BufferedFile::write(const void *data, const Size size) const
100 {
101  int fp;
102 
103  // Open the file
104  if ((fp = ::open(m_path, O_RDWR)) == -1)
105  {
106  ERROR("failed to open output file " << m_path << ": " << strerror(errno));
107  return IOError;
108  }
109 
110  // Write to the file
111  if (::write(fp, data, size) <= 0)
112  {
113  ERROR("failed to write output file " << m_path << ": " << strerror(errno));
114  ::close(fp);
115  return IOError;
116  }
117 
118  // Done
119  ::close(fp);
120  return Success;
121 }
stat::st_size
off_t st_size
For regular files, the file size in bytes.
Definition: stat.h:226
stat
The <sys/stat.h> header shall define the stat structure.
Definition: stat.h:176
BufferedFile::~BufferedFile
~BufferedFile()
Destructor.
Definition: BufferedFile.cpp:35
Macros.h
fcntl.h
errno
C int errno
The lvalue errno is used by many functions to return error values.
string.h
BufferedFile::NotFound
@ NotFound
Definition: BufferedFile.h:44
BufferedFile::Result
Result
Result codes.
Definition: BufferedFile.h:41
Assert.h
open
int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition: open.cpp:26
BufferedFile::write
Result write(const void *data, const Size size) const
Write the file (unbuffered)
Definition: BufferedFile.cpp:99
Log.h
BufferedFile::buffer
const void * buffer() const
Get file buffer.
Definition: BufferedFile.cpp:48
BufferedFile::read
Result read()
Read the file (buffered)
Definition: BufferedFile.cpp:58
BufferedFile::m_size
Size m_size
Size of the file in bytes.
Definition: BufferedFile.h:109
close
int close(int fildes)
Close a file descriptor.
Definition: close.cpp:22
BufferedFile::BufferedFile
BufferedFile(const char *path)
Constructor.
Definition: BufferedFile.cpp:28
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
BufferedFile.h
O_RDWR
#define O_RDWR
Open for reading and writing.
Definition: fcntl.h:84
stat
int stat(const char *path, struct stat *buf)
Get file status.
Definition: stat.cpp:25
stat.h
unistd.h
BufferedFile::size
const Size size() const
Get file size.
Definition: BufferedFile.cpp:53
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
BufferedFile::Success
@ Success
Definition: BufferedFile.h:43
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
BufferedFile::m_buffer
u8 * m_buffer
Stored contents of the file.
Definition: BufferedFile.h:106
O_RDONLY
#define O_RDONLY
Open for reading only.
Definition: fcntl.h:81
BufferedFile::IOError
@ IOError
Definition: BufferedFile.h:45
errno.h
BufferedFile::m_path
const char * m_path
Path to the file.
Definition: BufferedFile.h:103
BufferedFile::path
const char * path() const
Get file path.
Definition: BufferedFile.cpp:43