Hozer Central

Sat, 13 Jan 2007

Patching rm to avoid rm -fr /

Have you ever accidentally deleted all of /? Of course you haven't you read the man page and know what the -f flag does. You don't just blindly include it whenever you need to delete something.

I ran across this post on the Ubuntu Forums and my heart just sank. No longer are the days when Unix/Linux users actually understand what they are doing. Nowadays users tend to just blinding paste anything they find from Google into a terminal.

This Ubuntu user had an excellent idea. Let us write a patch to rm to prevent it from deleting all of / without asking when you pass it the -rf flags. This was one of those Lewis Black moments for me. I had to reply.

But I can't just reply like all the other ramrods suggesting to alias `rm -i` to `rm` in bash. I actually wrote the patch.

Ladies and Gentlemen, I present to you the --idiot (-I) flag to rm:

diff -Naur coreutils-5.97.orig/src/remove.h coreutils-5.97/src/remove.h
--- coreutils-5.97.orig/src/remove.h    2005-05-14 02:58:37.000000000 -0500
+++ coreutils-5.97/src/remove.h 2007-01-13 00:15:13.268298115 -0600
@@ -33,6 +33,9 @@
   /* If true, recursively remove directories.  */
   bool recursive;
 
+  /* If true, the user is an idiot. */
+  bool idiot;
+
   /* Pointer to the device and inode numbers of `/', when --recursive.
      Otherwise NULL.  */
   struct dev_ino *root_dev_ino;
diff -Naur coreutils-5.97.orig/src/rm.c coreutils-5.97/src/rm.c
--- coreutils-5.97.orig/src/rm.c        2005-08-29 16:13:32.000000000 -0500
+++ coreutils-5.97/src/rm.c     2007-01-13 00:17:38.396568504 -0600
@@ -80,6 +80,7 @@
   {"directory", no_argument, NULL, 'd'},
   {"force", no_argument, NULL, 'f'},
   {"interactive", no_argument, NULL, 'i'},
+  {"idiot", no_argument, NULL, 'I'},
 
   {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
   {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
@@ -142,6 +143,7 @@
                            supports `unlink' for nonempty directories)\n\
   -f, --force           ignore nonexistent files, never prompt\n\
   -i, --interactive     prompt before any removal\n\
+  -I, --idiot           let the ramrod who can't read the man page use -rf\n\ 
 "), stdout);
       fputs (_("\
       --no-preserve-root do not treat `/' specially (the default)\n\
@@ -183,6 +185,7 @@
   x->ignore_missing_files = false;
   x->interactive = false;
   x->recursive = false;
+  x->idiot = true;
   x->root_dev_ino = NULL;
   x->stdin_tty = isatty (STDIN_FILENO);
   x->verbose = false;
@@ -209,7 +212,7 @@
 
   rm_option_init (&x);
 
-  while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
+  while ((c = getopt_long (argc, argv, "dfiIrvR", long_opts, NULL)) != -1)
     {
       switch (c)
        {
@@ -227,6 +230,10 @@
          x.ignore_missing_files = false;
          break;
 
+    case 'I':
+      x.idiot = false;
+      break;
+
        case 'r':
        case 'R':
          x.recursive = true;
@@ -275,6 +282,11 @@
        error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
               quote ("/"));
     }
+  
+  if (x.recursive & x.ignore_missing_files & !x.interactive & x.idiot)
+  {
+      error (EXIT_FAILURE, 0, "You are an idiot. Rerun with the --idiot flag.");
+  }
 
   {
     size_t n_files = argc - optind;
I've also uploaded the the file here.

Tags: , ,

posted at: 01:22 | permanent link to this entry

January 2007 >
MoTuWeThFrSaSu
1 2 3 4 5 6 7
8 91011121314
15161718192021
22232425262728
293031    

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License .

Powered by PyBlosxom.

Icons from the Tango Project.