Valkka  1.6.1
OpenSource Video Management
rawrite.h
Go to the documentation of this file.
1 #ifndef rawrite_HEADER_GUARD
2 #define rawrite_HEADER_GUARD
3 /*
4  * rawrite.h : Write directly to files and devices with POSIX O_DIRECT
5  *
6  * (c) Copyright 2017-2024 Sampsa Riikonen
7  *
8  * Authors: Petri Eranko <petri.eranko@dasys.fi>
9  *
10  * This file is part of the Valkka library.
11  *
12  * Valkka is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
37 /*
38  * So, why this?
39  *
40  * - We want to avoid excessive caching, as we don't need it
41  * - We would even like to use DMA writes to a block device (fdd/sdd)
42  * - In the "pure-cpp" way, we should subclass fstream to support our own custom streambuffer class
43  * https://stackoverflow.com/questions/12997131/stdfstream-buffering-vs-manual-buffering-why-10x-gain-with-manual-buffering
44  * example of a custom streambuf:
45  * https://stackoverflow.com/questions/42647267/subclass-fstream-and-decrypt-data-on-the-fly?rq=1
46  * - And also subclass the fstream class itself. This would be quite tricky.
47  *
48  * In order to use the linux O_DIRECT, DMA (Direct Memory Acces) to the hdd / sdd must be enabled. This might be tricky:
49  *
50  * https://stackoverflow.com/questions/10512987/o-direct-flag-not-working
51  * https://www.tldp.org/HOWTO/archived/Ultra-DMA/Ultra-DMA-8.html
52  * https://www.linuxquestions.org/questions/linux-hardware-18/hdparm-d1-dev-hda-gives-me-hdio_set_dma-failed-operation-not-permitted-260894/
53  * https://www.linuxquestions.org/questions/debian-26/checking-the-dma-udma-modes-for-sata-drive-589717/
54  *
55  * Low-level file I/O in general in Linux:
56  *
57  * http://man7.org/linux/man-pages/man2/open.2.html
58  * http://man7.org/linux/man-pages/man3/errno.3.html
59  * http://man7.org/linux/man-pages/man2/close.2.html
60  * http://man7.org/linux/man-pages/man2/lseek.2.html
61  *
62  */
63 
64 #include "common.h"
65 #include "constant.h"
66 
67 class RaWriter {
68 
69 public:
70  RaWriter(const char* filename, bool direct = false);
71  ~RaWriter();
72 
73 protected:
74  std::string filename;
75  int fd;
76  int count;
77  char tmp[FS_GRAIN_SIZE]; // in'da'stack
78  void *ptmp = &tmp[0]; // alias
79  // char* tmp;
80  bool is_open;
81 
82 public:
83  bool writeGrain();
84  void dump(const char* buf, std::size_t len);
85  // void dump_(const char* buf, std::size_t len); ///< write without buffering and with immediate flush
86  void fill(std::size_t len);
87  void finish();
88  void fwd(off_t len);
89  void seek(off_t len);
90  off_t getPos();
91  int getCount();
92  void close_();
93 };
94 
95 
96 class RawReader {
97 
98 public:
99  RawReader(const char* filename, bool direct = false);
100  ~RawReader();
101 
102 protected:
103  std::string filename;
104  int fd;
105  int count;
106  char tmp[FS_GRAIN_SIZE]; // in'da'stack
107  void *ptmp = &tmp[0]; // alias
108  // char* tmp;
109  bool is_open;
110 
111 public:
112  bool readGrain();
113  void get(char* buf, std::size_t len);
114  void fwd(off_t len);
115  void seek(off_t len);
116  void close_();
117 };
118 
119 
120 
121 
122 
123 #endif
Definition: rawrite.h:67
void fill(std::size_t len)
write null bytes
Definition: rawrite.cpp:116
void dump(const char *buf, std::size_t len)
write with buffering. flush occurs at grain boundaries
Definition: rawrite.cpp:89
void finish()
write null bytes up to the end of grain
Definition: rawrite.cpp:145
Definition: rawrite.h:96
List of common header files.
Constant/default values, version numbers.