1 /* $Id: fileutils.hg,v 1.3 2004/01/22 18:38:12 murrayc Exp $ */
3 /* Copyright (C) 2002 The gtkmm Development Team
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 extern "C" { typedef struct _GDir GDir; }
29 #include <glibmmconfig.h>
30 #include <glibmm/error.h>
32 GLIBMM_USING_STD(input_iterator_tag)
33 GLIBMM_USING_STD(string)
39 _WRAP_ENUM(FileTest, GFileTest, NO_GTYPE)
41 /** @defgroup FileUtils File Utilities
42 * Various file-related classes and functions.
45 /** Exception class for file-related errors.
48 _WRAP_GERROR(FileError, GFileError, G_FILE_ERROR, NO_GTYPE,
50 s#^ISDIR$#IS_DIRECTORY#,
51 s#^ACCES$#ACCESS_DENIED#,
52 s#^NAMETOOLONG$#NAME_TOO_LONG#,
53 s#^NOENT$#NO_SUCH_ENTITY#,
54 s#^NOTDIR$#NOT_DIRECTORY#,
55 s#^NXIO$#NO_SUCH_DEVICE#,
56 s#^NODEV$#NOT_DEVICE#,
57 s#^ROFS$#READONLY_FILESYSTEM#,
58 s#^TXTBSY$#TEXT_FILE_BUSY#,
59 s#^FAULT$#FAULTY_ADDRESS#,
60 s#^LOOP$#SYMLINK_LOOP#,
61 s#^NOSPC$#NO_SPACE_LEFT#,
62 s#^NOMEM$#NOT_ENOUGH_MEMORY#,
63 s#^MFILE$#TOO_MANY_OPEN_FILES#,
64 s#^NFILE$#FILE_TABLE_OVERFLOW#,
65 s#^BADF$#BAD_FILE_DESCRIPTOR#,
66 s#^INVAL$#INVALID_ARGUMENT#,
67 s#^PIPE$#BROKEN_PIPE#,
69 s#^INTR$#INTERRUPTED#,
74 /** @enum FileError::Code
75 * Values corresponding to <tt>errno</tt> codes returned from file operations
77 * Unlike <tt>errno</tt> codes, FileError::Code values are available on all
78 * systems, even Windows. The exact meaning of each code depends on what sort
79 * of file operation you were performing; the UNIX documentation gives more
80 * details. The following error code descriptions come from the GNU C Library
81 * manual, and are under the copyright of that manual.
83 * It's not very portable to make detailed assumptions about exactly which
84 * errors will be returned from a given operation. Some errors don't occur on
85 * some systems, etc., sometimes there are subtle differences in when a system
86 * will report a given error, etc.
89 /** @var FileError::Code FileError::EXISTS
90 * <tt>(EEXIST)</tt> Operation not permitted; only the owner of the file (or
91 * other resource) or processes with special privileges can perform the operation.
94 /** @var FileError::Code FileError::IS_DIRECTORY
95 * <tt>(EISDIR)</tt> File is a directory; you cannot open a directory for writing,
96 * or create or remove hard links to it.
99 /** @var FileError::Code FileError::ACCESS_DENIED
100 * <tt>(EACCES)</tt> Permission denied; the file permissions do not allow the
101 * attempted operation.
104 /** @var FileError::Code FileError::NAME_TOO_LONG
105 * <tt>(ENAMETOOLONG)</tt> Filename too long.
108 /** @var FileError::Code FileError::NO_SUCH_ENTITY
109 * <tt>(ENOENT)</tt> No such file or directory. This is a "file doesn't exist"
110 * error for ordinary files that are referenced in contexts where they are expected
114 /** @var FileError::Code FileError::NOT_DIRECTORY
115 * <tt>(ENOTDIR)</tt> A file that isn't a directory was specified when a directory
119 /** @var FileError::Code FileError::NO_SUCH_DEVICE
120 * <tt>(ENXIO)</tt> No such device or address. The system tried to use the device
121 * represented by a file you specified, and it couldn't find the device. This can
122 * mean that the device file was installed incorrectly, or that the physical device
123 * is missing or not correctly attached to the computer.
126 /** @var FileError::Code FileError::NOT_DEVICE
127 * <tt>(ENODEV)</tt> This file is of a type that doesn't support mapping.
130 /** @var FileError::Code FileError::READONLY_FILESYSTEM
131 * <tt>(EROFS)</tt> The directory containing the new link can't be modified
132 * because it's on a read-only file system.
135 /** @var FileError::Code FileError::TEXT_FILE_BUSY
136 * <tt>(ETXTBSY)</tt> Text file busy.
139 /** @var FileError::Code FileError::FAULTY_ADDRESS
140 * <tt>(EFAULT)</tt> You passed in a pointer to bad memory. (Glib won't
141 * reliably return this, don't pass in pointers to bad memory.)
144 /** @var FileError::Code FileError::SYMLINK_LOOP
145 * <tt>(ELOOP)</tt> Too many levels of symbolic links were encountered in
146 * looking up a file name. This often indicates a cycle of symbolic links.
149 /** @var FileError::Code FileError::NO_SPACE_LEFT
150 * <tt>(ENOSPC)</tt> No space left on device; write operation on a file failed
151 * because the disk is full.
154 /** @var FileError::Code FileError::NOT_ENOUGH_MEMORY
155 * <tt>(ENOMEM)</tt> No memory available. The system cannot allocate more
156 * virtual memory because its capacity is full.
159 /** @var FileError::Code FileError::TOO_MANY_OPEN_FILES
160 * <tt>(EMFILE)</tt> The current process has too many files open and can't
161 * open any more. Duplicate descriptors do count toward this limit.
164 /** @var FileError::Code FileError::FILE_TABLE_OVERFLOW
165 * <tt>(ENFILE)</tt> There are too many distinct file openings in the
169 /** @var FileError::Code FileError::BAD_FILE_DESCRIPTOR
170 * <tt>(EBADF)</tt> Bad file descriptor; for example, I/O on a descriptor
171 * that has been closed or reading from a descriptor open only for writing
175 /** @var FileError::Code FileError::INVALID_ARGUMENT
176 * <tt>(EINVAL)</tt> Invalid argument. This is used to indicate various kinds
177 * of problems with passing the wrong argument to a library function.
180 /** @var FileError::Code FileError::BROKEN_PIPE
181 * <tt>(EPIPE)</tt> Broken pipe; there is no process reading from the other
182 * end of a pipe. Every library function that returns this error code also
183 * generates a <tt>SIGPIPE</tt> signal; this signal terminates the program
184 * if not handled or blocked. Thus, your program will never actually see
185 * this code unless it has handled or blocked <tt>SIGPIPE</tt>.
188 /** @var FileError::Code FileError::TRYAGAIN
189 * <tt>(EAGAIN)</tt> Resource temporarily unavailable; the call might work
190 * if you try again later.
191 * We used TRYAGAIN instead of TRY_AGAIN, because that is a defined as a macro by a Unix header.
194 /** @var FileError::Code FileError::INTERRUPTED
195 * <tt>(EINTR)</tt> Interrupted function call; an asynchronous signal occurred
196 * and prevented completion of the call. When this happens, you should try
200 /** @var FileError::Code FileError::IO_ERROR
201 * <tt>(EIO)</tt> Input/output error; usually used for physical read or write
202 * errors. I.e. the disk or other physical device hardware is returning errors.
205 /** @var FileError::Code FileError::NOT_OWNER
206 * <tt>(EPERM)</tt> Operation not permitted; only the owner of the file (or other
207 * resource) or processes with special privileges can perform the operation.
210 /** @var FileError::Code FileError::FAILED
211 * Does not correspond to a UNIX error code; this is the standard "failed for
212 * unspecified reason" error code present in all Glib::Error error code
213 * enumerations. Returned if no specific code applies.
218 /** The iterator type of Glib::Dir.
224 typedef std::input_iterator_tag iterator_category;
225 typedef std::string value_type;
226 typedef int difference_type;
227 typedef value_type reference;
228 typedef void pointer;
232 #ifndef DOXYGEN_SHOULD_SKIP_THIS
233 DirIterator(GDir* gobject, const char* current);
236 std::string operator*() const;
237 DirIterator& operator++();
239 /** @note DirIterator has input iterator semantics, which means real
240 * postfix increment is impossible. The return type is @c void to
241 * prevent surprising behaviour.
243 void operator++(int);
245 bool operator==(const DirIterator& rhs) const;
246 bool operator!=(const DirIterator& rhs) const;
250 const char* current_;
254 /** Utility class representing an open directory.
256 * It's highly recommended to use the iterator interface. With iterators,
257 * reading an entire directory into a STL container is really easy:
259 * Glib::Dir dir (directory_path);
260 * std::list<std::string> entries (dir.begin(), dir.end());
262 * @note The encoding of the directory entries isn't necessarily UTF-8.
263 * Use Glib::filename_to_utf8() if you need to display them.
268 typedef DirIterator iterator;
269 typedef DirIterator const_iterator;
271 /** Opens a directory for reading. The names of the files in the
272 * directory can then be retrieved using read_name().
273 * @param path The path to the directory you are interested in.
274 * @throw Glib::FileError
276 explicit Dir(const std::string& path);
278 #ifndef DOXYGEN_SHOULD_SKIP_THIS
279 explicit Dir(GDir* gobject);
282 /** Closes the directory and deallocates all related resources.
286 /** Retrieves the name of the next entry in the directory.
287 * The <tt>'.'</tt> and <tt>'..'</tt> entries are omitted.
288 * @return The entry's name or <tt>""</tt> if there are no more entries.
289 * @see begin(), end()
291 std::string read_name();
293 /** Resets the directory. The next call to
294 * read_name() will return the first entry again.
298 /** Closes the directory and deallocates all related resources.
299 * Note that close() is implicitely called by ~Dir(). Thus you don't
300 * need to call close() yourself unless you want to close the directory
301 * before the destructor runs.
305 /** Get the begin of an input iterator sequence.
306 * @return An input iterator pointing to the first directory entry.
310 /** Get the end of an input iterator sequence.
311 * @return An input iterator pointing behind the last directory entry.
320 Dir& operator=(const Dir&);
324 /** Returns @c true if any of the tests in the bitfield @a test are true.
326 * For example, <tt>(Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)</tt> will
327 * return @c true if the file exists; the check whether it's a directory
328 * doesn't matter since the existence test is true. With the current set of
329 * available tests, there's no point passing in more than one test at a time.
331 * Apart from <tt>Glib::FILE_TEST_IS_SYMLINK</tt> all tests follow symbolic
332 * links, so for a symbolic link to a regular file file_test() will return
333 * @c true for both <tt>Glib::FILE_TEST_IS_SYMLINK</tt> and
334 * <tt>Glib::FILE_TEST_IS_REGULAR</tt>.
336 * @note For a dangling symbolic link file_test() will return @c true for
337 * <tt>Glib::FILE_TEST_IS_SYMLINK</tt> and @c false for all other flags.
339 * @param filename A filename to test.
340 * @param test Bitfield of Glib::FileTest flags.
341 * @return Whether a test was true.
343 bool file_test(const std::string& filename, FileTest test);
345 /** Opens a temporary file.
347 * See the %mkstemp() documentation on most UNIX-like systems. This is a
348 * portability wrapper, which simply calls %mkstemp() on systems that have
349 * it, and implements it in GLib otherwise.
350 * @param filename_template A string that should match the rules for
351 * %mkstemp(), i.e. end in <tt>"XXXXXX"</tt>. The <tt>X</tt> string
352 * will be modified to form the name of a file that didn't exist.
353 * @return A file handle (as from open()) to the file opened for reading
354 * and writing. The file is opened in binary mode on platforms where there
355 * is a difference. The file handle should be closed with close(). In
356 * case of errors, <tt>-1</tt> is returned.
358 int mkstemp(std::string& filename_template);
360 /** Opens a file for writing in the preferred directory for temporary files
361 * (as returned by Glib::get_tmp_dir()).
363 * @a prefix should a basename template; it'll be suffixed by 6 characters
364 * in order to form a unique filename. No directory components are allowed.
366 * The actual name used is returned in @a name_used.
368 * @param prefix Template for file name, basename only.
369 * @retval name_used The actual name used.
370 * @return A file handle (as from <tt>open()</tt>) to the file opened for reading
371 * and writing. The file is opened in binary mode on platforms where there is a
372 * difference. The file handle should be closed with <tt>close()</tt>.
373 * @throw Glib::FileError
375 int file_open_tmp(std::string& name_used, const std::string& prefix);
377 /** Opens a file for writing in the preferred directory for temporary files
378 * (as returned by Glib::get_tmp_dir()).
380 * This function works like file_open_tmp(std::string&, const std::string&)
381 * but uses a default basename prefix.
383 * @retval name_used The actual name used.
384 * @return A file handle (as from <tt>open()</tt>) to the file opened for reading
385 * and writing. The file is opened in binary mode on platforms where there is a
386 * difference. The file handle should be closed with <tt>close()</tt>.
387 * @throw Glib::FileError
389 int file_open_tmp(std::string& name_used);
391 /** Reads an entire file into a string, with good error checking.
393 * @param filename A file to read contents from.
394 * @return The file contents.
395 * @throw Glib::FileError
397 std::string file_get_contents(const std::string& filename);