etcFiles.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "etcFiles.H"
27 #include "OSspecific.H"
28 #include "foamVersion.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
33 {
34  fileNameList dirs;
35 
36  // Search for user directories in
37  // * ~/.OpenFOAM/VERSION
38  // * ~/.OpenFOAM
39  //
40  fileName searchDir = home()/".OpenFOAM";
41  if (isDir(searchDir))
42  {
43  fileName fullName = searchDir/FOAMversion/local;
44  if (isDir(fullName))
45  {
46  dirs.append(fullName);
47  }
48 
49  fullName = searchDir/local;
50  if (isDir(fullName))
51  {
52  dirs.append(fullName);
53  }
54  }
55 
56  // Search for group (site) directories in
57  // * $WM_PROJECT_SITE/VERSION/etc/
58  // * $WM_PROJECT_SITE/etc/
59  //
60  searchDir = getEnv("WM_PROJECT_SITE");
61  if (searchDir.size())
62  {
63  if (isDir(searchDir))
64  {
65  fileName fullName = searchDir/FOAMversion/"etc"/local;
66  if (isDir(fullName))
67  {
68  dirs.append(fullName);
69  }
70 
71  fullName = searchDir/"etc"/local;
72  if (isDir(fullName))
73  {
74  dirs.append(fullName);
75  }
76  }
77  }
78  else
79  {
80  // Or search for group (site) files in
81  // * $WM_PROJECT_INST_DIR/site/VERSION/etc/
82  // * $WM_PROJECT_INST_DIR/site/etc/
83  //
84  searchDir = getEnv("WM_PROJECT_INST_DIR");
85  if (isDir(searchDir))
86  {
87  fileName fullName = searchDir/"site"/FOAMversion/"etc"/local;
88  if (isDir(fullName))
89  {
90  dirs.append(fullName);
91  }
92 
93  fullName = searchDir/"site/etc"/local;
94  if (isDir(fullName))
95  {
96  dirs.append(fullName);
97  }
98  }
99  }
100 
101  // Search for other (shipped) files in
102  // * $WM_PROJECT_DIR/etc
103  //
104  searchDir = getEnv("WM_PROJECT_DIR");
105  if (isDir(searchDir))
106  {
107  fileName fullName = searchDir/"etc"/local;
108  if (isDir(fullName))
109  {
110  dirs.append(fullName);
111  }
112  }
113 
114  return dirs;
115 }
116 
117 
119 (
120  const fileName& name,
121  bool mandatory,
122  bool findFirst
123 )
124 {
125  fileNameList results;
126 
127  // Search for user files in
128  // * ~/.OpenFOAM/VERSION
129  // * ~/.OpenFOAM
130  //
131  fileName searchDir = home()/".OpenFOAM";
132  if (isDir(searchDir))
133  {
134  fileName fullName = searchDir/FOAMversion/name;
135  if (isFile(fullName))
136  {
137  results.append(fullName);
138  if (findFirst)
139  {
140  return results;
141  }
142  }
143 
144  fullName = searchDir/name;
145  if (isFile(fullName))
146  {
147  results.append(fullName);
148  if (findFirst)
149  {
150  return results;
151  }
152  }
153  }
154 
155  // Search for group (site) files in
156  // * $WM_PROJECT_SITE/VERSION/etc/
157  // * $WM_PROJECT_SITE/etc/
158  //
159  searchDir = getEnv("WM_PROJECT_SITE");
160  if (searchDir.size())
161  {
162  if (isDir(searchDir))
163  {
164  fileName fullName = searchDir/FOAMversion/"etc"/name;
165  if (isFile(fullName))
166  {
167  results.append(fullName);
168  if (findFirst)
169  {
170  return results;
171  }
172  }
173 
174  fullName = searchDir/"etc"/name;
175  if (isFile(fullName))
176  {
177  results.append(fullName);
178  if (findFirst)
179  {
180  return results;
181  }
182  }
183  }
184  }
185  else
186  {
187  // Or search for group (site) files in
188  // * $WM_PROJECT_INST_DIR/site/VERSION/etc/
189  // * $WM_PROJECT_INST_DIR/site/etc/
190  //
191  searchDir = getEnv("WM_PROJECT_INST_DIR");
192  if (isDir(searchDir))
193  {
194  fileName fullName = searchDir/"site"/FOAMversion/"etc"/name;
195  if (isFile(fullName))
196  {
197  results.append(fullName);
198  if (findFirst)
199  {
200  return results;
201  }
202  }
203 
204  fullName = searchDir/"site/etc"/name;
205  if (isFile(fullName))
206  {
207  results.append(fullName);
208  if (findFirst)
209  {
210  return results;
211  }
212  }
213  }
214  }
215 
216  // Search for other (shipped) files in
217  // * $WM_PROJECT_DIR/etc
218  //
219  searchDir = getEnv("WM_PROJECT_DIR");
220  if (isDir(searchDir))
221  {
222  fileName fullName = searchDir/"etc"/name;
223  if (isFile(fullName))
224  {
225  results.append(fullName);
226  if (findFirst)
227  {
228  return results;
229  }
230  }
231  }
232 
233  // Not found
234  if (results.empty())
235  {
236  // Abort if the file is mandatory, otherwise return null
237  if (mandatory)
238  {
239  std::cerr
240  << "--> FOAM FATAL ERROR in Foam::findEtcFiles() :"
241  " could not find mandatory file\n '"
242  << name.c_str() << "'\n\n" << std::endl;
243  ::exit(1);
244  }
245  }
246 
247  // Return list of matching paths or empty list if none found
248  return results;
249 }
250 
251 
252 Foam::fileName Foam::findEtcFile(const fileName& name, bool mandatory)
253 {
254  fileNameList results(findEtcFiles(name, mandatory, true));
255 
256  if (results.size())
257  {
258  return results[0];
259  }
260  else
261  {
262  return fileName();
263  }
264 }
265 
266 
267 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
Definition: POSIX.C:555
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
fileName home()
Return home directory path name for the current user.
Definition: POSIX.C:186
const char *const FOAMversion
fileNameList findEtcFiles(const fileName &, bool mandatory=false, bool findFirst=false)
Search for files from user/group/shipped directories.
Definition: etcFiles.C:119
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
Functions to search &#39;etc&#39; directories for configuration files etc.
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: etcFiles.C:252
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
fileNameList findEtcDirs(const fileName &local=fileName::null)
Search for directories from user/group/shipped directories.
Definition: etcFiles.C:32