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
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
43const char * BufferedFile::path() const
44{
45 return m_path;
46}
47
48const void * BufferedFile::buffer() const
49{
50 return m_buffer;
51}
52
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];
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
99BufferedFile::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}
u64 fp
Definition ARM64Control.h:3
const Size size() const
Get file size.
Result
Result codes.
Result write(const void *data, const Size size) const
Write the file (unbuffered)
Result read()
Read the file (buffered)
~BufferedFile()
Destructor.
const char * m_path
Path to the file.
const char * path() const
Get file path.
BufferedFile(const char *path)
Constructor.
u8 * m_buffer
Stored contents of the file.
const void * buffer() const
Get file buffer.
Size m_size
Size of the file in bytes.
C int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition open.cpp:26
C char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition strerror.cpp:20
C int close(int fildes)
Close a file descriptor.
Definition close.cpp:22
C int errno
The lvalue errno is used by many functions to return error values.
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
#define O_RDONLY
Open for reading only.
Definition fcntl.h:81
#define O_RDWR
Open for reading and writing.
Definition fcntl.h:84
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
The <sys/stat.h> header shall define the stat structure.
Definition stat.h:177
off_t st_size
For regular files, the file size in bytes.
Definition stat.h:226