Applying a diff patch

diff()patch

I've never patched anything in Linux, and I can't exactly figure out what the guy who wrote this post (second paragraph) means. That target source is qemu from github and the changes to apply are:

diff --git a/linux-user/flatload.c b/linux-user/flatload.c
index 58f679e..c13a201 100644
--- a/linux-user/flatload.c
+++ b/linux-user/flatload.c
@@ -44,7 +44,7 @@
 #define ntohl(x) be32_to_cpu(x)
 #include 
 
-//#define DEBUG
+#define DEBUG
 
 #ifdef DEBUG
 #define        DBG_FLT(...)    printf(__VA_ARGS__)

This looks like a long diff command to me but attempting to execute it fails. After some searching, I though I had to save that as ie qemu.patch and run it with patch, but after trying a few combinations that failed as well.

In this context, what am I supposed to do with the above snippet?

Best Answer

This is actually a very short diff command. It says to remove the line //#define DEBUG and replace it with #define DEBUG.

In this diff format, lines starting with a single - are removed and those with + are added. The other lines are for context, and the @@ tells the offset into the file and number of lines referenced. (And the triple +++ --- lines tell you which file was modified.)

The two common reasons for a patch to fail to apply are:

  1. The actual affected lines have changed, or
  2. The context around those lines has changed (possibly, changed too much, because patch usually uses a "fuzzy" algorithm.

In this case, it's probably easiest to look at the section of code by hand (in the linux-user/flatload.c file, as you can see from the first line), and see if there's a commented-out #define DEBUG anywhere, and then remove the // comment characters.

Looking at the code myself, I see that the problem is probably actually reason #3 — the patch is mangled because it's been rendered in a way it wasn't supposed to be. See that line that just says #include? In the original source, it says #include <target_flat.h>. The blog software where you found the patch probably decided that <target_flat.h> is a suspicious HTML tag and silently deleted it, and the author didn't notice.

Related Question