The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

507 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE 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. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "linuxincludes.h"
  24. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  25. #include <sys/stat.h>
  26. #include <sys/dir.h>
  27. #include <sys/ptrace.h>
  28. #include <sys/vfs.h> // for statfs
  29. #include <sys/wait.h>
  30. #include <unistd.h>
  31. #include <fnmatch.h>
  32. #include <utime.h>
  33. #include <pwd.h>
  34. #include <fcntl.h>
  35. #include <dlfcn.h>
  36. #define U_ISOFS_SUPER_MAGIC (short) 0x9660 // linux/iso_fs.h
  37. #define U_MSDOS_SUPER_MAGIC (short) 0x4d44 // linux/msdos_fs.h
  38. #define U_NFS_SUPER_MAGIC (short) 0x6969 // linux/nfs_fs.h
  39. #define U_SMB_SUPER_MAGIC (short) 0x517B // linux/smb_fs.h
  40. BEGIN_JUCE_NAMESPACE
  41. #include "../../../src/juce_core/io/files/juce_FileInputStream.h"
  42. #include "../../../src/juce_core/io/files/juce_FileOutputStream.h"
  43. #include "../../../src/juce_core/basics/juce_SystemStats.h"
  44. #include "../../../src/juce_core/basics/juce_Time.h"
  45. #include "../../../src/juce_core/basics/juce_Random.h"
  46. #include "../../../src/juce_core/io/network/juce_URL.h"
  47. #include "../../../src/juce_core/io/files/juce_NamedPipe.h"
  48. #include "../../../src/juce_core/threads/juce_InterProcessLock.h"
  49. #include "../../../src/juce_core/threads/juce_Thread.h"
  50. //==============================================================================
  51. /*
  52. Note that a lot of methods that you'd expect to find in this file actually
  53. live in juce_posix_SharedCode.h!
  54. */
  55. #include "../../macosx/platform_specific_code/juce_posix_SharedCode.h"
  56. //==============================================================================
  57. static File executableFile;
  58. //==============================================================================
  59. void juce_getFileTimes (const String& fileName,
  60. int64& modificationTime,
  61. int64& accessTime,
  62. int64& creationTime) throw()
  63. {
  64. modificationTime = 0;
  65. accessTime = 0;
  66. creationTime = 0;
  67. struct stat info;
  68. const int res = stat (fileName.toUTF8(), &info);
  69. if (res == 0)
  70. {
  71. modificationTime = (int64) info.st_mtime * 1000;
  72. accessTime = (int64) info.st_atime * 1000;
  73. creationTime = (int64) info.st_ctime * 1000;
  74. }
  75. }
  76. bool juce_setFileTimes (const String& fileName,
  77. int64 modificationTime,
  78. int64 accessTime,
  79. int64 creationTime) throw()
  80. {
  81. struct utimbuf times;
  82. times.actime = (time_t) (accessTime / 1000);
  83. times.modtime = (time_t) (modificationTime / 1000);
  84. return utime (fileName.toUTF8(), &times) == 0;
  85. }
  86. bool juce_setFileReadOnly (const String& fileName, bool isReadOnly) throw()
  87. {
  88. struct stat info;
  89. const int res = stat (fileName.toUTF8(), &info);
  90. if (res != 0)
  91. return false;
  92. info.st_mode &= 0777; // Just permissions
  93. if( isReadOnly )
  94. info.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  95. else
  96. // Give everybody write permission?
  97. info.st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  98. return chmod (fileName.toUTF8(), info.st_mode) == 0;
  99. }
  100. bool juce_copyFile (const String& s, const String& d) throw()
  101. {
  102. const File source (s), dest (d);
  103. FileInputStream* in = source.createInputStream();
  104. bool ok = false;
  105. if (in != 0)
  106. {
  107. if (dest.deleteFile())
  108. {
  109. FileOutputStream* const out = dest.createOutputStream();
  110. if (out != 0)
  111. {
  112. const int bytesCopied = out->writeFromInputStream (*in, -1);
  113. delete out;
  114. ok = (bytesCopied == source.getSize());
  115. if (! ok)
  116. dest.deleteFile();
  117. }
  118. }
  119. delete in;
  120. }
  121. return ok;
  122. }
  123. const StringArray juce_getFileSystemRoots() throw()
  124. {
  125. StringArray s;
  126. s.add (T("/"));
  127. return s;
  128. }
  129. //==============================================================================
  130. bool File::isOnCDRomDrive() const throw()
  131. {
  132. struct statfs buf;
  133. if (statfs (getFullPathName().toUTF8(), &buf) == 0)
  134. return (buf.f_type == U_ISOFS_SUPER_MAGIC);
  135. // Assume not if this fails for some reason
  136. return false;
  137. }
  138. bool File::isOnHardDisk() const throw()
  139. {
  140. struct statfs buf;
  141. if (statfs (getFullPathName().toUTF8(), &buf) == 0)
  142. {
  143. switch (buf.f_type)
  144. {
  145. case U_ISOFS_SUPER_MAGIC: // CD-ROM
  146. case U_MSDOS_SUPER_MAGIC: // Probably floppy (but could be mounted FAT filesystem)
  147. case U_NFS_SUPER_MAGIC: // Network NFS
  148. case U_SMB_SUPER_MAGIC: // Network Samba
  149. return false;
  150. default:
  151. // Assume anything else is a hard-disk (but note it could
  152. // be a RAM disk. There isn't a good way of determining
  153. // this for sure)
  154. return true;
  155. }
  156. }
  157. // Assume so if this fails for some reason
  158. return true;
  159. }
  160. bool File::isOnRemovableDrive() const throw()
  161. {
  162. jassertfalse // xxx not implemented for linux!
  163. return false;
  164. }
  165. bool File::isHidden() const throw()
  166. {
  167. return getFileName().startsWithChar (T('.'));
  168. }
  169. //==============================================================================
  170. const File File::getSpecialLocation (const SpecialLocationType type)
  171. {
  172. switch (type)
  173. {
  174. case userHomeDirectory:
  175. {
  176. const char* homeDir = getenv ("HOME");
  177. if (homeDir == 0)
  178. {
  179. struct passwd* const pw = getpwuid (getuid());
  180. if (pw != 0)
  181. homeDir = pw->pw_dir;
  182. }
  183. return File (String::fromUTF8 ((const uint8*) homeDir));
  184. }
  185. case userDocumentsDirectory:
  186. case userMusicDirectory:
  187. case userMoviesDirectory:
  188. case userApplicationDataDirectory:
  189. return File ("~");
  190. case userDesktopDirectory:
  191. return File ("~/Desktop");
  192. case commonApplicationDataDirectory:
  193. return File ("/var");
  194. case globalApplicationsDirectory:
  195. return File ("/usr");
  196. case tempDirectory:
  197. {
  198. File tmp ("/var/tmp");
  199. if (! tmp.isDirectory())
  200. {
  201. tmp = T("/tmp");
  202. if (! tmp.isDirectory())
  203. tmp = File::getCurrentWorkingDirectory();
  204. }
  205. return tmp;
  206. }
  207. case currentExecutableFile:
  208. case currentApplicationFile:
  209. if (! executableFile.exists())
  210. {
  211. Dl_info executableInfo;
  212. dladdr ((const void*) juce_getFileTimes, &executableInfo);
  213. if (executableInfo.dli_fname != 0)
  214. executableFile = File (String (executableInfo.dli_fname));
  215. }
  216. // if this fails, it's probably because juce_setCurrentExecutableFileName()
  217. // was never called to set the filename - this should be done by the juce
  218. // main() function, so maybe you've hacked it to use your own custom main()?
  219. jassert (executableFile.exists());
  220. return executableFile;
  221. default:
  222. jassertfalse // unknown type?
  223. break;
  224. }
  225. return File::nonexistent;
  226. }
  227. //==============================================================================
  228. void juce_setCurrentExecutableFileName (const String& filename) throw()
  229. {
  230. executableFile = File::getCurrentWorkingDirectory().getChildFile (filename);
  231. }
  232. //==============================================================================
  233. const File File::getCurrentWorkingDirectory() throw()
  234. {
  235. char buf [2048];
  236. return File (String::fromUTF8 ((const uint8*) getcwd (buf, sizeof (buf))));
  237. }
  238. bool File::setAsCurrentWorkingDirectory() const throw()
  239. {
  240. return chdir (getFullPathName().toUTF8()) == 0;
  241. }
  242. //==============================================================================
  243. const String File::getVersion() const throw()
  244. {
  245. return String::empty; // xxx not yet implemented
  246. }
  247. //==============================================================================
  248. const File File::getLinkedTarget() const throw()
  249. {
  250. char buffer [4096];
  251. size_t numChars = readlink ((const char*) getFullPathName().toUTF8(),
  252. buffer, sizeof (buffer));
  253. if (numChars > 0 && numChars <= sizeof (buffer))
  254. return File (String::fromUTF8 ((const uint8*) buffer, (int) numChars));
  255. return *this;
  256. }
  257. //==============================================================================
  258. bool File::moveToTrash() const throw()
  259. {
  260. if (! exists())
  261. return true;
  262. File trashCan (T("~/.Trash"));
  263. if (! trashCan.isDirectory())
  264. trashCan = T("~/.local/share/Trash/files");
  265. if (! trashCan.isDirectory())
  266. return false;
  267. return moveFileTo (trashCan.getNonexistentChildFile (getFileNameWithoutExtension(),
  268. getFileExtension()));
  269. }
  270. //==============================================================================
  271. struct FindFileStruct
  272. {
  273. String parentDir, wildCard;
  274. DIR* dir;
  275. bool getNextMatch (String& result, bool* const isDir, bool* const isHidden, int64* const fileSize,
  276. Time* const modTime, Time* const creationTime, bool* const isReadOnly) throw()
  277. {
  278. const char* const wildcardUTF8 = wildCard.toUTF8();
  279. for (;;)
  280. {
  281. struct dirent* const de = readdir (dir);
  282. if (de == 0)
  283. break;
  284. if (fnmatch (wildcardUTF8, de->d_name, FNM_CASEFOLD) == 0)
  285. {
  286. result = String::fromUTF8 ((const uint8*) de->d_name);
  287. const String path (parentDir + result);
  288. if (isDir != 0 || fileSize != 0)
  289. {
  290. struct stat info;
  291. const bool statOk = (stat (path.toUTF8(), &info) == 0);
  292. if (isDir != 0)
  293. *isDir = path.isEmpty() || (statOk && ((info.st_mode & S_IFDIR) != 0));
  294. if (isHidden != 0)
  295. *isHidden = (de->d_name[0] == '.');
  296. if (fileSize != 0)
  297. *fileSize = statOk ? info.st_size : 0;
  298. }
  299. if (modTime != 0 || creationTime != 0)
  300. {
  301. int64 m, a, c;
  302. juce_getFileTimes (path, m, a, c);
  303. if (modTime != 0)
  304. *modTime = m;
  305. if (creationTime != 0)
  306. *creationTime = c;
  307. }
  308. if (isReadOnly != 0)
  309. *isReadOnly = ! juce_canWriteToFile (path);
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. };
  316. // returns 0 on failure
  317. void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
  318. bool* isDir, bool* isHidden, int64* fileSize, Time* modTime,
  319. Time* creationTime, bool* isReadOnly) throw()
  320. {
  321. DIR* d = opendir (directory.toUTF8());
  322. if (d != 0)
  323. {
  324. FindFileStruct* ff = new FindFileStruct();
  325. ff->parentDir = directory;
  326. if (!ff->parentDir.endsWithChar (File::separator))
  327. ff->parentDir += File::separator;
  328. ff->wildCard = wildCard;
  329. if (wildCard == T("*.*"))
  330. ff->wildCard = T("*");
  331. ff->dir = d;
  332. if (ff->getNextMatch (firstResultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly))
  333. {
  334. return ff;
  335. }
  336. else
  337. {
  338. firstResultFile = String::empty;
  339. isDir = false;
  340. isHidden = false;
  341. closedir (d);
  342. delete ff;
  343. }
  344. }
  345. return 0;
  346. }
  347. bool juce_findFileNext (void* handle, String& resultFile,
  348. bool* isDir, bool* isHidden, int64* fileSize, Time* modTime, Time* creationTime, bool* isReadOnly) throw()
  349. {
  350. FindFileStruct* const ff = (FindFileStruct*) handle;
  351. if (ff != 0)
  352. return ff->getNextMatch (resultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly);
  353. return false;
  354. }
  355. void juce_findFileClose (void* handle) throw()
  356. {
  357. FindFileStruct* const ff = (FindFileStruct*) handle;
  358. if (ff != 0)
  359. {
  360. closedir (ff->dir);
  361. delete ff;
  362. }
  363. }
  364. bool juce_launchFile (const String& fileName,
  365. const String& parameters) throw()
  366. {
  367. String cmdString (fileName);
  368. cmdString << " " << parameters;
  369. if (URL::isProbablyAWebsiteURL (fileName)
  370. || URL::isProbablyAnEmailAddress (fileName))
  371. {
  372. // create a command that tries to launch a bunch of likely browsers
  373. const char* const browserNames[] = { "/etc/alternatives/x-www-browser", "firefox", "mozilla", "konqueror", "opera" };
  374. StringArray cmdLines;
  375. for (int i = 0; i < numElementsInArray (browserNames); ++i)
  376. cmdLines.add (String (browserNames[i]) + T(" ") + cmdString.trim().quoted());
  377. cmdString = cmdLines.joinIntoString (T(" || "));
  378. }
  379. if (cmdString.startsWithIgnoreCase (T("file:")))
  380. cmdString = cmdString.substring (5);
  381. const char* const argv[4] = { "/bin/sh", "-c", (const char*) cmdString.toUTF8(), 0 };
  382. const int cpid = fork();
  383. if (cpid == 0)
  384. {
  385. setsid();
  386. // Child process
  387. execve (argv[0], (char**) argv, environ);
  388. exit (0);
  389. }
  390. return cpid >= 0;
  391. }
  392. END_JUCE_NAMESPACE