Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common/blkdev: get_block_device_base
And a test. Ideally the test would get run on a machine with weirdo devices like /dev/cciss/c0d1. This mirrors the logic in ceph-disk, which has been tested. Signed-off-by: Sage Weil <sage@redhat.com>
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- | ||
// vim: ts=8 sw=2 smarttab | ||
|
||
#include <iostream> | ||
#include <string.h> | ||
#include <errno.h> | ||
|
||
#include "include/types.h" | ||
#include "common/blkdev.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
TEST(blkdev, get_block_device_base) { | ||
char buf[PATH_MAX*2]; | ||
char buf2[PATH_MAX*2]; | ||
char buf3[PATH_MAX*2]; | ||
struct dirent *de, *de2; | ||
|
||
ASSERT_EQ(-ERANGE, get_block_device_base("/dev/sda1", buf, 1)); | ||
|
||
// work backwards | ||
DIR *dir = opendir("/sys/block"); | ||
ASSERT_TRUE(dir); | ||
while (!::readdir_r(dir, reinterpret_cast<struct dirent*>(buf), &de)) { | ||
if (!de) | ||
break; | ||
if (de->d_name[0] == '.') | ||
continue; | ||
|
||
char base[PATH_MAX]; | ||
sprintf(base, "/dev/%s", de->d_name); | ||
printf("base %s (%s)\n", base, de->d_name); | ||
for (char *p = base; *p; ++p) | ||
if (*p == '!') | ||
*p = '/'; | ||
|
||
ASSERT_EQ(0, get_block_device_base(base, buf3, sizeof(buf3))); | ||
printf(" got '%s' expected '%s'\n", buf3, de->d_name); | ||
ASSERT_EQ(0, strcmp(de->d_name, buf3)); | ||
|
||
char subdirfn[PATH_MAX]; | ||
sprintf(subdirfn, "/sys/block/%s", de->d_name); | ||
DIR *subdir = opendir(subdirfn); | ||
ASSERT_TRUE(subdir); | ||
while (!::readdir_r(subdir, reinterpret_cast<struct dirent*>(buf2), &de2)) { | ||
if (!de2) | ||
break; | ||
if (de2->d_name[0] == '.') | ||
continue; | ||
// partiions will be prefixed with the base name | ||
if (strncmp(de2->d_name, de->d_name, strlen(de->d_name))) { | ||
//printf("skipping %s\n", de2->d_name); | ||
continue; | ||
} | ||
char part[PATH_MAX]; | ||
sprintf(part, "/dev/%s", de2->d_name); | ||
for (char *p = part; *p; ++p) | ||
if (*p == '!') | ||
*p = '/'; | ||
printf(" part %s (%s %s)\n", part, de->d_name, de2->d_name); | ||
|
||
ASSERT_EQ(0, get_block_device_base(part, buf3, sizeof(buf3))); | ||
printf(" got '%s' expected '%s'\n", buf3, de->d_name); | ||
ASSERT_EQ(0, strcmp(buf3, de->d_name)); | ||
} | ||
|
||
closedir(subdir); | ||
} | ||
closedir(dir); | ||
} |