Skip to content

Commit

Permalink
Add example of reading file stats #106
Browse files Browse the repository at this point in the history
  • Loading branch information
ned14 committed Feb 10, 2023
1 parent 7b782eb commit d97807d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ Examples of use (more examples: https://github.com/ned14/llfio/tree/develop/exam
<table width="100%" border="0" cellpadding="4">
<tr>
<td width="50%" valign="top">
\snippet example/mapped_file.cpp mapped_file

\snippet example/sparse_array.cpp sparse_array
</td>
<td width="50%" valign="top">
\snippet tests/tls_socket_handle.cpp https_get
\snippet example/read_stats.cpp file_read_stats
</td>
</tr>
</table>
Expand Down
1 change: 1 addition & 0 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ set(llfio_COMPILE_TESTS
"example/map_file.cpp"
"example/mapped_file.cpp"
"example/read_entire_file1.cpp"
"example/read_stats.cpp"
"example/scatter_write.cpp"
"example/sparse_array.cpp"
"example/tls_socket_server.cpp"
Expand Down
83 changes: 83 additions & 0 deletions example/read_stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* Examples of LLFIO use
(C) 2018 - 2022 Niall Douglas <http://www.nedproductions.biz/> (2 commits)
File Created: Aug 2018
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file Licence.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "../include/llfio.hpp"

#include <ctime>
#include <string>

auto print = [](std::chrono::system_clock::time_point ts)
{
static time_t t;
t = std::chrono::system_clock::to_time_t(ts);
return [](std::ostream &os) -> std::ostream &
{
return os << std::ctime(&t);
};
};

// clang-format off

void read_stats()
{
//! [file_read_stats]
namespace llfio = LLFIO_V2_NAMESPACE;

// Open the file for read
llfio::file_handle fh = llfio::file( //
{}, // path_handle to base directory
"foo" // path_view to path fragment relative to base directory
// default mode is read only
// default creation is open existing
// default caching is all
// default flags is none
).value(); // If failed, throw a filesystem_error exception

// warning: default constructor does nothing. If you want all bits
// zero, construct with `nullptr`.
llfio::stat_t fhstat;
fhstat.fill(
fh // file handle from which to fill stat_t
// default stat_t::want is all
).value();
std::cout << "The file was last modified on "
<< print(fhstat.st_mtim) << std::endl;

// Note that default constructor fills with all bits one. This
// lets you do partial fills and usually detect what wasn't filled.
llfio::statfs_t fhfsstat;
fhfsstat.fill(
fh // file handle from which to fill statfs_t
// default statfs_t::want is all
).value();
std::cout << "The file's storage has "
<< (fhfsstat.f_bavail * fhfsstat.f_bsize)
<< "bytes free." << std::endl;
//! [file_read_stats]
}

int main()
{
return 0;
}
6 changes: 3 additions & 3 deletions include/llfio/revision.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define LLFIO_PREVIOUS_COMMIT_REF 4fc298913a085826c1cdff543e9cbd3435459460
#define LLFIO_PREVIOUS_COMMIT_DATE "2023-02-07 00:29:03 +00:00"
#define LLFIO_PREVIOUS_COMMIT_UNIQUE 4fc29891
#define LLFIO_PREVIOUS_COMMIT_REF 7b782ebda1e847bf18899cbb64bb13209cd77ddd
#define LLFIO_PREVIOUS_COMMIT_DATE "2023-02-09 00:46:50 +00:00"
#define LLFIO_PREVIOUS_COMMIT_UNIQUE 7b782ebd

0 comments on commit d97807d

Please sign in to comment.