]> git.lyx.org Git - features.git/blob - src/support/FileName.h
add first version of .lyx file encryption. It's disabled by default and could out...
[features.git] / src / support / FileName.h
1 // -*- C++ -*-
2 /**
3  * \file FileName.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef FILENAME_H
13 #define FILENAME_H
14
15 #include "support/os.h"
16 #include "support/strfwd.h"
17
18 #include <ctime>
19
20
21 namespace lyx {
22 namespace support {
23
24 /// Defined in "FileNameList.h".
25 class FileNameList;
26
27 /**
28  * Class for storing file names.
29  * The file name may be empty. If it is not empty it is an absolute path.
30  * The file may or may not exist.
31  */
32 class FileName {
33 public:
34         /// Constructor for empty filenames
35         FileName();
36         /** Constructor for nonempty filenames.
37          * explicit because we don't want implicit conversion of relative
38          * paths in function arguments (e.g. of unlink).
39          * \param abs_filename the file in question. Must have an absolute path.
40          * Encoding is always UTF-8.
41          */
42         explicit FileName(std::string const & abs_filename);
43
44         /// copy constructor.
45         FileName(FileName const &);
46
47         /// constructor with base name and suffix.
48         FileName(FileName const & fn, std::string const & suffix);
49
50         ///
51         FileName & operator=(FileName const &);
52
53         virtual ~FileName();
54         /** Set a new filename.
55          * \param filename the file in question. Must have an absolute path.
56          * Encoding is always UTF-8.
57          */
58         virtual void set(std::string const & filename);
59         virtual void set(FileName const & fn, std::string const & suffix);
60         virtual void erase();
61         /// Is this filename empty?
62         bool empty() const;
63         /// Is the filename absolute?
64         static bool isAbsolute(std::string const & name);
65
66         /// get the absolute file name in UTF-8 encoding
67         std::string absFileName() const;
68
69         /** returns an absolute pathname (whose resolution does not involve
70           * '.', '..', or symbolic links) in UTF-8 encoding
71           */
72         std::string realPath() const;
73
74         /**
75          * Get the file name in the encoding used by the file system.
76          * Only use this for passing file names to external commands.
77          * Warning: On Windows this is not unicode safe and should not
78          * be used for accessing files with an fstream, for example.
79          */
80         std::string toFilesystemEncoding() const;
81
82         /**
83          * Get the file name in a unicode safe encoding used by the file system.
84          * Only use this for accessing the file with standard I/O functions
85          * non explicitly unicode aware, e.g. with an fstream. This can also
86          * be used for passing file names to external commands, but only if
87          * you are sure that the stem of the name will not be used for
88          * producing derivative files. For example, don't use this for passing
89          * file names to LaTeX, as the stem of the .dvi file will not correspond
90          * to the stem of the .tex file anymore.
91          * Use os::CREATE if the file is to be accessed for writing.
92          */
93         std::string toSafeFilesystemEncoding(os::file_access how = os::EXISTING) const;
94
95         /// returns true if the file exists
96         bool exists() const;
97         /// refreshes the file info
98         void refresh() const;
99         /// \return true if this object points to a symbolic link.
100         bool isSymLink() const;
101         /// \return true if the file is empty.
102         bool isFileEmpty() const;
103         /// returns time of last write access
104         std::time_t lastModified() const;
105         /// generates a checksum of a file
106         virtual unsigned long checksum() const;
107         /// return true when file is readable but not writable
108         bool isReadOnly() const;
109         /// return true when it names a directory
110         bool isDirectory() const;
111         /// return true when directory is readable
112         bool isReadableDirectory() const;
113         /// return true when it is a file and readable
114         virtual bool isReadableFile() const;
115         /// return true when file/directory is writable
116         bool isWritable() const;
117         /// return true when file/directory is writable (write test file)
118         bool isDirWritable() const;
119         /// \return list other files in the directory having optional extension 'ext'.
120         FileNameList dirList(std::string const & ext) const;
121         
122         /// copy a file
123         /// \return true when file/directory is writable (write test file)
124         /// \warning This methods has different semantics when system level
125         /// copy command, it will overwrite the \c target file if it exists,
126         bool copyTo(FileName const & target) const;
127
128         /// remove pointed file.
129         /// \return true on success.
130         bool removeFile() const;
131
132         /// rename pointed file.
133         /// \return false if the operation fails or if the \param target file
134         /// already exists.
135         /// \return true on success.
136         bool renameTo(FileName const & target) const;
137
138         /// move pointed file to \param target.
139         /// \return true on success.
140         bool moveTo(FileName const & target) const;
141
142         /// change mode of pointed file.
143         /// This methods does nothing and return true on platforms that does not
144         /// support this.
145         /// \return true on success.
146         bool changePermission(unsigned long int mode) const;
147
148         /// remove pointed directory and all contents.
149         /// \return true on success.
150         bool destroyDirectory() const;
151         /// Creates pointed directory.
152         /// \return true on success.
153         bool createDirectory(int permissions) const;
154         /// Creates pointed path.
155         /// \return true on success.
156         bool createPath() const;
157
158         /// Get the contents of a file as a huge docstring.
159         /// \param encoding defines the encoding of the file contents.
160         /// Only four encodings are supported:
161         /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
162         /// current system locale.
163         docstring fileContents(std::string const & encoding) const;
164
165         /// Change extension.
166         /**
167         * If oldname does not have an extension, it is appended.
168         * If the extension is empty, any extension is removed from the name.
169         */
170         void changeExtension(std::string const & extension);
171
172         /** Guess the file format name (as in Format::name()) from contents.
173          Normally you don't want to use this directly, but rather
174          Formats::getFormatFromFile().
175          */
176         std::string guessFormatFromContents() const;
177
178         /// check for zipped file
179         bool isZippedFile() const;
180
181         /// check for zipped file
182         bool isEncryptedFile() const;
183         /// string which encypted LyX files starts
184         static std::string encryptionGuessString();
185         static std::string encryptionPrefix(int version, int keytype);
186         /// get version from guessbytes
187         int encryptionVersion() const;
188         /// get method how the key is generated
189         int encryptionKeytype() const;
190
191         static FileName fromFilesystemEncoding(std::string const & name);
192         /// (securely) create a temporary file with the given mask.
193         /// \p mask must be in filesystem encoding, if it contains a
194         /// relative path, the template file will be created in the global
195         /// temporary directory as given by 'package().temp_dir()'.
196         static FileName tempName(std::string const & mask = empty_string());
197         static FileName tempName(FileName const & temp_dir,
198                 std::string const & mask);
199
200         /// get the current working directory
201         static FileName getcwd();
202
203         static FileName tempPath();
204
205         /// filename without path
206         std::string onlyFileName() const;
207         /// filename without path and without extension
208         std::string onlyFileNameWithoutExt() const;
209         /// only extension after the last dot.
210         std::string extension() const;
211         /** checks if the file has the given extension
212                 on Windows and Mac it compares case insensitive */
213         bool hasExtension(const std::string & ext);
214         /// path without file name
215         FileName onlyPath() const;
216         /// used for display in the Gui
217         docstring displayName(int threshold = 1000) const;
218
219         /// change to a directory, return success
220         bool chdir() const;
221         
222         /// \param buffer_path if empty, uses `pwd`
223         docstring const relPath(std::string const & path) const;
224         
225         docstring const absoluteFilePath() const;
226
227 private:
228         friend bool equivalent(FileName const &, FileName const &);
229         ///
230         struct Private;
231         Private * const d;
232 };
233
234
235 /// \return true if lhs and rhs represent the same file. E.g.,
236 /// they might be hardlinks of one another.
237 bool equivalent(FileName const & lhs, FileName const & rhs);
238 /// \return true if the absolute path names are the same.
239 bool operator==(FileName const &, FileName const &);
240 ///
241 bool operator!=(FileName const &, FileName const &);
242 /// Lexically compares the absolute path names.
243 bool operator<(FileName const &, FileName const &);
244 /// Lexically compares the absolute path names.
245 bool operator>(FileName const &, FileName const &);
246 /// Writes the absolute path name to the stream.
247 std::ostream & operator<<(std::ostream &, FileName const &);
248
249
250 /**
251  * Class for storing file names that appear in documents (e. g. child
252  * documents, included figures etc).
253  * The file name must not denote a file in our temporary directory, but a
254  * file that the user chose.
255  */
256 class DocFileName : public FileName {
257 public:
258         DocFileName();
259         /** \param abs_filename the file in question. Must have an absolute path.
260          *  \param save_abs_path how is the filename to be output?
261          */
262         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
263         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
264
265         /** \param filename the file in question. May have either a relative
266          *  or an absolute path.
267          *  \param buffer_path if \c filename has a relative path, generate
268          *  the absolute path using this.
269          */
270         virtual void set(std::string const & filename, std::string const & buffer_path);
271         ///
272         void erase();
273         ///
274         bool saveAbsPath() const { return save_abs_path_; }
275         /// \param buffer_path if empty, uses `pwd`
276         std::string relFileName(std::string const & buffer_path = empty_string()) const;
277         /// \param buf_path if empty, uses `pwd`
278         std::string outputFileName(std::string const & buf_path = empty_string()) const;
279         
280         /** @returns a mangled representation of the absolute file name
281          *  suitable for use in the temp dir when, for example, converting
282          *  an image file to another format.
283          *
284          *  @param dir the directory that will contain this file with
285          *  its mangled name. This information is used by the mangling
286          *  algorithm when determining the maximum allowable length of
287          *  the mangled name.
288          *
289          *  An example of a mangled name:
290          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
291          *
292          *  It is guaranteed that
293          *  - two different filenames have different mangled names
294          *  - two FileName instances with the same filename have identical
295          *    mangled names.
296          *
297          *  Only the mangled file name is returned. It is not prepended
298          *  with @c dir.
299          */
300         std::string
301         mangledFileName(std::string const & dir = empty_string()) const;
302
303         /// \return true if the file is compressed.
304         bool isZipped() const;
305         /// \return the absolute file name without its .gz, .z, .Z extension
306         std::string unzippedFileName() const;
307
308 private:
309         /// Records whether we should save (or export) the filename as a relative
310         /// or absolute path.
311         bool save_abs_path_;
312         /// Cache for isZipped() because zippedFile() is expensive
313         mutable bool zipped_;
314         /// Is zipped_ valid?
315         mutable bool zipped_valid_;
316 };
317
318
319 /// \return true if these have the same absolute path name AND 
320 /// if save_abs_path_ has the same value in both cases.
321 bool operator==(DocFileName const &, DocFileName const &);
322 ///
323 bool operator!=(DocFileName const &, DocFileName const &);
324
325 } // namespace support
326 } // namespace lyx
327
328 #endif