Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
SFTP_File.h
Go to the documentation of this file.
1#ifndef joedb_SFTP_File_declared
2#define joedb_SFTP_File_declared
3
5#include "joedb/ssh/SFTP.h"
6
7#include <fcntl.h>
8
9namespace joedb
10{
11 /// @ingroup journal
13 {
14 private:
15 ssh::SFTP &sftp;
16 const sftp_file file;
17
18 void throw_last_error(const char *action, const char *file_name) const
19 {
20 sftp.throw_error
21 (
22 (std::string(action) + ' ' + std::string(file_name) + ": ").c_str()
23 );
24 }
25
26 mutable int64_t pos;
27
28 void seek(int64_t offset) const
29 {
30 if (offset == pos)
31 return;
32
33 if (sftp_seek64(file, uint64_t(offset)) < 0)
34 throw_last_error("seeking in", "sftp file");
35
36 pos = offset;
37 }
38
39 public:
40 SFTP_File(ssh::SFTP &sftp, const char *file_name):
42 sftp(sftp),
43 file(sftp_open(sftp.get(), file_name, O_RDONLY, 0))
44 {
45 if (!file)
46 throw_last_error("opening", file_name);
47 }
48
49 SFTP_File(const SFTP_File &) = delete;
50 SFTP_File& operator=(const SFTP_File &) = delete;
51
52 size_t pread(char *data, size_t size, int64_t offset) const override
53 {
54 seek(offset);
55
56 const ssize_t result = sftp_read(file, data, size);
57
58 if (result < 0)
59 throw_last_error("reading", "sftp file");
60
61 pos += result;
62 return size_t(result);
63 }
64
65 ~SFTP_File() override
66 {
67 sftp_close(file);
68 }
69 };
70}
71
72#endif
SFTP_File & operator=(const SFTP_File &)=delete
SFTP_File(const SFTP_File &)=delete
size_t pread(char *data, size_t size, int64_t offset) const override
Definition SFTP_File.h:52
~SFTP_File() override
Definition SFTP_File.h:65
SFTP_File(ssh::SFTP &sftp, const char *file_name)
Definition SFTP_File.h:40
void throw_error(const char *message="sftp error: ") const
Definition SFTP.cpp:28
Open_Mode
Definition Open_Mode.h:8
@ read_existing
fails if does not exist
Definition Blob.h:7