4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
15 #include "support/os.h"
16 #include "support/strfwd.h"
25 /// Defined in "FileNameList.h".
29 * Class for storing file names.
30 * The file name may be empty. If it is not empty it is an absolute path.
31 * The file may or may not exist.
35 /// Constructor for empty filenames
37 /** Constructor for nonempty filenames.
38 * explicit because we don't want implicit conversion of relative
39 * paths in function arguments (e.g. of unlink).
40 * \param abs_filename the file in question. Must have an absolute path.
41 * Encoding is always UTF-8.
43 explicit FileName(std::string const & abs_filename);
46 FileName(FileName const &);
49 FileName(FileName &&) noexcept;
51 /// constructor with base name and suffix
52 FileName(FileName const & fn, std::string const & suffix);
55 FileName & operator=(FileName const &);
58 FileName & operator=(FileName &&) noexcept;
61 /** Set a new filename.
62 * \param filename the file in question. Must have an absolute path.
63 * Encoding is always UTF-8.
65 void set(std::string const & filename);
66 void set(FileName const & fn, std::string const & suffix);
68 /// Is this filename empty?
70 /// Is the filename absolute?
71 static bool isAbsolute(std::string const & name);
73 /// get the absolute file name in UTF-8 encoding
74 std::string absFileName() const;
76 /** returns an absolute pathname (whose resolution does not involve
77 * '.', '..', or symbolic links) in UTF-8 encoding
79 std::string realPath() const;
82 * Get the file name in the encoding used by the file system.
83 * Only use this for passing file names to external commands.
84 * Warning: On Windows this is not unicode safe and should not
85 * be used for accessing files with an fstream, for example.
87 std::string toFilesystemEncoding() const;
90 * Get the file name in a unicode safe encoding used by the file system.
91 * Only use this for accessing the file with standard I/O functions
92 * non explicitly unicode aware, e.g. with an fstream. This can also
93 * be used for passing file names to external commands, but only if
94 * you are sure that the stem of the name will not be used for
95 * producing derivative files. For example, don't use this for passing
96 * file names to LaTeX, as the stem of the .dvi file will not correspond
97 * to the stem of the .tex file anymore.
98 * Use os::CREATE if the file is to be accessed for writing.
100 std::string toSafeFilesystemEncoding(os::file_access how = os::EXISTING) const;
102 /// returns true if the file exists
104 /// refreshes the file info
105 void refresh() const;
106 /// \return true if this object points to a symbolic link.
107 bool isSymLink() const;
108 /// \return true if the file is empty.
109 bool isFileEmpty() const;
110 /// returns time of last write access
111 std::time_t lastModified() const;
112 /// generates a checksum of a file
113 virtual unsigned long checksum() const;
114 /// return true when file is readable but not writable
115 bool isReadOnly() const;
116 /// return true when it names a directory
117 bool isDirectory() const;
118 /// return true when directory is readable
119 bool isReadableDirectory() const;
120 /// return true when it is a file and readable
121 virtual bool isReadableFile() const;
122 /// return true when file/directory is writable
123 bool isWritable() const;
124 /// return true when file/directory is writable (write test file)
125 bool isDirWritable() const;
126 /// \return list other files in the directory having optional extension 'ext'.
127 FileNameList dirList(std::string const & ext) const;
130 /// \return true when file/directory is writable (write test file)
131 /// \warning This methods has different semantics when system level
132 /// copy command, it will overwrite the \c target file if it exists,
133 /// If \p keepsymlink is true, the copy will be written to the symlink
134 /// target. Otherwise, the symlink will be destroyed.
135 bool copyTo(FileName const & target, bool keepsymlink = false) const;
137 /// remove pointed file.
138 /// \return true on success.
139 bool removeFile() const;
141 /// rename pointed file.
142 /// \return false if the operation fails or if the \param target file
144 /// \return true on success.
145 bool renameTo(FileName const & target) const;
147 /// move pointed file to \param target.
148 /// If \p target exists it will be overwritten (if it is a symlink,
149 /// the symlink will be destroyed).
150 /// \return true on success.
151 bool moveTo(FileName const & target) const;
152 bool link(FileName const & name) const;
154 /// change mode of pointed file.
155 /// This methods does nothing and return true on platforms that does not
157 /// \return true on success.
158 bool changePermission(unsigned long int mode) const;
159 // sets permission based on the file given as an argument
160 bool clonePermissions(FileName const & source);
162 /// remove pointed directory and all contents.
163 /// \return true on success.
164 bool destroyDirectory() const;
165 /// Creates pointed directory.
166 /// \return true on success.
167 bool createDirectory(int permissions) const;
168 /// Creates pointed path.
169 /// \return true on success.
170 bool createPath() const;
172 /// Get the contents of a file as a huge docstring.
173 /// \param encoding defines the encoding of the file contents.
174 /// Only four encodings are supported:
175 /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
176 /// current system locale.
177 docstring fileContents(std::string const & encoding) const;
179 /// Change extension.
181 * If oldname does not have an extension, it is appended.
182 * If the extension is empty, any extension is removed from the name.
184 void changeExtension(std::string const & extension);
186 static FileName fromFilesystemEncoding(std::string const & name);
188 /// get the current working directory
189 static FileName getcwd();
191 static FileName tempPath();
193 /// filename without path
194 std::string onlyFileName() const;
195 /// filename without path and without extension
196 std::string onlyFileNameWithoutExt() const;
197 /// only extension after the last dot.
198 std::string extension() const;
199 /** checks if the file has the given extension
200 on Windows and Mac it compares case insensitive */
201 bool hasExtension(const std::string & ext);
202 /// path without file name
203 FileName onlyPath() const;
204 /// path of parent directory
205 /// returns empty path for root directory
206 FileName parentPath() const;
207 /// used for display in the Gui
208 docstring displayName(int threshold = 1000) const;
210 /// change to a directory, return success
213 /// \param buffer_path if empty, uses `pwd`
214 docstring const relPath(std::string const & path) const;
216 docstring const absoluteFilePath() const;
219 friend bool equivalent(FileName const &, FileName const &);
220 /// Set for tracking of already visited file names.
221 /// Uses operator==() (which may be case insensitive), and not
222 /// equivalent(), so that symlinks are not resolved.
223 typedef std::set<FileName> FileNameSet;
224 /// Helper for public copyTo() to find circular symlink chains
225 bool copyTo(FileName const &, bool, FileNameSet &) const;
232 /// \return true if lhs and rhs represent the same file. E.g.,
233 /// they might be hardlinks of one another.
234 bool equivalent(FileName const & lhs, FileName const & rhs);
235 /// \return true if the absolute path names are the same.
236 bool operator==(FileName const &, FileName const &);
238 bool operator!=(FileName const &, FileName const &);
239 /// Lexically compares the absolute path names.
240 bool operator<(FileName const &, FileName const &);
241 /// Lexically compares the absolute path names.
242 bool operator>(FileName const &, FileName const &);
243 /// Writes the absolute path name to the stream.
244 std::ostream & operator<<(std::ostream &, FileName const &);
248 * Class for storing file names that appear in documents (e. g. child
249 * documents, included figures etc).
250 * The file name must not denote a file in our temporary directory, but a
251 * file that the user chose.
253 class DocFileName : public FileName {
256 /** \param abs_filename the file in question. Must have an absolute path.
257 * \param save_abs_path how is the filename to be output?
259 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
260 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
262 /** \param filename the file in question. May have either a relative
263 * or an absolute path.
264 * \param buffer_path if \c filename has a relative path, generate
265 * the absolute path using this.
267 void set(std::string const & filename, std::string const & buffer_path);
269 void erase() override;
271 bool saveAbsPath() const { return save_abs_path_; }
272 /// \param buffer_path if empty, uses `pwd`
273 std::string relFileName(std::string const & buffer_path = empty_string()) const;
274 /// \param buf_path if empty, uses `pwd`
275 std::string outputFileName(std::string const & buf_path = empty_string()) const;
277 /** @returns a mangled representation of the absolute file name
278 * suitable for use in the temp dir when, for example, converting
279 * an image file to another format.
281 * @param dir the directory that will contain this file with
282 * its mangled name. This information is used by the mangling
283 * algorithm when determining the maximum allowable length of
286 * An example of a mangled name:
287 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
289 * It is guaranteed that
290 * - two different filenames have different mangled names
291 * - two FileName instances with the same filename have identical
294 * Only the mangled file name is returned. It is not prepended
298 mangledFileName(std::string const & dir = empty_string()) const;
300 /** Identical to mangledFileName, with the following additions:
302 * @encrypt_path allows using hash (SHA-256) instead of full path.
303 * @use_counter allows disabling the counter in the filename.
306 mangledFileName(std::string const & dir, bool use_counter, bool encrypt_path) const;
308 /// \return the absolute file name without its .gz, .z, .Z extension
309 std::string unzippedFileName() const;
312 /// Records whether we should save (or export) the filename as a relative
313 /// or absolute path.
318 /// \return true if these have the same absolute path name AND
319 /// if save_abs_path_ has the same value in both cases.
320 bool operator==(DocFileName const &, DocFileName const &);
322 bool operator!=(DocFileName const &, DocFileName const &);
324 } // namespace support