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 &);
48 /// constructor with base name and suffix.
49 FileName(FileName const & fn, std::string const & suffix);
52 FileName & operator=(FileName const &);
55 /** Set a new filename.
56 * \param filename the file in question. Must have an absolute path.
57 * Encoding is always UTF-8.
59 void set(std::string const & filename);
60 void set(FileName const & fn, std::string const & suffix);
62 /// Is this filename empty?
64 /// Is the filename absolute?
65 static bool isAbsolute(std::string const & name);
67 /// get the absolute file name in UTF-8 encoding
68 std::string absFileName() const;
70 /** returns an absolute pathname (whose resolution does not involve
71 * '.', '..', or symbolic links) in UTF-8 encoding
73 std::string realPath() const;
76 * Get the file name in the encoding used by the file system.
77 * Only use this for passing file names to external commands.
78 * Warning: On Windows this is not unicode safe and should not
79 * be used for accessing files with an fstream, for example.
81 std::string toFilesystemEncoding() const;
84 * Get the file name in a unicode safe encoding used by the file system.
85 * Only use this for accessing the file with standard I/O functions
86 * non explicitly unicode aware, e.g. with an fstream. This can also
87 * be used for passing file names to external commands, but only if
88 * you are sure that the stem of the name will not be used for
89 * producing derivative files. For example, don't use this for passing
90 * file names to LaTeX, as the stem of the .dvi file will not correspond
91 * to the stem of the .tex file anymore.
92 * Use os::CREATE if the file is to be accessed for writing.
94 std::string toSafeFilesystemEncoding(os::file_access how = os::EXISTING) const;
96 /// returns true if the file exists
98 /// refreshes the file info
100 /// \return true if this object points to a symbolic link.
101 bool isSymLink() const;
102 /// \return true if the file is empty.
103 bool isFileEmpty() const;
104 /// returns time of last write access
105 std::time_t lastModified() const;
106 /// generates a checksum of a file
107 virtual unsigned long checksum() const;
108 /// return true when file is readable but not writable
109 bool isReadOnly() const;
110 /// return true when it names a directory
111 bool isDirectory() const;
112 /// return true when directory is readable
113 bool isReadableDirectory() const;
114 /// return true when it is a file and readable
115 virtual bool isReadableFile() const;
116 /// return true when file/directory is writable
117 bool isWritable() const;
118 /// return true when file/directory is writable (write test file)
119 bool isDirWritable() const;
120 /// \return list other files in the directory having optional extension 'ext'.
121 FileNameList dirList(std::string const & ext) const;
124 /// \return true when file/directory is writable (write test file)
125 /// \warning This methods has different semantics when system level
126 /// copy command, it will overwrite the \c target file if it exists,
127 /// If \p keepsymlink is true, the copy will be written to the symlink
128 /// target. Otherwise, the symlink will be destroyed.
129 bool copyTo(FileName const & target, bool keepsymlink = false) const;
131 /// remove pointed file.
132 /// \return true on success.
133 bool removeFile() const;
135 /// rename pointed file.
136 /// \return false if the operation fails or if the \param target file
138 /// \return true on success.
139 bool renameTo(FileName const & target) const;
141 /// move pointed file to \param target.
142 /// If \p target exists it will be overwritten (if it is a symlink,
143 /// the symlink will be destroyed).
144 /// \return true on success.
145 bool moveTo(FileName const & target) const;
146 bool link(FileName const & name) const;
148 /// change mode of pointed file.
149 /// This methods does nothing and return true on platforms that does not
151 /// \return true on success.
152 bool changePermission(unsigned long int mode) const;
153 // sets permission based on the file given as an argument
154 bool clonePermissions(FileName const & source);
156 /// remove pointed directory and all contents.
157 /// \return true on success.
158 bool destroyDirectory() const;
159 /// Creates pointed directory.
160 /// \return true on success.
161 bool createDirectory(int permissions) const;
162 /// Creates pointed path.
163 /// \return true on success.
164 bool createPath() const;
166 /// Get the contents of a file as a huge docstring.
167 /// \param encoding defines the encoding of the file contents.
168 /// Only four encodings are supported:
169 /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
170 /// current system locale.
171 docstring fileContents(std::string const & encoding) const;
173 /// Change extension.
175 * If oldname does not have an extension, it is appended.
176 * If the extension is empty, any extension is removed from the name.
178 void changeExtension(std::string const & extension);
180 static FileName fromFilesystemEncoding(std::string const & name);
182 /// get the current working directory
183 static FileName getcwd();
185 static FileName tempPath();
187 /// filename without path
188 std::string onlyFileName() const;
189 /// filename without path and without extension
190 std::string onlyFileNameWithoutExt() const;
191 /// only extension after the last dot.
192 std::string extension() const;
193 /** checks if the file has the given extension
194 on Windows and Mac it compares case insensitive */
195 bool hasExtension(const std::string & ext);
196 /// path without file name
197 FileName onlyPath() const;
198 /// path of parent directory
199 /// returns empty path for root directory
200 FileName parentPath() const;
201 /// used for display in the Gui
202 docstring displayName(int threshold = 1000) const;
204 /// change to a directory, return success
207 /// \param buffer_path if empty, uses `pwd`
208 docstring const relPath(std::string const & path) const;
210 docstring const absoluteFilePath() const;
213 friend bool equivalent(FileName const &, FileName const &);
214 /// Set for tracking of already visited file names.
215 /// Uses operator==() (which may be case insensitive), and not
216 /// equivalent(), so that symlinks are not resolved.
217 typedef std::set<FileName> FileNameSet;
218 /// Helper for public copyTo() to find circular symlink chains
219 bool copyTo(FileName const &, bool, FileNameSet &) const;
226 /// \return true if lhs and rhs represent the same file. E.g.,
227 /// they might be hardlinks of one another.
228 bool equivalent(FileName const & lhs, FileName const & rhs);
229 /// \return true if the absolute path names are the same.
230 bool operator==(FileName const &, FileName const &);
232 bool operator!=(FileName const &, FileName const &);
233 /// Lexically compares the absolute path names.
234 bool operator<(FileName const &, FileName const &);
235 /// Lexically compares the absolute path names.
236 bool operator>(FileName const &, FileName const &);
237 /// Writes the absolute path name to the stream.
238 std::ostream & operator<<(std::ostream &, FileName const &);
242 * Class for storing file names that appear in documents (e. g. child
243 * documents, included figures etc).
244 * The file name must not denote a file in our temporary directory, but a
245 * file that the user chose.
247 class DocFileName : public FileName {
250 /** \param abs_filename the file in question. Must have an absolute path.
251 * \param save_abs_path how is the filename to be output?
253 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
254 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
256 /** \param filename the file in question. May have either a relative
257 * or an absolute path.
258 * \param buffer_path if \c filename has a relative path, generate
259 * the absolute path using this.
261 void set(std::string const & filename, std::string const & buffer_path);
265 bool saveAbsPath() const { return save_abs_path_; }
266 /// \param buffer_path if empty, uses `pwd`
267 std::string relFileName(std::string const & buffer_path = empty_string()) const;
268 /// \param buf_path if empty, uses `pwd`
269 std::string outputFileName(std::string const & buf_path = empty_string()) const;
271 /** @returns a mangled representation of the absolute file name
272 * suitable for use in the temp dir when, for example, converting
273 * an image file to another format.
275 * @param dir the directory that will contain this file with
276 * its mangled name. This information is used by the mangling
277 * algorithm when determining the maximum allowable length of
280 * An example of a mangled name:
281 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
283 * It is guaranteed that
284 * - two different filenames have different mangled names
285 * - two FileName instances with the same filename have identical
288 * Only the mangled file name is returned. It is not prepended
292 mangledFileName(std::string const & dir = empty_string()) const;
294 /** Identical to mangledFileName, with the following additions:
296 * @encrypt_path allows using hash (SHA-256) instead of full path.
297 * @use_counter allows disabling the counter in the filename.
300 mangledFileName(std::string const & dir, bool use_counter, bool encrypt_path) const;
302 /// \return the absolute file name without its .gz, .z, .Z extension
303 std::string unzippedFileName() const;
306 /// Records whether we should save (or export) the filename as a relative
307 /// or absolute path.
312 /// \return true if these have the same absolute path name AND
313 /// if save_abs_path_ has the same value in both cases.
314 bool operator==(DocFileName const &, DocFileName const &);
316 bool operator!=(DocFileName const &, DocFileName const &);
318 } // namespace support