-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathbasic_ostream.cc
82 lines (66 loc) · 2.58 KB
/
basic_ostream.cc
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
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
#include "sql/basic_ostream.h"
#include "my_inttypes.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/psi/mysql_file.h"
#include "mysqld_error.h"
#include "sql/log.h"
IO_CACHE_ostream::IO_CACHE_ostream() {}
IO_CACHE_ostream::~IO_CACHE_ostream() { close(); }
bool IO_CACHE_ostream::open(
#ifdef HAVE_PSI_INTERFACE
PSI_file_key log_file_key MY_ATTRIBUTE((unused)),
#endif
const char *file_name, myf flags) {
File file = -1;
if ((file = mysql_file_open(log_file_key, file_name, O_CREAT | O_WRONLY,
MYF(MY_WME))) < 0)
return true;
if (init_io_cache(&m_io_cache, file, IO_SIZE, WRITE_CACHE, 0, 0, flags)) {
mysql_file_close(file, MYF(0));
return true;
}
return false;
}
bool IO_CACHE_ostream::close() {
if (my_b_inited(&m_io_cache)) {
int ret = end_io_cache(&m_io_cache);
ret |= mysql_file_close(m_io_cache.file, MYF(MY_WME));
return ret != 0;
}
return false;
}
bool IO_CACHE_ostream::seek(my_off_t offset) {
DBUG_ASSERT(my_b_inited(&m_io_cache));
return reinit_io_cache(&m_io_cache, WRITE_CACHE, offset, false, true);
}
bool IO_CACHE_ostream::write(const unsigned char *buffer, my_off_t length) {
DBUG_ASSERT(my_b_inited(&m_io_cache));
DBUG_EXECUTE_IF("simulate_ostream_write_failure", return true;);
return my_b_safe_write(&m_io_cache, buffer, length);
}
bool IO_CACHE_ostream::truncate(my_off_t offset) {
DBUG_ASSERT(my_b_inited(&m_io_cache));
DBUG_ASSERT(m_io_cache.file != -1);
if (my_chsize(m_io_cache.file, offset, 0, MYF(MY_WME))) return true;
reinit_io_cache(&m_io_cache, WRITE_CACHE, offset, false, true);
return false;
}
bool IO_CACHE_ostream::flush() {
DBUG_ASSERT(my_b_inited(&m_io_cache));
return flush_io_cache(&m_io_cache);
}
bool IO_CACHE_ostream::sync() {
DBUG_ASSERT(my_b_inited(&m_io_cache));
return mysql_file_sync(m_io_cache.file, MYF(MY_WME)) != 0;
}