newly created files for use in recording appear in a .stubs folder, and are moved...
[ardour.git] / libs / pbd / file_manager.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <cassert>
26 #include <iostream>
27 #include <cstdio>
28
29 #ifdef __APPLE__
30 #include <mach/mach_time.h>
31 #endif
32
33 #include "pbd/compose.h"
34 #include "pbd/file_manager.h"
35 #include "pbd/debug.h"
36
37 using namespace std;
38 using namespace PBD;
39
40 FileManager* FileDescriptor::_manager;
41
42 FileManager::FileManager ()
43         : _open (0)
44 {
45         struct rlimit rl;
46         int const r = getrlimit (RLIMIT_NOFILE, &rl);
47         
48         /* XXX: this is a bit arbitrary */
49         if (r == 0) {
50                 _max_open = rl.rlim_cur - 64;
51         } else {
52                 _max_open = 256;
53         }
54
55         DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
56 }
57
58 void
59 FileManager::add (FileDescriptor* d)
60 {
61         Glib::Mutex::Lock lm (_mutex);
62         _files.push_back (d);
63 }
64
65 /** @return true on error, otherwise false */
66 bool
67 FileManager::allocate (FileDescriptor* d)
68 {
69         Glib::Mutex::Lock lm (_mutex);
70
71         if (!d->is_open()) {
72                 
73                 /* this file needs to be opened */
74                 
75                 if (_open == _max_open) {
76
77                         /* We already have the maximum allowed number of files opened, so we must try to close one.
78                            Find the unallocated, open file with the lowest last_used time.
79                         */
80
81                         double lowest_last_used = DBL_MAX;
82                         list<FileDescriptor*>::iterator oldest = _files.end ();
83
84                         for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
85                                 if ((*i)->is_open() && (*i)->_refcount == 0) {
86                                         if ((*i)->_last_used < lowest_last_used) {
87                                                 lowest_last_used = (*i)->_last_used;
88                                                 oldest = i;
89                                         }
90                                 }
91                         }
92
93                         if (oldest == _files.end()) {
94                                 /* no unallocated and open files exist, so there's nothing we can do */
95                                 return true;
96                         }
97
98                         close (*oldest);
99                         DEBUG_TRACE (
100                                 DEBUG::FileManager,
101                                 string_compose (
102                                         "closed file for %1 to release file handle; now have %2 of %3 open\n",
103                                         (*oldest)->_path, _open, _max_open
104                                         )
105                                 );
106                 }
107
108                 if (d->open ()) {
109                         DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_path));
110                         return true;
111                 }
112
113                 _open++;
114
115                 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_path, _open, _max_open));
116         }
117
118 #ifdef __APPLE__
119         d->_last_used = get_absolute_time();
120 #else
121         struct timespec t;
122         clock_gettime (CLOCK_MONOTONIC, &t);
123         d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
124 #endif
125
126         d->_refcount++;
127         
128         return false;
129 }
130
131 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
132 void
133 FileManager::release (FileDescriptor* d)
134 {
135         Glib::Mutex::Lock lm (_mutex);
136
137         d->_refcount--;
138         assert (d->_refcount >= 0);
139 }
140
141 /** Remove a file from our lists.  It will be closed if it is currently open. */
142 void
143 FileManager::remove (FileDescriptor* d)
144 {
145         Glib::Mutex::Lock lm (_mutex);
146
147         if (d->is_open ()) {
148                 close (d);
149                 DEBUG_TRACE (
150                         DEBUG::FileManager,
151                         string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open)
152                         );
153         }
154
155         _files.remove (d);
156 }
157
158 void
159 FileManager::close (FileDescriptor* d)
160 {
161         /* we must have a lock on our mutex */
162
163         d->close ();
164         d->Closed (); /* EMIT SIGNAL */
165         _open--;
166 }
167
168 FileDescriptor::FileDescriptor (string const & n, bool w)
169         : _refcount (0)
170         , _last_used (0)
171         , _path (n)
172         , _writeable (w)
173 {
174
175 }
176
177 FileManager*
178 FileDescriptor::manager ()
179 {
180         if (_manager == 0) {
181                 _manager = new FileManager;
182         }
183
184         return _manager;
185 }
186
187 /** Release a previously allocated handle to this file */
188 void
189 FileDescriptor::release ()
190 {
191         manager()->release (this);
192 }
193
194
195
196 /** @param n Filename.
197  *  @param w true to open writeable, otherwise false.
198  *  @param m Open mode for the file.
199  */
200
201 FdFileDescriptor::FdFileDescriptor (string const & n, bool w, mode_t m)
202         : FileDescriptor (n, w)
203         , _fd (-1)
204         , _mode (m)
205 {
206         manager()->add (this);
207 }
208
209 FdFileDescriptor::~FdFileDescriptor ()
210 {
211         manager()->remove (this);
212 }
213
214 bool
215 FdFileDescriptor::is_open () const
216 {
217         /* we must have a lock on the FileManager's mutex */
218
219         return _fd != -1;
220 }
221
222 bool
223 FdFileDescriptor::open ()
224 {
225         /* we must have a lock on the FileManager's mutex */
226         
227         _fd = ::open (_path.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
228         return (_fd == -1);
229 }
230
231 void
232 FdFileDescriptor::close ()
233 {
234         /* we must have a lock on the FileManager's mutex */
235
236         ::close (_fd);
237         _fd = -1;
238 }
239
240 /** @return fd, or -1 on error */
241 int
242 FdFileDescriptor::allocate ()
243 {
244         bool const f = manager()->allocate (this);
245         if (f) {
246                 return -1;
247         }
248
249         /* this is ok thread-wise because allocate () has incremented
250            the Descriptor's refcount, so the file will not be closed
251         */
252         return _fd;
253 }
254
255
256 void
257 FileDescriptor::set_path (const string& p)
258 {
259         assert (!is_open());
260         _path = p;
261 }
262
263 /** @param n Filename.
264  *  @param w true to open writeable, otherwise false.
265  */
266
267 StdioFileDescriptor::StdioFileDescriptor (string const & n, std::string const & m)
268         : FileDescriptor (n, false)
269         , _file (0)
270         , _mode (m)
271 {
272         manager()->add (this);
273 }
274
275 StdioFileDescriptor::~StdioFileDescriptor ()
276 {
277         manager()->remove (this);
278 }
279
280 bool
281 StdioFileDescriptor::is_open () const
282 {
283         /* we must have a lock on the FileManager's mutex */
284
285         return _file != 0;
286 }
287
288 bool
289 StdioFileDescriptor::open ()
290 {
291         /* we must have a lock on the FileManager's mutex */
292         
293         _file = fopen (_path.c_str(), _mode.c_str());
294         return (_file == 0);
295 }
296
297 void
298 StdioFileDescriptor::close ()
299 {
300         /* we must have a lock on the FileManager's mutex */
301
302         fclose (_file);
303         _file = 0;
304 }
305
306 /** @return FILE*, or 0 on error */
307 FILE*
308 StdioFileDescriptor::allocate ()
309 {
310         bool const f = manager()->allocate (this);
311         if (f) {
312                 return 0;
313         }
314
315         /* this is ok thread-wise because allocate () has incremented
316            the Descriptor's refcount, so the file will not be closed
317         */
318         return _file;
319 }