Update instructions in containers.rst
[gromacs.git] / src / gromacs / utility / path.cpp
blob1b0f4645af6ec9f72660429a5aba23909fbbcf51
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \internal \file
37 * \brief
38 * Implements functions in path.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #include "gmxpre.h"
45 #include "path.h"
47 #include "config.h"
49 #include <cctype>
50 #include <cerrno>
51 #include <cstddef>
52 #include <cstdio>
53 #include <cstdlib>
54 #include <cstring>
56 #include <algorithm>
57 #include <string>
58 #include <string_view>
59 #include <utility>
61 #include <sys/stat.h>
63 #if GMX_NATIVE_WINDOWS
64 # include <Windows.h>
65 # include <direct.h>
66 #else
67 # ifdef HAVE_UNISTD_H
68 # include <unistd.h>
69 # endif
70 #endif
72 #include "gromacs/utility/dir_separator.h"
73 #include "gromacs/utility/exceptions.h"
74 #include "gromacs/utility/futil.h"
75 #include "gromacs/utility/stringutil.h"
77 namespace
80 //! Directory separator to use when joining paths.
81 const char cDirSeparator = '/';
82 //! Directory separators to use when parsing paths.
83 const char cDirSeparators[] = "/\\";
84 /*! \var cPathSeparator
85 * \brief
86 * Separator to use to split the PATH environment variable.
88 * When reading the PATH environment variable, Unix separates entries
89 * with colon, while windows uses semicolon.
91 #if GMX_NATIVE_WINDOWS
92 const char cPathSeparator = ';';
93 #else
94 const char cPathSeparator = ':';
95 #endif
97 //! Check whether a given character is a directory separator.
98 bool isDirSeparator(char chr)
100 return std::strchr(cDirSeparators, chr) != nullptr;
103 } // namespace
105 namespace gmx
108 /********************************************************************
109 * Path
112 bool Path::containsDirectory(const std::string& path)
114 return path.find_first_of(cDirSeparators) != std::string::npos;
117 /* Check if the program name begins with "/" on unix/cygwin, or
118 * with "\" or "X:\" on windows. If not, the program name
119 * is relative to the current directory.
121 bool Path::isAbsolute(const char* path)
123 #if GMX_NATIVE_WINDOWS
124 return path[0] != '\0' && path[1] == ':' && isDirSeparator(path[2]);
125 #else
126 return isDirSeparator(path[0]);
127 #endif
130 bool Path::isAbsolute(const std::string& path)
132 return isAbsolute(path.c_str());
135 #if GMX_NATIVE_WINDOWS
136 namespace
138 struct handle_wrapper
140 HANDLE handle;
141 handle_wrapper(HANDLE h) : handle(h) {}
142 ~handle_wrapper()
144 if (handle != INVALID_HANDLE_VALUE)
146 ::CloseHandle(handle);
150 } // namespace
151 #endif
153 bool Path::isEquivalent(const std::string& path1, const std::string& path2)
155 // based on boost_1_56_0/libs/filesystem/src/operations.cpp under BSL
156 #if GMX_NATIVE_WINDOWS
157 // Note well: Physical location on external media is part of the
158 // equivalence criteria. If there are no open handles, physical location
159 // can change due to defragmentation or other relocations. Thus handles
160 // must be held open until location information for both paths has
161 // been retrieved.
163 // p2 is done first, so any error reported is for p1
164 // FixME: #1635
165 handle_wrapper h2(CreateFile(path2.c_str(), 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
166 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
168 handle_wrapper h1(CreateFile(path1.c_str(), 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
169 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
171 if (h1.handle == INVALID_HANDLE_VALUE || h2.handle == INVALID_HANDLE_VALUE)
173 // if one is invalid and the other isn't, then they aren't equivalent,
174 // but if both are invalid then it is an error
175 if (h1.handle == INVALID_HANDLE_VALUE && h2.handle == INVALID_HANDLE_VALUE)
177 GMX_THROW(FileIOError("Path::isEquivalent called with two invalid files"));
180 return false;
183 // at this point, both handles are known to be valid
185 BY_HANDLE_FILE_INFORMATION info1, info2;
187 if (!GetFileInformationByHandle(h1.handle, &info1))
189 GMX_THROW(FileIOError("Path::isEquivalent: GetFileInformationByHandle failed"));
192 if (!GetFileInformationByHandle(h2.handle, &info2))
194 GMX_THROW(FileIOError("Path::isEquivalent: GetFileInformationByHandle failed"));
197 // In theory, volume serial numbers are sufficient to distinguish between
198 // devices, but in practice VSN's are sometimes duplicated, so last write
199 // time and file size are also checked.
200 return info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
201 && info1.nFileIndexHigh == info2.nFileIndexHigh && info1.nFileIndexLow == info2.nFileIndexLow
202 && info1.nFileSizeHigh == info2.nFileSizeHigh && info1.nFileSizeLow == info2.nFileSizeLow
203 && info1.ftLastWriteTime.dwLowDateTime == info2.ftLastWriteTime.dwLowDateTime
204 && info1.ftLastWriteTime.dwHighDateTime == info2.ftLastWriteTime.dwHighDateTime;
205 #else
206 struct stat s1, s2;
207 int e2 = stat(path2.c_str(), &s2);
208 int e1 = stat(path1.c_str(), &s1);
210 if (e1 != 0 || e2 != 0)
212 // if one is invalid and the other isn't then they aren't equivalent,
213 // but if both are invalid then it is an error.
214 if (e1 != 0 && e2 != 0)
216 GMX_THROW_WITH_ERRNO(FileIOError("Path::isEquivalent called with two invalid files"),
217 "stat", errno);
219 return false;
222 // both stats now known to be valid
223 return s1.st_dev == s2.st_dev
224 && s1.st_ino == s2.st_ino
225 // According to the POSIX stat specs, "The st_ino and st_dev fields
226 // taken together uniquely identify the file within the system."
227 // Just to be sure, size and mod time are also checked.
228 && s1.st_size == s2.st_size && s1.st_mtime == s2.st_mtime;
229 #endif
232 std::string Path::join(const std::string& path1, const std::string& path2)
234 // TODO: Remove extra separators if they are present in the input paths.
235 return path1 + cDirSeparator + path2;
239 std::string Path::join(const std::string& path1, const std::string& path2, const std::string& path3)
241 // TODO: Remove extra separators if they are present in the input paths.
242 return path1 + cDirSeparator + path2 + cDirSeparator + path3;
245 namespace
248 /*! \brief Returns a view of the parent path (ie. directory
249 * components) of \c input ie. up to but excluding the last directory
250 * separator (if one exists).
252 * \returns A view of the parent-path components, or empty if no
253 * directory separator exists. */
254 std::string_view getParentPathView(const std::string& input)
256 auto inputView = std::string_view(input);
257 size_t pos = inputView.find_last_of(cDirSeparators);
258 if (pos == std::string::npos)
260 return std::string_view();
262 return inputView.substr(0, pos);
265 /*! \brief Returns a view of the filename \c in input ie. after the
266 * last directory separator (if one exists).
268 * \returns A view of the filename component. */
269 std::string_view getFilenameView(const std::string_view input)
271 size_t pos = input.find_last_of(cDirSeparators);
272 if (pos == std::string::npos)
274 return input;
276 return input.substr(pos + 1);
279 /*! \brief Returns a view of the stem of the filename in \c input.
281 * The search for the extension separator takes place only within the
282 * filename component, ie. omitting any leading directories.
284 * \returns The view of the filename stem, or empty if none exists. */
285 std::string_view getStemView(const std::string& input)
287 auto filenameView = getFilenameView(input);
288 size_t extensionSeparatorPosition = filenameView.find_last_of('.');
289 // If no separator is found, the returned view is of the whole filename.
290 return filenameView.substr(0, extensionSeparatorPosition);
293 /*! \brief Returns a view of the file extension of \c input, including the dot.
295 * The search for the extension separator takes place only within the
296 * filename component, ie. omitting any leading directories.
298 * \returns The view of the file extension, or empty if none exists. */
299 std::string_view getExtensionView(const std::string_view input)
301 auto filenameView = getFilenameView(input);
302 size_t extensionSeparatorPosition = filenameView.find_last_of('.');
303 if (extensionSeparatorPosition == std::string_view::npos)
305 // No separator was found
306 return std::string_view();
308 return filenameView.substr(extensionSeparatorPosition);
311 } // namespace
313 std::string Path::getParentPath(const std::string& input)
315 return std::string(getParentPathView(input));
318 std::string Path::getFilename(const std::string& input)
320 return std::string(getFilenameView(input));
323 bool Path::hasExtension(const std::string& input)
325 // This could be implemented with getStemView, but that search is
326 // less efficient than just finding the first of possibly multiple
327 // separator characters.
328 return getFilenameView(input).find('.') != std::string::npos;
331 bool Path::extensionMatches(const std::string_view input, const std::string_view extension)
333 auto extensionWithSeparator = getExtensionView(input);
334 return (!extensionWithSeparator.empty() && extensionWithSeparator.substr(1) == extension);
337 std::string Path::stripExtension(const std::string& input)
339 auto pathView = getParentPathView(input);
340 // Make sure the returned string will have room for the directory
341 // separator between the parent path and the stem, but only where
342 // it is needed.
343 size_t pathLength = pathView.empty() ? 0 : pathView.length() + 1;
344 auto stemView = getStemView(input);
345 return std::string(std::begin(input), std::begin(input) + pathLength + stemView.length());
348 std::string Path::concatenateBeforeExtension(const std::string& input, const std::string& stringToAdd)
350 std::string output = stripExtension(input);
351 output += stringToAdd;
352 auto extensionView = getExtensionView(input);
353 output.append(std::begin(extensionView), std::end(extensionView));
354 return output;
357 std::string Path::normalize(const std::string& path)
359 std::string result(path);
360 #if DIR_SEPARATOR != '/'
361 std::replace(result.begin(), result.end(), '/', DIR_SEPARATOR);
362 #endif
363 return result;
366 const char* Path::stripSourcePrefix(const char* path)
368 const char* fallback = path;
369 const char* sep = path + std::strlen(path);
370 bool gromacsSubdirFound = false;
371 while (sep > path)
373 const char* prevSep = sep - 1;
374 while (prevSep >= path && !isDirSeparator(*prevSep))
376 --prevSep;
378 const std::ptrdiff_t length = sep - prevSep - 1;
379 if (gromacsSubdirFound)
381 if (std::strncmp(prevSep + 1, "src", length) == 0)
383 return prevSep + 1;
385 return fallback;
387 if (std::strncmp(prevSep + 1, "gromacs", length) == 0
388 || std::strncmp(prevSep + 1, "programs", length) == 0
389 || std::strncmp(prevSep + 1, "testutils", length) == 0)
391 gromacsSubdirFound = true;
393 if (fallback == path)
395 fallback = prevSep + 1;
397 sep = prevSep;
399 return fallback;
402 bool Path::exists(const char* path)
404 return gmx_fexist(path);
407 bool Path::exists(const std::string& path)
409 return exists(path.c_str());
412 std::string Path::getWorkingDirectory()
414 // TODO: Use exceptions instead of gmx_fatal().
415 char cwd[GMX_PATH_MAX];
416 gmx_getcwd(cwd, sizeof(cwd));
417 return cwd;
420 void Path::splitPathEnvironment(const std::string& pathEnv, std::vector<std::string>* result)
422 size_t prevPos = 0;
423 size_t separator;
426 separator = pathEnv.find(cPathSeparator, prevPos);
427 result->push_back(pathEnv.substr(prevPos, separator - prevPos));
428 prevPos = separator + 1;
429 } while (separator != std::string::npos);
432 std::vector<std::string> Path::getExecutablePaths()
434 std::vector<std::string> result;
435 #if GMX_NATIVE_WINDOWS
436 // Add the local dir since it is not in the path on Windows.
437 result.push_back("");
438 #endif
439 const char* path = std::getenv("PATH");
440 if (path != nullptr)
442 splitPathEnvironment(path, &result);
444 return result;
447 std::string Path::resolveSymlinks(const std::string& path)
449 /* Does not fully resolve the path like realpath/boost::canonical would.
450 * It doesn't resolve path elements (including "." or ".."), but only
451 * resolves the entire path (it does that recursively). */
452 std::string result(path);
453 #if !GMX_NATIVE_WINDOWS
454 char buf[GMX_PATH_MAX];
455 int length;
456 while ((length = readlink(result.c_str(), buf, sizeof(buf) - 1)) > 0)
458 buf[length] = '\0';
459 if (isAbsolute(buf))
461 result = buf;
463 else
465 result = join(getParentPath(result), buf);
468 #endif
469 return result;
472 /********************************************************************
473 * File
476 void File::returnFalseOnError(const NotFoundInfo& /*info*/) {}
478 void File::throwOnError(const NotFoundInfo& info)
480 if (info.wasError)
482 const std::string message =
483 formatString("Failed to access file '%s'.\n%s", info.filename, info.message);
484 GMX_THROW_WITH_ERRNO(FileIOError(message), info.call, info.err);
488 void File::throwOnNotFound(const NotFoundInfo& info)
490 throwOnError(info);
491 const std::string message = formatString("File '%s' does not exist or is not accessible.\n%s",
492 info.filename, info.message);
493 GMX_THROW_WITH_ERRNO(InvalidInputError(message), info.call, info.err);
496 // static
497 bool File::exists(const char* filename, NotFoundHandler onNotFound)
499 if (filename == nullptr)
501 return false;
503 FILE* test = std::fopen(filename, "r");
504 if (test == nullptr)
506 const bool wasError = (errno != ENOENT && errno != ENOTDIR);
507 NotFoundInfo info(filename, "The file could not be opened.", "fopen", wasError, errno);
508 onNotFound(info);
509 return false;
511 else
513 std::fclose(test);
514 // Windows doesn't allow fopen of directory, so we don't need to check
515 // this separately.
516 #if !GMX_NATIVE_WINDOWS
517 struct stat st_buf;
518 int status = stat(filename, &st_buf);
519 if (status != 0)
521 NotFoundInfo info(filename, "File information could not be read.", "stat", true, errno);
522 onNotFound(info);
523 return false;
525 if (!S_ISREG(st_buf.st_mode))
527 NotFoundInfo info(filename, "The file is not a regular file.", nullptr, true, 0);
528 onNotFound(info);
529 return false;
531 #endif
532 return true;
536 // static
537 bool File::exists(const std::string& filename, NotFoundHandler onNotFound)
539 return exists(filename.c_str(), onNotFound);
542 /********************************************************************
543 * Directory
546 int Directory::create(const char* path)
548 if (Directory::exists(path))
550 return 0;
552 #if GMX_NATIVE_WINDOWS
553 if (_mkdir(path))
554 #else
555 if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH) != 0)
556 #endif
558 // TODO: Proper error handling.
559 return -1;
561 return 0;
565 int Directory::create(const std::string& path)
567 return create(path.c_str());
571 bool Directory::exists(const char* path)
573 struct stat info;
574 if (stat(path, &info) != 0)
576 if (errno != ENOENT && errno != ENOTDIR)
578 // TODO: Proper error handling.
580 return false;
582 #if GMX_NATIVE_WINDOWS
583 return ((_S_IFDIR & info.st_mode) != 0);
584 #else
585 return S_ISDIR(info.st_mode);
586 #endif
590 bool Directory::exists(const std::string& path)
592 return exists(path.c_str());
595 } // namespace gmx