[fs.op.funcs] (original) (raw)

31 Input/output library [input.output]

31.12 File systems [filesystems]

31.12.13 Filesystem operation functions [fs.op.funcs]

31.12.13.1 General [fs.op.funcs.general]

Filesystem operation functions query or modify files, including directories, in external storage.

[Note 1:

Because hardware failures, network failures, file system races ([fs.race.behavior]), and many other kinds of errors occur frequently in file system operations, any filesystem operation function, no matter how apparently innocuous, can encounter an error; see [fs.err.report].

— _end note_]

31.12.13.2 Absolute [fs.op.absolute]

path filesystem::absolute(const path& p); path filesystem::absolute(const path& p, error_code& ec);

Effects: Composes an absolute path referencing the same file system location as p according to the operating system ([fs.conform.os]).

Returns: The composed path.

The signature with argument ec returns path() if an error occurs.

[Note 1:

For the returned path, rp, rp.is_absolute() is trueunless an error occurs.

— _end note_]

[Note 2:

To resolve symlinks or perform other sanitization that can involve queries to secondary storage, such as hard disks, consider canonical ([fs.op.canonical]).

— _end note_]

[Note 3:

Implementations are strongly encouraged to not query secondary storage, and not consider !exists(p) an error.

— _end note_]

[Example 1:

For POSIX-based operating systems,absolute(p) is simply current_path()/p.

For Windows-based operating systems,absolute might have the same semantics as GetFullPathNameW.

— _end example_]

31.12.13.3 Canonical [fs.op.canonical]

path filesystem::canonical(const path& p); path filesystem::canonical(const path& p, error_code& ec);

Effects: Converts p to an absolute path that has no symbolic link, dot, or dot-dot elements in its pathname in the generic format.

Returns: A path that refers to the same file system object as absolute(p).

The signature with argument ec returns path() if an error occurs.

Remarks: !exists(p) is an error.

31.12.13.4 Copy [fs.op.copy]

void filesystem::copy(const path& from, const path& to);

Effects: Equivalent tocopy(from, to, copy_options​::​none).

void filesystem::copy(const path& from, const path& to, error_code& ec);

Effects: Equivalent tocopy(from, to, copy_options​::​none, ec).

void filesystem::copy(const path& from, const path& to, copy_options options);void filesystem::copy(const path& from, const path& to, copy_options options, error_code& ec);

Preconditions: At most one element from each option group ([fs.enum.copy.opts]) is set in options.

Effects: Before the first use of f and t:

Effects are then as follows:

Remarks: For the signature with argument ec, any library functions called by the implementation shall have an error_code argument if applicable.

[Example 1:

Given this directory structure:

/dir1 file1 file2 dir2 file3

Calling copy("/dir1", "/dir3") would result in:

/dir1 file1 file2 dir2 file3 /dir3 file1 file2

Alternatively, calling copy("/dir1", "/dir3", copy_options​::​recursive) would result in:

/dir1 file1 file2 dir2 file3 /dir3 file1 file2 dir2 file3

— _end example_]

31.12.13.5 Copy file [fs.op.copy.file]

bool filesystem::copy_file(const path& from, const path& to);bool filesystem::copy_file(const path& from, const path& to, error_code& ec);

Returns: copy_file(from, to, copy_options​::​none) or

copy_file(from, to, copy_options​::​none, ec), respectively.

bool filesystem::copy_file(const path& from, const path& to, copy_options options);bool filesystem::copy_file(const path& from, const path& to, copy_options options, error_code& ec);

Preconditions: At most one element from each option group ([fs.enum.copy.opts]) is set in options.

Effects: As follows:

Returns: true if the from file was copied, otherwise false.

The signature with argument ec returnsfalse if an error occurs.

Complexity: At most one direct or indirect invocation of status(to).

void filesystem::copy_symlink(const path& existing_symlink, const path& new_symlink);void filesystem::copy_symlink(const path& existing_symlink, const path& new_symlink, error_code& ec) noexcept;

Effects: Equivalent to_function_(read_symlink(existing_symlink), new_symlink) or

function(read_symlink(existing_symlink, ec), new_symlink, ec), respectively, where in each case function is create_symlink orcreate_directory_symlink as appropriate.

31.12.13.7 Create directories [fs.op.create.directories]

bool filesystem::create_directories(const path& p);bool filesystem::create_directories(const path& p, error_code& ec);

Effects: Calls create_directory for each element of p that does not exist.

Returns: true if a new directory was created for the directory p resolves to, otherwise false.

Complexity: where n is the number of elements of p.

31.12.13.8 Create directory [fs.op.create.directory]

bool filesystem::create_directory(const path& p);bool filesystem::create_directory(const path& p, error_code& ec) noexcept;

Effects: Creates the directory p resolves to, as if by POSIX mkdir with a second argument ofstatic_cast<int>(perms​::​all).

If mkdir fails because p resolves to an existing directory, no error is reported.

Otherwise on failure an error is reported.

Returns: true if a new directory was created, otherwise false.

bool filesystem::create_directory(const path& p, const path& existing_p);bool filesystem::create_directory(const path& p, const path& existing_p, error_code& ec) noexcept;

Effects: Creates the directory p resolves to, with attributes copied from directory existing_p.

The set of attributes copied is operating system dependent.

If mkdir fails because p resolves to an existing directory, no error is reported.

Otherwise on failure an error is reported.

[Note 1:

For POSIX-based operating systems, the attributes are those copied by native API stat(existing_p.c_str(), &attributes_stat) followed by mkdir(p.c_str(), attributes_stat.st_mode).

For Windows-based operating systems, the attributes are those copied by native API CreateDirectoryExW(existing_p.c_str(), p.c_str(), 0).

— _end note_]

Returns: true if a new directory was created with attributes copied from directory existing_p, otherwise false.

void filesystem::create_directory_symlink(const path& to, const path& new_symlink);void filesystem::create_directory_symlink(const path& to, const path& new_symlink, error_code& ec) noexcept;

Effects: Establishes the postcondition, as if by POSIX symlink.

Postconditions: new_symlink resolves to a symbolic link file that contains an unspecified representation of to.

[Note 1:

Some operating systems require symlink creation to identify that the link is to a directory.

Thus, create_symlink (instead of create_directory_symlink) cannot be used reliably to create directory symlinks.

— _end note_]

[Note 2:

Some operating systems do not support symbolic links at all or support them only for regular files.

Some file systems (such as the FAT file system) do not support symbolic links regardless of the operating system.

— _end note_]

void filesystem::create_hard_link(const path& to, const path& new_hard_link);void filesystem::create_hard_link(const path& to, const path& new_hard_link, error_code& ec) noexcept;

Effects: Establishes the postcondition, as if by POSIX link.

Postconditions:

[Note 1:

Some operating systems do not support hard links at all or support them only for regular files.

Some file systems (such as the FAT file system) do not support hard links regardless of the operating system.

Some file systems limit the number of links per file.

— _end note_]

void filesystem::create_symlink(const path& to, const path& new_symlink);void filesystem::create_symlink(const path& to, const path& new_symlink, error_code& ec) noexcept;

Effects: Establishes the postcondition, as if by POSIX symlink.

Postconditions: new_symlink resolves to a symbolic link file that contains an unspecified representation of to.

[Note 1:

Some operating systems do not support symbolic links at all or support them only for regular files.

Some file systems (such as the FAT file system) do not support symbolic links regardless of the operating system.

— _end note_]

31.12.13.12 Current path [fs.op.current.path]

path filesystem::current_path(); path filesystem::current_path(error_code& ec);

Returns: The absolute path of the current working directory, whose pathname in the native format is obtained as if by POSIX getcwd.

The signature with argument ec returns path() if an error occurs.

Remarks: The current working directory is the directory, associated with the process, that is used as the starting location in pathname resolution for relative paths.

[Note 1:

The current path as returned by many operating systems is a dangerous global variable and can be changed unexpectedly by third-party or system library functions, or by another thread.

— _end note_]

void filesystem::current_path(const path& p);void filesystem::current_path(const path& p, error_code& ec) noexcept;

Effects: Establishes the postcondition, as if by POSIX chdir.

Postconditions: equivalent(p, current_path()).

[Note 2:

The current path for many operating systems is a dangerous global state and can be changed unexpectedly by third-party or system library functions, or by another thread.

— _end note_]

31.12.13.13 Equivalent [fs.op.equivalent]

bool filesystem::equivalent(const path& p1, const path& p2);bool filesystem::equivalent(const path& p1, const path& p2, error_code& ec) noexcept;

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.

[Note 1:

On POSIX platforms, this is determined as if by the values of the POSIX stat class, obtained as if by stat for the two paths, having equal st_dev values and equal st_ino values.

— _end note_]

Returns: true, if p1 and p2 resolve to the same file system entity, otherwise false.

The signature with argument ec returns false if an error occurs.

Remarks: !exists(p1) || !exists(p2) is an error.

31.12.13.14 Exists [fs.op.exists]

bool filesystem::exists(file_status s) noexcept;

Returns: status_known(s) && s.type() != file_type​::​not_found.

bool filesystem::exists(const path& p);bool filesystem::exists(const path& p, error_code& ec) noexcept;

Let s be a file_status, determined as if by status(p) or status(p, ec), respectively.

Effects: The signature with argument ec calls ec.clear()if status_known(s).

31.12.13.15 File size [fs.op.file.size]

uintmax_t filesystem::file_size(const path& p); uintmax_t filesystem::file_size(const path& p, error_code& ec) noexcept;

Returns:

The signature with argument ec returns static_cast<uintmax_t>(-1)if an error occurs.

uintmax_t filesystem::hard_link_count(const path& p); uintmax_t filesystem::hard_link_count(const path& p, error_code& ec) noexcept;

Returns: The number of hard links for p.

The signature with argument ec returns static_cast<uintmax_t>(-1) if an error occurs.

31.12.13.17 Is block file [fs.op.is.block.file]

bool filesystem::is_block_file(file_status s) noexcept;

Returns: s.type() == file_type​::​block.

bool filesystem::is_block_file(const path& p);bool filesystem::is_block_file(const path& p, error_code& ec) noexcept;

Returns: is_block_file(status(p)) or is_block_file(status(p, ec)), respectively.

The signature with argument ec returns false if an error occurs.

31.12.13.18 Is character file [fs.op.is.char.file]

bool filesystem::is_character_file(file_status s) noexcept;

Returns: s.type() == file_type​::​character.

bool filesystem::is_character_file(const path& p);bool filesystem::is_character_file(const path& p, error_code& ec) noexcept;

Returns: is_character_file(status(p)) or is_character_file(status(p, ec)), respectively.

The signature with argument ec returns false if an error occurs.

31.12.13.19 Is directory [fs.op.is.directory]

bool filesystem::is_directory(file_status s) noexcept;

Returns: s.type() == file_type​::​directory.

bool filesystem::is_directory(const path& p);bool filesystem::is_directory(const path& p, error_code& ec) noexcept;

Returns: is_directory(status(p)) or is_directory(status(p, ec)), respectively.

The signature with argumentec returns false if an error occurs.

31.12.13.20 Is empty [fs.op.is.empty]

bool filesystem::is_empty(const path& p);bool filesystem::is_empty(const path& p, error_code& ec);

Effects:

31.12.13.21 Is fifo [fs.op.is.fifo]

bool filesystem::is_fifo(file_status s) noexcept;

Returns: s.type() == file_type​::​fifo.

bool filesystem::is_fifo(const path& p);bool filesystem::is_fifo(const path& p, error_code& ec) noexcept;

Returns: is_fifo(status(p)) or is_fifo(status(p, ec)), respectively.

The signature with argument ec returns false if an error occurs.

31.12.13.22 Is other [fs.op.is.other]

bool filesystem::is_other(file_status s) noexcept;

Returns: exists(s) && !is_regular_file(s) && !is_directory(s) && !is_symlink(s).

bool filesystem::is_other(const path& p);bool filesystem::is_other(const path& p, error_code& ec) noexcept;

Returns: is_other(status(p)) or is_other(status(p, ec)), respectively.

The signature with argument ec returns false if an error occurs.

31.12.13.23 Is regular file [fs.op.is.regular.file]

bool filesystem::is_regular_file(file_status s) noexcept;

Returns: s.type() == file_type​::​regular.

bool filesystem::is_regular_file(const path& p);

Returns: is_regular_file(status(p)).

Throws: filesystem_error if status(p) would throw filesystem_error.

bool filesystem::is_regular_file(const path& p, error_code& ec) noexcept;

Effects: Sets ec as if by status(p, ec).

[Note 1:

file_type​::​none, file_type​::​not_found andfile_type​::​unknown cases set ec to error values.

To distinguish between cases, call the status function directly.

— _end note_]

Returns: is_regular_file(status(p, ec)).

Returns false if an error occurs.

31.12.13.24 Is socket [fs.op.is.socket]

bool filesystem::is_socket(file_status s) noexcept;

Returns: s.type() == file_type​::​socket.

bool filesystem::is_socket(const path& p);bool filesystem::is_socket(const path& p, error_code& ec) noexcept;

Returns: is_socket(status(p)) oris_socket(status(p, ec)), respectively.

The signature with argumentec returns false if an error occurs.

bool filesystem::is_symlink(file_status s) noexcept;

Returns: s.type() == file_type​::​symlink.

bool filesystem::is_symlink(const path& p);bool filesystem::is_symlink(const path& p, error_code& ec) noexcept;

Returns: is_symlink(symlink_status(p)) or is_symlink(symlink_status(p, ec)), respectively.

The signature with argument ec returns false if an error occurs.

31.12.13.26 Last write time [fs.op.last.write.time]

file_time_type filesystem::last_write_time(const path& p); file_time_type filesystem::last_write_time(const path& p, error_code& ec) noexcept;

Returns: The time of last data modification of p, determined as if by the value of the POSIX stat class member st_mtime obtained as if by POSIX stat.

The signature with argument ec returns file_time_type​::​min() if an error occurs.

void filesystem::last_write_time(const path& p, file_time_type new_time);void filesystem::last_write_time(const path& p, file_time_type new_time, error_code& ec) noexcept;

Effects: Sets the time of last data modification of the file resolved to by p to new_time, as if by POSIX futimens.

[Note 1:

A postcondition of last_write_time(p) == new_time is not specified because it does not necessarily hold for file systems with coarse time granularity.

— _end note_]

31.12.13.27 Permissions [fs.op.permissions]

void filesystem::permissions(const path& p, perms prms, perm_options opts=perm_options::replace);void filesystem::permissions(const path& p, perms prms, error_code& ec) noexcept;void filesystem::permissions(const path& p, perms prms, perm_options opts, error_code& ec);

Preconditions: Exactly one of the perm_options constantsreplace, add, or remove is present in opts.

Effects: Applies the action specified by optsto the file p resolves to, or to file p itself if p is a symbolic link and perm_options​::​nofollow is set in opts.

The action is applied as if by POSIX fchmodat.

[Note 1:

Conceptually permissions are viewed as bits, but the actual implementation can use some other mechanism.

— _end note_]

Remarks: The second signature behaves as if it had an additional parameterperm_options opts with an argument of perm_options​::​replace.

31.12.13.28 Proximate [fs.op.proximate]

path filesystem::proximate(const path& p, error_code& ec);

Returns: proximate(p, current_path(), ec).

path filesystem::proximate(const path& p, const path& base = current_path()); path filesystem::proximate(const path& p, const path& base, error_code& ec);

Returns: For the first form:weakly_canonical(p).lexically_proximate(weakly_canonical(base));

For the second form:weakly_canonical(p, ec).lexically_proximate(weakly_canonical(base, ec)); or path() at the first error occurrence, if any.

path filesystem::read_symlink(const path& p); path filesystem::read_symlink(const path& p, error_code& ec);

Returns: If p resolves to a symbolic link, a path object containing the contents of that symbolic link.

The signature with argument ec returns path() if an error occurs.

[Note 1:

It is an error if p does not resolve to a symbolic link.

— _end note_]

31.12.13.30 Relative [fs.op.relative]

path filesystem::relative(const path& p, error_code& ec);

Returns: relative(p, current_path(), ec).

path filesystem::relative(const path& p, const path& base = current_path()); path filesystem::relative(const path& p, const path& base, error_code& ec);

Returns: For the first form:weakly_canonical(p).lexically_relative(weakly_canonical(base));

For the second form:weakly_canonical(p, ec).lexically_relative(weakly_canonical(base, ec)); or path() at the first error occurrence, if any.

31.12.13.31 Remove [fs.op.remove]

bool filesystem::remove(const path& p);bool filesystem::remove(const path& p, error_code& ec) noexcept;

Effects: If exists(symlink_status(p, ec)), the file p is removed as if by POSIX remove.

[Note 1:

A symbolic link is itself removed, rather than the file it resolves to.

— _end note_]

Postconditions: exists(symlink_status(p)) is false.

Returns: true if a file p has been removed and false otherwise.

[Note 2:

Absence of a file p is not an error.

— _end note_]

31.12.13.32 Remove all [fs.op.remove.all]

uintmax_t filesystem::remove_all(const path& p); uintmax_t filesystem::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 POSIX remove.

[Note 1:

A symbolic link is itself removed, rather than the file it resolves to.

— _end note_]

Postconditions: exists(symlink_status(p)) is false.

Returns: The number of files removed.

The signature with argumentec returns static_cast< uintmax_t>(-1) if an error occurs.

31.12.13.33 Rename [fs.op.rename]

void filesystem::rename(const path& old_p, const path& new_p);void filesystem::rename(const path& old_p, const path& new_p, error_code& ec) noexcept;

Effects: Renames old_p to new_p, as if by POSIX rename.

[Note 1:

A symbolic link is itself renamed, rather than the file it resolves to.

— _end note_]

31.12.13.34 Resize file [fs.op.resize.file]

void filesystem::resize_file(const path& p, uintmax_t new_size);void filesystem::resize_file(const path& p, uintmax_t new_size, error_code& ec) noexcept;

Effects: Causes the size that would be returned by file_size(p) to be equal to new_size, as if by POSIX truncate.

31.12.13.35 Space [fs.op.space]

space_info filesystem::space(const path& p); space_info filesystem::space(const path& p, error_code& ec) noexcept;

Returns: An object of type space_info.

The value of the space_info object is determined as if by using POSIX statvfs to obtain a POSIX struct statvfs, and then multiplying its f_blocks, f_bfree, and f_bavail members by its f_frsize member, and assigning the results to the capacity, free, and available members respectively.

Any members for which the value cannot be determined shall be set to static_cast<uintmax_t>(-1).

For the signature with argument ec, all members are set tostatic_cast<uintmax_t>(-1) if an error occurs.

Remarks: The value of member space_info​::​available is operating system dependent.

[Note 1:

available might be less than free.

— _end note_]

31.12.13.36 Status [fs.op.status]

file_status filesystem::status(const path& p);

Effects: As if by:error_code ec; file_status result = status(p, ec);if (result.type() == file_type::none) throw filesystem_error(implementation-supplied-message, p, ec);return result;

Throws: filesystem_error.

[Note 1:

result values of file_status(file_type​::​not_found) and file_status(file_type​::​unknown) are not considered failures and do not cause an exception to be thrown.

— _end note_]

file_status filesystem::status(const path& p, error_code& ec) noexcept;

Effects: If possible, determines the attributes of the file p resolves to, as if by using POSIX stat to obtain a POSIX struct stat.

If, during attribute determination, the underlying file system API reports an error, sets ec to indicate the specific error reported.

Otherwise, ec.clear().

[Note 2:

This allows users to inspect the specifics of underlying API errors even when the value returned by status is notfile_status(file_type​::​none).

— _end note_]

Let prms denote the result of (m & perms​::​mask), where m is determined as if by converting the st_mode member of the obtained struct stat to the type perms.

Returns:

Remarks: If a symbolic link is encountered during pathname resolution, pathname resolution continues using the contents of the symbolic link.

31.12.13.37 Status known [fs.op.status.known]

bool filesystem::status_known(file_status s) noexcept;

Returns: s.type() != file_type​::​none.

file_status filesystem::symlink_status(const path& p); file_status filesystem::symlink_status(const path& p, error_code& ec) noexcept;

Effects: Same as status, above, except that the attributes of p are determined as if by using POSIX lstat to obtain a POSIX struct stat.

Let prms denote the result of (m & perms​::​mask), where m is determined as if by converting the st_mode member of the obtained struct stat to the type perms.

Returns: Same as status, above, except that if the attributes indicate a symbolic link, as if by POSIX S_ISLNK, returns file_status(file_type​::​symlink, prms).

The signature with argument ec returnsfile_status(file_type​::​none) if an error occurs.

Remarks: Pathname resolution terminates if p names a symbolic link.

31.12.13.39 Temporary directory path [fs.op.temp.dir.path]

path filesystem::temp_directory_path(); path filesystem::temp_directory_path(error_code& ec);

Let p be an unspecified directory path suitable for temporary files.

Effects: If exists(p) is false or is_directory(p) isfalse, an error is reported ([fs.err.report]).

Returns: The path p.

The signature with argument ec returns path() if an error occurs.

[Example 1:

For POSIX-based operating systems, an implementation might return the path supplied by the first environment variable found in the list TMPDIR, TMP, TEMP, TEMPDIR, or if none of these are found, "/tmp".

For Windows-based operating systems, an implementation might return the path reported by the Windows GetTempPath API function.

— _end example_]

31.12.13.40 Weakly canonical [fs.op.weakly.canonical]

path filesystem::weakly_canonical(const path& p); path filesystem::weakly_canonical(const path& p, error_code& ec);

Effects: Using status(p) or status(p, ec), respectively, to determine existence, return a path composed by operator/= from the result of calling canonical with a path argument composed of the leading elements of p that exist, if any, followed by the elements of p that do not exist, if any.

For the first form,canonical is called without an error_code argument.

For the second form,canonical is called with ec as an error_code argument, andpath() is returned at the first error occurrence, if any.