Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Jul 2012 09:35:06 +0000 (UTC)
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org
Subject:   svn commit: r238829 - stable/9/usr.sbin/pkg
Message-ID:  <201207270935.q6R9Z6ZP088180@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: bapt
Date: Fri Jul 27 09:35:06 2012
New Revision: 238829
URL: http://svn.freebsd.org/changeset/base/238829

Log:
  MFC: r238461
  
  Make pkg bootstrap program ask for confirmation before proceeding.
  
  The previous behaviour was to silently download and install the pkg
  package, without ever telling user about what it was doing and why.
  
  Approved by:	re, des (mentor)

Modified:
  stable/9/usr.sbin/pkg/pkg.c
Directory Properties:
  stable/9/usr.sbin/pkg/   (props changed)

Modified: stable/9/usr.sbin/pkg/pkg.c
==============================================================================
--- stable/9/usr.sbin/pkg/pkg.c	Fri Jul 27 09:16:48 2012	(r238828)
+++ stable/9/usr.sbin/pkg/pkg.c	Fri Jul 27 09:35:06 2012	(r238829)
@@ -389,6 +389,28 @@ cleanup:
 	return (ret);
 }
 
+static const char confirmation_message[] =
+"The package management tool is not yet installed on your system.\n"
+"Do you want to fetch and install it now? [y/N]: ";
+
+static int
+pkg_query_yes_no(void)
+{
+	int ret, c;
+
+	c = getchar();
+
+	if (c == 'y' || c == 'Y')
+		ret = 1;
+	else
+		ret = 0;
+
+	while (c != '\n' && c != EOF)
+		c = getchar();
+
+	return (ret);
+}
+
 int
 main(__unused int argc, char *argv[])
 {
@@ -397,9 +419,21 @@ main(__unused int argc, char *argv[])
 	snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
 	    getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
 
-	if (access(pkgpath, X_OK) == -1)
+	if (access(pkgpath, X_OK) == -1) {
+		/*
+		 * Do not ask for confirmation if either of stdin or stdout is
+		 * not tty. Check the environment to see if user has answer
+		 * tucked in there already.
+		 */
+		if (getenv("ALWAYS_ASSUME_YES") == NULL &&
+		    isatty(fileno(stdin))) {
+			printf("%s", confirmation_message);
+			if (pkg_query_yes_no() == 0)
+				exit(EXIT_FAILURE);
+		}
 		if (bootstrap_pkg() != 0)
 			exit(EXIT_FAILURE);
+	}
 
 	execv(pkgpath, argv);
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201207270935.q6R9Z6ZP088180>