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 | |
---|
35 | #include "wandio.h" |
---|
36 | #include "config.h" |
---|
37 | #include <stdlib.h> |
---|
38 | #include <assert.h> |
---|
39 | #include <errno.h> |
---|
40 | |
---|
41 | /* This file contains the implementation of the libtrace IO API, which format |
---|
42 | * modules should use to open, read from, write to, seek and close trace files. |
---|
43 | */ |
---|
44 | |
---|
45 | struct compression_type compression_type[] = { |
---|
46 | { "GZ", "gz", WANDIO_COMPRESS_ZLIB }, |
---|
47 | { "BZ2", "bz2", WANDIO_COMPRESS_BZ2 }, |
---|
48 | { "NONE", "", WANDIO_COMPRESS_NONE } |
---|
49 | }; |
---|
50 | |
---|
51 | #define READ_TRACE 0 |
---|
52 | #define WRITE_TRACE 0 |
---|
53 | |
---|
54 | io_t *wandio_create(const char *filename) |
---|
55 | { |
---|
56 | /* Use a peeking reader to look at the start of the trace file and |
---|
57 | * determine what type of compression may have been used to write |
---|
58 | * the file */ |
---|
59 | |
---|
60 | io_t *io = peek_open(stdio_open(filename)); |
---|
61 | char buffer[1024]; |
---|
62 | int len; |
---|
63 | if (!io) |
---|
64 | return NULL; |
---|
65 | len = wandio_peek(io, buffer, sizeof(buffer)); |
---|
66 | #if HAVE_LIBZ |
---|
67 | /* Auto detect gzip compressed data */ |
---|
68 | if (len>=2 && buffer[0] == '\037' && buffer[1] == '\213') { |
---|
69 | io = zlib_open(io); |
---|
70 | } |
---|
71 | /* Auto detect compress(1) compressed data (gzip can read this) */ |
---|
72 | if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') { |
---|
73 | io = zlib_open(io); |
---|
74 | } |
---|
75 | #endif |
---|
76 | #if HAVE_LIBBZ2 |
---|
77 | /* Auto detect bzip compressed data */ |
---|
78 | if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { |
---|
79 | io = bz_open(io); |
---|
80 | } |
---|
81 | #endif |
---|
82 | |
---|
83 | /* Now open a threaded, peekable reader using the appropriate module |
---|
84 | * to read the data */ |
---|
85 | |
---|
86 | return peek_open(thread_open(io)); |
---|
87 | } |
---|
88 | |
---|
89 | off_t wandio_tell(io_t *io) |
---|
90 | { |
---|
91 | if (!io->source->tell) { |
---|
92 | errno = -ENOSYS; |
---|
93 | return -1; |
---|
94 | } |
---|
95 | return io->source->tell(io); |
---|
96 | } |
---|
97 | |
---|
98 | off_t wandio_seek(io_t *io, off_t offset, int whence) |
---|
99 | { |
---|
100 | if (!io->source->seek) { |
---|
101 | errno = -ENOSYS; |
---|
102 | return -1; |
---|
103 | } |
---|
104 | return io->source->seek(io,offset,whence); |
---|
105 | } |
---|
106 | |
---|
107 | off_t wandio_read(io_t *io, void *buffer, off_t len) |
---|
108 | { |
---|
109 | off_t ret; |
---|
110 | #if READ_TRACE |
---|
111 | fprintf(stderr,"read(%s): %d bytes\n",io->source->name, (int)len); |
---|
112 | #endif |
---|
113 | ret=io->source->read(io,buffer,len); |
---|
114 | return ret; |
---|
115 | } |
---|
116 | |
---|
117 | off_t wandio_peek(io_t *io, void *buffer, off_t len) |
---|
118 | { |
---|
119 | off_t ret; |
---|
120 | assert(io->source->peek); /* If this fails, it means you're calling |
---|
121 | * peek on something that doesn't support |
---|
122 | * peeking. Push a peek_open() on the io |
---|
123 | * first. |
---|
124 | */ |
---|
125 | ret=io->source->peek(io, buffer, len); |
---|
126 | return ret; |
---|
127 | } |
---|
128 | |
---|
129 | void wandio_destroy(io_t *io) |
---|
130 | { io->source->close(io); } |
---|
131 | |
---|
132 | iow_t *wandio_wcreate(const char *filename, int compression_level, int flags) |
---|
133 | { |
---|
134 | iow_t *iow; |
---|
135 | |
---|
136 | assert ( compression_level >= 0 && compression_level <= 9 ); |
---|
137 | |
---|
138 | iow=stdio_wopen(filename); |
---|
139 | |
---|
140 | /* We prefer zlib if available, otherwise we'll use bzip. If neither |
---|
141 | * are present, guess we'll just have to write uncompressed */ |
---|
142 | #if HAVE_LIBZ |
---|
143 | if (compression_level != 0 && |
---|
144 | (flags & WANDIO_COMPRESS_MASK) == WANDIO_COMPRESS_ZLIB) { |
---|
145 | iow = zlib_wopen(iow,compression_level); |
---|
146 | } |
---|
147 | #endif |
---|
148 | #if HAVE_LIBBZ2 |
---|
149 | else if (compression_level != 0 && |
---|
150 | (flags & WANDIO_COMPRESS_MASK) == WANDIO_COMPRESS_BZ2) { |
---|
151 | iow = bz_wopen(iow,compression_level); |
---|
152 | } |
---|
153 | #endif |
---|
154 | /* Open a threaded writer */ |
---|
155 | return thread_wopen(iow); |
---|
156 | } |
---|
157 | |
---|
158 | off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len) |
---|
159 | { |
---|
160 | #if WRITE_TRACE |
---|
161 | fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len); |
---|
162 | #endif |
---|
163 | return iow->source->write(iow,buffer,len); |
---|
164 | } |
---|
165 | |
---|
166 | void wandio_wdestroy(iow_t *iow) |
---|
167 | { |
---|
168 | iow->source->close(iow); |
---|
169 | } |
---|
170 | |
---|