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.

484 lines
14KB

  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. void juce_getFileTimes (const String& fileName,
  58. int64& modificationTime,
  59. int64& accessTime,
  60. int64& creationTime) throw()
  61. {
  62. modificationTime = 0;
  63. accessTime = 0;
  64. creationTime = 0;
  65. struct stat info;
  66. const int res = stat (fileName.toUTF8(), &info);
  67. if (res == 0)
  68. {
  69. modificationTime = (int64) info.st_mtime * 1000;
  70. accessTime = (int64) info.st_atime * 1000;
  71. creationTime = (int64) info.st_ctime * 1000;
  72. }
  73. }
  74. bool juce_setFileTimes (const String& fileName,
  75. int64 modificationTime,
  76. int64 accessTime,
  77. int64 creationTime) throw()
  78. {
  79. struct utimbuf times;
  80. times.actime = (time_t) (accessTime / 1000);
  81. times.modtime = (time_t) (modificationTime / 1000);
  82. return utime (fileName.toUTF8(), &times) == 0;
  83. }
  84. bool juce_setFileReadOnly (const String& fileName, bool isReadOnly) throw()
  85. {
  86. struct stat info;
  87. const int res = stat (fileName.toUTF8(), &info);
  88. if (res != 0)
  89. return false;
  90. info.st_mode &= 0777; // Just permissions
  91. if( isReadOnly )
  92. info.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  93. else
  94. // Give everybody write permission?
  95. info.st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  96. return chmod (fileName.toUTF8(), info.st_mode) == 0;
  97. }
  98. bool juce_copyFile (const String& s, const String& d) throw()
  99. {
  100. const File source (s), dest (d);
  101. FileInputStream* in = source.createInputStream();
  102. bool ok = false;
  103. if (in != 0)
  104. {
  105. if (dest.deleteFile())
  106. {
  107. FileOutputStream* const out = dest.createOutputStream();
  108. if (out != 0)
  109. {
  110. const int bytesCopied = out->writeFromInputStream (*in, -1);
  111. delete out;
  112. ok = (bytesCopied == source.getSize());
  113. if (! ok)
  114. dest.deleteFile();
  115. }
  116. }
  117. delete in;
  118. }
  119. return ok;
  120. }
  121. const StringArray juce_getFileSystemRoots() throw()
  122. {
  123. StringArray s;
  124. s.add (T("/"));
  125. return s;
  126. }
  127. //==============================================================================
  128. bool File::isOnCDRomDrive() const throw()
  129. {
  130. struct statfs buf;
  131. if (statfs (getFullPathName().toUTF8(), &buf) == 0)
  132. return (buf.f_type == U_ISOFS_SUPER_MAGIC);
  133. // Assume not if this fails for some reason
  134. return false;
  135. }
  136. bool File::isOnHardDisk() const throw()
  137. {
  138. struct statfs buf;
  139. if (statfs (getFullPathName().toUTF8(), &buf) == 0)
  140. {
  141. switch (buf.f_type)
  142. {
  143. case U_ISOFS_SUPER_MAGIC: // CD-ROM
  144. case U_MSDOS_SUPER_MAGIC: // Probably floppy (but could be mounted FAT filesystem)
  145. case U_NFS_SUPER_MAGIC: // Network NFS
  146. case U_SMB_SUPER_MAGIC: // Network Samba
  147. return false;
  148. default:
  149. // Assume anything else is a hard-disk (but note it could
  150. // be a RAM disk. There isn't a good way of determining
  151. // this for sure)
  152. return true;
  153. }
  154. }
  155. // Assume so if this fails for some reason
  156. return true;
  157. }
  158. bool File::isOnRemovableDrive() const throw()
  159. {
  160. jassertfalse // xxx not implemented for linux!
  161. return false;
  162. }
  163. bool File::isHidden() const throw()
  164. {
  165. return getFileName().startsWithChar (T('.'));
  166. }
  167. //==============================================================================
  168. const File File::getSpecialLocation (const SpecialLocationType type)
  169. {
  170. switch (type)
  171. {
  172. case userHomeDirectory:
  173. {
  174. const char* homeDir = getenv ("HOME");
  175. if (homeDir == 0)
  176. {
  177. struct passwd* const pw = getpwuid (getuid());
  178. if (pw != 0)
  179. homeDir = pw->pw_dir;
  180. }
  181. return File (String::fromUTF8 ((const uint8*) homeDir));
  182. }
  183. case userDocumentsDirectory:
  184. case userMusicDirectory:
  185. case userMoviesDirectory:
  186. case userApplicationDataDirectory:
  187. return File ("~");
  188. case userDesktopDirectory:
  189. return File ("~/Desktop");
  190. case commonApplicationDataDirectory:
  191. return File ("/var");
  192. case globalApplicationsDirectory:
  193. return File ("/usr");
  194. case tempDirectory:
  195. {
  196. File tmp ("/var/tmp");
  197. if (! tmp.isDirectory())
  198. {
  199. tmp = T("/tmp");
  200. if (! tmp.isDirectory())
  201. tmp = File::getCurrentWorkingDirectory();
  202. }
  203. return tmp;
  204. }
  205. case currentExecutableFile:
  206. case currentApplicationFile:
  207. return juce_getExecutableFile();
  208. default:
  209. jassertfalse // unknown type?
  210. break;
  211. }
  212. return File::nonexistent;
  213. }
  214. //==============================================================================
  215. const File File::getCurrentWorkingDirectory() throw()
  216. {
  217. char buf [2048];
  218. return File (String::fromUTF8 ((const uint8*) getcwd (buf, sizeof (buf))));
  219. }
  220. bool File::setAsCurrentWorkingDirectory() const throw()
  221. {
  222. return chdir (getFullPathName().toUTF8()) == 0;
  223. }
  224. //==============================================================================
  225. const String File::getVersion() const throw()
  226. {
  227. return String::empty; // xxx not yet implemented
  228. }
  229. //==============================================================================
  230. const File File::getLinkedTarget() const throw()
  231. {
  232. char buffer [4096];
  233. size_t numChars = readlink ((const char*) getFullPathName().toUTF8(),
  234. buffer, sizeof (buffer));
  235. if (numChars > 0 && numChars <= sizeof (buffer))
  236. return File (String::fromUTF8 ((const uint8*) buffer, (int) numChars));
  237. return *this;
  238. }
  239. //==============================================================================
  240. bool File::moveToTrash() const throw()
  241. {
  242. if (! exists())
  243. return true;
  244. File trashCan (T("~/.Trash"));
  245. if (! trashCan.isDirectory())
  246. trashCan = T("~/.local/share/Trash/files");
  247. if (! trashCan.isDirectory())
  248. return false;
  249. return moveFileTo (trashCan.getNonexistentChildFile (getFileNameWithoutExtension(),
  250. getFileExtension()));
  251. }
  252. //==============================================================================
  253. struct FindFileStruct
  254. {
  255. String parentDir, wildCard;
  256. DIR* dir;
  257. bool getNextMatch (String& result, bool* const isDir, bool* const isHidden, int64* const fileSize,
  258. Time* const modTime, Time* const creationTime, bool* const isReadOnly) throw()
  259. {
  260. const char* const wildcardUTF8 = wildCard.toUTF8();
  261. for (;;)
  262. {
  263. struct dirent* const de = readdir (dir);
  264. if (de == 0)
  265. break;
  266. if (fnmatch (wildcardUTF8, de->d_name, FNM_CASEFOLD) == 0)
  267. {
  268. result = String::fromUTF8 ((const uint8*) de->d_name);
  269. const String path (parentDir + result);
  270. if (isDir != 0 || fileSize != 0)
  271. {
  272. struct stat info;
  273. const bool statOk = (stat (path.toUTF8(), &info) == 0);
  274. if (isDir != 0)
  275. *isDir = path.isEmpty() || (statOk && ((info.st_mode & S_IFDIR) != 0));
  276. if (isHidden != 0)
  277. *isHidden = (de->d_name[0] == '.');
  278. if (fileSize != 0)
  279. *fileSize = statOk ? info.st_size : 0;
  280. }
  281. if (modTime != 0 || creationTime != 0)
  282. {
  283. int64 m, a, c;
  284. juce_getFileTimes (path, m, a, c);
  285. if (modTime != 0)
  286. *modTime = m;
  287. if (creationTime != 0)
  288. *creationTime = c;
  289. }
  290. if (isReadOnly != 0)
  291. *isReadOnly = ! juce_canWriteToFile (path);
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. };
  298. // returns 0 on failure
  299. void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
  300. bool* isDir, bool* isHidden, int64* fileSize, Time* modTime,
  301. Time* creationTime, bool* isReadOnly) throw()
  302. {
  303. DIR* d = opendir (directory.toUTF8());
  304. if (d != 0)
  305. {
  306. FindFileStruct* ff = new FindFileStruct();
  307. ff->parentDir = directory;
  308. if (!ff->parentDir.endsWithChar (File::separator))
  309. ff->parentDir += File::separator;
  310. ff->wildCard = wildCard;
  311. if (wildCard == T("*.*"))
  312. ff->wildCard = T("*");
  313. ff->dir = d;
  314. if (ff->getNextMatch (firstResultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly))
  315. {
  316. return ff;
  317. }
  318. else
  319. {
  320. firstResultFile = String::empty;
  321. isDir = false;
  322. isHidden = false;
  323. closedir (d);
  324. delete ff;
  325. }
  326. }
  327. return 0;
  328. }
  329. bool juce_findFileNext (void* handle, String& resultFile,
  330. bool* isDir, bool* isHidden, int64* fileSize, Time* modTime, Time* creationTime, bool* isReadOnly) throw()
  331. {
  332. FindFileStruct* const ff = (FindFileStruct*) handle;
  333. if (ff != 0)
  334. return ff->getNextMatch (resultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly);
  335. return false;
  336. }
  337. void juce_findFileClose (void* handle) throw()
  338. {
  339. FindFileStruct* const ff = (FindFileStruct*) handle;
  340. if (ff != 0)
  341. {
  342. closedir (ff->dir);
  343. delete ff;
  344. }
  345. }
  346. bool juce_launchFile (const String& fileName,
  347. const String& parameters) throw()
  348. {
  349. String cmdString (fileName);
  350. cmdString << " " << parameters;
  351. if (URL::isProbablyAWebsiteURL (fileName)
  352. || URL::isProbablyAnEmailAddress (fileName))
  353. {
  354. // create a command that tries to launch a bunch of likely browsers
  355. const char* const browserNames[] = { "/etc/alternatives/x-www-browser", "firefox", "mozilla", "konqueror", "opera" };
  356. StringArray cmdLines;
  357. for (int i = 0; i < numElementsInArray (browserNames); ++i)
  358. cmdLines.add (String (browserNames[i]) + T(" ") + cmdString.trim().quoted());
  359. cmdString = cmdLines.joinIntoString (T(" || "));
  360. }
  361. if (cmdString.startsWithIgnoreCase (T("file:")))
  362. cmdString = cmdString.substring (5);
  363. const char* const argv[4] = { "/bin/sh", "-c", (const char*) cmdString.toUTF8(), 0 };
  364. const int cpid = fork();
  365. if (cpid == 0)
  366. {
  367. setsid();
  368. // Child process
  369. execve (argv[0], (char**) argv, environ);
  370. exit (0);
  371. }
  372. return cpid >= 0;
  373. }
  374. END_JUCE_NAMESPACE