client: use temp buffer for single output flush for json/disk util
[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
19 D = opendir(path);
20 if (!D)
21 return 0;
22
23 while ((dir = readdir(D)) != NULL) {
1c3fa6c9 24 char full_path[257];
40bafa33
JA
25
26 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
27 continue;
28
22497370 29 sprintf(full_path, "%s/%s", path, dir->d_name);
40bafa33
JA
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}