Skip to content

Commit b078ebe

Browse files
committed
Move ~/.rpmmacros and ~/.rpmrc to ~/.config/rpm/
The XDG cofiguration directory is the new default location for the macros and rpmrc file. Migrate legacy files automatically. Create symlinks from the home directory for compatibility with older rpm versions. Resolves: #3467
1 parent 49285ea commit b078ebe

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

lib/rpmrc.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include <unordered_map>
77
#include <vector>
8+
#include <filesystem>
89

910
#include <fcntl.h>
1011
#include <stdarg.h>
@@ -358,6 +359,41 @@ const char * lookupInDefaultTable(const char * name,
358359
return name;
359360
}
360361

362+
static void moveConfigFile(const char * from, const char * userdir, const char * name) {
363+
std::string home = std::getenv("HOME");
364+
std::error_code ec;
365+
std::string from_path = from;
366+
std::string userdir_path = userdir;
367+
368+
if (from_path.substr(0, 2) == "~/")
369+
from_path.replace(0, 1, home);
370+
if (userdir_path.substr(0, 2) == "~/")
371+
userdir_path.replace(0, 1, home);
372+
std::filesystem::path to_path = std::filesystem::path(userdir_path) / name;
373+
374+
if (std::filesystem::is_regular_file(from_path) &&
375+
!std::filesystem::exists(to_path))
376+
{
377+
std::filesystem::create_directories(userdir_path, ec);
378+
if (ec) goto err;
379+
std::filesystem::path target = std::filesystem::relative(to_path, std::filesystem::path(from_path).parent_path(), ec);
380+
if (ec) goto err;
381+
382+
std::filesystem::copy(from_path, to_path, ec);
383+
if (ec) goto err;
384+
std::filesystem::remove(from_path, ec);
385+
if (ec) goto err;
386+
std::filesystem::create_symlink(target , from_path, ec);
387+
if (ec) goto err;
388+
rpmlog(RPMLOG_WARNING, "Moved and symlinked %s to %s\n",
389+
from_path.c_str(), to_path.c_str());
390+
}
391+
return;
392+
err:
393+
rpmlog(RPMLOG_ERR, "Could not move %s to %s: %s\n",
394+
from_path.c_str(), to_path.c_str(), ec.message().c_str());
395+
}
396+
361397
static void setDefaults(void)
362398
{
363399
/* If either is missing, we need to go through this whole dance */
@@ -380,6 +416,10 @@ static void setDefaults(void)
380416
const char *oldmacros = "~/.rpmmacros";
381417
const char *oldrc = "~/.rpmrc";
382418
if (rpmGlob(oldmacros, NULL, NULL) == 0 || rpmGlob(oldrc, NULL, NULL) == 0) {
419+
420+
moveConfigFile(oldmacros, userdir, "macros");
421+
moveConfigFile(oldrc, userdir, "rpmrc");
422+
383423
free(usermacros);
384424
free(userrc);
385425
usermacros = xstrdup(oldmacros);

tests/rpmmacro.at

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ AT_KEYWORDS([macros])
77

88
# .rpmmacros exists, new directory not
99
RPMTEST_CHECK([[
10-
rm -rf ~/.config/rpm ~/.rpmmacros
11-
touch ~/.rpmmacros
12-
rpm --showrc | awk '/^Macro path/{print(a[split($0,a,":")])}'
10+
runroot rm -rf /root/.config/rpm /root/.rpmmacros
11+
runroot touch /root/.rpmmacros
12+
runroot rpm --showrc | awk '/^Macro path/{print(a[split($0,a,":")])}'
13+
runroot rpm --showrc | awk '/^Macro path/{print(a[split($0,a,":")])}'
1314
]],
1415
[0],
1516
[~/.rpmmacros
17+
~/.config/rpm/macros
1618
],
17-
[])
19+
[warning: Moved and symlinked /root/.rpmmacros to /root/.config/rpm/macros
20+
])
1821

1922
# prefer new style if it exists
2023
RPMTEST_CHECK([[
@@ -50,6 +53,36 @@ SOMENV=/tmp/somewhere rpm --macros "%{getenv:SOMENV}" --eval "%somewhere"
5053
[])
5154
RPMTEST_CLEANUP
5255

56+
RPMTEST_SETUP_RW([macro path])
57+
AT_KEYWORDS([convert .macros])
58+
RPMTEST_USER
59+
60+
RPMTEST_CHECK([[
61+
runroot_user rm -f /home/$RPMUSER/.config/rpm
62+
runroot_user rm -f /home/$RPMUSER/.rpmmacros
63+
runroot_user rm -f /home/$RPMUSER/.rpmrc
64+
runroot_user bash -c "echo %foo bar > /home/$RPMUSER/.rpmmacros"
65+
runroot_user touch "/home/$RPMUSER/.rpmrc"
66+
67+
runroot_user readlink /home/$RPMUSER/.rpmmacros
68+
runroot_user rpm -E "%{foo}"
69+
runroot_user readlink /home/$RPMUSER/.rpmmacros
70+
runroot_user readlink /home/$RPMUSER/.rpmrc
71+
runroot_user rpm -E "%{foo}"
72+
runroot_user rpm --showrc | awk '/^Macro path/{print(a[split($0,a,":")])}'
73+
]],
74+
[0],
75+
[bar
76+
.config/rpm/macros
77+
.config/rpm/rpmrc
78+
bar
79+
~/.config/rpm/macros
80+
],
81+
[warning: Moved and symlinked /home/klang/.rpmmacros to /home/klang/.config/rpm/macros
82+
warning: Moved and symlinked /home/klang/.rpmrc to /home/klang/.config/rpm/rpmrc
83+
])
84+
RPMTEST_CLEANUP
85+
5386
# ------------------------------
5487
RPMTEST_SETUP_RW([macro path: skip editor backups])
5588
AT_KEYWORDS([macros])

0 commit comments

Comments
 (0)