Skip to content

Commit 2b88e60

Browse files
committed
base: Add get_active_transaction_info() method
The method returns unique_ptr to ActiveTransactionInfo if a transaction is running, nullptr otherwise.
1 parent 3573297 commit 2b88e60

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/libdnf5/base/base.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef LIBDNF5_BASE_BASE_HPP
2121
#define LIBDNF5_BASE_BASE_HPP
2222

23+
#include "libdnf5/base/active_transaction_info.hpp"
2324
#include "libdnf5/base/base_weak.hpp"
2425
#include "libdnf5/common/exception.hpp"
2526
#include "libdnf5/common/impl_ptr.hpp"
@@ -132,6 +133,10 @@ class LIBDNF_API Base {
132133
/// Gets base variables. They can be used in configuration files. Syntax in the config - ${var_name} or $var_name.
133134
VarsWeakPtr get_vars();
134135

136+
/// Returns information about the currently active transaction, if any.
137+
/// @return unique_ptr to ActiveTransactionInfo if a transaction is running, nullptr otherwise
138+
std::unique_ptr<base::ActiveTransactionInfo> get_active_transaction_info();
139+
135140
libdnf5::BaseWeakPtr get_weak_ptr();
136141

137142
private:

libdnf5/base/base.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "libdnf5/conf/config_parser.hpp"
3939
#include "libdnf5/conf/const.hpp"
4040
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
41+
#include "libdnf5/utils/locker.hpp"
4142

4243
#include <atomic>
4344
#include <cstdlib>
@@ -300,6 +301,29 @@ VarsWeakPtr Base::get_vars() {
300301
return {&p_impl->vars, &p_impl->vars_guard};
301302
}
302303

304+
std::unique_ptr<base::ActiveTransactionInfo> Base::get_active_transaction_info() {
305+
std::filesystem::path lock_file_path = get_config().get_installroot_option().get_value();
306+
lock_file_path /= std::filesystem::path(libdnf5::TRANSACTION_LOCK_FILEPATH).relative_path();
307+
308+
libdnf5::utils::Locker locker(lock_file_path.string());
309+
310+
// Try to acquire read lock - if successful, no write lock exists (no active transaction)
311+
if (locker.read_lock()) {
312+
return nullptr;
313+
}
314+
315+
// Read lock failed - write lock exists, read content from the opened file descriptor
316+
std::string content = locker.read_content();
317+
318+
if (content.empty()) {
319+
// Empty lock file means no valid transaction info
320+
return nullptr;
321+
}
322+
323+
// Parse TOML content
324+
return std::make_unique<base::ActiveTransactionInfo>(base::ActiveTransactionInfo::from_toml(content));
325+
}
326+
303327
libdnf5::BaseWeakPtr Base::get_weak_ptr() {
304328
return {this, &base_guard};
305329
}

0 commit comments

Comments
 (0)