Use _fseeki64 on Windows when handling content files.
[dcpomatic.git] / src / lib / cross.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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 <fstream>
21 #include <boost/algorithm/string.hpp>
22 #include "cross.h"
23 #include "compose.hpp"
24 #include "log.h"
25 #ifdef DCPOMATIC_LINUX
26 #include <unistd.h>
27 #include <mntent.h>
28 #endif
29 #ifdef DCPOMATIC_WINDOWS
30 #include <windows.h>
31 #undef DATADIR
32 #include <shlwapi.h>
33 #endif
34 #ifdef DCPOMATIC_OSX
35 #include <sys/sysctl.h>
36 #include <mach-o/dyld.h>
37 #endif
38 #ifdef DCPOMATIC_POSIX
39 #include <sys/types.h>
40 #include <ifaddrs.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #endif
44 #include "exceptions.h"
45
46 using std::pair;
47 using std::list;
48 using std::ifstream;
49 using std::string;
50 using std::wstring;
51 using std::make_pair;
52 using boost::shared_ptr;
53
54 /** @param s Number of seconds to sleep for */
55 void
56 dcpomatic_sleep (int s)
57 {
58 #ifdef DCPOMATIC_POSIX
59         sleep (s);
60 #endif
61 #ifdef DCPOMATIC_WINDOWS
62         Sleep (s * 1000);
63 #endif
64 }
65
66 /** @return A string of CPU information (model name etc.) */
67 string
68 cpu_info ()
69 {
70         string info;
71         
72 #ifdef DCPOMATIC_LINUX
73         /* This use of ifstream is ok; the filename can never
74            be non-Latin
75         */
76         ifstream f ("/proc/cpuinfo");
77         while (f.good ()) {
78                 string l;
79                 getline (f, l);
80                 if (boost::algorithm::starts_with (l, "model name")) {
81                         string::size_type const c = l.find (':');
82                         if (c != string::npos) {
83                                 info = l.substr (c + 2);
84                         }
85                 }
86         }
87 #endif
88
89 #ifdef DCPOMATIC_OSX
90         char buffer[64];
91         size_t N = sizeof (buffer);
92         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
93                 info = buffer;
94         }
95 #endif          
96
97 #ifdef DCPOMATIC_WINDOWS
98         HKEY key;
99         if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
100                 return info;
101         }
102
103         DWORD type;
104         DWORD data;
105         if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
106                 return info;
107         }
108
109         if (type != REG_SZ) {
110                 return info;
111         }
112
113         wstring value (data / sizeof (wchar_t), L'\0');
114         if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
115                 RegCloseKey (key);
116                 return info;
117         }
118
119         info = string (value.begin(), value.end());
120         
121         RegCloseKey (key);
122
123 #endif  
124         
125         return info;
126 }
127
128 #ifdef DCPOMATIC_OSX
129 /** @return Path of the Contents directory in the .app */
130 boost::filesystem::path
131 app_contents ()
132 {
133         uint32_t size = 1024;
134         char buffer[size];
135         if (_NSGetExecutablePath (buffer, &size)) {
136                 throw StringError ("_NSGetExecutablePath failed");
137         }
138         
139         boost::filesystem::path path (buffer);
140         path = boost::filesystem::canonical (path);
141         path = path.parent_path ();
142         path = path.parent_path ();
143         return path;
144 }
145 #endif
146
147 void
148 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
149 {
150 #ifdef DCPOMATIC_WINDOWS
151         SECURITY_ATTRIBUTES security;
152         security.nLength = sizeof (security);
153         security.bInheritHandle = TRUE;
154         security.lpSecurityDescriptor = 0;
155
156         HANDLE child_stderr_read;
157         HANDLE child_stderr_write;
158         if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
159                 log->log ("ffprobe call failed (could not CreatePipe)");
160                 return;
161         }
162
163         wchar_t dir[512];
164         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
165         PathRemoveFileSpec (dir);
166         SetCurrentDirectory (dir);
167
168         STARTUPINFO startup_info;
169         ZeroMemory (&startup_info, sizeof (startup_info));
170         startup_info.cb = sizeof (startup_info);
171         startup_info.hStdError = child_stderr_write;
172         startup_info.dwFlags |= STARTF_USESTDHANDLES;
173
174         wchar_t command[512];
175         wcscpy (command, L"ffprobe.exe \"");
176
177         wchar_t file[512];
178         MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
179         wcscat (command, file);
180
181         wcscat (command, L"\"");
182
183         PROCESS_INFORMATION process_info;
184         ZeroMemory (&process_info, sizeof (process_info));
185         if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
186                 log->log ("ffprobe call failed (could not CreateProcess)");
187                 return;
188         }
189
190         FILE* o = fopen_boost (out, "w");
191         if (!o) {
192                 log->log ("ffprobe call failed (could not create output file)");
193                 return;
194         }
195
196         CloseHandle (child_stderr_write);
197
198         while (1) {
199                 char buffer[512];
200                 DWORD read;
201                 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
202                         break;
203                 }
204                 fwrite (buffer, read, 1, o);
205         }
206
207         fclose (o);
208
209         WaitForSingleObject (process_info.hProcess, INFINITE);
210         CloseHandle (process_info.hProcess);
211         CloseHandle (process_info.hThread);
212         CloseHandle (child_stderr_read);
213 #endif
214
215 #ifdef DCPOMATIC_LINUX 
216         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
217         log->log (String::compose ("Probing with %1", ffprobe));
218         system (ffprobe.c_str ());
219 #endif
220
221 #ifdef DCPOMATIC_OSX
222         boost::filesystem::path path = app_contents();
223         path /= "MacOS";
224         path /= "ffprobe";
225         
226         string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
227         log->log (String::compose ("Probing with %1", ffprobe));
228         system (ffprobe.c_str ());
229 #endif
230 }
231
232 list<pair<string, string> >
233 mount_info ()
234 {
235         list<pair<string, string> > m;
236         
237 #ifdef DCPOMATIC_LINUX
238         FILE* f = setmntent ("/etc/mtab", "r");
239         if (!f) {
240                 return m;
241         }
242         
243         while (1) {
244                 struct mntent* mnt = getmntent (f);
245                 if (!mnt) {
246                         break;
247                 }
248
249                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
250         }
251
252         endmntent (f);
253 #endif
254
255         return m;
256 }
257
258 boost::filesystem::path
259 openssl_path ()
260 {
261 #ifdef DCPOMATIC_WINDOWS
262         wchar_t dir[512];
263         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
264         PathRemoveFileSpec (dir);
265         
266         boost::filesystem::path path = dir;
267         path /= "openssl.exe";
268         return path;
269 #else   
270         /* We assume that it's on the path for Linux and OS X */
271         return "openssl";
272 #endif
273
274 }
275
276 /* Apparently there is no way to create an ofstream using a UTF-8
277    filename under Windows.  We are hence reduced to using fopen
278    with this wrapper.
279 */
280 FILE *
281 fopen_boost (boost::filesystem::path p, string t)
282 {
283 #ifdef DCPOMATIC_WINDOWS
284         wstring w (t.begin(), t.end());
285         /* c_str() here should give a UTF-16 string */
286         return _wfopen (p.c_str(), w.c_str ());
287 #else
288         return fopen (p.c_str(), t.c_str ());
289 #endif
290 }
291
292 int
293 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
294 {
295 #ifdef DCPOMATIC_WINDOWS
296         return _fseeki64 (stream, offset, whence);
297 #else   
298         return fseek (stream, offset, whence);
299 #endif  
300 }