1

I am new to rpm building and for the past week I am trying to code a spec file to install a file on the system only if a specific files is missing. Here is a snippet

%global homedir         %{_var}/lib/test

Source10: main.db
Source11: back.db

%files data
%defattr(-,%{updateuser},%{updateuser},-)
%config(noreplace) %verify(not size md5 mtime) %{homedir}/*.db

I would like to add a condition where, If file /var/lib/test/file1.txt is missing, install /var/lib/test/back.db

I am aware I need to use the %files macro but so far I am not able to figure out how to write the if condition

2
  • The question that I have here is whether this is really desirable: your package will be missing a file when someone deletes the file that's not under control of your package? That sounds like you're missing a dependency on some other software package there. Commented Aug 24, 2024 at 0:53
  • concretely, it sounds like you'd want this package to depend on the package that installs /var/lib/test/file1.txt OR the package that contains /var/lib/test/back.db, and put the back.db file in that separate package. Generally, however: Software installation should be fully independent of the data files that are on a system, so even that sounds like a bad idea. Commented Aug 24, 2024 at 0:56

1 Answer 1

2

The %files section is for specifying which files are included in the package, but it doesn’t support conditional logic.

The best way to handle the requirement is through a %post script.

You can check if /var/lib/test/file1.txt is missing using [ ! -e %{homedir}/file1.txt ] and then conditionally install back.db if needed.

The %post section is where such logic belongs, as it runs after the package is installed.

4
  • 1
    Wouldn't [ ! -e /var/lib/test/file1.txt ]; be better? Why assume it needs to be a regular file? Commented Aug 23, 2024 at 8:44
  • yes, [ ! -e /var/lib/test/file1.txt ]; is better Commented Aug 23, 2024 at 9:50
  • @MaxHaase Since the back.db file will be included in the %files section, isn't going to be installed by default? Or should i place back.db file outside %file? Commented Aug 23, 2024 at 12:59
  • Good question! Since back.db is listed in the %files section, it will be installed by default. To handle this conditionally, you can still include back.db in %files (so it’s part of the package), but during %post, you’ll check if file1.txt is missing and only then move or rename back.db to the desired location. There’s no need to remove it from %files; the logic in %post determines if it gets used. Commented Aug 23, 2024 at 13:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.