Skip to content

Commit

Permalink
util: Introduce virFileWaitForExists
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
eskultety committed Oct 19, 2017
1 parent cdbe133 commit caf2641
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libvirt_private.syms
Expand Up @@ -1756,6 +1756,7 @@ virFileStripSuffix;
virFileTouch;
virFileUnlock;
virFileUpdatePerm;
virFileWaitForExists;
virFileWrapperFdClose;
virFileWrapperFdFree;
virFileWrapperFdNew;
Expand Down
31 changes: 31 additions & 0 deletions src/util/virfile.c
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/util/virfile.h
Expand Up @@ -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,
Expand Down

0 comments on commit caf2641

Please sign in to comment.