Skip to content

Commit

Permalink
Add APIs to get group details and to change ownership of file.
Browse files Browse the repository at this point in the history
  • Loading branch information
muthu90tech committed Aug 17, 2024
1 parent e42e382 commit 5424c1d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/seastar/core/reactor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include <string_view>
#include <unordered_map>
#include <vector>
#include <grp.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
Expand Down Expand Up @@ -493,6 +494,8 @@ public:
future<> touch_directory(std::string_view name, file_permissions permissions = file_permissions::default_dir_permissions) noexcept;
future<std::optional<directory_entry_type>> file_type(std::string_view name, follow_symlink = follow_symlink::yes) noexcept;
future<stat_data> file_stat(std::string_view pathname, follow_symlink) noexcept;
future<> chown(std::string_view filepath, uid_t owner, gid_t group);
future<std::optional<struct group>> getgrnam(std::string_view name, char *buf, size_t buflen);
future<uint64_t> file_size(std::string_view pathname) noexcept;
future<bool> file_accessible(std::string_view pathname, access_flags flags) noexcept;
future<bool> file_exists(std::string_view pathname) noexcept {
Expand Down
19 changes: 19 additions & 0 deletions include/seastar/core/seastar.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@
#include <filesystem>
#include <optional>
#include <string_view>
#include <grp.h>
#endif

struct group;

namespace seastar {

SEASTAR_MODULE_EXPORT_BEGIN
Expand Down Expand Up @@ -359,6 +362,22 @@ using follow_symlink = bool_class<follow_symlink_tag>;
/// with follow_symlink::yes, or for the link itself, with follow_symlink::no.
future<stat_data> file_stat(std::string_view name, follow_symlink fs = follow_symlink::yes) noexcept;

/// Wrapper around getgrnam_t.
/// If the group is found, the std::optional contains the struct group information; otherwise, it is empty.
/// The function throws std::error_code, when the getgrnam_t syscall fails.
/// \param groupname name of the group
/// \param buf buffer to store the string fields pointed to by the members of group structure.
/// \param buflen size of the buffer
///
/// \return optional struct group of the group identified by name. struct group has details of the group from the group database.
future<std::optional<struct group>> getgrnam(std::string_view name, char *buf, size_t buflen);

/// Change the owner and group of file. This is a wrapper around chown syscall.
/// The function throws std::system_error, when the chown syscall fails.
/// \param filepath
/// \param owner
/// \param group
future<> chown(std::string_view filepath, uid_t owner, gid_t group);
/// Return the size of a file.
///
/// \param name name of the file to return the size
Expand Down
31 changes: 31 additions & 0 deletions src/core/reactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ module;
#include <cassert>
#include <chrono>
#include <cmath>
#include <coroutine>
#include <exception>
#include <filesystem>
#include <fstream>
#include <regex>
#include <thread>

#include <grp.h>
#include <spawn.h>
#include <sys/syscall.h>
#include <sys/vfs.h>
Expand Down Expand Up @@ -2200,6 +2202,35 @@ void reactor::kill(pid_t pid, int sig) {
ret.throw_if_error();
}

future<std::optional<struct group>> reactor::getgrnam(std::string_view name, char *buf, size_t buflen) {
syscall_result_extra<std::optional<struct group>> sr = co_await _thread_pool->submit<syscall_result_extra<std::optional<struct group>>>(
[name = sstring(name), buf, buflen] {
struct group grp;
struct group *result;
memset(&grp, 0, sizeof(struct group));
errno = 0;
int ret = ::getgrnam_r(name.c_str(), &grp, buf, buflen, &result);
return wrap_syscall(ret, result ? std::optional<struct group>(*result) : std::nullopt);
});

if (sr.result != 0) {
throw std::error_code(sr.result, std::system_category());
}

co_return std::move(sr.extra);
}

future<> reactor::chown(std::string_view filepath, uid_t owner, gid_t group) {
syscall_result<int> sr = co_await _thread_pool->submit<syscall_result<int>>(
[filepath = sstring(filepath), owner, group] {
int ret = ::chown(filepath.c_str(), owner, group);
return wrap_syscall(ret);
});

sr.throw_if_error();
co_return;
}

future<stat_data>
reactor::file_stat(std::string_view pathname, follow_symlink follow) noexcept {
// Allocating memory for a sstring can throw, hence the futurize_invoke
Expand Down
9 changes: 9 additions & 0 deletions src/util/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module;
#include <iostream>
#include <list>
#include <vector>
#include <grp.h>
#include <sys/statvfs.h>

#ifdef SEASTAR_MODULE
Expand Down Expand Up @@ -132,6 +133,14 @@ future<stat_data> file_stat(std::string_view name, follow_symlink follow) noexce
return engine().file_stat(name, follow);
}

future<std::optional<struct group>> getgrnam(std::string_view name, char *buf, size_t buflen) {
return engine().getgrnam(name, buf, buflen);
}

future<> chown(std::string_view filepath, uid_t owner, gid_t group) {
return engine().chown(filepath, owner, group);
}

future<uint64_t> file_size(std::string_view name) noexcept {
return engine().file_size(name);
}
Expand Down

0 comments on commit 5424c1d

Please sign in to comment.