Throw on failure in gmx_file_rename
[alexxy/gromacs.git] / src / gromacs / utility / futil.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2018,2019,2021, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \file
38  * \brief
39  * Low-level wrappers for OS-specific file handling with some \Gromacs
40  * customizations.
41  *
42  * \inpublicapi
43  * \ingroup module_utility
44  */
45 #ifndef GMX_UTILITY_FUTIL_H
46 #define GMX_UTILITY_FUTIL_H
47
48 #include <climits>
49 #include <cstdio>
50
51 #include <string>
52
53 #include "gromacs/utility/basedefinitions.h"
54 #include "gromacs/utility/fileptr.h"
55
56 /*! \def GMX_PATH_MAX
57  * \brief
58  * Maximum path length, if the OS provides one, otherwise a fixed constant.
59  */
60 #ifdef PATH_MAX
61 #    define GMX_PATH_MAX PATH_MAX
62 #elif defined MAX_PATH
63 #    define GMX_PATH_MAX MAX_PATH
64 #else
65 #    define GMX_PATH_MAX 4096
66 #endif
67
68 /** \Gromacs definition to use instead of `off_t`. */
69 typedef int64_t gmx_off_t;
70
71 /*! \brief
72  * Turn off buffering for output files (which is default) for debugging
73  * purposes.
74  *
75  * This only has effect on files opened with gmx_ffopen().
76  */
77 void gmx_disable_file_buffering();
78
79 /*! \brief
80  * Enables backups with the specified number of maximum backups.
81  *
82  * If \p count == 0, disables backups.  If not called, this is the default.
83  * If \p count == -1, reads the count from an environment variable.
84  *
85  * This is not currently thread-safe, as it is only called during
86  * initialization code.
87  */
88 void gmx_set_max_backup_count(int count);
89
90 /*! \brief
91  * Check whether a path exists.
92  *
93  * \returns `TRUE` when \p fname exists.
94  *
95  * Note that this returns `TRUE` even if \p fname is a directory instead of a
96  * file.
97  */
98 gmx_bool gmx_fexist(const std::string& fname);
99
100 /*! \brief
101  * Makes a backup of file if the file exists.
102  */
103 void make_backup(const std::string& file);
104
105 /*! \brief
106  * Opens a file, with \Gromacs-specific additions.
107  *
108  * If the file is in compressed format, opens a pipe which uncompresses the
109  * file on the fly.  For this to work, gmx_ffclose() and frewind() should
110  * always be used for files opened with gmx_ffopen() instead of fclose() and
111  * rewind().  For compressed files, the \p file parameter should be passed
112  * without the compressed extension; if that file is not found, then a few
113  * compression extensions are tried.
114  * Creates a backup if a file opened for writing already exists before
115  * overwriting it.
116  * A fatal error results if the file cannot be opened, for whatever reason.
117  */
118 FILE* gmx_ffopen(const std::string& file, const char* mode);
119
120 /** Closes a file opened with gmx_ffopen(). */
121 int gmx_ffclose(FILE* fp);
122
123 /*! \brief
124  * Wraps rewind() for files opened with gmx_ffopen().
125  *
126  * A fatal error results if this function is called for a pipe (a compressed
127  * input file).
128  */
129 void frewind(FILE* fp);
130
131 /** OS-independent 64-bit fseek().
132  *
133  * \return 0 when successful, or -1 (and set errno) in case of error.
134  */
135 int gmx_fseek(FILE* stream, gmx_off_t offset, int whence);
136
137 /** OS-independent 64-bit ftell().
138  *
139  * \return The current offset when successful, or -1 (and set errno) in case of error.
140  */
141 gmx_off_t gmx_ftell(FILE* stream);
142
143 /** OS-independent truncate(). */
144 int gmx_truncate(const std::string& filename, gmx_off_t length);
145
146 namespace gmx
147 {
148
149 /*! \brief
150  * Finds full path for a library file.
151  *
152  * Searches in the configured library directories for \c filename. If
153  * \c bAddCWD is true, searches first in the current directory. Fatal
154  * error results if the file is not found in any location and \c
155  * bFatal is true.
156  */
157 std::string findLibraryFile(const std::string& filename, bool bAddCWD = true, bool bFatal = true);
158 //! \copydoc findLibraryFile(const std::string &, bool, bool)
159 std::string findLibraryFile(const char* filename, bool bAddCWD = true, bool bFatal = true);
160
161 /*! \brief
162  * Opens a library file for reading in an RAII-style `FILE` handle.
163  *
164  * Works as findLibraryFile(), except that it opens the file and
165  * returns a file handle.
166  */
167 FilePtr openLibraryFile(const std::string& filename, bool bAddCWD = true, bool bFatal = true);
168 //! \copydoc openLibraryFile(const std::string &, bool, bool)
169 FilePtr openLibraryFile(const char* filename, bool bAddCWD = true, bool bFatal = true);
170
171 } // namespace gmx
172
173 /*! \brief
174  * Creates unique name for temp file (wrapper around mkstemp) and opens it.
175  *
176  * \p buf should be at least 7 bytes long
177  */
178 FILE* gmx_fopen_temporary(char* buf);
179
180 /*! \brief
181  * Creates unique name for temp file (wrapper around mkstemp).
182  *
183  * \p buf should be at least 7 bytes long
184  */
185 void gmx_tmpnam(char* buf);
186
187 /*! \brief
188  * OS-independent rename().
189  *
190  * Renames/moves a file atomically, if the OS makes that available.
191  */
192 void gmx_file_rename(const char* oldname, const char* newname);
193
194 /*! \brief
195  * Copies a file (data only) oldname to newname.
196  *
197  * If \p copy_if_empty is `FALSE`, the file won't be copied if it's empty.
198  */
199 int gmx_file_copy(const char* oldname, const char* newname, gmx_bool copy_if_empty);
200
201 /*! \brief
202  * OS-independent fsync().
203  *
204  * Only use this during checkpointing!
205  */
206 int gmx_fsync(FILE* fp);
207
208 /*! \brief
209  * OS-independent chdir().
210  *
211  * Exits with a fatal error if changing the directory fails.
212  */
213 void gmx_chdir(const char* directory);
214 /*! \brief
215  * OS-independent getcwd().
216  *
217  * Exits with a fatal error if the call fails.
218  */
219 void gmx_getcwd(char* buffer, size_t size);
220
221 namespace gmx
222 {
223
224 class DataFileFinder;
225
226 /*! \brief
227  * Gets a finder for locating data files from share/top/.
228  *
229  * \returns Finder set with setLibraryFileFinder(), or a default finder.
230  *
231  * If setLibraryFileFinder() has not been called (or a `NULL` finder has been
232  * set), a default finder is returned.
233  * The default finder searches data files from the directory identified by the
234  * global program context; it does not respect GMXLIB environment variable.
235  * Calling initForCommandLine() sets a finder that respects GMXLIB.
236  *
237  * Does not throw.
238  *
239  * See setLibraryFileFinder() for thread safety.
240  *
241  * \ingroup module_utility
242  */
243 const DataFileFinder& getLibraryFileFinder();
244 /*! \brief
245  * Sets a finder for location data files from share/top/.
246  *
247  * \param[in] finder  finder to set
248  *     (can be NULL to restore the default finder).
249  *
250  * The library does not take ownership of \p finder.
251  * The provided object must remain valid until the global instance is changed
252  * by another call to setLibraryFileFinder().
253  *
254  * The global instance is used by findLibraryFile() and openLibraryFile().
255  *
256  * This method is not thread-safe.  See setProgramContext(); the same
257  * constraints apply here as well.
258  *
259  * Does not throw.
260  */
261 void setLibraryFileFinder(const DataFileFinder* finder);
262
263 } // namespace gmx
264
265 #endif