Home | History | Annotate | Download | only in oslib
      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 "linux-dev-lookup.h"
      9 
     10 int blktrace_lookup_device(const char *redirect, char *path, unsigned int maj,
     11 			   unsigned int min)
     12 {
     13 	struct dirent *dir;
     14 	struct stat st;
     15 	int found = 0;
     16 	DIR *D;
     17 
     18 	D = opendir(path);
     19 	if (!D)
     20 		return 0;
     21 
     22 	while ((dir = readdir(D)) != NULL) {
     23 		char full_path[256];
     24 
     25 		if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
     26 			continue;
     27 
     28 		sprintf(full_path, "%s/%s", path, dir->d_name);
     29 		if (lstat(full_path, &st) == -1) {
     30 			perror("lstat");
     31 			break;
     32 		}
     33 
     34 		if (S_ISDIR(st.st_mode)) {
     35 			found = blktrace_lookup_device(redirect, full_path,
     36 								maj, min);
     37 			if (found) {
     38 				strcpy(path, full_path);
     39 				break;
     40 			}
     41 		}
     42 
     43 		if (!S_ISBLK(st.st_mode))
     44 			continue;
     45 
     46 		/*
     47 		 * If replay_redirect is set then always return this device
     48 		 * upon lookup which overrides the device lookup based on
     49 		 * major minor in the actual blktrace
     50 		 */
     51 		if (redirect) {
     52 			strcpy(path, redirect);
     53 			found = 1;
     54 			break;
     55 		}
     56 
     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 }
     67