aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/src/largeobject.cxx
blob: 8020a2fc04a023d1193a85c15a40509290ea9bd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/** Implementation of the Large Objects interface.
 *
 * Allows direct access to large objects, as well as though I/O streams.
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#include "pqxx/compiler-internal.hxx"

#include <algorithm>
#include <cerrno>
#include <stdexcept>

extern "C"
{
#include "libpq-fe.h"
}

#include "pqxx/largeobject"

#include "pqxx/internal/gates/connection-largeobject.hxx"


using namespace pqxx::internal;

namespace
{
inline int StdModeToPQMode(std::ios::openmode mode)
{
  /// Mode bits, copied from libpq-fs.h so that we no longer need that header.
  constexpr int
    INV_WRITE = 0x00020000,
    INV_READ = 0x00040000;

  return
	((mode & std::ios::in)  ? INV_READ  : 0) |
	((mode & std::ios::out) ? INV_WRITE : 0);
}


inline int StdDirToPQDir(std::ios::seekdir dir) noexcept
{
  // TODO: Figure out whether seekdir values match C counterparts!
#ifdef PQXX_SEEKDIRS_MATCH_C
  return dir;
#else
  int pqdir;
  switch (dir)
  {
  case std::ios::beg: pqdir=SEEK_SET; break;
  case std::ios::cur: pqdir=SEEK_CUR; break;
  case std::ios::end: pqdir=SEEK_END; break;

  /* Added mostly to silence compiler warning, but also to help compiler detect
   * cases where this function can be optimized away completely.  This latter
   * reason should go away as soon as PQXX_SEEKDIRS_MATCH_C works.
   */
  default: pqdir = dir; break;
  }
  return pqdir;
#endif
}


} // namespace


pqxx::largeobject::largeobject(dbtransaction &T) :
  m_id{}
{
  // (Mode is ignored as of postgres 8.1.)
  m_id = lo_creat(raw_connection(T), 0);
  if (m_id == oid_none)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    throw failure{"Could not create large object: " + reason(T.conn(), err)};
  }
}


pqxx::largeobject::largeobject(dbtransaction &T, const std::string &File) :
  m_id{}
{
  m_id = lo_import(raw_connection(T), File.c_str());
  if (m_id == oid_none)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    throw failure{
	"Could not import file '" + File + "' to large object: " +
	reason(T.conn(), err)};
  }
}


pqxx::largeobject::largeobject(const largeobjectaccess &O) noexcept :
  m_id{O.id()}
{
}


void pqxx::largeobject::to_file(
	dbtransaction &T,
	const std::string &File) const
{
  if (lo_export(raw_connection(T), id(), File.c_str()) == -1)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    throw failure{
	"Could not export large object " + to_string(m_id) + " "
	"to file '" + File + "': " + reason(T.conn(), err)};
  }
}


void pqxx::largeobject::remove(dbtransaction &T) const
{
  if (lo_unlink(raw_connection(T), id()) == -1)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    throw failure{
	"Could not delete large object " + to_string(m_id) + ": " +
	reason(T.conn(), err)};
  }
}


pqxx::internal::pq::PGconn *pqxx::largeobject::raw_connection(
	const dbtransaction &T)
{
  return gate::connection_largeobject{T.conn()}.raw_connection();
}


std::string pqxx::largeobject::reason(const connection_base &c, int err) const
{
  if (err == ENOMEM) return "Out of memory";
  if (id() == oid_none) return "No object selected";
  return gate::const_connection_largeobject{c}.error_message();
}


pqxx::largeobjectaccess::largeobjectaccess(dbtransaction &T, openmode mode) :
  largeobject{T},
  m_trans{T}
{
  open(mode);
}


pqxx::largeobjectaccess::largeobjectaccess(
	dbtransaction &T,
	oid O,
	openmode mode) :
  largeobject{O},
  m_trans{T}
{
  open(mode);
}


pqxx::largeobjectaccess::largeobjectaccess(
	dbtransaction &T,
	largeobject O,
	openmode mode) :
  largeobject{O},
  m_trans{T}
{
  open(mode);
}


pqxx::largeobjectaccess::largeobjectaccess(
	dbtransaction &T,
	const std::string &File,
	openmode mode) :
  largeobject{T, File},
  m_trans{T}
{
  open(mode);
}


pqxx::largeobjectaccess::size_type
pqxx::largeobjectaccess::seek(size_type dest, seekdir dir)
{
  const auto Result = cseek(dest, dir);
  if (Result == -1)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    throw failure{"Error seeking in large object: " + reason(err)};
  }

  return Result;
}


pqxx::largeobjectaccess::pos_type
pqxx::largeobjectaccess::cseek(off_type dest, seekdir dir) noexcept
{
  return lo_lseek(raw_connection(), m_fd, int(dest), StdDirToPQDir(dir));
}


pqxx::largeobjectaccess::pos_type
pqxx::largeobjectaccess::cwrite(const char Buf[], size_type Len) noexcept
{
  return
    std::max(
	lo_write(raw_connection(), m_fd,const_cast<char *>(Buf), size_t(Len)),
        -1);
}


pqxx::largeobjectaccess::pos_type
pqxx::largeobjectaccess::cread(char Buf[], size_type Bytes) noexcept
{
  return std::max(lo_read(raw_connection(), m_fd, Buf, size_t(Bytes)), -1);
}


pqxx::largeobjectaccess::pos_type
pqxx::largeobjectaccess::ctell() const noexcept
{
  return lo_tell(raw_connection(), m_fd);
}


void pqxx::largeobjectaccess::write(const char Buf[], size_type Len)
{
  const auto Bytes = cwrite(Buf, Len);
  if (Bytes < Len)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    if (Bytes < 0)
      throw failure{
	"Error writing to large object #" + to_string(id()) + ": " +
	reason(err)};
    if (Bytes == 0)
      throw failure{
	"Could not write to large object #" + to_string(id()) + ": " +
	reason(err)};

    throw failure{
	"Wanted to write " + to_string(Len) + " bytes to large object #" +
	to_string(id()) + "; " "could only write " + to_string(Bytes)};
  }
}


pqxx::largeobjectaccess::size_type
pqxx::largeobjectaccess::read(char Buf[], size_type Len)
{
  const auto Bytes = cread(Buf, Len);
  if (Bytes < 0)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    throw failure{
	"Error reading from large object #" + to_string(id()) + ": " +
	reason(err)};
  }
  return Bytes;
}


void pqxx::largeobjectaccess::open(openmode mode)
{
  m_fd = lo_open(raw_connection(), id(), StdModeToPQMode(mode));
  if (m_fd < 0)
  {
    const int err = errno;
    if (err == ENOMEM) throw std::bad_alloc{};
    throw failure{
	"Could not open large object " + to_string(id()) + ": " +
	reason(err)};
  }
}


void pqxx::largeobjectaccess::close() noexcept
{
  if (m_fd >= 0) lo_close(raw_connection(), m_fd);
}


pqxx::largeobjectaccess::size_type pqxx::largeobjectaccess::tell() const
{
  const size_type res = ctell();
  if (res == -1) throw failure{reason(errno)};
  return res;
}


std::string pqxx::largeobjectaccess::reason(int err) const
{
  if (m_fd == -1) return "No object opened.";
  return largeobject::reason(m_trans.conn(), err);
}


void pqxx::largeobjectaccess::process_notice(const std::string &s) noexcept
{
  m_trans.process_notice(s);
}