[libc++] Implement Directory Entry Caching -- Sort of.
authorEric Fiselier <eric@efcs.ca>
Fri, 20 Jul 2018 01:22:32 +0000 (01:22 +0000)
committerEric Fiselier <eric@efcs.ca>
Fri, 20 Jul 2018 01:22:32 +0000 (01:22 +0000)
commitc16998649e1f55c61158004f461fd629d9b8b7f9
treec1434a61a1da354f770f4ac06994211375a84253
parent40fa4a1a559765ada93268c4d45c54b62779166b
[libc++] Implement Directory Entry Caching -- Sort of.

Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.

The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:

* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`.  This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.

As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.

@BillyONeal  has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.

Some additional semantics which differ from the Windows implementation:

1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).

It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).

If the changes to the specification don't get accepted, we'll be able to make the changes later.

[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html

Reviewers: mclow.lists, gromer, ldionne, aaron.ballman

Subscribers: BillyONeal, christof, cfe-commits

Differential Revision: https://reviews.llvm.org/D49530

llvm-svn: 337516
27 files changed:
libcxx/include/experimental/filesystem
libcxx/src/experimental/filesystem/directory_iterator.cpp
libcxx/src/experimental/filesystem/filesystem_common.h [moved from libcxx/src/experimental/filesystem/filesystem_time_helper.h with 58% similarity]
libcxx/src/experimental/filesystem/operations.cpp
libcxx/test/libcxx/experimental/filesystem/class.directory_entry/directory_entry.mods/last_write_time.sh.cpp [new file with mode: 0644]
libcxx/test/libcxx/experimental/filesystem/convert_file_time.sh.cpp
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.cons.pass.cpp [deleted file]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.cons/copy.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.cons/copy_assign.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.cons/default.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.cons/move.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.cons/move_assign.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.cons/path.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.mods.pass.cpp [deleted file]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.mods/assign.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.mods/refresh.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.mods/replace_filename.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.obs/file_size.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.obs/file_type_obs.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.obs/hard_link_count.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.obs/last_write_time.pass.cpp [new file with mode: 0644]
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.obs/status.pass.cpp
libcxx/test/std/experimental/filesystem/class.directory_entry/directory_entry.obs/symlink_status.pass.cpp
libcxx/test/std/experimental/filesystem/fs.op.funcs/fs.op.file_size/file_size.pass.cpp
libcxx/test/support/filesystem_test_helper.hpp
libcxx/test/support/rapid-cxx-test.hpp
libcxx/www/cxx1z_status.html