source: lib/wandio.h @ 325f86f

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 325f86f was baef9df, checked in by Perry Lorier <perry@…>, 11 years ago

Add back support for opening files for write with arbitary flags

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34#ifndef IO_H
35#define IO_H 1 /**< Guard Define */
36#include <sys/types.h>
37#include <stdio.h>
38#include <inttypes.h>
39#include <stdbool.h>
40
41/** @file
42 *
43 * @brief Header file dealing with the Libtrace IO sub-system
44 *
45 * @author Perry Lorier
46 * @author Shane Alcock
47 *
48 * @version $Id$
49 */
50
51typedef struct io_t io_t; /**< Opaque IO handle structure for reading */
52typedef struct iow_t iow_t; /**< Opaque IO handle structure for writing */
53
54/** Structure defining a supported compression method */
55struct compression_type {
56        /** Name of the compression method */
57        const char *name;
58        /** Extension to add to the filename of files written using this
59         *  method */
60        const char *ext;
61        /** Internal type identifying the compression method */
62        int compress_type;
63};
64
65/** The list of supported compression methods */
66extern struct compression_type compression_type[];
67
68/** Structure defining a libtrace IO reader module */
69typedef struct {
70        /** Module name */
71        const char *name;
72
73        /** Reads from the IO source into the provided buffer.
74         *
75         * @param io            The IO reader
76         * @param buffer        The buffer to read into
77         * @param len           The amount of space available in the buffer
78         * @return The amount of bytes read, 0 if end of file is reached, -1
79         * if an error occurs
80         */
81        off_t (*read)(io_t *io, void *buffer, off_t len);
82
83        /** Reads from the IO source into the provided buffer but does not
84         *  advance the read pointer.
85         *
86         * @param io            The IO reader
87         * @param buffer        The buffer to read into
88         * @param len           The amount of space available in the buffer
89         * @return The amount of bytes read, 0 if end of file is reached, -1
90         * if an error occurs
91         */
92        off_t (*peek)(io_t *io, void *buffer, off_t len);
93
94        /** Returns the current offset of the read pointer for an IO source.
95         *
96         * @param io            The IO reader to get the read offset for
97         * @return The offset of the read pointer, or -1 if an error occurs
98         */
99        off_t (*tell)(io_t *io);
100       
101        /** Moves the read pointer for an IO source.
102         *
103         * @param io            The IO reader to move the read pointer for
104         * @param offset        The new read pointer offset
105         * @param whence        Where to start counting the new offset from.
106         *                      whence can be one of three values: SEEK_SET,
107         *                      SEEK_CUR and SEEK_END. See the lseek(2) manpage
108         *                      for more details as to what these mean.
109         * @return The value of the new read pointer, or -1 if an error occurs
110         */
111        off_t (*seek)(io_t *io, off_t offset, int whence);
112       
113        /** Closes an IO reader. This function should free the IO reader.
114         *
115         * @param io            The IO reader to close
116         */
117        void (*close)(io_t *io);
118} io_source_t;
119
120/** Structure defining a libtrace IO writer module */
121typedef struct {
122        /** The name of the module */
123        const char *name;
124       
125        /** Writes the contents of a buffer using an IO writer.
126         *
127         * @param iow           The IO writer to write the data with
128         * @param buffer        The buffer to be written
129         * @param len           The amount of writable data in the buffer
130         * @return The amount of data written, or -1 if an error occurs
131         */
132        off_t (*write)(iow_t *iow, const char *buffer, off_t len);
133
134        /** Closes an IO writer. This function should free the IO writer.
135         *
136         * @param iow           The IO writer to close
137         */
138        void (*close)(iow_t *iow);
139} iow_source_t;
140
141/** A libtrace IO reader */
142struct io_t {
143        /** The IO module that is used by the reader */
144        io_source_t *source;
145        /** Generic pointer to data required by the IO module */
146        void *data;
147};
148
149/** A libtrace IO writer */
150struct iow_t {
151        /** The IO module that is used by the writer */
152        iow_source_t *source;
153        /** Generic pointer to data required by the IO module */
154        void *data;
155};
156
157/** Enumeration of all supported compression methods */
158enum {
159        /** No compression */
160        WANDIO_COMPRESS_NONE    = 0,
161        /** Zlib compression */
162        WANDIO_COMPRESS_ZLIB    = 1,
163        /** Bzip compression */
164        WANDIO_COMPRESS_BZ2     = 2,
165        /** LZO compression */
166        WANDIO_COMPRESS_LZO     = 3,
167        /** All supported methods - used as a bitmask */
168        WANDIO_COMPRESS_MASK    = 7
169};
170
171/** @name IO open functions
172 *
173 * These functions deal with creating and initialising a new IO reader or
174 * writer.
175 *
176 * @{
177 */
178
179io_t *bz_open(io_t *parent);
180io_t *zlib_open(io_t *parent);
181io_t *thread_open(io_t *parent);
182io_t *peek_open(io_t *parent);
183io_t *stdio_open(const char *filename);
184
185iow_t *zlib_wopen(iow_t *child, int compress_level);
186iow_t *bz_wopen(iow_t *child, int compress_level);
187iow_t *lzo_wopen(iow_t *child, int compress_level);
188iow_t *thread_wopen(iow_t *child);
189iow_t *stdio_wopen(const char *filename, int fileflags);
190
191/* @} */
192
193/**
194 * @name Libtrace IO API functions
195 *
196 * These are the functions that should be called by the format modules to open
197 * and use files with the libtrace IO sub-system.
198 *
199 * @{ */
200
201/** Creates a new libtrace IO reader and opens the provided file for reading.
202 *
203 * @param filename      The name of the file to open
204 * @return A pointer to a new libtrace IO reader, or NULL if an error occurs
205 *
206 * This function will attempt to detect the compression format used for the
207 * given file (if any), provided that libtrace was built with the appropriate
208 * libraries.
209 */
210io_t *wandio_create(const char *filename);
211
212/** Returns the current offset of the read pointer for a libtrace IO reader.
213 *
214 * @param io            The IO reader to get the read offset for
215 * @return The offset of the read pointer, or -1 if an error occurs
216 */
217off_t wandio_tell(io_t *io);
218
219/** Changes the read pointer offset to the specified value for a libtrace IO
220 * reader.
221 *
222 * @param io            The IO reader to adjust the read pointer for
223 * @param offset        The new offset for the read pointer
224 * @param whence        Indicates where to set the read pointer from. Can be
225 *                      one of SEEK_SET, SEEK_CUR or SEEK_END.
226 * @return The new value for the read pointer, or -1 if an error occurs
227 *
228 * The arguments for this function are the same as those for lseek(2). See the
229 * lseek(2) manpage for more details.
230 */
231off_t wandio_seek(io_t *io, off_t offset, int whence);
232
233/** Reads from a libtrace IO reader into the provided buffer.
234 *
235 * @param io            The IO reader to read from
236 * @param buffer        The buffer to read into
237 * @param len           The size of the buffer
238 * @return The amount of bytes read, 0 if EOF is reached, -1 if an error occurs
239 */
240off_t wandio_read(io_t *io, void *buffer, off_t len);
241
242/** Reads from a libtrace IO reader into the provided buffer, but does not
243 * update the read pointer.
244 *
245 * @param io            The IO reader to read from
246 * @param buffer        The buffer to read into
247 * @param len           The size of the buffer
248 * @return The amount of bytes read, 0 if EOF is reached, -1 if an error occurs
249 */
250off_t wandio_peek(io_t *io, void *buffer, off_t len);
251
252/** Destroys a libtrace IO reader, closing the file and freeing the reader
253 * structure.
254 *
255 * @param io            The IO reader to destroy
256 */
257void wandio_destroy(io_t *io);
258
259/** Creates a new libtrace IO writer and opens the provided file for writing.
260 *
261 * @param filename              The name of the file to open
262 * @param compression_type      Compression type
263 * @param compression_level     The compression level to use when writing
264 * @param flags                 Flags to apply when opening the file, e.g.
265 *                              O_CREATE
266 * @return A pointer to the new libtrace IO writer, or NULL if an error occurs
267 */
268iow_t *wandio_wcreate(const char *filename, int compression_type, int compression_level, int flags);
269
270/** Writes the contents of a buffer using a libtrace IO writer.
271 *
272 * @param iow           The IO writer to write the data with
273 * @param buffer        The buffer to write out
274 * @param len           The amount of writable data in the buffer
275 * @return The amount of data written, or -1 if an error occurs
276 */
277off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len);
278
279/** Destroys a libtrace IO writer, closing the file and freeing the writer
280 * structure.
281 *
282 * @param iow           The IO writer to destroy
283 */
284void wandio_wdestroy(iow_t *iow);
285
286/** @} */
287
288/** @name libtraceio options
289 * @{ */
290extern int force_directio_read;
291extern int force_directio_write;
292extern uint64_t write_waits;
293extern uint64_t read_waits;
294extern unsigned int use_threads;
295/* @} */
296
297#endif
Note: See TracBrowser for help on using the repository browser.