linux-dev-lookup.c: Put the check for replay_redirect in the beginning
[fio.git] / oslib / linux-dev-lookup.c
CommitLineData
40bafa33
JA
1#include <sys/types.h>
2#include <sys/stat.h>
036a1599 3#include <sys/sysmacros.h>
40bafa33
JA
4#include <dirent.h>
5#include <string.h>
6#include <stdio.h>
7#include <unistd.h>
8
10c37df1 9#include "linux-dev-lookup.h"
40bafa33
JA
10
11int blktrace_lookup_device(const char *redirect, char *path, unsigned int maj,
12 unsigned int min)
13{
14 struct dirent *dir;
15 struct stat st;
16 int found = 0;
17 DIR *D;
18
d9d60dbf
LS
19 /*
20 * If replay_redirect is set then always return this device
21 * upon lookup which overrides the device lookup based on
22 * major minor in the actual blktrace
23 */
24 if (redirect) {
25 strcpy(path, redirect);
26 return 1;
27 }
28
40bafa33
JA
29 D = opendir(path);
30 if (!D)
31 return 0;
32
33 while ((dir = readdir(D)) != NULL) {
1c3fa6c9 34 char full_path[257];
40bafa33
JA
35
36 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
37 continue;
38
22497370 39 sprintf(full_path, "%s/%s", path, dir->d_name);
40bafa33
JA
40 if (lstat(full_path, &st) == -1) {
41 perror("lstat");
42 break;
43 }
44
45 if (S_ISDIR(st.st_mode)) {
46 found = blktrace_lookup_device(redirect, full_path,
47 maj, min);
48 if (found) {
49 strcpy(path, full_path);
50 break;
51 }
52 }
53
54 if (!S_ISBLK(st.st_mode))
55 continue;
56
40bafa33
JA
57 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
58 strcpy(path, full_path);
59 found = 1;
60 break;
61 }
62 }
63
64 closedir(D);
65 return found;
66}