| 1 | /****************************************************** |
| 2 | Copyright (c) 2011-2017 Percona LLC and/or its affiliates. |
| 3 | |
| 4 | The xbstream format interface. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; version 2 of the License. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 18 | |
| 19 | *******************************************************/ |
| 20 | |
| 21 | #ifndef XBSTREAM_H |
| 22 | #define XBSTREAM_H |
| 23 | |
| 24 | #include <my_base.h> |
| 25 | |
| 26 | /* Magic value in a chunk header */ |
| 27 | #define XB_STREAM_CHUNK_MAGIC "XBSTCK01" |
| 28 | |
| 29 | /* Chunk flags */ |
| 30 | /* Chunk can be ignored if unknown version/format */ |
| 31 | #define XB_STREAM_FLAG_IGNORABLE 0x01 |
| 32 | |
| 33 | /* Magic + flags + type + path len */ |
| 34 | #define ((sizeof(XB_STREAM_CHUNK_MAGIC) - 1) + \ |
| 35 | 1 + 1 + 4) |
| 36 | #define CHUNK_TYPE_OFFSET (sizeof(XB_STREAM_CHUNK_MAGIC) - 1 + 1) |
| 37 | #define PATH_LENGTH_OFFSET (sizeof(XB_STREAM_CHUNK_MAGIC) - 1 + 1 + 1) |
| 38 | |
| 39 | typedef struct xb_wstream_struct xb_wstream_t; |
| 40 | |
| 41 | typedef struct xb_wstream_file_struct xb_wstream_file_t; |
| 42 | |
| 43 | typedef enum { |
| 44 | XB_STREAM_FMT_NONE, |
| 45 | XB_STREAM_FMT_XBSTREAM |
| 46 | } xb_stream_fmt_t; |
| 47 | |
| 48 | /************************************************************************ |
| 49 | Write interface. */ |
| 50 | |
| 51 | typedef ssize_t xb_stream_write_callback(xb_wstream_file_t *file, |
| 52 | void *userdata, |
| 53 | const void *buf, size_t len); |
| 54 | |
| 55 | xb_wstream_t *xb_stream_write_new(void); |
| 56 | |
| 57 | xb_wstream_file_t *xb_stream_write_open(xb_wstream_t *stream, const char *path, |
| 58 | MY_STAT *mystat, void *userdata, |
| 59 | xb_stream_write_callback *onwrite); |
| 60 | |
| 61 | int xb_stream_write_data(xb_wstream_file_t *file, const void *buf, size_t len); |
| 62 | |
| 63 | int xb_stream_write_close(xb_wstream_file_t *file); |
| 64 | |
| 65 | int xb_stream_write_done(xb_wstream_t *stream); |
| 66 | |
| 67 | /************************************************************************ |
| 68 | Read interface. */ |
| 69 | |
| 70 | typedef enum { |
| 71 | XB_STREAM_READ_CHUNK, |
| 72 | XB_STREAM_READ_EOF, |
| 73 | XB_STREAM_READ_ERROR |
| 74 | } xb_rstream_result_t; |
| 75 | |
| 76 | typedef enum { |
| 77 | XB_CHUNK_TYPE_UNKNOWN = '\0', |
| 78 | XB_CHUNK_TYPE_PAYLOAD = 'P', |
| 79 | XB_CHUNK_TYPE_EOF = 'E' |
| 80 | } xb_chunk_type_t; |
| 81 | |
| 82 | typedef struct xb_rstream_struct xb_rstream_t; |
| 83 | |
| 84 | typedef struct { |
| 85 | uchar flags; |
| 86 | xb_chunk_type_t type; |
| 87 | uint pathlen; |
| 88 | char path[FN_REFLEN]; |
| 89 | size_t length; |
| 90 | my_off_t offset; |
| 91 | my_off_t checksum_offset; |
| 92 | void *data; |
| 93 | ulong checksum; |
| 94 | size_t buflen; |
| 95 | } xb_rstream_chunk_t; |
| 96 | |
| 97 | xb_rstream_t *xb_stream_read_new(void); |
| 98 | |
| 99 | xb_rstream_result_t xb_stream_read_chunk(xb_rstream_t *stream, |
| 100 | xb_rstream_chunk_t *chunk); |
| 101 | |
| 102 | int xb_stream_read_done(xb_rstream_t *stream); |
| 103 | |
| 104 | int xb_stream_validate_checksum(xb_rstream_chunk_t *chunk); |
| 105 | |
| 106 | #endif |
| 107 | |