automotive-dlt
dlt_cdh_streamer.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <syslog.h>
33 #include "dlt_cdh_streamer.h"
34 
35 #define Z_CHUNK_SZ 1024*128
36 #define Z_MODE_STR "wb1"
37 
38 cdh_status_t stream_init(file_streamer_t* p_fs, const char* p_src_fname, const char* p_dst_fname)
39 {
40  if (p_fs == NULL)
41  {
42  syslog(LOG_ERR, "Internal pointer error in 'stream_init'");
43  return CDH_NOK;
44  }
45 
46  memset(p_fs, 0, sizeof(file_streamer_t));
47 
48  /* Allow to not save the coredump */
49  if (p_dst_fname == NULL)
50  {
51  p_fs->gz_dst_file = 0;
52  }
53  else
54  {
55  // Create output file
56  p_fs->gz_dst_file = gzopen(p_dst_fname, Z_MODE_STR);
57  if (p_fs->gz_dst_file == Z_NULL)
58  {
59  //return CDH_NOK;
60  syslog(LOG_ERR, "Cannot open output filename <%s>. %s", p_dst_fname, strerror(errno));
61  p_fs->gz_dst_file = 0;
62 
63  }
64  }
65 
66  if (p_fs->gz_dst_file == Z_NULL)
67  syslog(LOG_WARNING, "The coredump will be processed, but not written");
68 
69  // Open input file
70  if (p_src_fname == NULL)
71  {
72  p_fs->stream = stdin;
73  }
74  else
75  {
76  if ((p_fs->stream = fopen(p_src_fname, "rb")) == NULL)
77  {
78  syslog(LOG_ERR, "Cannot open filename <%s>. %s", p_src_fname, strerror(errno));
79  return CDH_NOK;
80  }
81  }
82 
83  // Allocate read buffer
84  if ((p_fs->read_buf = (unsigned char*) malloc(Z_CHUNK_SZ)) == NULL)
85  {
86  syslog(LOG_ERR, "Cannot allocate %d bytes for read buffer. %s", Z_CHUNK_SZ, strerror(errno));
87  return CDH_NOK;
88  }
89 
90  return CDH_OK;
91 }
92 
94 {
95  if (p_fs == NULL)
96  {
97  syslog(LOG_ERR, "Internal pointer error in 'stream_close'");
98  return CDH_NOK;
99  }
100 
101  if (p_fs->gz_dst_file != NULL)
102  {
103  gzflush(p_fs->gz_dst_file, Z_FINISH);
104  gzclose(p_fs->gz_dst_file);
105  p_fs->gz_dst_file = NULL;
106  }
107 
108  if (p_fs->stream != NULL)
109  {
110  fclose(p_fs->stream);
111  p_fs->stream = NULL;
112  }
113 
114  if (p_fs->read_buf != NULL)
115  {
116  free(p_fs->read_buf);
117  p_fs->read_buf = NULL;
118  }
119 
120  return CDH_OK;
121 }
122 
123 cdh_status_t stream_read(file_streamer_t* p_fs, void* p_buf, unsigned int p_size)
124 {
125  unsigned int byte_read = 0;
126 
127  if (p_fs == NULL)
128  {
129  syslog(LOG_ERR, "Internal pointer error in 'stream_read'");
130  return CDH_NOK;
131  }
132 
133  if (p_buf == NULL)
134  {
135  syslog(LOG_ERR, "Internal buffer pointer error in 'stream_read'");
136  return CDH_NOK;
137  }
138 
139  if ((byte_read = fread(p_buf, 1, p_size, p_fs->stream)) != p_size)
140  {
141  syslog(LOG_WARNING, "Cannot read %d bytes from src. %s", p_size, strerror(errno));
142  return CDH_NOK;
143  }
144 
145  p_fs->offset += byte_read;
146 
147  if (p_fs->gz_dst_file != NULL)
148  gzwrite(p_fs->gz_dst_file, p_buf, byte_read);
149 
150  return CDH_OK;
151 }
152 
154 {
155  if (p_fs == NULL || p_fs->stream == NULL)
156  {
157  syslog(LOG_ERR, "Internal pointer error in 'stream_move_ahead'");
158  return CDH_NOK;
159  }
160 
161  while (!feof(p_fs->stream))
162  {
163  size_t read_bytes = fread(p_fs->read_buf, 1, Z_CHUNK_SZ, p_fs->stream);
164 
165  if (p_fs->gz_dst_file != NULL)
166  gzwrite(p_fs->gz_dst_file, p_fs->read_buf, read_bytes);
167 
168  p_fs->offset += read_bytes;
169  if (ferror(p_fs->stream))
170  {
171  syslog(LOG_WARNING, "Error reading from the src stream: %s", strerror(errno));
172  return CDH_NOK;
173  }
174  }
175 
176  return CDH_OK;
177 }
178 
179 int stream_move_to_offest(file_streamer_t* p_fs, unsigned int p_offset)
180 {
181  int bytes_to_read = 0;
182 
183  if (p_fs == NULL)
184  {
185  syslog(LOG_ERR, "Internal pointer error in 'stream_move_to_offest'");
186  return CDH_NOK;
187  }
188 
189  bytes_to_read = p_offset - p_fs->offset;
190 
191  return stream_move_ahead(p_fs, bytes_to_read);
192 }
193 
194 int stream_move_ahead(file_streamer_t* p_fs, unsigned int p_nbbytes)
195 {
196  int bytes_to_read = p_nbbytes;
197 
198  if (p_fs == NULL)
199  {
200  syslog(LOG_ERR, "Internal pointer error in 'stream_move_ahead'");
201  return CDH_NOK;
202  }
203 
204  while (bytes_to_read > 0)
205  {
206  size_t chunk_size = bytes_to_read > Z_CHUNK_SZ ? Z_CHUNK_SZ : bytes_to_read;
207  size_t read_bytes = fread(p_fs->read_buf, 1, chunk_size, p_fs->stream);
208 
209  if (read_bytes != chunk_size)
210  {
211  syslog(LOG_WARNING, "Cannot move ahead by %d bytes from src. Read %lu bytes", p_nbbytes, read_bytes);
212  return CDH_NOK;
213  }
214 
215  if (p_fs->gz_dst_file != 0)
216  gzwrite(p_fs->gz_dst_file, p_fs->read_buf, chunk_size);
217 
218  bytes_to_read -= chunk_size;
219  }
220 
221  p_fs->offset += p_nbbytes;
222 
223  return CDH_OK;
224 }
225 
227 {
228  if (p_fs == NULL)
229  {
230  syslog(LOG_ERR, "Internal pointer error in 'stream_get_offset'");
231  return CDH_NOK;
232  }
233 
234  return p_fs->offset;
235 }
unsigned int offset
int stream_finish(file_streamer_t *p_fs)
#define Z_CHUNK_SZ
unsigned char * read_buf
unsigned int stream_get_offset(file_streamer_t *p_fs)
cdh_status_t
int stream_move_to_offest(file_streamer_t *p_fs, unsigned int p_offset)
cdh_status_t stream_read(file_streamer_t *p_fs, void *p_buf, unsigned int p_size)
cdh_status_t stream_close(file_streamer_t *p_fs)
cdh_status_t stream_init(file_streamer_t *p_fs, const char *p_src_fname, const char *p_dst_fname)
int stream_move_ahead(file_streamer_t *p_fs, unsigned int p_nbbytes)
#define Z_MODE_STR
#define NULL
Definition: dlt_common.h:232