]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virFileWaitForExists
authorErik Skultety <eskultet@redhat.com>
Tue, 20 Jun 2017 14:09:33 +0000 (16:09 +0200)
committerErik Skultety <eskultet@redhat.com>
Thu, 19 Oct 2017 06:54:53 +0000 (08:54 +0200)
Since we have a number of places where we workaround timing issues with
devices, attributes (files in general) not being available at the time
of processing them by calling usleep in a loop for a fixed number of
tries, we could as well have a utility function that would do that.
Therefore we won't have to duplicate this ugly workaround even more.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index 7bd21ae233b877f7abfc4f59ccfe9ed9864506af..3b5df28e51cfa16ce090b760e640529adf4d34a2 100644 (file)
@@ -1756,6 +1756,7 @@ virFileStripSuffix;
 virFileTouch;
 virFileUnlock;
 virFileUpdatePerm;
+virFileWaitForExists;
 virFileWrapperFdClose;
 virFileWrapperFdFree;
 virFileWrapperFdNew;
index 7ca60052d2009f24759d1359fc4b292e3d0323d5..82cb36dbca9ca9a6ababe47be42fb5ccda1c4c56 100644 (file)
@@ -4177,3 +4177,34 @@ virFileReadValueString(char **value, const char *format, ...)
     VIR_FREE(str);
     return ret;
 }
+
+
+/**
+ * virFileWaitForExists:
+ * @path: absolute path to a sysfs attribute (can be a symlink)
+ * @ms: how long to wait (in milliseconds)
+ * @tries: how many times should we try to wait for @path to become accessible
+ *
+ * Checks the existence of @path. In case the file defined by @path
+ * doesn't exist, we wait for it to appear in @ms milliseconds (for up to
+ * @tries attempts).
+ *
+ * Returns 0 on success, -1 on error, setting errno appropriately.
+ */
+int
+virFileWaitForExists(const char *path,
+                     size_t ms,
+                     size_t tries)
+{
+    errno = 0;
+
+    /* wait for @path to be accessible in @ms milliseconds, up to @tries */
+    while (tries-- > 0 && !virFileExists(path)) {
+        if (tries == 0 || errno != ENOENT)
+            return -1;
+
+        usleep(ms * 1000);
+    }
+
+    return 0;
+}
index 21fb41b70750623ad3fffc1cd2a0f4fb0820d088..91d31862233e7d8c45ae9ef9dcf82bd979025d9c 100644 (file)
@@ -349,6 +349,8 @@ int virFileReadValueScaledInt(unsigned long long *value, const char *format, ...
 int virFileReadValueString(char **value, const char *format, ...)
  ATTRIBUTE_FMT_PRINTF(2, 3);
 
+int virFileWaitForExists(const char *path, size_t ms, size_t tries);
+
 
 int virFileInData(int fd,
                   int *inData,