27

I have a patch with absolute paths that I wish to use. i.e. the first few lines are as follows.

--- /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml  2014-10-10 18:47:23.000000000 +1100
+++ /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml.mod  2014-11-11 09:44:17.786200477 +1100

However, it fails unless I am in the root directory.

~$ cd
~$ sudo patch -i /tmp/fix_kde_icons.patch -p0
Ignoring potentially dangerous file name /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml
Ignoring potentially dangerous file name /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml.mod
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
...
~$ cd /tmp
/tmp$ sudo patch -i /tmp/fix_kde_icons.patch -p0
... #same error as above
/tmp$ cd /usr
/usr$ sudo patch -i /tmp/fix_kde_icons.patch -p0
... #same error as above
/usr$ cd /
/$ sudo patch -i /tmp/fix_kde_icons.patch -p0
patching file /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml

Is there a way to make patch use the absolute path with any working directory?

  • I would edit the patch itself instead. – Braiam Nov 10 '14 at 23:12
  • @Braiam What specific edits would I make to the patch? I'm basically wondering if I can avoid having to specify any target paths in the patch command. – Sparhawk Nov 10 '14 at 23:14
  • Some context, please. Are you trying to apply this patch to a running system? If so, why? What is your distribution/OS? What does this patch do? It looks like a patch for a KDE thing. What component? I general, if you distribution isn't applying the patch for you, you should rebuild the relevant package from source with the patch applied. And you can trim of the leading portion of the path. That shouldn't be a problem. How much you trim depends on what the source looks like. – Faheem Mitha Nov 11 '14 at 2:56
  • 1
    @FaheemMitha Yes, it's running, with Arch/KDE. It's to fix this bug. The fix works if I manually apply changes. However, I don't think that context matters so much in this case, as I can replicate this problem with a test file in /tmp/foo. I'm more interested in a generic answer that applies to patch. – Sparhawk Nov 11 '14 at 3:01
36

Looking at the source code of GNU patch, this behavior is built in since version 2.7. As of GNU patch 2.7.1, only relative paths not containing .. are accepted, unless the current directory is the root directory.

To apply a patch containing absolute paths, you can use

(cd / && sudo patch -p0) <foo.patch

In recent versions of GNU patch, you can simply

sudo patch -d/ -p0 <foo.patch
  • Okay, it is what it is. However, if you check my commands, cd /usr also failed, despite the target files being under this directory. – Sparhawk Nov 11 '14 at 2:49
  • @Sparhawk Sorry, my description was partly incorrect. The test is: if the current directory is not /, then reject any absolute path or path containing a .. component. – Gilles 'SO- stop being evil' Nov 11 '14 at 3:11
  • 7
    Or more simply, sudo patch -d/ -p0 <foo.patch – Qian Feb 27 '17 at 21:22
  • @TheoYou I missed that comment at the time. You should write it up as an answer. – Sparhawk Dec 12 '17 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.