38

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?

Sparhawk
  • 19,561
  • 18
  • 86
  • 152
  • I would edit the patch itself instead. – Braiam Nov 10 '14 at 23:12
  • 1
    @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 02:56
  • 1
    @FaheemMitha Yes, it's running, with Arch/KDE. It's to fix this [bug](https://bugs.kde.org/show_bug.cgi?id=323885). 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 03:01

2 Answers2

49

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
Sparhawk
  • 19,561
  • 18
  • 86
  • 152
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
3

With GNU patch 2.7.6 at least, the filename can be specified:

patch /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml /tmp/fix_kde_icons.patch
gerardw
  • 153
  • 5