1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006,2007,2009,2013 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 <gnuradio/blocks/file_sink_base.h>
28 #include <gnuradio/thread/thread.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <cstdio>
34 #include <stdexcept>
35 
36 // win32 (mingw/msvc) specific
37 #ifdef HAVE_IO_H
38 #include <io.h>
39 #endif
40 #ifdef O_BINARY
41 #define OUR_O_BINARY O_BINARY
42 #else
43 #define OUR_O_BINARY 0
44 #endif
45 
46 // should be handled via configure
47 #ifdef O_LARGEFILE
48 #define OUR_O_LARGEFILE O_LARGEFILE
49 #else
50 #define OUR_O_LARGEFILE 0
51 #endif
52 
53 namespace gr {
54 namespace blocks {
55 
file_sink_base(const char * filename,bool is_binary,bool append)56 file_sink_base::file_sink_base(const char* filename, bool is_binary, bool append)
57     : d_fp(0), d_new_fp(0), d_updated(false), d_is_binary(is_binary), d_append(append)
58 {
59     if (!open(filename))
60         throw std::runtime_error("can't open file");
61 }
62 
~file_sink_base()63 file_sink_base::~file_sink_base()
64 {
65     close();
66     if (d_fp) {
67         fclose(d_fp);
68         d_fp = 0;
69     }
70 }
71 
open(const char * filename)72 bool file_sink_base::open(const char* filename)
73 {
74     gr::thread::scoped_lock guard(d_mutex); // hold mutex for duration of this function
75 
76     // we use the open system call to get access to the O_LARGEFILE flag.
77     int fd;
78     int flags;
79     if (d_append) {
80         flags = O_WRONLY | O_CREAT | O_APPEND | OUR_O_LARGEFILE | OUR_O_BINARY;
81     } else {
82         flags = O_WRONLY | O_CREAT | O_TRUNC | OUR_O_LARGEFILE | OUR_O_BINARY;
83     }
84     if ((fd = ::open(filename, flags, 0664)) < 0) {
85         perror(filename);
86         return false;
87     }
88     if (d_new_fp) { // if we've already got a new one open, close it
89         fclose(d_new_fp);
90         d_new_fp = 0;
91     }
92 
93     if ((d_new_fp = fdopen(fd, d_is_binary ? "wb" : "w")) == NULL) {
94         perror(filename);
95         ::close(fd); // don't leak file descriptor if fdopen fails.
96     }
97 
98     d_updated = true;
99     return d_new_fp != 0;
100 }
101 
close()102 void file_sink_base::close()
103 {
104     gr::thread::scoped_lock guard(d_mutex); // hold mutex for duration of this function
105 
106     if (d_new_fp) {
107         fclose(d_new_fp);
108         d_new_fp = 0;
109     }
110     d_updated = true;
111 }
112 
do_update()113 void file_sink_base::do_update()
114 {
115     if (d_updated) {
116         gr::thread::scoped_lock guard(d_mutex); // hold mutex for duration of this block
117         if (d_fp)
118             fclose(d_fp);
119         d_fp = d_new_fp; // install new file pointer
120         d_new_fp = 0;
121         d_updated = false;
122     }
123 }
124 
set_unbuffered(bool unbuffered)125 void file_sink_base::set_unbuffered(bool unbuffered) { d_unbuffered = unbuffered; }
126 
127 } /* namespace blocks */
128 } /* namespace gr */
129