Blame SOURCES/0031-Add-index-constant-definitions-instead-of-open-coded.patch

24fce8
From 624a75f2d01d986ec78f60319ebc2acd37faada2 Mon Sep 17 00:00:00 2001
24fce8
From: Robert Marshall <rmarshall@redhat.com>
24fce8
Date: Tue, 13 Dec 2016 14:36:31 -0500
24fce8
Subject: [PATCH 31/55] Add index constant definitions instead of open-coded
24fce8
 values.
24fce8
24fce8
Added numeric constants NO_DEFAULT_ENTRY and FIRST_ENTRY_INDEX. This
24fce8
clarifies the intent of various assignment operations throughout the
24fce8
source file.
24fce8
24fce8
Related: rhbz#1285601
24fce8
---
24fce8
 grubby.c | 44 ++++++++++++++++++++++++--------------------
24fce8
 1 file changed, 24 insertions(+), 20 deletions(-)
24fce8
24fce8
diff --git a/grubby.c b/grubby.c
24fce8
index 9af8d17b345..a717c18999b 100644
24fce8
--- a/grubby.c
24fce8
+++ b/grubby.c
24fce8
@@ -132,6 +132,10 @@ struct singleEntry {
24fce8
 #define NEED_DEVTREE (1 << 6)
24fce8
 
24fce8
 #define MAIN_DEFAULT	    (1 << 0)
24fce8
+#define FIRST_ENTRY_INDEX    0	/* boot entry index value begin and increment
24fce8
+				   from this initial value */
24fce8
+#define NO_DEFAULT_ENTRY    -1	/* indicates that no specific default boot
24fce8
+				   entry was set or currently exists */
24fce8
 #define DEFAULT_SAVED       -2
24fce8
 #define DEFAULT_SAVED_GRUB2 -3
24fce8
 
24fce8
@@ -1612,7 +1616,7 @@ static struct grubConfig *readConfig(const char *inName,
24fce8
 						*end == ' ' || *end == '\t'))
24fce8
 					end++;
24fce8
 				if (*end)
24fce8
-					cfg->defaultImage = -1;
24fce8
+					cfg->defaultImage = NO_DEFAULT_ENTRY;
24fce8
 			} else if (defaultLine->numElements == 3) {
24fce8
 				char *value = defaultLine->elements[2].item;
24fce8
 				while (*value && (*value == '"' ||
24fce8
@@ -1625,7 +1629,7 @@ static struct grubConfig *readConfig(const char *inName,
24fce8
 						*end == ' ' || *end == '\t'))
24fce8
 					end++;
24fce8
 				if (*end)
24fce8
-					cfg->defaultImage = -1;
24fce8
+					cfg->defaultImage = NO_DEFAULT_ENTRY;
24fce8
 			}
24fce8
 		} else if (cfi->defaultSupportSaved &&
24fce8
 			   !strncmp(defaultLine->elements[1].item, "saved",
24fce8
@@ -1635,7 +1639,7 @@ static struct grubConfig *readConfig(const char *inName,
24fce8
 			cfg->defaultImage =
24fce8
 			    strtol(defaultLine->elements[1].item, &end, 10);
24fce8
 			if (*end)
24fce8
-				cfg->defaultImage = -1;
24fce8
+				cfg->defaultImage = NO_DEFAULT_ENTRY;
24fce8
 		} else if (defaultLine->numElements >= 2) {
24fce8
 			int i = 0;
24fce8
 			while ((entry = findEntryByIndex(cfg, i))) {
24fce8
@@ -1663,7 +1667,7 @@ static struct grubConfig *readConfig(const char *inName,
24fce8
 			if (entry) {
24fce8
 				cfg->defaultImage = i;
24fce8
 			} else {
24fce8
-				cfg->defaultImage = -1;
24fce8
+				cfg->defaultImage = NO_DEFAULT_ENTRY;
24fce8
 			}
24fce8
 		}
24fce8
 	} else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
24fce8
@@ -1680,7 +1684,7 @@ static struct grubConfig *readConfig(const char *inName,
24fce8
 				cfg->defaultImage = index;
24fce8
 		}
24fce8
 	} else {
24fce8
-		cfg->defaultImage = 0;
24fce8
+		cfg->defaultImage = FIRST_ENTRY_INDEX;
24fce8
 	}
24fce8
 
24fce8
 	return cfg;
24fce8
@@ -1700,7 +1704,7 @@ static void writeDefault(FILE * out, char *indent,
24fce8
 		fprintf(out, "%sdefault%ssaved\n", indent, separator);
24fce8
 	else if (cfg->cfi->defaultIsSaved) {
24fce8
 		fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
24fce8
-		if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
24fce8
+		if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) {
24fce8
 			char *title;
24fce8
 			entry = findEntryByIndex(cfg, cfg->defaultImage);
24fce8
 			line = getLineByType(LT_MENUENTRY, entry->lines);
24fce8
@@ -1713,7 +1717,7 @@ static void writeDefault(FILE * out, char *indent,
24fce8
 							 "saved_entry", title);
24fce8
 			}
24fce8
 		}
24fce8
-	} else if (cfg->defaultImage > -1) {
24fce8
+	} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
24fce8
 		if (cfg->cfi->defaultIsIndex) {
24fce8
 			if (cfg->cfi->defaultIsVariable) {
24fce8
 				fprintf(out, "%sset default=\"%d\"\n", indent,
24fce8
@@ -2417,7 +2421,7 @@ struct singleEntry *findTemplate(struct grubConfig *cfg, const char *prefix,
24fce8
 				}
24fce8
 			}
24fce8
 		}
24fce8
-	} else if (cfg->defaultImage > -1) {
24fce8
+	} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
24fce8
 		entry = findEntryByIndex(cfg, cfg->defaultImage);
24fce8
 		if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
24fce8
 			if (indexPtr)
24fce8
@@ -2499,20 +2503,20 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
24fce8
 	int i, j;
24fce8
 
24fce8
 	if (newBootEntryIsDefault) {
24fce8
-		config->defaultImage = 0;
24fce8
+		config->defaultImage = FIRST_ENTRY_INDEX;
24fce8
 		return;
24fce8
 	} else if ((newDefaultBootEntryIndex >= 0) && config->cfi->defaultIsIndex) {
24fce8
 		if (findEntryByIndex(config, newDefaultBootEntryIndex))
24fce8
 			config->defaultImage = newDefaultBootEntryIndex;
24fce8
 		else
24fce8
-			config->defaultImage = -1;
24fce8
+			config->defaultImage = NO_DEFAULT_ENTRY;
24fce8
 		return;
24fce8
 	} else if (defaultKernelPath) {
24fce8
 		i = 0;
24fce8
 		if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
24fce8
 			config->defaultImage = i;
24fce8
 		} else {
24fce8
-			config->defaultImage = -1;
24fce8
+			config->defaultImage = NO_DEFAULT_ENTRY;
24fce8
 			return;
24fce8
 		}
24fce8
 	}
24fce8
@@ -2524,7 +2528,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
24fce8
 		/* default is set to saved, we don't want to change it */
24fce8
 		return;
24fce8
 
24fce8
-	if (config->defaultImage > -1)
24fce8
+	if (config->defaultImage >= FIRST_ENTRY_INDEX)
24fce8
 		entry = findEntryByIndex(config, config->defaultImage);
24fce8
 	else
24fce8
 		entry = NULL;
24fce8
@@ -2541,7 +2545,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
24fce8
 				config->defaultImage--;
24fce8
 		}
24fce8
 	} else if (isUserSpecifiedKernelPath) {
24fce8
-		config->defaultImage = 0;
24fce8
+		config->defaultImage = FIRST_ENTRY_INDEX;
24fce8
 	} else {
24fce8
 		/* Either we just erased the default (or the default line was
24fce8
 		 * bad to begin with) and didn't put a new one in. We'll use
24fce8
@@ -2550,7 +2554,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
24fce8
 		    findTemplate(config, prefix, &config->defaultImage, 1,
24fce8
 				 flags);
24fce8
 		if (!newDefault)
24fce8
-			config->defaultImage = -1;
24fce8
+			config->defaultImage = NO_DEFAULT_ENTRY;
24fce8
 	}
24fce8
 }
24fce8
 
24fce8
@@ -5175,11 +5179,11 @@ int main(int argc, const char **argv)
24fce8
 		struct singleEntry *entry;
24fce8
 		char *rootspec;
24fce8
 
24fce8
-		if (config->defaultImage == -1)
24fce8
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
24fce8
 			return 0;
24fce8
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
24fce8
 		    cfi->defaultIsSaved)
24fce8
-			config->defaultImage = 0;
24fce8
+			config->defaultImage = FIRST_ENTRY_INDEX;
24fce8
 		entry = findEntryByIndex(config, config->defaultImage);
24fce8
 		if (!entry)
24fce8
 			return 0;
24fce8
@@ -5202,11 +5206,11 @@ int main(int argc, const char **argv)
24fce8
 		struct singleLine *line;
24fce8
 		struct singleEntry *entry;
24fce8
 
24fce8
-		if (config->defaultImage == -1)
24fce8
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
24fce8
 			return 0;
24fce8
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
24fce8
 		    cfi->defaultIsSaved)
24fce8
-			config->defaultImage = 0;
24fce8
+			config->defaultImage = FIRST_ENTRY_INDEX;
24fce8
 		entry = findEntryByIndex(config, config->defaultImage);
24fce8
 		if (!entry)
24fce8
 			return 0;
24fce8
@@ -5236,11 +5240,11 @@ int main(int argc, const char **argv)
24fce8
 		return 0;
24fce8
 
24fce8
 	} else if (displayDefaultIndex) {
24fce8
-		if (config->defaultImage == -1)
24fce8
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
24fce8
 			return 0;
24fce8
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
24fce8
 		    cfi->defaultIsSaved)
24fce8
-			config->defaultImage = 0;
24fce8
+			config->defaultImage = FIRST_ENTRY_INDEX;
24fce8
 		printf("%i\n", config->defaultImage);
24fce8
 		return 0;
24fce8
 
24fce8
-- 
24fce8
2.17.1
24fce8