ewmscp  ..
statvfs.cpp
Go to the documentation of this file.
1 #include <sys/statvfs.h>
2 #include <sys/vfs.h>
3 #include <iostream>
4 #include <list>
5 #include "Options.h"
6 #include "throwcall.h"
7 
9  protected:
10  std::string fieldName;
11  std::string explanation;
12  public:
14  fieldHandlerBase(char shortName,
15  const std::string& aName,
16  const std::string& aExplanation) :
17  fieldName(aName),
18  explanation(aExplanation),
19  opt(shortName, aName, aExplanation) {};
20  virtual void print(const std::string& fsname,
21  const struct statvfs& stat,
22  bool printAll) = 0;
23 };
24 
25 static options::single<bool> quiet('q', "quiet", "be quiet: no prefix to values");
26 
27 template <typename T> class fieldHandlerTpl : public fieldHandlerBase {
28  T statvfs::* field;
29  public:
30  fieldHandlerTpl(decltype(field) aField,
31  char shortName,
32  const std::string& aName,
33  const std::string& aExplanation) : fieldHandlerBase(shortName, aName, aExplanation),
34  field(aField) {};
35  void print(const std::string& fsname,
36  const struct statvfs& stat,
37  bool printAll) override {
38  if (printAll || opt) {
39  if (!quiet) {
40  std::cout << fsname << " " << fieldName << ": ";
41  }
42  std::cout << stat.*field << "\n";
43  }
44  }
45 };
46 
47 
48 class idHandler : public fieldHandlerTpl<unsigned long> {
49  public:
50  idHandler(char shortName, const std::string& aName, const std::string& aExplanation) : fieldHandlerTpl<unsigned long>(&statvfs::f_fsid, shortName, aName, aExplanation) {};
51  void print(const std::string& fsname,
52  const struct statvfs& stat,
53  bool printAll) override {
54  std::cout << std::hex;
55  fieldHandlerTpl::print(fsname, stat, printAll);
56  std::cout << std::dec;
57  }
58 };
59 
60 // magic map, create via
61 // awk '/_MAGIC[ \t]/{print "\t{"$3",@"$2"@},"}' /usr/include/linux/magic.h /usr/include/gpfs.h | tr @ '"'
62 static std::map<__fsword_t, std::string> magicMap {
63  {0xadf5, "ADFS_SUPER_MAGIC"},
64  {0xadff, "AFFS_SUPER_MAGIC"},
65  {0x5346414F, "AFS_SUPER_MAGIC"},
66  {0x0187, "AUTOFS_SUPER_MAGIC"},
67  {0x73757245, "CODA_SUPER_MAGIC"},
68  {0x28cd3d45, "CRAMFS_MAGIC"},
69  {0x64626720, "DEBUGFS_MAGIC"},
70  {0x73636673, "SECURITYFS_MAGIC"},
71  {0xf97cff8c, "SELINUX_MAGIC"},
72  {0x43415d53, "SMACK_MAGIC"},
73  {0x858458f6, "RAMFS_MAGIC"},
74  {0x01021994, "TMPFS_MAGIC"},
75  {0x958458f6, "HUGETLBFS_MAGIC"},
76  {0x73717368, "SQUASHFS_MAGIC"},
77  {0xf15f, "ECRYPTFS_SUPER_MAGIC"},
78  {0x414A53, "EFS_SUPER_MAGIC"},
79  {0xEF53, "EXT2_SUPER_MAGIC"},
80  {0xEF53, "EXT3_SUPER_MAGIC"},
81  {0xabba1974, "XENFS_SUPER_MAGIC"},
82  {0xEF53, "EXT4_SUPER_MAGIC"},
83  {0x9123683E, "BTRFS_SUPER_MAGIC"},
84  {0x3434, "NILFS_SUPER_MAGIC"},
85  {0xF2F52010, "F2FS_SUPER_MAGIC"},
86  {0xf995e849, "HPFS_SUPER_MAGIC"},
87  {0x9660, "ISOFS_SUPER_MAGIC"},
88  {0x72b6, "JFFS2_SUPER_MAGIC"},
89  {0x6165676C, "PSTOREFS_MAGIC"},
90  {0xde5e81e4, "EFIVARFS_MAGIC"},
91  {0x00c0ffee, "HOSTFS_SUPER_MAGIC"},
92  {0x794c7630, "OVERLAYFS_SUPER_MAGIC"},
93  {0x137F, "MINIX_SUPER_MAGIC"},
94  {0x2468, "MINIX2_SUPER_MAGIC"},
95  {0x4d5a, "MINIX3_SUPER_MAGIC"},
96  {0x4d44, "MSDOS_SUPER_MAGIC"},
97  {0x564c, "NCP_SUPER_MAGIC"},
98  {0x6969, "NFS_SUPER_MAGIC"},
99  {0x9fa1, "OPENPROM_SUPER_MAGIC"},
100  {0x002f, "QNX4_SUPER_MAGIC"},
101  {0x68191122, "QNX6_SUPER_MAGIC"},
102  {0x52654973, "REISERFS_SUPER_MAGIC"},
103  {0x517B, "SMB_SUPER_MAGIC"},
104  {0x27e0eb, "CGROUP_SUPER_MAGIC"},
105  {0x7655821, "RDTGROUP_SUPER_MAGIC"},
106  {0x57AC6E9D, "STACK_END_MAGIC"},
107  {0x01021997, "V9FS_MAGIC"},
108  {0x62646576, "BDEVFS_MAGIC"},
109  {0x64646178, "DAXFS_MAGIC"},
110  {0x42494e4d, "BINFMTFS_MAGIC"},
111  {0x1cd1, "DEVPTS_SUPER_MAGIC"},
112  {0xBAD1DEA, "FUTEXFS_SUPER_MAGIC"},
113  {0x50495045, "PIPEFS_MAGIC"},
114  {0x9fa0, "PROC_SUPER_MAGIC"},
115  {0x534F434B, "SOCKFS_MAGIC"},
116  {0x62656572, "SYSFS_MAGIC"},
117  {0x9fa2, "USBDEVICE_SUPER_MAGIC"},
118  {0x11307854, "MTD_INODE_FS_MAGIC"},
119  {0x09041934, "ANON_INODE_FS_MAGIC"},
120  {0x73727279, "BTRFS_TEST_MAGIC"},
121  {0xcafe4a11, "BPF_FS_MAGIC"},
122  // extras that are NOT in the magic.h
123  {0x47504653, "GPFS_SUPER_MAGIC"}
124 };
125 
126 int main(int argc, const char *argv[]) {
127  options::parser parser("", "", {});
128  std::list<fieldHandlerBase*> handlers;
129  handlers.emplace_back(new fieldHandlerTpl<unsigned long>(&statvfs::f_bsize, 'b', "bsize", "block size"));
130  handlers.emplace_back(new fieldHandlerTpl<unsigned long>(&statvfs::f_frsize, 'f', "frsize", "fragment size"));
131  handlers.emplace_back(new fieldHandlerTpl<fsblkcnt_t>(&statvfs::f_blocks, 'B', "blocks", "size in frsize units"));
132  handlers.emplace_back(new fieldHandlerTpl<fsblkcnt_t>(&statvfs::f_bfree, 'F', "bfree", "free blocks"));
133  handlers.emplace_back(new fieldHandlerTpl<fsblkcnt_t>(&statvfs::f_bavail, 'A', "bavail", "available blocks"));
134  handlers.emplace_back(new fieldHandlerTpl<fsfilcnt_t>(&statvfs::f_files, 'I', "files", "number of inodes"));
135  handlers.emplace_back(new fieldHandlerTpl<fsfilcnt_t>(&statvfs::f_ffree, 'J', "ffree", "free inodes"));
136  handlers.emplace_back(new fieldHandlerTpl<fsfilcnt_t>(&statvfs::f_favail, 'a', "favail", "available inodes"));
137  handlers.emplace_back(new idHandler('i', "fsid", "filesystem id"));
138  handlers.emplace_back(new fieldHandlerTpl<unsigned long>(&statvfs::f_flag, 'l', "fflag", "flags"));
139  handlers.emplace_back(new fieldHandlerTpl<unsigned long>(&statvfs::f_namemax, 'm', "namemax", "max filename length"));
140  options::single<bool> type('t', "type", "type of filesystem as number");
141  options::single<bool> Type('T', "Type", "type of filesystem as string");
142  options::positional<options::container<std::string>> fileSystems(10, "filesystems", "filesystems to look at");
143  parser.fParse(argc, argv);
144 
145  bool printAll = !(type || Type);
146  for (auto& handler : handlers) {
147  if (handler->opt) {
148  printAll = false;
149  }
150  }
151 
152  for (const auto& filesystem : fileSystems) {
153  {
154  struct statvfs stat;
155  throwcall::good0(statvfs(filesystem.c_str(), &stat), "can't statvfs ", filesystem);
156  for (auto& handler : handlers) {
157  handler->print(filesystem, stat, printAll);
158  }
159  }
160  if (type || Type || printAll) {
161  struct statfs stat;
162  throwcall::good0(statfs(filesystem.c_str(), &stat), "can't statfs ", filesystem);
163  if (!quiet) {
164  std::cout << filesystem << " " << "type" << ": ";
165  }
166  if (type || printAll) {
167  std::cout << stat.f_type;
168  if (Type || printAll) {
169  std::cout << " ";
170  }
171  }
172  if (Type || printAll) {
173  auto item = magicMap.find(stat.f_type);
174  if (item == magicMap.end()) {
175  std::cout << "unknown";
176  } else {
177  std::cout << item->second;
178  }
179  }
180  std::cout << "\n";
181  }
182  }
183 }
options::parser
class that contains the parser, i.e. does that option handling
Definition: Options.h:363
fieldHandlerTpl
Definition: statvfs.cpp:27
options::single< bool >
class specialisation for options of type bool
Definition: Options.h:595
fieldHandlerBase::fieldName
std::string fieldName
Definition: statvfs.cpp:10
fieldHandlerTpl::field
T statvfs::* field
Definition: statvfs.cpp:28
magicMap
static std::map< __fsword_t, std::string > magicMap
Definition: statvfs.cpp:62
fieldHandlerBase::explanation
std::string explanation
Definition: statvfs.cpp:11
idHandler::print
void print(const std::string &fsname, const struct statvfs &stat, bool printAll) override
Definition: statvfs.cpp:51
idHandler::idHandler
idHandler(char shortName, const std::string &aName, const std::string &aExplanation)
Definition: statvfs.cpp:50
Options.h
fieldHandlerBase::fieldHandlerBase
fieldHandlerBase(char shortName, const std::string &aName, const std::string &aExplanation)
Definition: statvfs.cpp:14
main
int main(int argc, const char *argv[])
Definition: statvfs.cpp:126
fieldHandlerBase::print
virtual void print(const std::string &fsname, const struct statvfs &stat, bool printAll)=0
fieldHandlerTpl::fieldHandlerTpl
fieldHandlerTpl(decltype(field) aField, char shortName, const std::string &aName, const std::string &aExplanation)
Definition: statvfs.cpp:30
fieldHandlerBase
Definition: statvfs.cpp:8
fieldHandlerTpl::print
void print(const std::string &fsname, const struct statvfs &stat, bool printAll) override
Definition: statvfs.cpp:35
throwcall.h
fieldHandlerBase::opt
options::single< bool > opt
Definition: statvfs.cpp:13
quiet
static options::single< bool > quiet('q', "quiet", "be quiet: no prefix to values")
options::positional
Definition: Options.h:876
idHandler
Definition: statvfs.cpp:48
throwcall::good0
void good0(T call, const Args &... args)
template function to wrap system calls that return 0 on success
Definition: throwcall.h:40