55#include < string>
66#include < unordered_map>
77#include < vector>
8+ #include < filesystem>
89
910#include < fcntl.h>
1011#include < stdarg.h>
4849using wrlock = std::unique_lock<std::shared_mutex>;
4950using rdlock = std::shared_lock<std::shared_mutex>;
5051
52+ namespace fs = std::filesystem;
53+
5154static const char * defrcfiles = NULL ;
5255const char * macrofiles = NULL ;
5356
@@ -358,6 +361,83 @@ const char * lookupInDefaultTable(const char * name,
358361 return name;
359362}
360363
364+ static void moveConfigFiles (std::string userdir)
365+ {
366+ fs::path home = std::getenv (" HOME" );
367+ std::error_code ec;
368+ std::error_code ec2;
369+ fs::path oldmacros = home / " .rpmmacros" ;
370+ fs::path oldrpmrc = home / " .rpmrc" ;
371+ fs::path macros_target = " " ;
372+ fs::path rpmrc_target = " " ;
373+
374+ if (userdir.substr (0 , 2 ) == " ~/" )
375+ userdir.replace (0 , 1 , home);
376+
377+ fs::path userdir_path = userdir;
378+ fs::path newmacros = userdir_path / " macros" ;
379+ fs::path newrpmrc = userdir_path / " rpmrc" ;
380+
381+ if (!fs::is_regular_file (oldmacros) && !fs::is_regular_file (oldrpmrc))
382+ return ;
383+
384+ fs::create_directories (userdir_path, ec);
385+ if (ec) goto err;
386+
387+ if (fs::is_regular_file (oldmacros)) {
388+ fs::copy (oldmacros, newmacros, ec);
389+ if (ec) goto err;
390+ macros_target = fs::relative (newmacros, home, ec);
391+ if (ec) goto err;
392+ }
393+ if (fs::is_regular_file (oldrpmrc)) {
394+ fs::copy (oldrpmrc, newrpmrc, ec);
395+ if (ec) goto err;
396+ rpmrc_target = fs::relative (newrpmrc, home, ec);
397+ if (ec) goto err;
398+ }
399+
400+ if (fs::is_regular_file (oldmacros)) {
401+ fs::remove (oldmacros, ec);
402+ if (ec) goto undo_remove;
403+ fs::create_symlink (macros_target , oldmacros, ec);
404+ if (ec) goto undo_remove;
405+ }
406+ if (fs::is_regular_file (oldrpmrc)) {
407+ fs::remove (oldrpmrc, ec);
408+ if (ec) goto undo_remove;
409+ fs::create_symlink (rpmrc_target , oldrpmrc, ec);
410+ if (ec) goto undo_remove;
411+ }
412+
413+ if (fs::is_symlink (oldmacros))
414+ rpmlog (RPMLOG_WARNING, " Migrated %s to %s (leaving a symlink)\n " ,
415+ oldmacros.c_str (), newmacros.c_str ());
416+ if (fs::is_symlink (oldrpmrc))
417+ rpmlog (RPMLOG_WARNING, " Migrated %s to %s (leaving a symlink)\n " ,
418+ oldrpmrc.c_str (), newrpmrc.c_str ());
419+ return ;
420+ undo_remove:
421+ if (fs::is_symlink (oldmacros))
422+ fs::remove (oldmacros, ec2);
423+ if (fs::is_symlink (oldrpmrc))
424+ fs::remove (oldrpmrc, ec2);
425+
426+ if (fs::is_regular_file (newmacros))
427+ fs::copy (oldmacros, newmacros, ec2);
428+ if (fs::is_regular_file (newrpmrc))
429+ fs::copy (oldrpmrc, newrpmrc, ec2);
430+ err:
431+ /* userdir_path did not exist at the beginning */
432+ fs::remove_all (userdir_path, ec2);
433+ if (fs::exists (oldmacros))
434+ rpmlog (RPMLOG_ERR, " Could not migrate %s to %s: %s\n " ,
435+ oldmacros.c_str (), newmacros.c_str (), ec.message ().c_str ());
436+ if (fs::exists (oldrpmrc))
437+ rpmlog (RPMLOG_ERR, " Could not migrate %s to %s: %s\n " ,
438+ oldrpmrc.c_str (), newrpmrc.c_str (), ec.message ().c_str ());
439+ }
440+
361441static void setDefaults (void )
362442{
363443 /* If either is missing, we need to go through this whole dance */
@@ -380,6 +460,9 @@ static void setDefaults(void)
380460 const char *oldmacros = " ~/.rpmmacros" ;
381461 const char *oldrc = " ~/.rpmrc" ;
382462 if (rpmGlob (oldmacros, NULL , NULL ) == 0 || rpmGlob (oldrc, NULL , NULL ) == 0 ) {
463+
464+ moveConfigFiles (userdir);
465+
383466 free (usermacros);
384467 free (userrc);
385468 usermacros = xstrdup (oldmacros);
0 commit comments