Entendiendo a diff
De NetBSD Mexico
[editar] Introducción
Durante la actualización del sistema, etcuptate muestra las diferencias entre el archivo de configuración actual y el de la nueva versión, además de que nos pregunta si queremos mantener la versión actual, reemplazarlo o fusionarlo con el de la nueva versión:
-- /etc/defaults/security.conf 2007-09-17 15:27:16.000000000 -0500
+++ /tmp/temproot/etc/defaults/security.conf 2009-02-23 18:22:10.000000000 -0
600
@@ -1,4 +1,4 @@
-# $NetBSD: security.conf,v 1.18.4.2 2007/09/17 20:27:16 bouyer Exp $
+# $NetBSD: security.conf,v 1.20 2007/08/27 19:57:02 adrianp Exp $
#
# /etc/defaults/security.conf --
# default configuration of /etc/security.conf
File: /etc/defaults/security.conf (modified)
Please select one of the following operations:
d Don't install the new file (keep your old file)
i Install the new file (overwrites your local modifications!)
m Merge the currently installed and new files
s Show the differences between the currently installed and new files
su Show differences in unified format ("diff -u")
sc Show differences in context format ("diff -c")
ss Show differences side by side ("sdiff -w80")
scommand Show differences using the specified diff-like command
v Show the new file
What do you want to do? [Leave it for later]
Quienes nunca hayan utilizado la utilería diff anteriormente están literalmente perdidos.
Ya que este es un paso crucial en la actualización del sistema, y por ende para el buen funcionamiento de la nueva versión, vamos a realizar un pequño experimiento para comprender cómo funciona diff.
[editar] Creando los archivos para comparar
Vamos a suponer que formamos parte de un proyecto y alguien crea el siguiente archivo:
/* main.c: Modulo principal del proyecto X */
#include <stdio.h>
main(int argc, char* argv[])
{
printf("Hello, world!\n");
return(0);
}
y hacemos las respectivas correcciones:
/* main.c: Modulo principal del proyecto X */
#include <stdio.h>
#include <stdlib.h>
main(int argc, char* argv[])
{
fprintf(stdout, "Hello, world!\n");
return(EXIT_SUCCESS);
}
y le pedimos a diff que nos muestre los cambios entre el archivo original y el modificado por nostros:
$ diff -u main.c.0 main.c.1
--- main.c.0 2009-02-24 12:54:31.000000000 -0600
+++ main.c.1 2009-02-24 12:26:41.000000000 -0600
@@ -1,7 +1,8 @@
#include <stdio.h>
+#include <stdlib.h>
main(int argc, char* argv[])
{
- printf("Hello, world\n");
- return(0);
+ fprintf(stdout, "Hello, world!\n");
+ return(EXIT_SUCCESS);
}
la opcion -u (unified context) es la opción que etcupdate usa por default:
$ man etcupdate
etcupdate compares the new configuration files against the currently
installed files. The user is given the option of installing, merging or
deleting each modified or missing file. The user can also view the dif-
ferences between the files. By default, it shows the differences in the
unified diff format. The default format can be toggled to show the dif-
ferences in unified, context, or side by side formats or an user-defined
command may be used to view differences. (And if wdiff is installed, it
can also show differences on a word by word basis.)
[editar] Interpretando el resultado
Todas las líneas precedidas por el símbolo “-” eran el código que antes existía y que ahora no
Todas las líneas precedidas por el símbolo “+” es el código nuevo.
NOTA
Es importante descatar que el orden de los archivos *SI* altera el resultado:
$ diff -u main.c.1 main.c.0
--- main.c.1 2009-02-24 12:26:41.000000000 -0600
+++ main.c.0 2009-02-24 12:54:31.000000000 -0600
@@ -1,8 +1,7 @@
#include <stdio.h>
-#include <stdlib.h>
main(int argc, char* argv[])
{
- fprintf(stdout, "Hello, world!\n");
- return(EXIT_SUCCESS);
+ printf("Hello, world\n");
+ return(0);
}