Commits

Michał Górny committed cc577d6

Always reset the context before performing mounts/umounts.

  • Participants
  • Parent commits 0c34fcd

Comments (0)

Files changed (1)

File pymountboot.cxx

 	mnt_free_context(b->mnt_context);
 }
 
+static void BootMountpoint_reset(struct libmnt_context *ctx) {
+	if (mnt_reset_context(ctx))
+		throw std::runtime_error("unable to reset mount context");
+	if (mnt_context_set_target(ctx, "/boot"))
+		throw std::runtime_error("unable to set mountpoint to /boot");
+}
+
 static PyObject *BootMountpoint_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
 	try {
 		struct libmnt_context *ctx = mnt_new_context();
 			throw std::runtime_error("unable to create libmount context");
 
 		try {
-			if (mnt_context_set_target(ctx, "/boot"))
-				throw std::runtime_error("unable to set mountpoint to /boot");
-
 			PyObject *self = type->tp_alloc(type, 0);
 
 			if (self != NULL) {
 		BootMountpoint *self = reinterpret_cast<BootMountpoint*>(args);
 		struct libmnt_context* const ctx = self->mnt_context;
 
+		BootMountpoint_reset(ctx);
+
 		struct libmnt_table *mtab;
 		if (mnt_context_get_mtab(ctx, &mtab))
 			throw std::runtime_error("unable to get mtab");
 		BootMountpoint *self = reinterpret_cast<BootMountpoint*>(args);
 		struct libmnt_context* const ctx = self->mnt_context;
 
+		BootMountpoint_reset(ctx);
+
 		struct libmnt_table *mtab;
 		if (mnt_context_get_mtab(ctx, &mtab))
 			throw std::runtime_error("unable to get mtab");
 			case MOUNTPOINT_NONE:
 				break;
 			case MOUNTPOINT_MOUNTED:
-				if (mnt_reset_context(ctx))
-					throw std::runtime_error("unable to reset mount context");
-				if (mnt_context_set_target(ctx, "/boot"))
-					throw std::runtime_error("unable to set mountpoint to /boot");
+				BootMountpoint_reset(ctx);
 
 				if (mnt_context_enable_lazy(ctx, true))
 					throw std::runtime_error("unable to enable lazy umount");
 
 				break;
 			case MOUNTPOINT_REMOUNTED_RW:
-				if (mnt_reset_context(ctx))
-					throw std::runtime_error("unable to reset mount context");
-				if (mnt_context_set_target(ctx, "/boot"))
-					throw std::runtime_error("unable to set mountpoint to /boot");
+				BootMountpoint_reset(ctx);
 
 				if (mnt_context_set_options(ctx, "remount,ro"))
 					throw std::runtime_error("unable to set mount options (to 'remount,ro')");