1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012, 2018 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "file_source_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <gnuradio/thread/thread.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <cstdio>
35 #include <stdexcept>
36 
37 #ifdef _MSC_VER
38 #define GR_FSEEK _fseeki64
39 #define GR_FTELL _ftelli64
40 #define GR_FSTAT _fstati64
41 #define GR_FILENO _fileno
42 #define GR_STAT _stati64
43 #define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
44 #else
45 #define GR_FSEEK fseeko
46 #define GR_FTELL ftello
47 #define GR_FSTAT fstat
48 #define GR_FILENO fileno
49 #define GR_STAT stat
50 #endif
51 
52 namespace gr {
53 namespace blocks {
54 
make(size_t itemsize,const char * filename,bool repeat,uint64_t start_offset_items,uint64_t length_items)55 file_source::sptr file_source::make(size_t itemsize,
56                                     const char* filename,
57                                     bool repeat,
58                                     uint64_t start_offset_items,
59                                     uint64_t length_items)
60 {
61     return gnuradio::get_initial_sptr(new file_source_impl(
62         itemsize, filename, repeat, start_offset_items, length_items));
63 }
64 
file_source_impl(size_t itemsize,const char * filename,bool repeat,uint64_t start_offset_items,uint64_t length_items)65 file_source_impl::file_source_impl(size_t itemsize,
66                                    const char* filename,
67                                    bool repeat,
68                                    uint64_t start_offset_items,
69                                    uint64_t length_items)
70     : sync_block(
71           "file_source", io_signature::make(0, 0, 0), io_signature::make(1, 1, itemsize)),
72       d_itemsize(itemsize),
73       d_start_offset_items(start_offset_items),
74       d_length_items(length_items),
75       d_fp(0),
76       d_new_fp(0),
77       d_repeat(repeat),
78       d_updated(false),
79       d_file_begin(true),
80       d_repeat_cnt(0),
81       d_add_begin_tag(pmt::PMT_NIL)
82 {
83     open(filename, repeat, start_offset_items, length_items);
84     do_update();
85 
86     std::stringstream str;
87     str << name() << unique_id();
88     _id = pmt::string_to_symbol(str.str());
89 }
90 
~file_source_impl()91 file_source_impl::~file_source_impl()
92 {
93     if (d_fp)
94         fclose((FILE*)d_fp);
95     if (d_new_fp)
96         fclose((FILE*)d_new_fp);
97 }
98 
seek(int64_t seek_point,int whence)99 bool file_source_impl::seek(int64_t seek_point, int whence)
100 {
101     if (d_seekable) {
102         seek_point += d_start_offset_items;
103 
104         switch (whence) {
105         case SEEK_SET:
106             break;
107         case SEEK_CUR:
108             seek_point += (d_length_items - d_items_remaining);
109             break;
110         case SEEK_END:
111             seek_point = d_length_items - seek_point;
112             break;
113         default:
114             GR_LOG_WARN(d_logger, "bad seek mode");
115             return 0;
116         }
117 
118         if ((seek_point < (int64_t)d_start_offset_items) ||
119             (seek_point > (int64_t)(d_start_offset_items + d_length_items - 1))) {
120             GR_LOG_WARN(d_logger, "bad seek point");
121             return 0;
122         }
123         return GR_FSEEK((FILE*)d_fp, seek_point * d_itemsize, SEEK_SET) == 0;
124     } else {
125         GR_LOG_WARN(d_logger, "file not seekable");
126         return 0;
127     }
128 }
129 
130 
open(const char * filename,bool repeat,uint64_t start_offset_items,uint64_t length_items)131 void file_source_impl::open(const char* filename,
132                             bool repeat,
133                             uint64_t start_offset_items,
134                             uint64_t length_items)
135 {
136     // obtain exclusive access for duration of this function
137     gr::thread::scoped_lock lock(fp_mutex);
138 
139     if (d_new_fp) {
140         fclose(d_new_fp);
141         d_new_fp = 0;
142     }
143 
144     if ((d_new_fp = fopen(filename, "rb")) == NULL) {
145         GR_LOG_ERROR(d_logger, boost::format("%s: %s") % filename % strerror(errno));
146         throw std::runtime_error("can't open file");
147     }
148 
149     struct GR_STAT st;
150 
151     if (GR_FSTAT(GR_FILENO(d_new_fp), &st)) {
152         GR_LOG_ERROR(d_logger, boost::format("%s: %s") % filename % strerror(errno));
153         throw std::runtime_error("can't fstat file");
154     }
155     if (S_ISREG(st.st_mode)) {
156         d_seekable = true;
157     } else {
158         d_seekable = false;
159     }
160 
161     uint64_t file_size;
162 
163     if (d_seekable) {
164         // Check to ensure the file will be consumed according to item size
165         GR_FSEEK(d_new_fp, 0, SEEK_END);
166         file_size = GR_FTELL(d_new_fp);
167 
168         // Make sure there will be at least one item available
169         if ((file_size / d_itemsize) < (start_offset_items + 1)) {
170             if (start_offset_items) {
171                 GR_LOG_WARN(d_logger, "file is too small for start offset");
172             } else {
173                 GR_LOG_WARN(d_logger, "file is too small");
174             }
175             fclose(d_new_fp);
176             throw std::runtime_error("file is too small");
177         }
178     } else {
179         file_size = INT64_MAX;
180     }
181 
182     uint64_t items_available = (file_size / d_itemsize - start_offset_items);
183 
184     // If length is not specified, use the remainder of the file. Check alignment at end.
185     if (length_items == 0) {
186         length_items = items_available;
187         if (file_size % d_itemsize) {
188             GR_LOG_WARN(d_logger, "file size is not a multiple of item size");
189         }
190     }
191 
192     // Check specified length. Warn and use available items instead of throwing an
193     // exception.
194     if (length_items > items_available) {
195         length_items = items_available;
196         GR_LOG_WARN(d_logger, "file too short, will read fewer than requested items");
197     }
198 
199     // Rewind to start offset
200     if (d_seekable) {
201         GR_FSEEK(d_new_fp, start_offset_items * d_itemsize, SEEK_SET);
202     }
203 
204     d_updated = true;
205     d_repeat = repeat;
206     d_start_offset_items = start_offset_items;
207     d_length_items = length_items;
208     d_items_remaining = length_items;
209 }
210 
close()211 void file_source_impl::close()
212 {
213     // obtain exclusive access for duration of this function
214     gr::thread::scoped_lock lock(fp_mutex);
215 
216     if (d_new_fp != NULL) {
217         fclose(d_new_fp);
218         d_new_fp = NULL;
219     }
220     d_updated = true;
221 }
222 
do_update()223 void file_source_impl::do_update()
224 {
225     if (d_updated) {
226         gr::thread::scoped_lock lock(fp_mutex); // hold while in scope
227 
228         if (d_fp)
229             fclose(d_fp);
230 
231         d_fp = d_new_fp; // install new file pointer
232         d_new_fp = 0;
233         d_updated = false;
234         d_file_begin = true;
235     }
236 }
237 
set_begin_tag(pmt::pmt_t val)238 void file_source_impl::set_begin_tag(pmt::pmt_t val) { d_add_begin_tag = val; }
239 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)240 int file_source_impl::work(int noutput_items,
241                            gr_vector_const_void_star& input_items,
242                            gr_vector_void_star& output_items)
243 {
244     char* o = (char*)output_items[0];
245     uint64_t size = noutput_items;
246 
247     do_update(); // update d_fp is reqd
248     if (d_fp == NULL)
249         throw std::runtime_error("work with file not open");
250 
251     gr::thread::scoped_lock lock(fp_mutex); // hold for the rest of this function
252 
253     // No items remaining - all done
254     if (d_items_remaining == 0) {
255         return WORK_DONE;
256     }
257 
258     while (size) {
259 
260         // Add stream tag whenever the file starts again
261         if (d_file_begin && d_add_begin_tag != pmt::PMT_NIL) {
262             add_item_tag(0,
263                          nitems_written(0) + noutput_items - size,
264                          d_add_begin_tag,
265                          pmt::from_long(d_repeat_cnt),
266                          _id);
267             d_file_begin = false;
268         }
269 
270         uint64_t nitems_to_read = std::min(size, d_items_remaining);
271 
272         size_t nitems_read = fread(o, d_itemsize, nitems_to_read, (FILE*)d_fp);
273         if (nitems_to_read != nitems_read) {
274             // Size of non-seekable files is unknown. EOF is normal.
275             if (!d_seekable && feof((FILE*)d_fp)) {
276                 size -= nitems_read;
277                 d_items_remaining = 0;
278                 break;
279             }
280 
281             throw std::runtime_error("fread error");
282         }
283 
284         size -= nitems_read;
285         d_items_remaining -= nitems_read;
286         o += nitems_read * d_itemsize;
287 
288         // Ran out of items ("EOF")
289         if (d_items_remaining == 0) {
290 
291             // Repeat: rewind and request tag
292             if (d_repeat && d_seekable) {
293                 GR_FSEEK(d_fp, d_start_offset_items * d_itemsize, SEEK_SET);
294                 d_items_remaining = d_length_items;
295                 if (d_add_begin_tag != pmt::PMT_NIL) {
296                     d_file_begin = true;
297                     d_repeat_cnt++;
298                 }
299             }
300 
301             // No repeat: return
302             else {
303                 break;
304             }
305         }
306     }
307 
308     return (noutput_items - size);
309 }
310 
311 } /* namespace blocks */
312 } /* namespace gr */
313