]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: guarantee sane errno in virFileIsExecutable
authorEric Blake <eblake@redhat.com>
Fri, 18 Mar 2011 20:22:19 +0000 (14:22 -0600)
committerEric Blake <eblake@redhat.com>
Mon, 21 Mar 2011 15:22:30 +0000 (09:22 -0600)
If virFileIsExecutable is to replace access(file,X_OK), then
errno must be usable on failure.

* src/util/util.c (virFileIsExecutable): Set errno on failure.

src/util/util.c

index 5b5dd5e208605b94db4000b98291c0021a4a5d9a..1e4e2abb4b4741da2a2c34b3bae6ce21fc053696 100644 (file)
@@ -1357,7 +1357,8 @@ bool virFileExists(const char *path)
     return access(path, F_OK) == 0;
 }
 
-/* Check that a file is regular and has executable bits.
+/* Check that a file is regular and has executable bits.  If false is
+ * returned, errno is valid.
  *
  * Note: In the presence of ACLs, this may return true for a file that
  * would actually fail with EACCES for a given user, or false for a
@@ -1370,9 +1371,12 @@ virFileIsExecutable(const char *file)
 
     /* We would also want to check faccessat if we cared about ACLs,
      * but we don't.  */
-    return (stat(file, &sb) == 0 &&
-            S_ISREG(sb.st_mode) &&
-            (sb.st_mode & 0111) != 0);
+    if (stat(file, &sb) < 0)
+        return false;
+    if (S_ISREG(sb.st_mode) && (sb.st_mode & 0111) != 0)
+        return true;
+    errno = S_ISDIR(sb.st_mode) ? EISDIR : EACCES;
+    return false;
 }
 
 #ifndef WIN32