Skip to content

Commit a9bef53

Browse files
bhalevyavikivity
authored andcommitted
file: add file_system_space
Return space_info for the filesystem identified by the given file name. space_info provides simpler and standard space information about the filesystem, in contrast to the posix statvfs which requires knowledge about the how to convert f_block to bytes by multiplying by f_frsize. Signed-off-by: Benny Halevy <bhalevy@scylladb.com>
1 parent 84e9889 commit a9bef53

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

include/seastar/core/reactor.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ public:
487487
return file_accessible(pathname, access_flags::exists);
488488
}
489489
future<fs_type> file_system_at(std::string_view pathname) noexcept;
490+
future<std::filesystem::space_info> file_system_space(std::string_view pathname) noexcept;
490491
future<struct statvfs> statvfs(std::string_view pathname) noexcept;
491492
future<> remove_file(std::string_view pathname) noexcept;
492493
future<> rename_file(std::string_view old_pathname, std::string_view new_pathname) noexcept;

include/seastar/core/seastar.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ future<uint64_t> fs_avail(std::string_view name) noexcept;
445445
future<uint64_t> fs_free(std::string_view name) noexcept;
446446
/// @}
447447

448+
/// Return filesystem-wide space_info where a file is located.
449+
///
450+
/// \param name name of the file in the filesystem to inspect
451+
future<std::filesystem::space_info> file_system_space(std::string_view name) noexcept;
452+
448453
namespace experimental {
449454
/// \defgroup interprocess-module Interprocess Communication
450455
///

src/core/reactor.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,6 +2263,17 @@ reactor::fstatfs(int fd) noexcept {
22632263
});
22642264
}
22652265

2266+
future<std::filesystem::space_info>
2267+
reactor::file_system_space(std::string_view pathname) noexcept {
2268+
auto sr = co_await _thread_pool->submit<syscall_result_extra<std::filesystem::space_info>>([path = std::filesystem::path(pathname)] {
2269+
std::error_code ec;
2270+
auto si = std::filesystem::space(path, ec);
2271+
return wrap_syscall(ec.value(), si);
2272+
});
2273+
sr.throw_fs_exception_if_error("std::filesystem::space failed", sstring(pathname));
2274+
co_return sr.extra;
2275+
}
2276+
22662277
future<struct statvfs>
22672278
reactor::statvfs(std::string_view pathname) noexcept {
22682279
// Allocating memory for a sstring can throw, hence the futurize_invoke

src/util/file.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ future<uint64_t> fs_free(std::string_view name) noexcept {
128128
});
129129
}
130130

131+
future<std::filesystem::space_info> file_system_space(std::string_view name) noexcept {
132+
return engine().file_system_space(name);
133+
}
134+
131135
future<stat_data> file_stat(std::string_view name, follow_symlink follow) noexcept {
132136
return engine().file_stat(name, follow);
133137
}

tests/unit/file_io_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
* Copyright (C) 2014-2015 Cloudius Systems, Ltd.
2020
*/
2121

22+
#include <filesystem>
23+
2224
#include <seastar/testing/random.hh>
2325
#include <seastar/testing/test_case.hh>
2426
#include <seastar/testing/thread_test_case.hh>
2527
#include <seastar/testing/test_runner.hh>
2628

29+
#include <seastar/core/reactor.hh>
2730
#include <seastar/core/seastar.hh>
2831
#include <seastar/core/semaphore.hh>
2932
#include <seastar/core/condition-variable.hh>
@@ -947,3 +950,16 @@ SEASTAR_TEST_CASE(test_oversized_io_works) {
947950
BOOST_REQUIRE((size_t)std::count_if(buf.get(), buf.get() + buf_size, [](auto x) { return x == 'a'; }) == buf_size);
948951
});
949952
}
953+
954+
SEASTAR_TEST_CASE(test_file_system_space) {
955+
return tmp_dir::do_with_thread([] (tmp_dir& t) {
956+
const auto& name = t.get_path().native();
957+
auto st = engine().statvfs(name).get();
958+
auto si = file_system_space(name).get();
959+
960+
BOOST_REQUIRE_EQUAL(st.f_blocks * st.f_frsize, si.capacity);
961+
BOOST_REQUIRE_LT(si.free, si.capacity);
962+
BOOST_REQUIRE_LT(si.available, si.capacity);
963+
BOOST_REQUIRE_LE(si.available, si.free);
964+
});
965+
}

0 commit comments

Comments
 (0)