-
Notifications
You must be signed in to change notification settings - Fork 1
/
safepath_linux.cc
82 lines (79 loc) · 2.51 KB
/
safepath_linux.cc
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
/*
* This file is part of vbig.
* Copyright (C) 2019 Richard Kettlewell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vbig.h"
#if __linux__
#include <sys/stat.h>
#include <sys/types.h>
#include <cstdio>
#include <cerrno>
#include <unistd.h>
#include <sstream>
#include <json/json.h>
// Return true if path is a block device
bool is_block_device(const std::string &path) {
struct stat sb;
if(stat(path.c_str(), &sb) < 0)
return 0;
if(S_ISBLK(sb.st_mode))
return true;
#if __APPLE__
// /dev/rdisk* are character versions of the disk devices
if(S_ISCHR(sb.st_mode) && major(sb.st_rdev) == 1)
return true;
#endif
return false;
}
// Return true if path is in use (it must be a block device)
bool block_device_in_use(const std::string &path) {
// Ask lslbk about the target path
const char *args[] = {"lsblk", "--json", path.c_str(), (char *)NULL};
std::string json;
capture(json, args[0], args);
Json::Value root;
std::istringstream s(json);
s >> root; // throws on error.
Json::Value &dev = root["blockdevices"][0];
Json::Value &mountpoint = dev["mountpoint"];
// Don't overwrite mounted filesystems
if(!mountpoint.isNull()) {
fprintf(stderr, "WARNING: %s is mounted on %s\n",
mountpoint.asString().c_str(), path.c_str());
return true;
}
// Don't overwrite devices with children.
// This means devices with partition tables, LVM containers, etc.
Json::Value &children = dev["children"];
if(!children.isNull()) {
// Get list of children
std::ostringstream childs;
bool first = true;
for(Json::Value::iterator it = children.begin(); it != children.end();
++it) {
const Json::Value &child = *it;
if(!first)
childs << ",";
first = false;
childs << child["name"].asString();
}
fprintf(stderr, "WARNING: %s is in use: %s\n", path.c_str(),
childs.str().c_str());
return true;
}
return false;
}
#endif