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