-
Notifications
You must be signed in to change notification settings - Fork 3
/
fsview_name.cpp
144 lines (128 loc) · 3.98 KB
/
fsview_name.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2022 Light Labs Inc.
* All Rights Reserved
* Licensed under the MIT license.
*/
#include "wrapper.h"
#include "conf/config.h"
#include "impl/mapper.h"
struct Origami
{
std::string name;
bool hasDevId = false;
dev_t devId = 0;
std::string path;
void setDevId( dev_t id ) { devId = id; hasDevId = true; }
};
int main( int argc, char ** argv )
{
NameConf cfg;
cfg.parse( argc, argv );
if( !argc ) { return 0; }
if( cfg.oneprop )
{
if( ( argc > 1 ) )
{
fprintf( stderr, "More than one name to query, can't use --property. "
"Use --properties=<property.prefix> to query multiple names.\n" );
exit( 1 );
}
if( cfg.setprop )
{
fprintf( stderr, "Both --property and --properties set. Use one.\n" );
exit( 1 );
}
}
std::vector<Origami> requests;
requests.resize( argc );
std::set<dev_t> devIds; // multimap with numbers
// std::vector<dev_t> lsdev;
std::map<dev_t, std::string> devnm;
// get dev_t from name
Mapper mapper( cfg.dmControl, O_RDONLY );
for( int i = 0; i < argc; ++i )
{
const char * name = argv[i];
Origami & request = requests[i];
request.name = name;
// get device status. don't throw!
if( mapper.deviceStatus( name ) >= 0 )
{
auto dev = mapper.dmw().dev;
devIds.insert( dev );
request.setDevId( dev );
}
else
{
fprintf( stderr, "Name not found: %s\n", name );
}
}
int errors = 0;
// extract as findBlkDev( onBlkDev )
if( devIds.size() )
{
std::string prop;
if( cfg.oneprop ) { prop = cfg.oneprop; }
else if( cfg.setprop ) { prop = cfg.setprop; prop.push_back( '.' ); } // DELIMITER2
size_t prop_trim = prop.size();
DIR * catDir = opendir( cfg.dev_cat );
if( catDir )
{
struct dirent64 entry;
struct dirent64 * canary;
struct stat64 st;
while( devIds.size() && ( readdir64_r( catDir, &entry, &canary ), canary ) )
{
if( entry.d_type == DT_BLK
&& fstatat64( dirfd( catDir ), entry.d_name, &st, AT_NO_AUTOMOUNT ) >= 0 )
{
auto itr = devIds.find( st.st_rdev );
if( itr != devIds.end() )
{
devnm[st.st_rdev] = entry.d_name;
devIds.erase( itr );
}
}
}
closedir( catDir );
}
else { perror( cfg.dev_cat ); exit( 2 ); }
if( 0 /*cfg.tmp_cat*/ )
{
//
}
else
{
for( auto dev : devIds )
{
fprintf( stderr, "Node not found: %u:%u\n",
major( dev ), minor( dev ) );
}
}
for( Origami & request : requests )
{
if( request.hasDevId )
{
auto itr = devnm.find( request.devId );
if( itr != devnm.end() )
{
request.path = cfg.dev_cat;
request.path.push_back( '/' ); // DELIMITER1
request.path.append( itr->second );
if( cfg.setprop || cfg.oneprop )
{
if( cfg.setprop ) // else oneprop
{
prop.resize( prop_trim );
prop.append( request.name );
}
errors |= __system_property_set( prop.c_str(),
request.path.c_str() );
}
}
printf( "%s\n", request.path.c_str() );
}
}
}
return errors; // nonzero if any __system_property_set() call has failed
}