From d2bfb6878aed404fdba1447d3f813edb4442ff47 Mon Sep 17 00:00:00 2001 From: Marco Nenciarini Date: Sat, 31 Jan 2015 14:06:54 +0100 Subject: [PATCH] Improve pg_check_dir comments and code Update the pg_check_dir comment to explain all the possible return values (0 to 4). Fix the beaviour in presence of lost+found directory. The previous version was returning 3 (mount point) even if the readdir returns something after the lost+found directory. In this case the output should be 4 (not empty). Ensure that closedir are always called even if readdir returns an error. --- src/port/pgcheckdir.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/port/pgcheckdir.c b/src/port/pgcheckdir.c index 7061893..02a048c 100644 *** a/src/port/pgcheckdir.c --- b/src/port/pgcheckdir.c *************** *** 22,28 **** * Returns: * 0 if nonexistent * 1 if exists and empty ! * 2 if exists and not empty * -1 if trouble accessing directory (errno reflects the error) */ int --- 22,30 ---- * Returns: * 0 if nonexistent * 1 if exists and empty ! * 2 if exists and contains _only_ dot files ! * 3 if exists and contains a mount point ! * 4 if exists and not empty * -1 if trouble accessing directory (errno reflects the error) */ int *************** pg_check_dir(const char *dir) *** 32,37 **** --- 34,40 ---- DIR *chkdir; struct dirent *file; bool dot_found = false; + bool mount_found = false; chkdir = opendir(dir); if (chkdir == NULL) *************** pg_check_dir(const char *dir) *** 51,60 **** { dot_found = true; } else if (strcmp("lost+found", file->d_name) == 0) { ! result = 3; /* not empty, mount point */ ! break; } #endif else --- 54,63 ---- { dot_found = true; } + /* lost+found directory */ else if (strcmp("lost+found", file->d_name) == 0) { ! mount_found = true; } #endif else *************** pg_check_dir(const char *dir) *** 64,72 **** } } ! if (errno || closedir(chkdir)) result = -1; /* some kind of I/O error? */ /* We report on dot-files if we _only_ find dot files */ if (result == 1 && dot_found) result = 2; --- 67,82 ---- } } ! if (errno) result = -1; /* some kind of I/O error? */ + if (closedir(chkdir)) + result = -1; /* error executing closedir */ + + /* We report on mount point if we find a lost+found directory */ + if (result == 1 && mount_found) + result = 3; + /* We report on dot-files if we _only_ find dot files */ if (result == 1 && dot_found) result = 2; -- 2.2.2