Document number: | N3239 = 11-0009 |
Date: | 2011-02-25 |
Project: | Programming Language C++, Library Working Group |
Reply-to: | Beman Dawes <bdawes at acm dot org> |
This paper proposes that the filesystem library component of C++ Standard Library Technical Report 2 be based on Version 3 of the Boost Filesystem Library (see www.boost.org/libs/filesystem). Preliminary wording is provided. A TODO list identifies remaining work to be done.
J16/06-0045 = WG21/N1975, Filesystem Library Proposal for TR2 (Revision 3), was adopted by the committee in April, 2006, at the Berlin meeting. Shortly afterward the Library Working Group set aside work on TR2 to concentrate on C++0x. In the meantime, work on the Boost version of the Filesystem Library has continued, and Version 3 of the library has been released. Changes include:
path
handles all aspects of
internationalization, replacing the previous template and its path
and wpath
instantiations. Character types char
,
wchar_t
, char16_t
, and char32_t
are
supported. This is a major simplification of the path abstraction,
particularly for functions that take path arguments. This change was based
on a suggestion by Peter Dimov.error_code
is now uniform
throughout the operations functions.|The motivation and scope for a filesystem library were described in N1975, and are not repeated here. A minor scope reduction is that an addition to the current C++ runtime library is no longer needed.
Boost Filesystem Version 3 introduced a single path type that interoperates well with both
basic_string
and user defined string types. Thus the following Design
alternatives paragraph is no long applicable:
Single path type which can at runtime accept narrow or wide character pathnames. Although certainly interesting, and possibly superior, such a design would not interoperate well with the current Standard Library's compile-time typedbasic_string
. A new runtime polymorphic string class would be the best place to experiment with this concept, not a path class.
char16_t
and char32_t
support to verify the
specification for these is correct.file_status
is overdesigned. It holds just a single file_type
variable, and that's unlikely to change
because of performance considerations. It would be simpler to eliminate class
file_status
, and just use file_type
in its stead. Perhaps
rename to file_status
. This hasn't been done in the Boost
because it would break existing code, albeit noisily.boost::io::quoted
.boost::iterator_facade
.Source
is not specified as actually
implemented. Expose path_traits
?copy
and copy_directory
need to be reviewed, revised, tested.Gray-shaded italic text is commentary on the proposal. It is not to be added to the TR.
The following wording is a lighted edited version of the Boost Filesystem reference documentation. Although further editing is required, the features described are relatively stable and major feature changes are not anticipated.
Add the following to the Technical Report's front matter:
The following standard contains provisions which, through reference in this text, constitute provisions of this Technical Report. At the time of publication, the editions indicated were valid. All standards are subject to revision, and parties to agreements based on this Technical Report are encouraged to investigate the possibility of applying the most recent editions of the standard indicated below. Members of IEC and ISO maintain registers of currently valid International Standards.
[Note: ISO/IEC 9945:2003 is also IEEE Std 1003.1-2001, and The Open Group Base Specifications, Issue 6, and is also known as The Single Unix2 Specification, Version 3. It is available from each of those organizations, and may be read online or downloaded from www.unix.org/single_unix_specification/ -- end note]
ISO/IEC 9945:2003, with the indicated corrections, is hereinafter called POSIX.
Footnote 1: POSIX® is a registered trademark of The IEEE.
Footnote 2: UNIX® is a registered trademark of The Open Group.
Add the following to the Technical Report as a new Clause:
This Clause describes components that perform operations on file systems and their components, such as paths, regular files, and directories.
Operating systems such as MAC OS, UNIX, and Windows are mentioned in this Clause for purposes of illustration or to give guidance to implementers. No slight to other operating systems is implied or intended.
Footnote 3: MAC OS® is a registered trademark of Apple Inc.
Footnote 4: Windows® is a registered trademark of Microsoft Corporation.
Some library behavior is specified in this Clause by reference to POSIX. How such behavior is actually implemented is unspecified.
[Note: This constitutes an "as if" rule for implementation of operating system dependent behavior. Presumably implementations will call native operating system API's, but this is not required. --end note]
Implementations are encouraged, but not required, to support such behavior as it is defined by POSIX. Implementations shall document behavior that differs from the POSIX defined behavior. Implementations that do not support exact POSIX behavior are encouraged to provide behavior as close to POSIX behavior as is reasonable given the limitations of actual operating systems and file systems. A diagnostic is required if an implementation cannot provide reasonable behavior.
The behavior of functions described in this Clause may not be achieved in the presence of race conditions. No diagnostic is required.
If the possibility of race conditions would make it unreliable for a program to test for a precondition before calling a function described in this clause, the precondition is not specified in the function's Requires element.
File
An object that can be written to, or read from, or both. A file
has certain attributes, including type. Common types of files include regular files
and directories. Other types of files, such as symbolic links, may be supported by the
implementation.
File system
A collection of files and certain of their attributes.
Filename
The name of a file. Slash and 0x00
characters are not permitted. Implementations may define additional
characters or specific names that are not permitted. Filenames .
and ..
have special meaning. Implementations may define
additional filenames that have special meaning.
[Note: Most operating systems prohibit the ASCII control characters (0x00-0x1F) in filenames.
Windows prohibits the characters 0x00-0x1F,
"
,*
,:
,<
,>
,?
,\
,/
, and|
--end note]
Path
A sequence of elements that identify
a location within a filesystem. The elements are the root-nameopt,
root-directoryopt, and an optional sequence of filenames. [Note:
A pathname is the concrete representation of a path. --end note]
Absolute path
A path that uniquely
identifies a file. The format is implementation defined.
[Note: For POSIX-like implementations, including Unix variants, Linux, and Mac OS X, only paths that begin with a slash are absolute paths.
For Windows-like implementations, including Cygwin and MinGW, only paths that begin with a drive specifier followed by a slash, or begin with two slashes, are absolute paths. --end note]
Relative path
A path that uniquely
identifies a file only when considered relative to some other path. [Note:
Paths "." and ".." are considered to be relative paths. --end note]
Pathname
A character string that represents a
path. Pathnames are formatted according to the generic pathname format or the
native pathname format.
pathname:
root-nameopt root-directoryopt relative-pathoptroot-name:
implementation-defined[Note: Most POSIX and Windows based operating system define a name beginning with two slashes (or backslashes, for Windows) as a root-name identifying network locations. Windows defines a single letter followed by a
":"
as a root-name identifying a disc drive. --end note]root-directory:
directory-separatorrelative-path:
filename
relative-path directory-separator
relative-path directory-separator filenamefilename:
name
"."
".."
directory-separator:
"/"
directory-separator
"/"Multiple successive directory-separator characters are considered to be the same as one directory-separator character. The filename
"."
is considered to be a reference to the current directory. The filename".."
is considered to be a reference to the current directory. Specific filenames may have special meaning for a particular operating system.
Native pathname format
An implementation defined format. [Note: For POSIX-like operating
systems, the native format is the same as the generic format. For Windows, the
native format is similar to the generic format, but the directory-separator
characters can be either slashes or backslashes. --end note]
Link
A directory entry object that associates a
filename with a file. On some file systems, several directory entries can
associate names with the same file.
Hard link
A link to an existing file. Some
file systems support multiple hard links to a file. If the last hard link to a
file is removed, the file itself is removed.
[Note: A hard link can be thought of as a shared-ownership smart pointer to a file. -- end note]
Symbolic link
A type of file with the
property that when the file is encountered during pathname resolution, a string
stored by the file is used to modify the pathname resolution.
[Note: A symbolic link can be thought of as a raw pointer to a file. If the file pointed to does not exist, the symbolic link is said to be a "dangling" symbolic link. -- end note]
Race condition
The condition that occurs
when multiple threads, processes, or computers interleave access and
modification of
the same object within a file system.
Dot, Dot Dot: Synonyms for the filenames "."
and ".."
, respectively. The dot filename names the current
directory. The dot dot filename names the parent directory.
<filesystem>
synopsis
The proposal places the library in namespace Would the LWG prefer something shorter, such as |
namespace std { namespace tr2 {
{
namespace filesystem
{
class path;
class filesystem_error;
class directory_entry;
class directory_iterator;
class recursive_directory_iterator;
class file_status;
void swap(path& lhs, path& rhs);
bool lexicographical_compare(path::iterator first1, path::iterator last1,
path::iterator first2, path::iterator last2);
bool operator==(const path& lhs, const path& rhs);
bool operator!=(const path& lhs, const path& rhs);
bool operator< (const path& lhs, const path& rhs);
bool operator<=(const path& lhs, const path& rhs);
bool operator> (const path& lhs, const path& rhs);
bool operator>=(const path& lhs, const path& rhs);
path operator/ (const path& lhs, const path& rhs);
ostream& operator<<( ostream& os, const path& p );
wostream& operator<<( wostream& os, const path& p );
istream& operator>>( istream& is, path& p );
wistream& operator>>( wistream& is, path& p )
enum file_type { status_error, file_not_found, regular_file, directory_file,
symlink_file, block_file, character_file, fifo_file, socket_file,
type_unknown
};
struct space_info // returned by space function
{
uintmax_t capacity;
uintmax_t free;
uintmax_t available; // free space available to a non-privileged process
};
enum class copy_option
{
none
fail_if_exists = none,
overwrite_if_exists
};
enum class symlink_option
{
none
no_recurse = none,
recurse
};
// operational functions
path absolute(const path& p, const path& base=current_path());
void copy(const path& from, const path& to);
void copy(const path& from, const path& to, error_code& ec);
void copy_directory(const path& from, const path& to);
void copy_directory(const path& from, const path& to, error_code& ec);
void copy_file(const path& from, const path& to);
void copy_file(const path& from, const path& to, error_code& ec);
void copy_file(const path& from, const path& to, copy_option option);
void copy_file(const path& from, const path& to, copy_option option,
error_code& ec);
void copy_symlink(const path& existing_symlink, const path& new_symlink);
void copy_symlink(const path& existing_symlink, const path& new_symlink, error_code& ec);
bool create_directories(const path& p);
bool create_directories(const path& p, error_code& ec);
bool create_directory(const path& p);
bool create_directory(const path& p, error_code& ec);
void create_directory_symlink(const path& to, const path& new_symlink);
void create_directory_symlink(const path& to, const path& new_symlink, error_code& ec);
void create_hard_link(const path& to, const path& new_hard_link);
void create_hard_link(const path& to, const path& new_hard_link, error_code& ec);
void create_symlink(const path& to, const path& new_symlink);
void create_symlink(const path& to, const path& new_symlink, error_code& ec);
path current_path();
path current_path(error_code& ec);
void current_path(const path& p);
void current_path(const path& p, error_code& ec);
bool exists(file_status s);
bool exists(const path& p);
bool exists(const path& p, error_code& ec);
bool equivalent(const path& p1, const path& p2);
bool equivalent(const path& p1, const path& p2, error_code& ec);
uintmax_t file_size(const path& p);
uintmax_t file_size(const path& p, error_code& ec);
uintmax_t hard_link_count(const path& p);
uintmax_t hard_link_count(const path& p, error_code& ec);
const path& initial_path();
const path& initial_path(error_code& ec
);
bool is_directory(file_status s);
bool is_directory(const path& p);
bool is_directory(const path& p, error_code& ec);
bool is_empty(const path& p);
bool is_empty(const path& p, error_code& ec);
bool is_other(file_status s);
bool is_other(const path& p,);
bool is_other(const path& p, error_code& ec);
bool is_regular_file(file_status s);
bool is_regular_file(const path& p);
bool is_regular_file(const path& p, error_code& ec);
bool is_symlink(file_status s);
bool is_symlink(const path& p);
bool is_symlink(const path& p, error_code& ec);
time_t last_write_time(const path& p);
time_t last_write_time(const path& p, error_code& ec);
void last_write_time(const path& p, const time_t new_time);
void last_write_time(const path& p, const time_t new_time, error_code& ec);
path read_symlink(const path& p);
path read_symlink(const path& p, error_code& ec);
bool remove(const path& p);
bool remove(const path& p, error_code& ec);
uintmax_t remove_all(const path& p);
uintmax_t remove_all(const path& p, error_code& ec);
void rename(const path& from, const path& to);
void rename(const path& from, const path& to, error_code& ec);
void resize_file(const path& p, uintmax_t size);
void resize_file(const path& p, uintmax_t size, error_code& ec);
space_info space(const path& p);
space_info space(const path& p, error_code& ec);
file_status status(const path& p);
file_status status(const path& p, error_code& ec);
bool status_known(file_status s);
file_status symlink_status(const path& p);
file_status symlink_status(const path& p, error_code& ec);
path system_complete(const path& p);
path system_complete(const path& p, error_code& ec);
path temp_directory_path();
path temp_directory_path(error_code& ec);
path unique_path(const path& model="%%%%-%%%%-%%%%-%%%%");
path unique_path(const path& model, error_code& ec);
} // namespace filesystem
}} // namespaces std and tr2
Filesystem library functions often provide two overloads, one that
throws an exception to report file system errors, and another that sets an
error_code
.
[Note: This supports two common use cases:
Uses where file system errors are truly exceptional and indicate a serious failure. Throwing an exception is the most appropriate response. This is the recommended approach for much everyday programming.
try
- catch
blocks is an indication
that error reporting by error_code
may be more appropriate.--end note]
Functions not having an argument of type error_code&
report errors as follows, unless otherwise specified:
When a call by the
implementation to an operating system or other underlying API results in an
error that prevents the function from meeting its specifications, an exception
of type
filesystem_error
is thrown.
Failure to allocate storage is reported by throwing an exception as described in the C++ standard, 17.6.4.10 [res.on.exception.handling].
Destructors throw nothing.
Functions having an argument of type error_code&
report errors as follows, unless otherwise
specified:
If a call by the
implementation to an operating system or other underlying API results in an
error that prevents the function from meeting its specifications, the error_code&
argument is set as
appropriate appropriate for the specific error. Otherwise, clear()
is called on the error_code&
argument.
Failure to allocate storage is reported by throwing an exception as described in the C++ standard, 17.6.4.10 [res.on.exception.handling].
path
An object of class path
represents a path,
and contains a pathname Such an object is concerned only with the lexical and syntactic aspects
of a path. The path may not actually exist in external storage, and may contain pathnames which are not even valid for the current operating
system.
namespace std { namespace tr2 { { namespace filesystem { class path { public: typedef see below value_type; // char for POSIX, wchar_t for Windows typedef basic_string<value_type> string_type; typedef codecvt<wchar_t, char, mbstate_t> codecvt_type; // constructors and destructor path(); path(const path& p); template <class Source> path(Source const& source, const codecvt_type& cvt=codecvt()); template <class InputIterator> path(InputIterator begin, InputIterator end, const codecvt_type& cvt=codecvt()); ~path(); // assignments path& operator=(const path& p); template <class Source> path& operator=(Source const& source); template <class Source> path& assign(Source const& source, const codecvt_type& cvt) template <class InputIterator> path& assign(InputIterator begin, InputIterator end, const codecvt_type& cvt=codecvt()); // appends path& operator/=(const path& p); template <class Source> path& operator/=(Source const& source); template <class Source> path& append(Source const& source, const codecvt_type& cvt); template <class InputIterator> path& append(InputIterator begin, InputIterator end, const codecvt_type& cvt=codecvt()); // modifiers void clear(); path& make_absolute(const path& base); path& make_preferred(); // POSIX: no effect. Windows: convert slashes to backslashes path& remove_filename(); path& replace_extension(const path& new_extension = path()); void swap(path& rhs); // native format observers const string_type& native() const; // native format, encoding const value_type* c_str() const; // native().c_str() template <class String> String string(const codecvt_type& cvt=codecvt()) const; // native format const string string(const codecvt_type& cvt=codecvt()) const; // native format const wstring wstring(const codecvt_type& cvt=codecvt()) const; // ditto const u16string u16string() const; // ditto const u32string u32string() const; // ditto // generic format observers template <class String> String generic_string() const; const string generic_string(const codecvt_type& cvt=codecvt()) const; // generic format const wstring generic_wstring(const codecvt_type& cvt=codecvt()) const; // ditto const u16string generic_u16string() const; // ditto const u32string generic_u32string() const; // ditto // decomposition path root_name() const; path root_directory() const; path root_path() const; path relative_path() const; path parent_path() const; path filename() const; path stem() const; path extension() const; // query bool empty() const; bool has_root_name() const; bool has_root_directory() const; bool has_root_path() const; bool has_relative_path() const; bool has_parent_path() const; bool has_filename() const; bool has_stem() const; bool has_extension() const; bool is_absolute() const; bool is_relative() const; // iterators class iterator; typedef iterator const_iterator; iterator begin() const; iterator end() const; // encoding conversion static locale imbue( const locale& loc ); static const codecvt_type & codecvt(); private: string_type pathname; // exposition only }; } // namespace filesystem }} // namespaces std and tr2
value_type
is an implementation-defined
typedef
for the
character type used by the implementation to represent pathnames.
If the value type of [begin
,end
)
or source
arguments for member functions is not value_type
,
and no cvt
argument is supplied, conversion to value_type
occurs using an imbued locale. This imbued locale is initialized with a
codecvt
facet appropriate for the operating system.
For Apple OS X implementations,
path::value_type
ischar
. The default imbued locale provides a UTF-8codecvt
facet. [Rationale: "All BSD system functions expect their string parameters to be in UTF-8 encoding and nothing else." See Apple docs. -- end rationale]For Windows-like implementations, including Cygwin and MinGW,
path::value_type
iswchar_t
. The default imbued locale provides acodecvt
facet that invokes WindowsMultiByteToWideChar
orWideCharToMultiByte
API's with a codepage ofCP_THREAD_ACP
if WindowsAreFileApisANSI()
is true, otherwise codepageCP_OEMCP
. [Rationale: this is the current behavior of C and C++ programs that perform file operations using narrow character string to identify paths. Changing this in the Filesystem library would be too surprising, particularly where user input is involved. -- end rationale]For all other implementations, including Linux,
path::value_type
ischar
. The default imbued locale islocale("")
. [Rationale: ISO C specifies this as "the locale-specific native environment", while POSIX says it "Specifies an implementation-defined native environment." -- end rationale
For member functions described as returning const string
,
const wstring
, const u16string
, or const u32string
,
implementations are permitted to return const string&
, const
wstring&
, const u16string&
, or const u32string&
,
respectively.
[Note: This allows implementations to avoid unnecessary copies when no conversion is required. Return-by-value is specified as
const
to ensure programs won't break if moved to a return-by-reference implementation. -- end note]
InputIterator
is required to meet the RandomIterator
requirements. The iterator's value type is required to be char
,
wchar_t
, char16_t
, or char32_t
.
Source
is required to be one of:
A container with a value type of char
,
wchar_t
, char16_t
, or char32_t
.
An iterator for a null terminated byte-string. The value type is required
to be char
, wchar_t
, char16_t
, or
char32_t
.
A C-array. The value type is required to be char
,
wchar_t
, char16_t
, or char32_t
.
A directory_entry
.
The specifications for path
functions require that
arguments in the generic pathname format be converted to native pathname format
as they are stored in pathname
. If the native format requires
regular file paths and directory paths to be formatted differently, the
implementation shall determine which format to use according to whether or not
the last element of the generic format string is a separator. [Example:
On OpenVMS, a path
constructed from "/cats/jane"
would considered a regular file
path, and have a native format of "[CATS]JANE"
, while a
path constructed from "/cats/jane/"
would be considered a
directory path, and have a native format of "[CATS.JANE]"
.
--end example] [Note: POSIX and Windows use the same native format
for both regular file and directory pathnames, so this paragraph does not apply to
them. --end note]
Class
|
path
constructorspath();
Postcondition:
empty()
.
template <class Source> path(Source const& source, const codecvt_type& cvt=codecvt());
template <class InputIterator> path(InputIterator begin, InputIterator end, const codecvt_type& cvt=codecvt());
Effects: Stores the contents [
begin
,end
) orsource
inpathname
. If the contents are in the generic format and the generic format is unacceptable to the operating system's API, they are converted to the native format. [Note: For POSIX and Windows implementations, the generic format is already acceptable as a native format, so no generic to native conversion is performed. --end note]Remarks: If the value type of [
begin
,end
) orsource
is notvalue_type
, conversion is performed bycvt
.
path
assignmentstemplate <class Source> path& operator=(Source const& source);
template <class Source> path& assign(Source const& source, const codecvt_type& cvt);
template <class InputIterator> path& assign(InputIterator begin, InputIterator end, const codecvt_type& cvt=codecvt());
Effects: Stores the contents [
begin
,end
) orsource
inpathname
. If the contents are in the generic format, they are converted to the native format. [Note: For POSIX and Windows based implementations, the generic format is already acceptable as a native format, so no generic to native conversion is performed. --end note]Returns:
*this
Remarks: If the value type of [
begin
,end
) orsource
is notvalue_type
, conversion is performed bycvt
.
path
appendsThe append operations use operator/=
to denote their semantic
effect of appending the platform's preferred directory separator when needed. The
preferred
directory separator is implementation-defined.
[Note: For POSIX-like implementations, including Unix variants, Linux, and Mac OS X, the preferred directory separator is a single forward slash.
For Windows-like implementations, including Cygwin and MinGW, the preferred directory separator is a single backslash.--end note]
path& operator/=(const path& p);
Effects:
Appends the preferred directory separator to the contained pathname, unless:
an added separator would be redundant, or
would change an relative path to an absolute path, or
p.empty()
, or
*p.native().cbegin()
is a directory separator.Appends
p.native()
topathname
.Returns:
*this
template <class Source> path& operator/=(Source const & source);
template <class Source> path& append(Source const & source, const codecvt_type& cvt);
template <class InputIterator> path& append(InputIterator begin, InputIterator end, const codecvt_type& cvt=codecvt());
Effects:
Appends a native directory separator to the contained pathname, unless:
an added separator would be redundant, or
would change an relative path to an absoute path, or
p.empty()
, or
*p.native().cbegin()
is a separator.Appends the contents [
begin
,end
) orsource
topathname
. If the contents are in the generic format, they are converted to the native format. [Note: For POSIX and Windows based implementations, the generic format is already acceptable as a native format, so no generic to native conversion is performed. --end note]Remarks: If the value type of [
begin
,end
) orsource
is notvalue_type
, conversion is performed bycvt
.Returns:
*this
path
modifiersvoid clear();
Postcondition:
this->empty()
is true.
path& make_preferred();
Effects: The contained pathname is converted to the preferred native format. [Note: On Windows, the effect is to replace slashes with backslashes. On POSIX, there is no effect. -- end note]
Returns:
*this
path& remove_filename();
Returns: As if,
*this = parent_path();
[Note: This function is needed to efficiently implement
directory_iterator
. It is exposed to allow additional uses. The actual implementation may be much more efficient than*this = parent_path()
-- end note]
path& replace_extension(const path& new_extension = path());
Postcondition:
extension() == replacement
, wherereplacement
isnew_extension
ifnew_extension.empty() || new_extension[0] ==
the dot character, otherwisereplacement
is the dot character followed bynew_extension
.Returns:
*this
void swap(path& rhs);
Effects: Swaps the contents of the two paths.
Throws: nothing.
Complexity: constant time.
path
native format observersThe string returned by all native format observers is in the native pathname format.
const string_type& native() const;
Returns:
pathname
.Throws: nothing.
const value_type* c_str() const;
Returns:
pathname.c_str()
.Throws: nothing.
template <class String> String string(const codecvt_type& cvt=codecvt()) const;
Returns:
pathname
.Remarks: If
string_type
is a different type thanString
, conversion is performed bycvt
.
const string string(const codecvt_type& cvt=codecvt()) const; const wstring wstring(const codecvt_type& cvt=codecvt()) const; const u16string u16string() const; const u32wstring u32wstring() const;
Returns:
pathname
.Remarks: If
string_type
is a different type than function's return type, conversion is performed bycvt
.If
string_type
is the same type as the function's return type, the function is permitted to return byconst&
rather thanconst
value. [Note: For POSIX, this occurs forstring()
, for Windows,wstring()
. --end note]
path
generic format observersThe string returned by all generic format observers is in the generic pathname format.
[Note: For POSIX, no conversion occurs, since the native format and generic format are the same. For Windows, backslashes are converted to slashes --end note]
template <class String> String generic_string(const codecvt_type& cvt=codecvt()) const;
Returns:
pathname
.Remarks: If
string_type
is a different type thanString
, conversion is performed bycvt
.
const string generic_string(const codecvt_type& cvt=codecvt()) const; const wstring generic_wstring(const codecvt_type& cvt=codecvt()) const; const u16string generic_u16string() const; const u32wstring generic_u32wstring() const;
Returns:
pathname
.Remarks: If
string_type
is a different type than function's return type, conversion is performed bycvt
.If
string_type
is of the same type as the function's return type, and the generic format is the same as the native format, the function is permitted to return byconst&
rather thanconst
value. [Note: For POSIX, this occurs forstring()
. It never occurs for Windows, because backslashes must be converted to slashes. --end note]
path
decompositionSee the Path decomposition table for examples for values returned by decomposition functions. The Tutorial may also be helpful.
path root_name() const;
Returns: root-name, if
pathname
includes root-name, otherwisepath()
.
path root_directory() const;
Returns: root-directory, if
pathname
includes root-directory, otherwisepath()
.If root-directory is composed of slash name, slash is excluded from the returned string.
path root_path() const;
Returns:
root_name() / root_directory()
path relative_path() const;
Returns: A
path
composed frompathname
, if!empty()
, beginning with the first filename after root-path. Otherwise,path()
.
path parent_path() const;
Returns:
(empty() || begin() == --end()) ? path() : pp
, wherepp
is constructed as if by starting with an emptypath
and successively applyingoperator/=
for each element in the rangebegin()
,--end()
.
path filename() const;
Returns:
empty() ? path() : *--end()
[Example:
cout << path("/foo/bar.txt").filename();
// outputs "bar.txt
" (without the quotes)--end example]
path stem(const path& p) const;
Returns: if
p.filename()
contains a dot but does not consist solely of one or to two dots, returns the substring ofp.filename()
starting at its beginning and ending at the last dot (the dot is not included). Otherwise, returnsp.filename()
.[Example:
cout << path("/foo/bar.txt").stem();
// outputs "bar
" (without the quotes)path p = "foo.bar.baz.tar"; for (; !p.extension().empty(); p = p.stem()) cout << p.extension() << '\n'; // outputs: .tar // .baz // .bar--end example]
path extension(const path& p) const;
Returns: if
p.filename()
contains a dot but does not consist solely of one or to two dots, returns the substring ofp.filename()
starting at the rightmost dot and ending at the path's end. Otherwise, returns an emptypath
object.Remarks: Implementations are permitted but not required to define additional behavior for file systems which append additional elements to extensions, such as alternate data streams or partitioned dataset names.
[Example:
cout << path("/foo/bar.txt").extension(); //
outputs ".txt
" (without the quotes)--end example]
[Note: The dot is included in the return value so that it is possible to distinguish between no extension and an empty extension. -- end note]
See http://permalink.gmane.org/gmane.comp.lib.boost.devel/199744 for more extensive rationale.
path
querybool empty() const;
Returns:
m_pathname.empty()
.
bool has_root_path() const;
Returns:
!root_path().empty()
bool has_root_name() const;
Returns:
!root_name().empty()
bool has_root_directory() const;
Returns:
!root_directory().empty()
bool has_relative_path() const;
Returns:
!relative_path().empty()
bool has_parent_path() const;
Returns:
!parent_path().empty()
bool has_filename() const;
Returns:
!filename().empty()
bool has_stem() const;
Returns:
!stem().empty()
bool has_extension() const;
Returns:
!extension().empty()
bool is_absolute() const;
Returns:
true
if the elements ofroot_path()
uniquely identify a directory, elsefalse
.[Note: On POSIX,
path("/foo").is_absolute()
returnstrue
. On Windows,path("/foo").is_absolute()
returnsfalse
. --end note]
bool is_relative() const;
Returns:
!is_absolute()
.
path
iterators A path::iterator
is a constant iterator satisfying all
the requirements of a bidirectional iterator (C++ Std, 24.1.4 Bidirectional
iterators [lib.bidirectional.iterators]). Its value_type
is
path
.
Calling any non-const member function of a path
object
invalidates all iterators referring to elements of that object.
The forward traversal order is as follows:
The root-name element, if present.
The root-directory element, if present.
Each successive filename element, if present.
Dot, if one or more trailing non-root slash characters are present.
The backward traversal order is the reverse of forward traversal.
iterator begin() const;
Returns: An iterator for the first present element in the traversal list above. If no elements are present, the end iterator.
iterator end() const;
Returns: The end iterator.
path
encoding conversionstatic locale imbue(const locale& loc);
Effects: Stores
loc
as the default locale for all objects of typepath
.Returns: The previous default locale for all objects of type
path
.
static const codecvt_type& codecvt();
Returns: The
codecvt
facet for the default locale for all objects of typepath
.
path
non-member functionsvoid swap( path& lhs, path& rhs )
Effects:
lhs.swap(rhs)
.
bool lexicographical_compare(path::iterator first1, path::iterator last1, path::iterator first2, path::iterator last2)
Returns:
true
if the sequence ofnative()
strings for the elements defined by the range[first1,last1)
is lexicographically less than the sequence ofnative()
strings for the elements defined by the range[first2,last2)
. Returnsfalse
otherwise.Remarks: If two sequences have the same number of elements and their corresponding elements are equivalent, then neither sequence is lexicographically less than the other. If one sequence is a prefix of the other, then the shorter sequence is lexicographically less than the longer sequence. Otherwise, the lexicographical comparison of the sequences yields the same result as the comparison of the first corresponding pair of elements that are not equivalent.
for ( ; first1 != last1 && first2 != last2 ; ++first1, ++first2) { if (first1->native() < first2->native()) return true; if (first2->native() < first1->native()) return false; } return first1 == last1 && first2 != last2;[Note: A
path
awarelexicographical_compare
is provided to avoid infinite recursion inlexicographical_compare
due to thepath
iterator's value type itself beingpath
. --end note]
bool operator< (const path& lhs, const path& rhs);
Returns:
return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end())
.
bool operator<=(const path& lhs, const path& rhs);
Returns:
!(rhs < lhs)
.
bool operator> (const path& lhs, const path& rhs);
Returns:
rhs < lhs
.
bool operator>=(const path& lhs, const path& rhs);
Returns:
!(lhs < rhs)
.
bool operator==(const path& lhs, const path& rhs);
Returns:
!(lhs < rhs) && !(rhs < lhs)
.[Note: Actual implementations may use an equivalent, but more efficient, algorithm. --end note]
[Note: Path equality and path equivalence have different semantics.
Equality is determined by the
path
non-memberoperator==
, which considers the two path's lexical representations only. Thuspath("foo") == "bar"
is nevertrue
.Equivalence is determined by the
equivalent()
non-member function, which determines if two paths resolve to the same file system entity. Thusequivalent("foo", "bar")
will betrue
when both paths resolve to the same file.Programmers wishing to determine if two paths are "the same" must decide if "the same" means "the same representation" or "resolve to the same actual file", and choose the appropriate function accordingly. -- end note]
bool operator!=(const path& lhs, const path& rhs);
Returns:
!(lhs == rhs)
.
path operator/ (const path& lhs, const path& rhs);
Returns:
path(lhs) /= rhs
.
path
inserter
and extractor The inserter and extractor delimit the string with double-quotes ("
)
to ensure that paths with embedded spaces will round trip correctly. Ampersand (&
)
is used as an escape character, so the path can itself contain double quotes.
template <class Char, class Traits> basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, const path& p)
Effects:
os << boost::io::quoted(p.string<basic_string<Char>>(), static_cast<Char>('&'));
Returns:
os
template <class Char, class Traits> inline basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is, path& p)
Effects:
basic_string<Char> str;
is >> boost::io::quoted(str, static_cast<Char>('&'));
p = str;Returns:
is
filesystem_error
namespace std { namespace tr2 { { namespace filesystem { class basic_filesystem_error : public system_error { public: filesystem_error(); filesystem_error(const filesystem_error&); filesystem_error(const string& what_arg, error_code ec); filesystem_error(const string& what_arg, const path& p1, error_code ec); filesystem_error(const string& what_arg, const path& p1, const path& p2, error_code ec); filesystem_error& filesystem_error(const filesystem_error&); ~filesystem_error(); filesystem_error& operator=(const filesystem_error&); const path& path1() const; const path& path2() const; const char * what() const; }; } // namespace filesystem }} // namespaces std and tr2
The class template basic_filesystem_error
defines the type of
objects thrown as exceptions to report file system errors from functions described in this
clause.
filesystem_error
membersfilesystem_error(const string& what_arg, error_code ec);
Postcondition:
Expression
Value
runtime_error::what()
what_arg.c_str()
code()
ec
path1().empty()
true
path2().empty()
true
filesystem_error(const string& what_arg, const path_type& p1, error_code ec);
Postcondition:
Expression
Value
runtime_error::what()
what_arg.c_str()
code()
ec
path1()
Reference to stored copy of
p1
path2().empty()
true
filesystem_error(const string& what_arg, const path_type& p1, const path_type& p2, error_code ec);
Postcondition:
Expression
Value
runtime_error::what()
w
hat_arg.c_str()
code()
ec
path1()
Reference to stored copy of
p1
path2()
Reference to stored copy of
p2
const path& path1() const;
Returns: Reference to copy of
p1
stored by the constructor, or, if none, an empty path.
const path& path2() const;
Returns: Reference to copy of
p2
stored by the constructor, or, if none, an empty path.
const char* what() const;
Returns: A string containing
runtime_error::what()
. The exact format is unspecified. Implementations are encouraged but not required to includepath1.native_string()
if not empty,path2.native_string()
if not empty, andsystem_error::what()
strings in the returned string.
directory_entry
namespace std { namespace tr2 { { namespace filesystem { class directory_entry { public: // constructors and destructor directory_entry(); directory_entry(const directory_entry&); explicit directory_entry(const path_type& p, file_status st=file_status(), file_status symlink_st=file_status()); ~directory_entry(); // modifiers directory_entry& operator=(const directory_entry&); void assign(const path_type& p, file_status st=file_status(), file_status symlink_st=file_status()); void replace_filename(const path& p, file_status st=file_status(), file_status symlink_st=file_status()); // observers const path& path() const; file_status status() const; file_status status(error_code& ec) const; file_status symlink_status() const; file_status symlink_status(error_code& ec) const; bool operator< (const directory_entry& rhs); bool operator==(const directory_entry& rhs); bool operator!=(const directory_entry& rhs); bool operator< (const directory_entry& rhs); bool operator<=(const directory_entry& rhs); bool operator> (const directory_entry& rhs); bool operator>=(const directory_entry& rhs); private: path_type m_path; // for exposition only mutable file_status m_status; // for exposition only; stat()-like mutable file_status m_symlink_status; // for exposition only; lstat()-like }; } // namespace filesystem }} // namespaces std and tr2
A directory_entry
object stores a path object
,
a file_status
object for non-symbolic link status, and a
file_status
object for symbolic link status. The file_status
objects act as value caches.
[Note: Because
status()
on a pathname may be a very expensive operation, some operating systems provide status information as a byproduct of directory iteration. Caching such status information can result is significant time savings. Cached and non-cached results may differ in the presence of race conditions. -- end note]Actual cold-boot timing of iteration over a directory with 15,047 entries was six seconds for non-cached status queries versus one second for cached status queries. Windows XP, 3.0 GHz processor, with a moderately fast hard-drive. Similar speedups are expected on Linux and BSD-derived systems that provide status as a by-product of directory iteration.
directory_entry
constructorsdirectory_entry();
Postcondition:
Expression
Value
path().empty()
true
status()
file_status()
symlink_status()
file_status()
explicit directory_entry(const path_type& p, file_status st=file_status(), file_status symlink_st=file_status());
Postcondition:
Expression
Value
path()
p
status()
st
symlink_status()
symlink_st
directory_entry
modifiersvoid assign(const path_type& p, file_status st=file_status(), file_status symlink_st=file_status());
Postcondition:
Expression
Value
path()
p
status()
st
symlink_status()
symlink_st
void replace_filename(const path& p, file_status st=file_status(), file_status symlink_st=file_status());
Postcondition:
Expression
Value
path()
path().branch() / s
status()
st
symlink_status()
symlink_st
directory_entry
observersconst path& path() const;
Returns:
m_path
file_status status() const; file_status status(error_code& ec) const;
Effects: As if,
if ( !status_known( m_status ) ) { if ( status_known(m_symlink_status) && !is_symlink(m_symlink_status) ) { m_status = m_symlink_status; } else { m_status = status(m_path[, ec]); } }Returns:
m_status
Throws: As specified in Error reporting.
file_status symlink_status() const; file_status symlink_status(error_code& ec) const;
Effects: As if,
if ( !status_known( m_symlink_status ) ) { m_symlink_status = symlink_status(m_path[, ec]); }Returns:
m_symlink_status
Throws: As specified in Error reporting.
bool operator==(const directory_entry& rhs);
Returns:
m_path == rhs.m_path
.
bool operator!=(const directory_entry& rhs);
Returns:
m_path != rhs.m_path
.
bool operator< (const directory_entry& rhs);
Returns:
m_path < rhs.m_path
.
bool operator<=(const directory_entry& rhs);
Returns:
m_path <= rhs.m_path
.
bool operator> (const directory_entry& rhs);
Returns:
m_path > rhs.m_path
.
bool operator>=(const directory_entry& rhs);
Returns:
m_path >= rhs.m_path
.
directory_iterator
Objects of type directory_iterator
provide standard library
compliant iteration over the contents of a directory. Also see class
recursive_directory_iterator
.
namespace std { namespace tr2 { { namespace filesystem { class directory_iterator : public boost::iterator_facade< directory_iterator, directory_entry, boost::single_pass_traversal_tag > { public: // member functions directory_iterator(); // creates the "end" iterator directory_iterator(const directory_iterator&); explicit directory_iterator(const path& p); directory_iterator(const path& p, error_code& ec); ~directory_iterator(); directory_iterator& operator=(const directory_iterator&); directory_iterator& operator++(); directory_iterator& increment(error_code& ec); // other members as required by // C++ Std, 24.1.1 Input iterators [input.iterators] }; } // namespace filesystem }} // namespaces std and tr2
directory_iterator
satisfies the requirements of an
input iterator (C++ Std, 24.2.1, Input iterators [input.iterators]).
A directory_iterator
reads successive elements from the directory for
which it was constructed, as if by calling POSIX
readdir_r()
. After a directory_iterator
is constructed, and every time
operator++
is called,
it reads a directory element and stores information about it in a object of type
directory_entry
.
operator++
is not equality preserving; that is, i == j
does not imply that
++i == ++j
.
[Note: The practical consequence of not preserving equality is that directory iterators can only be used for single-pass algorithms. --end note]
If the end of the directory elements is reached, the iterator becomes equal to
the end iterator value. The constructor directory_iterator()
with no arguments always constructs an end iterator object, which is the only
legitimate iterator to be used for the end condition. The result of
operator*
on an end iterator is not defined. For any other iterator value
a const directory_entry&
is returned. The result of
operator->
on an end iterator is not defined. For any other iterator value a const directory_entry*
is
returned.
Two end iterators are always equal. An end iterator is not equal to a non-end iterator.
The above wording is based on the Standard Library's istream_iterator wording.
The result of calling the path()
member of the
directory_entry
object obtained by dereferencing a
directory_iterator
is a reference to a path
object composed of the directory argument from which the iterator was
constructed with filename of the directory entry appended as if by
operator/=
.
Directory iteration shall not yield directory entries for the current (dot) and parent (dot dot) directories.
The order of directory entries obtained by dereferencing successive
increments of a directory_iterator
is unspecified.
[Note: Programs performing directory iteration may wish to test if the path obtained by dereferencing a directory iterator actually exists. It could be a symbolic link to a non-existent file. Programs recursively walking directory trees for purposes of removing and renaming entries may wish to avoid following symbolic links.
If a file is removed from or added to a directory after the construction of a
directory_iterator
for the directory, it is unspecified whether or not subsequent incrementing of the iterator will ever result in an iterator whose value is the removed or added directory entry. See POSIXreaddir_r()
. --end note]
directory_iterator
membersEffects: Constructs the end iterator.
Throws: Nothing.
explicit directory_iterator(
const path& p); directory_iterator(
const path& p, error_code& ec);
Effects: Constructs a iterator representing the first entry in the directory
p
resolves to, if any; otherwise, the end iterator.Throws: As specified in Error reporting.
[Note: To iterate over the current directory, use
directory_iterator(".")
rather thandirectory_iterator("")
. -- end note]
directory_iterator& operator++(); directory_iterator& increment(error_code& ec);
Effects: As specified by the C++ Standard, 24.1.1 Input iterators [input.iterators]
Returns:
*this
.Throws: As specified in Error reporting.
recursive_directory_iterator
Objects of type recursive_directory_iterator
provide standard library
compliant iteration over the contents of a directory, including recursion into
its sub-directories.
namespace std { namespace tr2 { { namespace filesystem { class recursive_directory_iterator : public iterator<input_iterator_tag, directory_entry> { public: // constructors and destructor recursive_directory_iterator(); recursive_directory_iterator(const recursive_directory_iterator&); explicit recursive_directory_iterator(const path& p, symlink_option opt = symlink_option::none); recursive_directory_iterator(const path& p, symlink_option opt, error_code& ec); recursive_directory_iterator(const path& p, error_code& ec); ~recursive_directory_iterator(); // observers int level() const; bool no_push_pending
() const; // modifiers recursive_directory_iterator& operator=(const recursive_directory_iterator&); recursive_directory_iterator& operator++(); recursive_directory_iterator& increment(error_code& ec); void pop(); void no_push(bool value=true); // other members as required by // C++ Std, 24.1.2 Input iterators [input.iterators] private: // actual data members may be stored in a shared pimpl object, or some // similar mechanism, to achieve the required input iterator copy semantics int m_level; // for exposition only bool m_no_push
; // for exposition only symlink_option m_options; // for exposition only }; } // namespace filesystem }} // namespaces std and tr2
The behavior of a recursive_directory_iterator
is the same
as a directory_iterator
unless otherwise specified.
Incrementing a recursive_directory_iterator
pointing to a
directory causes that directory itself to be iterated ovee, as specified by
the operator++
and increment
functions.
When a recursive_directory_iterator
reaches the end of the directory currently being iterated
over, or when pop()
is called, m_level
is
decremented, and iteration of the parent directory continues.
recursive_directory_iterator();
Effects: Constructs the end iterator.
Throws: Nothing.
explicit recursive_directory_iterator(const path& p, symlink_option opt = symlink_option::none); recursive_directory_iterator(const path& p, symlink_option opt, error_code& ec); recursive_directory_iterator(
const path& p, error_code& ec);
Effects: Constructs a iterator representing the first entry in the directory
p
resolves to, if any; otherwise, the end iterator.Postcondition: Unless the end iterator was constructed,
level() == 0 && no_push_pending() == false && m_options == opt
. For the signature without asymlink_option
argument,opt
is assumed to besymlink_option::none
.Throws: As specified in Error reporting.
[Note: To iterate over the current directory, use
recursive_directory_iterator(".")
rather thanrecursive_directory_iterator("")
. -- end note][Note: By default,
recursive_directory_iterator
does not follow directory symlinks. To follow directory symlinks, specifyopt
assymlink_option::recurse
-- end note]
int level() const;
Requires:
*this != recursive_directory_iterator()
.Returns:
m_level
.Throws: Nothing.
bool no_push_pending
() const;
Requires:
*this != recursive_directory_iterator()
.Returns:
m_no_push
.Throws: Nothing.
recursive_directory_iterator
& operator++();
recursive_directory_iterator& increment(error_code& ec);
Effects: As specified by the C++ Standard, 24.1.1 Input iterators [input.iterators], except:
if
!no_push_pending() && is_directory(this->status()) && (!is_symlink(this->symlink_status()) || (m_options & symlink_option::recurse) != 0)
thenm_level
is incremented and directory(*this)->path()
is recursively iterated into.
if there are no more directory entries at this level then
m_level
is decremented and iteration of the parent directory resumes.Postcondition:
no_push_pending() == false
.Returns:
*this
.Throws: As specified in Error reporting.
void pop();
Requires:
*this != recursive_directory_iterator()
.Effects: If
level() == 0
, set*this
torecursive_directory_iterator()
. Otherwise,--m_level
, cease iteration of the directory currently being iterated over, and continue iteration over the parent directory.Throws: Nothing.
void no_push(bool value=true);
Requires:
*this != recursive_directory_iterator()
.Postcondition:
no_push_pending() == value
.Throws: Nothing.
[Note:
no_push()
is used to prevent unwanted recursion into a directory. --end note]
namespace std { namespace tr2 { { namespace filesystem { class file_status { public: file_status(); file_status(const file_status&); explicit file_status(file_type v=status_error); ~file_status(); file_type type() const; void type(file_type v); }; } // namespace filesystem } // namespace std { namespace tr2 {
An object of type file_status
stores information about the status of a
file. The internal form of the stored information is unspecified.
[Note: The class may be extended in the future to store additional status information. --end note]
explicit file_status(file_type v=status_error);
Effects: Stores
v
.Throws: Nothing.
file_type type() const;
Returns: The stored
file_type
.
void type(file_type v);
Effects: Stores
v
, replacing the previously stored value.
Operational functions query or modify files, including directories, in external storage.
Operational functions access a file by resolving an
object of class path
to a particular file in a file hierarchy. The
path is resolved as if by the POSIX
Pathname Resolution mechanism.
[Note: Because hardware failures, network failures, race conditions, and many other kinds of errors occur frequently in file system operations, users should be aware that any filesystem operational function, no matter how apparently innocuous, may encounter an error. See Error reporting. -- end note]
path absolute(const path& p, const path& base=current_path());
Returns: A path composed according to to the following table
p.has_root_directory()
!p.has_root_directory()
p.has_root_name()
return p
return p.root_name() / absolute(base).root_directory()
/ absolute(base).relative_path() / p.relative_path()
!p.has_root_name()
return absolute(base).root_name()
/ p
return absolute(base) / p
Postcondition: For the returned path,
rp,
rp.is_absolute()
is true.Throws: If
base.is_absolute()
is true, throws only if memory allocation fails.
void copy(const path& from, const path& to); void copy(const path& from, const path& to, error_code& ec);
Effects: As if
file_status s(symlink_status(from[, ec
])); if(is_symlink(s)) copy_symlink(from, to[, ec
]); else if(is_directory(s)) copy_directory(from, to[, ec
]); else if(is_regular_file(s)) copy_file(from, to, copy_option::fail_if_exists[, ec
]); else Report error as specified in Error reporting.Throws: As specified in Error reporting.
void copy_directory(const path& from, const path& to); void copy_directory(const path& from, const path& to, error_code& ec);
Effects:
Throws: As specified in Error reporting.
void copy_file(const path& from, const path& to); void copy_file(const path& from, const path& to, error_code& ec);
Effects:
copy_file(from, to, copy_option::fail_if_exists
[, ec
])
.Throws: As specified in Error reporting.
void copy_file(const path& from, const path& to, copy_option option); void copy_file(const path& from, const path& to, copy_option option, error_code& ec);
Effects: If
option == copy_option::
fail_if_exists && exists(to)
, an error is reported. Otherwise, the contents and attributes of the filefrom
resolves to are copied to the fileto
resolves to.Throws: As specified in Error reporting.
void copy_symlink(const path& existing_symlink, const path& new_symlink); void copy_symlink(const path& existing_symlink, const path& new_symlink, error_code& ec);
Effects:
create_symlink(read_symlink(existing_symlink
[, ec
]), new_symlink
[, ec
])
.Throws: As specified in Error reporting.
bool create_directories(const path& p); bool create_directories(const path& p, error_code& ec);
Requires:
p.empty() ||
forall px: px == p || is_parent(px, p): is_directory(px) || !exists( px )Postcondition:
is_directory(p)
Returns: The value of
!exists(p)
prior to the establishment of the postcondition.Throws: As specified in Error reporting.
bool create_directory(const path& p); bool create_directory(const path& p, error_code& ec);
Effects: Attempts to create the directory
p
resolves to, as if by POSIXmkdir()
with a second argument of S_IRWXU|S_IRWXG|S_IRWXO.Postcondition:
is_directory(p)
Returns:
true
if a new directory was created, otherwisefalse
.Throws: As specified in Error reporting.
void create_directory_symlink(const path& to, const path& new_symlink); void create_directory_symlink(const path& to, const path& new_symlink, error_code& ec);
Effects: Establishes the postcondition, as if by POSIX
symlink()
.Postcondition:
new_symlink
resolves to a symbolic link file that contains an unspecified representation ofto
.Throws: As specified in Error reporting.
[Note: Some operating systems, such as Windows, require symlink creation to identify that the link is to a directory. Portable code should use
create_directory_symlink()
to create directory symlinks rather thancreate_symlink()
-- end note][Note: Some operating systems do not support symbolic links at all or support them only for regular files. Windows prior to Vista, for example, did not support symbolic links. Some file systems do not support symbolic links regardless of the operating system - the FAT system used on floppy discs, memory cards and flash drives, for example. Thus symbolic links should only be used if these situations are not concerns, or if workarounds are provided. -- end note]
void create_hard_link(const path& to, const path& new_hard_link); void create_hard_link(const path& to, const path& new_hard_link, error_code& ec);
Effects: Establishes the postcondition, as if by POSIX
link()
.Postcondition:
exists(to) && exists(
new_hard_link
) && equivalent(to,
new_hard_link
)
The contents of the file or directory
to
resolves to are unchanged.Throws: As specified in Error reporting.
[Note: Some operating systems do not support hard links at all or support them only for regular files. Some file systems do not support hard links regardless of the operating system - the FAT system used on floppy discs, memory cards and flash drives, for example. Some file systems limit the number of links per file. Thus hard links should only be used if these situations are not concerns, or if workarounds are provided. -- end note]
void create_symlink(const path& to, const path& new_symlink); void create_symlink(const path& to, const path& new_symlink, error_code& ec);
Effects: Establishes the postcondition, as if by POSIX
symlink()
.Postcondition:
new_symlink
resolves to a symbolic link file that contains an unspecified representation ofto
.Throws: As specified in Error reporting.
[Note: Some operating systems do not support symbolic links at all or support them only for regular files. Windows prior to Vista, for example, did not support symbolic links. Some file systems do not support symbolic links regardless of the operating system - the FAT system used on floppy discs, memory cards and flash drives, for example. Thus symbolic links should only be used if these situations are not concerns, or if workarounds are provided. -- end note]
path current_path(); path current_path(error_code& ec);
Returns: The current working directory path, as if by POSIX
getcwd()
.is_absolute()
is true for the returned path.Throws: As specified in Error reporting.
[Note: The
current_path()
name was chosen to emphasize that the return is a path, not just a single directory name.The current path as returned by many operating systems is a dangerous global variable. It may be changed unexpectedly by a third-party or system library functions, or by another thread. -- end note]
void current_path(const path& p); void current_path(const path& p, error_code& ec);
Effects: Establishes the postcondition, as if by POSIX
chdir()
.Postcondition:
equivalent(p, current_path())
.Throws: As specified in Error reporting.
[Note: The current path for many operating systems is a dangerous global state. It may be changed unexpectedly by a third-party or system library functions, or by another thread. -- end note]
bool exists(file_status s);
Returns:
status_known(s) && s.type() != file_not_found
Throws: Nothing.
bool exists(const path& p); bool exists(const path& p, error_code& ec);
Returns:
exists(status(p))
orexists(status(p, ec))
, respectively.Throws:
filesystem_error
; overload witherror_code&
throws nothing.
bool equivalent(const path& p1, const path& p2);
bool equivalent(const path& p1, const path& p2, error_code& ec);
Effects: Determines
file_status s1
ands2
, as if bystatus(p1)
andstatus(p2)
, respectively.Returns:
true
, ifsf1 == sf2
andp1
andp2
resolve to the same file system entity, elsefalse
.Two paths are considered to resolve to the same file system entity if two candidate entities reside on the same device at the same location. This is determined as if by the values of the POSIX
stat
structure,
obtained as if bystat()
for the two paths, having equalst_dev
values and equalst_ino
values.[Note: POSIX requires that "st_dev must be unique within a Local Area Network". Conservative POSIX implementations may also wish to check for equal
st_size
andst_mtime
values. Windows implementations may useGetFileInformationByHandle()
as a surrogate forstat()
, and consider "same" to be equal values fordwVolumeSerialNumber
,nFileIndexHigh
,nFileIndexLow
,nFileSizeHigh
,nFileSizeLow
,ftLastWriteTime.dwLowDateTime
, andftLastWriteTime.dwHighDateTime
. -- end note]Throws:
filesystem_error
if(!exists(s1) && !exists(s2)) || (is_other(s1) && is_other(s2))
, otherwise as specified in Error reporting.
uintmax_t file_size(const path& p); uintmax_t file_size(const path& p, error_code& ec);
Remarks:
Returns: If
exists(p) && is_regular_file(p)
, the size in bytes of the filep
resolves to, determined as if by the value of the POSIXstat
structure memberst_size
obtained as if by POSIXstat()
. Otherwise,static_cast<uintmax_t>(-1)
.Throws: As specified in Error reporting.
uintmax_t hard_link_count(const path& p); uintmax_t hard_link_count(const path& p, error_code& ec);
Returns: The number of hard links for
p
.Throws: As specified in Error reporting.
const path& initial_path();
const path& initial_path(error_code& ec
);
Returns:
current_path()
as of the first call toinitial_path()
.[Note:
initial_path()
is not thread safe, and may return an undesirable result if called subsequent to a change to the current directory. These problems can be avoided by callinginitial_path()
immediately on entry to main(). --end note]Throws: For the first call, as specified in Error reporting. Subsequent calls throw nothing.
bool is_directory
(file_status s);
Returns:
s.type() == directory_file
Throws: Nothing.
bool is_directory(const path& p);
bool is_directory(const path& p, error_code& ec);
Returns:
is_directory(status(p))
oris_directory(status(p, ec))
, respectively.Throws:
filesystem_error
; overload witherror_code&
throws nothing.
bool is_empty(const path& p);
bool is_empty(const path& p, error_code& ec);
Effects: Determines
file_status s
, as if bystatus(p, ec)
.Returns:
is_directory(s)
? directory_iterator(p) == directory_iterator()
: file_size(p) == 0;
bool is_regular_file
(file_status s);
Returns:
s.type() == regular_file
Throws: Nothing.
bool is_regular_file(const path& p);
Returns:
is_regular_file(status(p))
.Throws:
filesystem_error
ifstatus(p)
would throwfilesystem_error.
bool is_regular_file(const path& p, error_code& ec);
Effects: Sets
ec
as if bystatus(p, ec)
. [Note:status_error
,file_not_found
andtype_unknown
cases setec
to error values. To distinguish between cases, call thestatus
function directly. -- end note]Returns:
is_regular_file(status(p, ec))
.Throws: Nothing.
bool is_other(file_status s);
Returns:
return exists(s) && !is_regular_file(s) && !is_directory(s) && !is_symlink(s)
Throws: Nothing.
bool is_other(const path& p);
bool is_other(const path& p, error_code& ec);
Returns:
is_other(status(p))
oris_other(status(p, ec))
, respectively.Throws:
filesystem_error
; overload witherror_code&
throws nothing.
bool is_symlink(file_status s);
Returns:
s.type() == symlink_file
Throws: Nothing.
bool is_symlink(const path& p);
bool is_symlink(const path& p, error_code& ec);
Returns:
is_symlink(symlink_status(p))
oris_symlink(symlink_status(p, ec))
, respectively.Throws:
filesystem_error
; overload witherror_code&
throws nothing.
time_t last_write_time(const path& p);
time_t last_write_time(const path& p, error_code& ec
);
Returns: The time of last data modification of
p
, determined as if by the value of the POSIXstat
structure memberst_mtime
obtained as if by POSIXstat()
.
void last_write_time(const path& p, const time_t new_time);
void last_write_time(const path& p, const time_t new_time, error_code& ec
);
Effects: Sets the time of last data modification of the file resolved to by
p
tonew_time
, as if by POSIXstat()
followed by POSIXutime()
.Throws: As specified in Error reporting.
[Note: A postcondition of
last_write_time(p) == new_time
is not specified since it might not hold for file systems with coarse time granularity. -- end note]
path read_symlink(const path& p); path read_symlink(const path& p, error_code& ec);
Returns: If
p
resolves to a symbolic link, apath
object containing the contents of that symbolic link. Otherwise an emptypath
object.Throws: As specified in Error reporting. [Note: It is an error if
p
does not resolve to a symbolic link. -- end note]
bool remove(const path& p); bool remove(const path& p, error_code& ec);
Effects: If
exists(symlink_status(p,ec))
, it is removed as if by POSIXremove()
.[Note: A symbolic link is itself removed, rather than the file it resolves to being removed. -- end note]
Postcondition:
!exists(symlink_status(p))
.Returns:
false
if p did not exist in the first place, otherwisetrue
.Throws: As specified in Error reporting.
uintmax_t remove_all(const path& p); uintmax_t remove_all(const path& p, error_code& ec);
Effects: Recursively deletes the contents of p if it exists, then deletes file
p
itself, as if by POSIXremove()
.[Note: A symbolic link is itself removed, rather than the file it resolves to being removed. -- end note]
Postcondition:
!exists(p)
Returns: The number of files removed.
Throws: As specified in Error reporting.
void rename(const path& old_p, const path& new_p); void rename(const path& old_p, const path& new_p, error_code& ec);
Effects: Renames
old_p
tonew_p
, as if by POSIXrename()
.[Note: If
old_p
andnew_p
resolve to the same existing file, no action is taken. Otherwise, ifnew_p
resolves to an existing non-directory file, it is removed, while ifnew_p
resolves to an existing directory, it is removed if empty on POSIX but is an error on Windows. A symbolic link is itself renamed, rather than the file it resolves to being renamed. -- end note]Throws: As specified in Error reporting.
void resize_file(const path& p, uintmax_t new_size); void resize_file(const path& p, uintmax_t new_size, error_code& ec);
Postcondition:
file_size() == new_size
.Throws: As specified in Error reporting.
Remarks: Achieves its postconditions as if by POSIX
truncate()
.
space_info space(const path& p); space_info space(const path& p, error_code& ec);
Returns: An object of type
space_info
. The value of thespace_info
object is determined as if by using POSIXstatvfs()
to obtain a POSIX structstatvfs
, and then multiplying itsf_blocks
,f_bfree
, andf_bavail
members by itsf_frsize
member, and assigning the results to thecapacity
,free
, andavailable
members respectively. Any members for which the value cannot be determined shall be set to -1.Throws: As specified in Error reporting.
file_status status(const path& p);
Effects: As if:
error_code ec; file_status result = status(p, ec); if (result == status_error) throw filesystem_error(implementation-supplied-message, p, ec); return result;Returns: See above.
Throws:
filesystem_error
. [Note:result
values offile_status(file_not_found)
andfile_status(type_unknown)
are not considered failures and do not cause an exception to be thrown. -- end note]
file_status status(const path& p, error_code& ec);
Effects:
If possible, determines the attributes of the file
p
resolves to, as if by POSIXstat()
.If, during attribute determination, the underlying file system API reports an error, sets
ec
to indicate the specific error reported. Otherwise,ec.clear()
.[Note: This allows users to inspect the specifics of underlying API errors even when the value returned by
status()
is notfile_status(status_error)
. --end note]Returns:
If
ec != error_code()
:
If the specific error indicates that
p
cannot be resolved because some element of the path does not exist, returnfile_status(file_not_found)
. [Note: POSIX errors that indicate this are ENOENT or ENOTDIR. Windows equivalents include ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, ERROR_INVALID_NAME, ERROR_INVALID_PARAMETER, ERROR_BAD_PATHNAME, and ERROR_BAD_NETPATH. -- end note]
Otherwise, if the specific error indicates that
p
can be resolved but the attributes cannot be determined, returnfile_status(type_unknown)
. [Note: For example, Windows ERROR_SHARING_VIOLATION errors. For POSIX, the case never arises. -- end note]
Otherwise, return
file_status(status_error)
.[Note: These semantics distinguish between
p
being known not to exist,p
existing but not being able to determine its attributes, and there being an error that prevents even knowing ifp
exists. These distinctions are important to some use cases. --end note]Otherwise,
If the attributes indicate a regular file, as if by POSIX S_ISREG(), return
file_status(regular_file)
. [Note:regular_file
implies appropriate<fstream>
operations would succeed, assuming no hardware, permission, access, or race condition errors. Lack ofregular_file
does not necessarily imply<fstream>
operations would fail on a directory. -- end note]
Otherwise, if the attributes indicate a directory, as if by POSIX S_ISDIR(), return
file_status(directory_file)
. [Note:directory_file
impliesdirectory_iterator(p)
would succeed. -- end note]
Otherwise, if the attributes indicate a block special file, as if by POSIX S_ISBLK(), return
file_status(block_file)
.
Otherwise, if the attributes indicate a character special file, as if by POSIX S_ISCHR(), return
file_status(character_file)
.
Otherwise, if the attributes indicate a fifo or pipe file, as if by POSIX S_ISFIFO(), return
file_status(fifo_file)
.
Otherwise, if the attributes indicate a socket, as if by POSIX S_ISSOCK(), return
file_status(socket_file)
.
Otherwise, return
file_status(type_unknown)
.Throws: Nothing.
Remarks: If a symbolic link is encountered during pathname resolution, pathname resolution continues using the contents of the symbolic link.
bool status_known(file_status s);
Returns:
s.type() != status_error
Throws: Nothing.
file_status symlink_status(const path& p); file_status symlink_status(const path& p, error_code& ec);
Effects: Same as status(), above, except that the attributes of
p
are determined as if by POSIXlstat()
.
Returns: Same as status(), above, except that if the attributes indicate a symbolic link, as if by POSIX S_ISLNK(), return
file_status(symlink_file)
.Throws: Nothing.
Remarks: Pathname resolution terminates if
p
names a symbolic link.
path system_complete(const path& p); path system_complete(const path& p, error_code& ec);
Effects: Composes an absolute path from
p
, using the same rules used by the operating system to resolve a path passed as the filename argument to standard library open functions.Returns: The composed path.
Postcondition: For the returned path,
rp,
rp.is_absolute()
is true.[Note: For POSIX,
system_complete(p)
has the same semantics ascomplete(p, current_path())
.For Windows,
system_complete(p)
has the same semantics ascomplete(ph, current_path())
ifp.is_absolute() || !p.has_root_name()
orp
andbase
have the sameroot_name()
. Otherwise it acts likecomplete(p, kinky)
, wherekinky
is the current directory for thep.root_name()
drive. This will be the current directory of that drive the last time it was set, and thus may be residue left over from a prior program run by the command processor! Although these semantics are often useful, they are also very error-prone.See complete() note for usage suggestions. -- end note]
path temp_directory_path(); path temp_directory_path(error_code& ec);
Returns: A directory path suitable for temporary files under the conventions of the operating system. The specifics of how this path is determined are implementation defined. An error shall be reported if
!exists(p) || !is_directory(p)
, wherep
is the path to be returned.POSIX: The path supplied by the first environment variable found in the list TMPDIR, TMP, TEMP, TEMPDIR. If none of these are found,
"/tmp"
.Windows: The path reported by the Windows
GetTempPath
API function.Throws: As specified in Error reporting.
[Note: The
temp_directory_path()
name was chosen to emphasize that the return is a path, not just a single directory name. -- end note]
path unique_path(const path& model="%%%%-%%%%-%%%%-%%%%"); path unique_path(const path& model, error_code& ec);
The
unique_path
function generates a path name suitable for creating temporary files, including directories. The name is based on a model that uses the percent sign character to specify replacement by a random hexadecimal digit. [Note: The more bits of randomness in the generated path name, the less likelihood of prior existence or being guessed. Each replacement hexadecimal digit in the model adds four bits of randomness. The default model thus provides 64 bits of randomness. This is sufficient for most applications. --end note]Returns: A path identical to
model
, except that each occurrence of a percent sign character is replaced by a random hexadecimal digit character in the range 0-9, a-f.Throws: As specified in Error reporting.
Remarks: Implementations are encouraged to obtain the required randomness via a cryptographically secure pseudo-random number generator, such as one provided by the operating system. [Note: Such generators may block until sufficient entropy develops. --end note]
<fstream>
Replacements are provided for the file stream classes from the C++ standard
library's <fstream>
header. These replacement classes publicly
inherit from the standard library classes. Constructors and open functions take
const path&
arguments. There are no other differences in syntax or
semantics.
namespace std { namespace tr2 { namespace filesystem { template < class charT, class traits = char_traits<charT> > class basic_filebuf : public std::basic_filebuf<charT,traits> { public: basic_filebuf<charT,traits>* open(const path& p, std::ios_base::openmode mode); }; template < class charT, class traits = char_traits<charT> > class basic_ifstream : public std::basic_ifstream<charT,traits> { public: explicit basic_ifstream(const path& p, std::ios_base::openmode mode=ios_base::in) void open(const path& p, std::ios_base::openmode mode=std::ios_base::in); }; template < class charT, class traits = char_traits<charT> > class basic_ofstream : public std::basic_ofstream<charT,traits> { public: explicit basic_ofstream(const path& p, std::ios_base::openmode mode=ios_base::out); void open(const path& p, std::ios_base::openmode mode=std::ios_base::out); }; template < class charT, class traits = char_traits<charT> > class basic_fstream : public std::basic_fstream<charT,traits> { public: explicit basic_fstream(const path& p, std::ios_base::openmode mode=std::ios_base::in | std::ios_base::out); void open(const path& p, std::ios_base::openmode mode=std::ios_base::in | std::ios_base::out); }; typedef basic_filebuf<char> filebuf; typedef basic_ifstream<char> ifstream; typedef basic_ofstream<char> ofstream; typedef basic_fstream<char> fstream; typedef basic_filebuf<wchar_t> wfilebuf; typedef basic_ifstream<wchar_t> wifstream; typedef basic_fstream<wchar_t> wfstream; typedef basic_ofstream<wchar_t> wofstream; } // namespace filesystem }} // namespaces std and tr2
End of the proposed wording
The table is generated by a program compiled with the Boost implementation.
Shaded entries indicate cases where POSIX and Windows implementations yield different results. The top value is the POSIX result and the bottom value is the Windows result.
Path Decomposition Table
Constructor |
Iteration |
|
|
|
|
|
|
|
|
empty |
empty |
empty |
empty |
empty |
empty |
empty |
empty |
empty |
empty |
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
|
empty |
|
empty |
empty |
|
|
|
|
|
|
empty |
|
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
|
empty |
|
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
|
empty |
|
|
|
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
empty |
|
|
|
|
|
|
|
|
|
empty |
|
|
|
|
|
|
|
|
|
empty |
|
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
|
empty |
|
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
empty |
|
|
|
|
|
empty |
empty |
empty |
|
|
|
|
|
|
|
empty |
empty |
empty |
|
|
|
This Filesystem Library is dedicated to my wife, Sonda, who provided the support necessary to see both a trial implementation and the proposal itself through to completion. She gave me the strength to continue after a difficult year of cancer treatment in the middle of it all.
Many people contributed technical comments, ideas, and suggestions to the Boost Filesystem Library.
Dietmar Kuehl contributed the original Boost Filesystem Library directory_iterator design. Peter Dimov, Walter Landry, Rob Stewart, and Thomas Witt were particularly helpful in refining the library.
The create_directories, extension, basename, and replace_extension functions were developed by Vladimir Prus. The temp_directory_path function was contributed by Jeff Flinn.
Howard Hinnant and John Maddock reviewed a draft of the version 2 proposal, and identified a number of mistakes or weaknesses, resulting in a more polished final document.
Peter Dimov suggested a single class path, with member templates to adapt to multiple string types. His idea became the basis for the version 3 path design.