File ppc64-diag.bug-1072708_create_diag_disk_log_directory.patch of Package ppc64-diag

commit d2ec2733829b69f8b935f328f5cc2396e4c2af70
Author: Ankit Kumar <ankit@linux.vnet.ibm.com>
Date:   Tue Dec 5 14:56:14 2017 +0530

    diags: Create diag_disk log directory manually if not present
    
    disk diagnostics code populates disk health information under
    /var/log/ppc64-diag/diag_disk. If above path is not found then
    disk diagnostics will fail.
    This patch creates /var/log/ppc64-diag/diag_disk/ manually part of
    diag_disk code if not already created.
    It will help diag_disk code to remove dependency on ppc64-diag installation
    process.
    
    Signed-off-by: Ankit Kumar <ankit@linux.vnet.ibm.com>
    [Removed inline function, renamed function name and also killed some
     of the redundant checks - Vasant]
    Signed-off-by: Vasant Hegde <hegdevasant@linux.vnet.ibm.com>

Index: ppc64-diag-2.7.4/diags/diag_disk.c
===================================================================
--- ppc64-diag-2.7.4.orig/diags/diag_disk.c
+++ ppc64-diag-2.7.4/diags/diag_disk.c
@@ -38,7 +38,8 @@
 
 #include "encl_util.h"
 
-#define OUTPUT_PATH			"/var/log/ppc64-diag/diag_disk"
+#define DIAG_OUTPUT_PATH		"/var/log/ppc64-diag/"
+#define DISK_OUTPUT_PATH		DIAG_OUTPUT_PATH"diag_disk"
 #define SYSFS_SG_PATH			"/sys/class/scsi_generic"
 #define DEVICE_TREE			"/proc/device-tree/"
 #define DEVICE_TREE_SYSTEM_ID		DEVICE_TREE"system-id"
@@ -142,12 +143,53 @@ static int get_page_34_data(int device_f
 	return 0;
 }
 
-static inline int open_output_xml_file(const char *xml_filename)
+static inline void dir_sync(char * path)
+{
+	int dir_fd;
+
+	dir_fd = open(path, O_RDONLY|O_DIRECTORY);
+	if (dir_fd >= 0) {
+		fsync(dir_fd);
+		close(dir_fd);
+	}
+}
+
+static int open_output_xml_file(const char *xml_filename)
 {
 	char filename[PATH_MAX];
+	int rc;
+
+	rc = access(DISK_OUTPUT_PATH, W_OK);
+	if (rc) {
+		/* Return if it fails with error code other than ENOENT */
+		if (errno != ENOENT)
+			return -1;
+
+		/* Check for the existence of parent directory */
+		rc = access(DIAG_OUTPUT_PATH, W_OK);
+		if (rc) {
+			if (errno != ENOENT)
+				return -1;
+
+			rc = mkdir(DIAG_OUTPUT_PATH,
+				S_IRGRP | S_IRUSR | S_IWGRP | S_IWUSR | S_IXUSR);
+			if (rc)
+				return -1;
+
+			dir_sync(DIAG_OUTPUT_PATH);
+		}
+
+		rc = mkdir(DISK_OUTPUT_PATH,
+			   S_IRGRP | S_IRUSR | S_IWGRP | S_IWUSR | S_IXUSR);
+		if (rc)
+			return -1;
+
+		dir_sync(DISK_OUTPUT_PATH);
+	}
+
 
 	snprintf(filename, sizeof(filename) - 1, "%s/%s",
-		 OUTPUT_PATH, xml_filename);
+		 DISK_OUTPUT_PATH, xml_filename);
 
 	result_file = fopen(filename, "w");
 	if (!result_file)
@@ -346,9 +388,8 @@ static int remove_old_log_file(void)
 	DIR *d;
 	struct dirent *namelist;
 	char filename[PATH_MAX];
-	int dir_fd;
 
-	d = opendir(OUTPUT_PATH);
+	d = opendir(DISK_OUTPUT_PATH);
 	if (!d)
 		return -errno;
 
@@ -356,22 +397,17 @@ static int remove_old_log_file(void)
 		if (namelist->d_name[0] == '.')
 			continue;
 
-		snprintf(filename, sizeof(filename) - 1, "%s/%s", OUTPUT_PATH,
-			namelist->d_name);
+		snprintf(filename, sizeof(filename) - 1, "%s/%s",
+				DISK_OUTPUT_PATH, namelist->d_name);
 		if (unlink(filename) < 0) {
 			fprintf(stderr,
 			"\nUnable to remove old log file[%s]. continuing.\n\n",
 			filename);
 		}
 	}
-	closedir(d);
-
-	dir_fd = open(OUTPUT_PATH, O_RDONLY|O_DIRECTORY);
-	if (dir_fd >= 0) {
-		fsync(dir_fd);
-		close(dir_fd);
-	}
 
+	closedir(d);
+	dir_sync(DISK_OUTPUT_PATH);
 	return 0;
 }
 
openSUSE Build Service is sponsored by