[pacman-dev] [PATCH] Use access() instead of stat() when possible

Dan McGee dan at archlinux.org
Sun Jun 15 23:49:15 EDT 2008


We were using the stat() system call in quite a few places when we didn't
actually need anything the stat struct returned- we were simply checking for
file existence. access() will be more efficient in those cases.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 lib/libalpm/be_files.c |    5 ++---
 lib/libalpm/trans.c    |    8 ++++----
 lib/libalpm/util.c     |   12 ++++--------
 src/pacman/package.c   |    5 ++---
 src/pacman/util.c      |    6 ++----
 src/util/testdb.c      |    8 +++-----
 6 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index 12c60b2..f5d4e82 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -355,7 +355,6 @@ static char *get_pkgpath(pmdb_t *db, pmpkg_t *info)
 int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
 {
 	FILE *fp = NULL;
-	struct stat buf;
 	char path[PATH_MAX];
 	char line[513];
 	char *pkgpath = NULL;
@@ -393,7 +392,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
 
 	pkgpath = get_pkgpath(db, info);
 
-	if(stat(pkgpath, &buf)) {
+	if(access(pkgpath, F_OK)) {
 		/* directory doesn't exist or can't be opened */
 		_alpm_log(PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
 				info->name, info->version, db->treename);
@@ -631,7 +630,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
 	/* INSTALL */
 	if(inforeq & INFRQ_SCRIPTLET) {
 		snprintf(path, PATH_MAX, "%sinstall", pkgpath);
-		if(!stat(path, &buf)) {
+		if(access(path, F_OK) == 0) {
 			info->scriptlet = 1;
 		}
 	}
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c
index eb53e95..06084ae 100644
--- a/lib/libalpm/trans.c
+++ b/lib/libalpm/trans.c
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/wait.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -451,7 +452,6 @@ int _alpm_runscriptlet(const char *root, const char *installfn,
 	char tmpdir[PATH_MAX];
 	char cwd[PATH_MAX];
 	char *scriptpath;
-	struct stat buf;
 	pid_t pid;
 	int clean_tmpdir = 0;
 	int restore_cwd = 0;
@@ -459,14 +459,14 @@ int _alpm_runscriptlet(const char *root, const char *installfn,
 
 	ALPM_LOG_FUNC;
 
-	if(stat(installfn, &buf)) {
+	if(access(installfn, R_OK)) {
 		/* not found */
 		_alpm_log(PM_LOG_DEBUG, "scriptlet '%s' not found\n", installfn);
 		return(0);
 	}
 
 	/* NOTE: popen will use the PARENT's /bin/sh, not the chroot's */
-	if(stat("/bin/sh", &buf)) {
+	if(access("/bin/sh", X_OK)) {
 		/* not found */
 		_alpm_log(PM_LOG_ERROR, _("No /bin/sh in parent environment, aborting scriptlet\n"));
 		return(0);
@@ -474,7 +474,7 @@ int _alpm_runscriptlet(const char *root, const char *installfn,
 
 	/* creates a directory in $root/tmp/ for copying/extracting the scriptlet */
 	snprintf(tmpdir, PATH_MAX, "%stmp/", root);
-	if(stat(tmpdir, &buf)) {
+	if(access(tmpdir, F_OK) != 0) {
 		_alpm_makepath_mode(tmpdir, 01777);
 	}
 	snprintf(tmpdir, PATH_MAX, "%stmp/alpm_XXXXXX", root);
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 55bd46a..9199545 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -200,12 +200,11 @@ int _alpm_makepath_mode(const char *path, mode_t mode)
 	str = orig;
 	while((ptr = strsep(&str, "/"))) {
 		if(strlen(ptr)) {
-			struct stat buf;
 			/* we have another path component- append the newest component to
 			 * existing string and create one more level of dir structure */
 			strcat(incr, "/");
 			strcat(incr, ptr);
-			if(stat(incr, &buf)) {
+			if(access(incr, F_OK)) {
 				if(mkdir(incr, mode)) {
 					ret = 1;
 					break;
@@ -533,12 +532,11 @@ int _alpm_logaction(unsigned short usesyslog, FILE *f, const char *fmt, va_list
 int _alpm_ldconfig(const char *root)
 {
 	char line[PATH_MAX];
-	struct stat buf;
 
 	snprintf(line, PATH_MAX, "%setc/ld.so.conf", root);
-	if(stat(line, &buf) == 0) {
+	if(access(line, F_OK) == 0) {
 		snprintf(line, PATH_MAX, "%ssbin/ldconfig", root);
-		if(stat(line, &buf) == 0) {
+		if(access(line, X_OK) == 0) {
 			char cmd[PATH_MAX];
 			snprintf(cmd, PATH_MAX, "%s -r %s", line, root);
 			system(cmd);
@@ -561,7 +559,6 @@ int _alpm_str_cmp(const void *s1, const void *s2)
  */
 char *_alpm_filecache_find(const char* filename)
 {
-	struct stat buf;
 	char path[PATH_MAX];
 	char *retpath;
 	alpm_list_t *i;
@@ -570,8 +567,7 @@ char *_alpm_filecache_find(const char* filename)
 	for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
 		snprintf(path, PATH_MAX, "%s%s", (char*)alpm_list_getdata(i),
 				filename);
-		if(stat(path, &buf) == 0) {
-			/* TODO maybe check to make sure it is readable? */
+		if(access(path, R_OK) == 0) {
 			retpath = strdup(path);
 			_alpm_log(PM_LOG_DEBUG, "found cached pkg: %s\n", retpath);
 			return(retpath);
diff --git a/src/pacman/package.c b/src/pacman/package.c
index 1698806..0680037 100644
--- a/src/pacman/package.c
+++ b/src/pacman/package.c
@@ -22,8 +22,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 #include <limits.h>
-#include <sys/stat.h>
 #include <wchar.h>
 
 #include <alpm.h>
@@ -176,7 +176,6 @@ void dump_pkg_backups(pmpkg_t *pkg)
 	if(alpm_pkg_get_backup(pkg)) {
 		/* package has backup files, so print them */
 		for(i = alpm_pkg_get_backup(pkg); i; i = alpm_list_next(i)) {
-			struct stat buf;
 			char path[PATH_MAX];
 			char *str = strdup(alpm_list_getdata(i));
 			char *ptr = index(str, '\t');
@@ -188,7 +187,7 @@ void dump_pkg_backups(pmpkg_t *pkg)
 			ptr++;
 			snprintf(path, PATH_MAX-1, "%s%s", root, str);
 			/* if we find the file, calculate checksums, otherwise it is missing */
-			if(!stat(path, &buf)) {
+			if(access(path, R_OK) == 0) {
 				char *md5sum = alpm_get_md5sum(path);
 
 				if(md5sum == NULL) {
diff --git a/src/pacman/util.c b/src/pacman/util.c
index e702886..d7ac9e3 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -22,7 +22,6 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
-#include <sys/stat.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -125,7 +124,7 @@ int makepath(const char *path)
 	 * orig - a copy of path so we can safely butcher it with strsep
 	 * str - the current position in the path string (after the delimiter)
 	 * ptr - the original position of str after calling strsep
-	 * incr - incrementally generated path for use in stat/mkdir call
+	 * incr - incrementally generated path for use in access/mkdir call
 	 */
 	char *orig, *str, *ptr, *incr;
 	mode_t oldmask = umask(0000);
@@ -136,12 +135,11 @@ int makepath(const char *path)
 	str = orig;
 	while((ptr = strsep(&str, "/"))) {
 		if(strlen(ptr)) {
-			struct stat buf;
 			/* we have another path component- append the newest component to
 			 * existing string and create one more level of dir structure */
 			strcat(incr, "/");
 			strcat(incr, ptr);
-			if(stat(incr, &buf)) {
+			if(access(incr, F_OK)) {
 				if(mkdir(incr, 0755)) {
 					ret = 1;
 					break;
diff --git a/src/util/testdb.c b/src/util/testdb.c
index f354eca..87bfcf9 100644
--- a/src/util/testdb.c
+++ b/src/util/testdb.c
@@ -23,7 +23,6 @@
 #include <errno.h>
 #include <limits.h>
 #include <string.h>
-#include <sys/stat.h>
 #include <dirent.h>
 #include <libgen.h>
 
@@ -61,7 +60,6 @@ static int db_test(char *dbpath)
 {
 	struct dirent *ent;
 	char path[PATH_MAX];
-	struct stat buf;
 	int ret = 0;
 
 	DIR *dir;
@@ -77,17 +75,17 @@ static int db_test(char *dbpath)
 		}
 		/* check for desc, depends, and files */
 		snprintf(path, PATH_MAX, "%s/%s/desc", dbpath, ent->d_name);
-		if(stat(path, &buf)) {
+		if(access(path, F_OK)) {
 			printf("%s: description file is missing\n", ent->d_name);
 			ret++;
 		}
 		snprintf(path, PATH_MAX, "%s/%s/depends", dbpath, ent->d_name);
-		if(stat(path, &buf)) {
+		if(access(path, F_OK)) {
 			printf("%s: dependency file is missing\n", ent->d_name);
 			ret++;
 		}
 		snprintf(path, PATH_MAX, "%s/%s/files", dbpath, ent->d_name);
-		if(stat(path, &buf)) {
+		if(access(path, F_OK)) {
 			printf("%s: file list is missing\n", ent->d_name);
 			ret++;
 		}
-- 
1.5.5.3





More information about the pacman-dev mailing list