Hi,

I implemented in swapon the -e/--ifexists option close as to what's in 
util-linux' swapon. It basically ignores unexisting devices/files when 
doing `swapon -a`, i.e. only for the devices found in fstab (not for the 
ones passed as arguments to `swapon'.

What do you think?
Could it be making our tools like swapon/mount/etc a bit more compatible 
(in term of command line options) with the other OSes' ones a (low 
priority) goal?

-- 
Pino Toscano
From d34079adfadebc5f0e936e92257811676b2ebe88 Mon Sep 17 00:00:00 2001
From: Pino Toscano <toscano.p...@tiscali.it>
Date: Sat, 28 Jan 2012 22:41:53 +0100
Subject: [PATCH] swapon: add -e/--ifexists option

Add the same command line option as in util-linux' swapon to not
consider a error if the device/file of a swap entry in fstab does not
exist (and not when activating a device specified as argument to
`swapon').

* sutils/swapon.c (ifexists, options, parse_opt): Add new variable.
Register the new -e/--ifexists option, and associate the `ifexists'
variable with it.
(swaponoff, main): New argument `skipnotexist'.
Return 0 if `open_store' fails with ENOENT and `skipnotexist' is on.
Adapt `swaponoff' calls with 0 as new argument for command line
arguments, while use the value of `ifexists' for swap entries from
fstab.
---
 sutils/swapon.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/sutils/swapon.c b/sutils/swapon.c
index c0c773b..13e4e64 100644
--- a/sutils/swapon.c
+++ b/sutils/swapon.c
@@ -42,12 +42,14 @@ const char *argp_program_version = STANDARD_HURD_VERSION (swapoff);
 const char *argp_program_version = STANDARD_HURD_VERSION (swapon);
 #endif
 
-static int ignore_signature, require_signature, quiet;
+static int ignore_signature, require_signature, quiet, ifexists;
 
 static struct argp_option options[] =
 {
   {"standard",	  'a', 0, 0,
     "Use all devices marked as `swap' in " _PATH_MNTTAB},
+  {"ifexists",    'e', 0, 0,
+   "Silently skip devices that do not exist"},
   {"no-signature",'n', 0, 0,
    "Do not check for a Linux swap signature page"},
   {"require-signature", 's', 0, 0,
@@ -319,7 +321,7 @@ check_signature (const char *name, struct store **storep, int no_remap,
 /* Process a single argument file.  */
 
 static int
-swaponoff (const char *file, int add)
+swaponoff (const char *file, int add, int skipnotexist)
 {
   error_t err;
   struct store *store;
@@ -332,6 +334,10 @@ swaponoff (const char *file, int add)
   err = store_open (file, 0, 0, &store);
   if (err)
     {
+      /* If the device does not exist but we were told to ignore such error,
+         return cleanly.  */
+      if (err == ENOENT && skipnotexist)
+	return 0;
       error (0, err, "%s", file);
       return err;
     }
@@ -467,6 +473,10 @@ main (int argc, char *argv[])
 	  do_all = 1;
 	  break;
 
+	case 'e':
+	  ifexists = 1;
+	  break;
+
 	case 'n':
 	  ignore_signature = 1;
 	  break;
@@ -486,7 +496,7 @@ main (int argc, char *argv[])
 #else
 #define ONOFF 1
 #endif
-	  swaponoff (arg, ONOFF);
+	  swaponoff (arg, ONOFF, 0);
 	  break;
 
 	default:
@@ -523,7 +533,7 @@ main (int argc, char *argv[])
 	      {
 		done = 1;
 
-		err |= swaponoff (me->mnt_fsname, ONOFF);
+		err |= swaponoff (me->mnt_fsname, ONOFF, ifexists);
 	      }
 	  if (done == 0)
 	    error (2, 0, "No swap partitions found in %s", _PATH_MNTTAB);
-- 
1.7.8.3

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to