JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Files_linux.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26enum
27{
28 U_ISOFS_SUPER_MAGIC = 0x9660, // linux/iso_fs.h
29 U_MSDOS_SUPER_MAGIC = 0x4d44, // linux/msdos_fs.h
30 U_NFS_SUPER_MAGIC = 0x6969, // linux/nfs_fs.h
31 U_SMB_SUPER_MAGIC = 0x517B // linux/smb_fs.h
32};
33
35{
36 struct statfs buf;
37
38 return statfs (getFullPathName().toUTF8(), &buf) == 0
39 && buf.f_type == (unsigned int) U_ISOFS_SUPER_MAGIC;
40}
41
43{
44 struct statfs buf;
45
46 if (statfs (getFullPathName().toUTF8(), &buf) == 0)
47 {
48 switch (buf.f_type)
49 {
50 case U_ISOFS_SUPER_MAGIC: // CD-ROM
51 case U_MSDOS_SUPER_MAGIC: // Probably floppy (but could be mounted FAT filesystem)
52 case U_NFS_SUPER_MAGIC: // Network NFS
53 case U_SMB_SUPER_MAGIC: // Network Samba
54 return false;
55
56 default: break;
57 }
58 }
59
60 // Assume so if this fails for some reason
61 return true;
62}
63
65{
66 jassertfalse; // xxx not implemented for linux!
67 return false;
68}
69
71{
72 return {}; // xxx not yet implemented
73}
74
75//==============================================================================
76static File resolveXDGFolder (const char* const type, const char* const fallbackFolder)
77{
79 File ("~/.config/user-dirs.dirs").readLines (confLines);
80
81 for (int i = 0; i < confLines.size(); ++i)
82 {
83 const String line (confLines[i].trimStart());
84
85 if (line.startsWith (type))
86 {
87 // eg. resolve XDG_MUSIC_DIR="$HOME/Music" to /home/user/Music
88 const File f (line.replace ("$HOME", File ("~").getFullPathName())
89 .fromFirstOccurrenceOf ("=", false, false)
90 .trim().unquoted());
91
92 if (f.isDirectory())
93 return f;
94 }
95 }
96
97 return File (fallbackFolder);
98}
99
100const char* const* juce_argv = nullptr;
101int juce_argc = 0;
102
104{
105 switch (type)
106 {
108 {
109 if (const char* homeDir = getenv ("HOME"))
110 return File (CharPointer_UTF8 (homeDir));
111
112 if (auto* pw = getpwuid (getuid()))
113 return File (CharPointer_UTF8 (pw->pw_dir));
114
115 return {};
116 }
117
118 case userDocumentsDirectory: return resolveXDGFolder ("XDG_DOCUMENTS_DIR", "~/Documents");
119 case userMusicDirectory: return resolveXDGFolder ("XDG_MUSIC_DIR", "~/Music");
120 case userMoviesDirectory: return resolveXDGFolder ("XDG_VIDEOS_DIR", "~/Videos");
121 case userPicturesDirectory: return resolveXDGFolder ("XDG_PICTURES_DIR", "~/Pictures");
122 case userDesktopDirectory: return resolveXDGFolder ("XDG_DESKTOP_DIR", "~/Desktop");
123 case userApplicationDataDirectory: return resolveXDGFolder ("XDG_CONFIG_HOME", "~/.config");
125 case commonApplicationDataDirectory: return File ("/opt");
126 case globalApplicationsDirectory: return File ("/usr");
127
128 case tempDirectory:
129 {
130 if (const char* tmpDir = getenv ("TMPDIR"))
131 return File (CharPointer_UTF8 (tmpDir));
132
133 return File ("/tmp");
134 }
135
137 if (juce_argv != nullptr && juce_argc > 0)
138 return File (String (CharPointer_UTF8 (juce_argv[0])));
139 // Falls through
141
144 {
145 const auto f = juce_getExecutableFile();
146 return f.isSymbolicLink() ? f.getLinkedTarget() : f;
147 }
148
150 {
151 #if JUCE_BSD
152 return juce_getExecutableFile();
153 #else
154 const File f ("/proc/self/exe");
155 return f.isSymbolicLink() ? f.getLinkedTarget() : juce_getExecutableFile();
156 #endif
157 }
158
159 default:
160 jassertfalse; // unknown type?
161 break;
162 }
163
164 return {};
165}
166
167//==============================================================================
169{
170 if (! exists())
171 return true;
172
173 File trashCan ("~/.Trash");
174
175 if (! trashCan.isDirectory())
176 trashCan = "~/.local/share/Trash/files";
177
178 if (! trashCan.isDirectory())
179 return false;
180
181 return moveFileTo (trashCan.getNonexistentChildFile (getFileNameWithoutExtension(),
183}
184
185//==============================================================================
186static bool isFileExecutable (const String& filename)
187{
188 juce_statStruct info;
189
190 return juce_stat (filename, info)
191 && S_ISREG (info.st_mode)
192 && access (filename.toUTF8(), X_OK) == 0;
193}
194
195bool Process::openDocument (const String& fileName, const String& parameters)
196{
197 const auto cmdString = [&]
198 {
199 if (fileName.startsWithIgnoreCase ("file:")
201 || ! isFileExecutable (fileName))
202 {
203 const auto singleCommand = fileName.trim().quoted();
204
206
207 for (auto browserName : { "xdg-open", "/etc/alternatives/x-www-browser", "firefox", "mozilla",
208 "google-chrome", "chromium-browser", "opera", "konqueror" })
209 {
211 }
212
213 return cmdLines.joinIntoString (" || ");
214 }
215
216 return (fileName.replace (" ", "\\ ", false) + " " + parameters).trim();
217 }();
218
219 const char* const argv[] = { "/bin/sh", "-c", cmdString.toUTF8(), nullptr };
220
221 const auto cpid = fork();
222
223 if (cpid == 0)
224 {
225 setsid();
226
227 // Child process
228 execv (argv[0], (char**) argv);
229 exit (0);
230 }
231
232 return cpid >= 0;
233}
234
236{
237 if (isDirectory())
239 else if (getParentDirectory().exists())
241}
242
243} // namespace juce
access
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
Represents a local file or directory.
Definition juce_File.h:45
bool isSymbolicLink() const
Returns true if this file is a link or alias that can be followed using getLinkedTarget().
bool moveFileTo(const File &targetLocation) const
Moves or renames a file.
bool isOnHardDisk() const
Returns true if this file is on a hard disk.
bool isDirectory() const
Checks whether the file is a directory that exists.
bool isOnCDRomDrive() const
Returns true if this file is on a CD or DVD drive.
bool isOnRemovableDrive() const
Returns true if this file is on a removable disk drive.
String getFileExtension() const
Returns the file's extension.
const String & getFullPathName() const noexcept
Returns the complete, absolute path of this file.
Definition juce_File.h:153
void readLines(StringArray &destLines) const
Reads the contents of this file as text and splits it into lines, which are appended to the given Str...
void revealToUser() const
Opens Finder, Explorer, or whatever the OS uses, to show the user this file's location.
String getFileNameWithoutExtension() const
Returns the last part of the filename, without its file extension.
SpecialLocationType
A set of types of location that can be passed to the getSpecialLocation() method.
Definition juce_File.h:867
@ userMoviesDirectory
The most likely place where a user might store their movie files.
Definition juce_File.h:884
@ userMusicDirectory
The most likely place where a user might store their music files.
Definition juce_File.h:881
@ tempDirectory
The folder that should be used for temporary files.
Definition juce_File.h:919
@ globalApplicationsDirectory
The directory in which applications normally get installed.
Definition juce_File.h:963
@ userDocumentsDirectory
The user's default documents folder.
Definition juce_File.h:875
@ currentApplicationFile
Returns this application's location.
Definition juce_File.h:942
@ invokedExecutableFile
Returns the file that was invoked to launch this executable.
Definition juce_File.h:949
@ commonDocumentsDirectory
A place to put documents which are shared by all users of the machine.
Definition juce_File.h:914
@ userApplicationDataDirectory
The folder in which applications store their persistent user-specific settings.
Definition juce_File.h:895
@ userPicturesDirectory
The most likely place where a user might store their picture files.
Definition juce_File.h:887
@ commonApplicationDataDirectory
An equivalent of the userApplicationDataDirectory folder that is shared by all users of the computer,...
Definition juce_File.h:907
@ userDesktopDirectory
The folder that contains the user's desktop objects.
Definition juce_File.h:878
@ hostApplicationPath
In a plugin, this will return the path of the host executable.
Definition juce_File.h:952
@ currentExecutableFile
Returns this application's executable file.
Definition juce_File.h:932
@ userHomeDirectory
The user's home folder.
Definition juce_File.h:869
static File JUCE_CALLTYPE getSpecialLocation(const SpecialLocationType type)
Finds the location of a special type of file or directory, such as a home folder or documents folder.
File getLinkedTarget() const
If this file is a link or alias, this returns the file that it points to.
File getParentDirectory() const
Returns the directory that contains this file or directory.
bool moveToTrash() const
Moves this file or folder to the trash.
File()=default
Creates an (invalid) file object.
String getVersion() const
If possible, this will try to create a version string for the given file.
static File createFileWithoutCheckingPath(const String &absolutePath) noexcept
Creates a file that simply contains this string, without doing the sanity-checking that the normal co...
Definition juce_File.cpp:31
bool startAsProcess(const String &parameters=String()) const
Launches the file as a process.
bool exists() const
Checks whether the file actually exists.
static bool JUCE_CALLTYPE openDocument(const String &documentURL, const String &parameters)
Tries to launch the OS's default reader application for a given file or URL.
A special array for holding a list of strings.
void add(String stringToAdd)
Appends a string at the end of the array.
The JUCE String class!
Definition juce_String.h:53
CharPointer_UTF8 toUTF8() const
Returns a pointer to a UTF-8 version of this string.
execv
exit
fork
getenv
getpwuid
getuid
#define jassertfalse
This will always cause an assertion failure.
#define JUCE_FALLTHROUGH
Used to silence Wimplicit-fallthrough on Clang and GCC where available as there are a few places in t...
typedef int
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
setsid