1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | |
---|
21 | #include <sbuild/sbuild-mntstream.h> |
---|
22 | |
---|
23 | #include "schroot-listmounts-main.h" |
---|
24 | |
---|
25 | #include <cerrno> |
---|
26 | #include <climits> |
---|
27 | #include <cstdio> |
---|
28 | #include <cstdlib> |
---|
29 | #include <ctime> |
---|
30 | #include <iostream> |
---|
31 | #include <locale> |
---|
32 | |
---|
33 | #include <sys/types.h> |
---|
34 | #include <sys/stat.h> |
---|
35 | #include <unistd.h> |
---|
36 | |
---|
37 | #include <boost/format.hpp> |
---|
38 | |
---|
39 | using std::endl; |
---|
40 | using boost::format; |
---|
41 | using sbuild::_; |
---|
42 | using sbuild::N_; |
---|
43 | using namespace schroot_listmounts; |
---|
44 | |
---|
45 | namespace |
---|
46 | { |
---|
47 | |
---|
48 | typedef std::pair<main::error_code,const char *> emap; |
---|
49 | |
---|
50 | /** |
---|
51 | * This is a list of the supported error codes. It's used to |
---|
52 | * construct the real error codes map. |
---|
53 | */ |
---|
54 | emap init_errors[] = |
---|
55 | { |
---|
56 | // TRANSLATORS: %1% = file |
---|
57 | emap(main::FIND, N_("Failed to find '%1%'")) |
---|
58 | }; |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | template<> |
---|
63 | sbuild::error<main::error_code>::map_type |
---|
64 | sbuild::error<main::error_code>::error_strings |
---|
65 | (init_errors, |
---|
66 | init_errors + (sizeof(init_errors) / sizeof(init_errors[0]))); |
---|
67 | |
---|
68 | main::main (options::ptr& options): |
---|
69 | schroot_base::main("schroot-listmounts", |
---|
70 | // TRANSLATORS: '...' is an ellipsis e.g. U+2026, |
---|
71 | // and '-' is an em-dash. |
---|
72 | _("[OPTION...] - list mount points"), |
---|
73 | options, |
---|
74 | false), |
---|
75 | opts(options) |
---|
76 | { |
---|
77 | } |
---|
78 | |
---|
79 | main::~main () |
---|
80 | { |
---|
81 | } |
---|
82 | |
---|
83 | void |
---|
84 | main::action_listmounts () |
---|
85 | { |
---|
86 | std::string to_find = sbuild::normalname(this->opts->mountpoint); |
---|
87 | |
---|
88 | { |
---|
89 | // NOTE: This is a non-standard GNU extension. |
---|
90 | char *rpath = realpath(to_find.c_str(), NULL); |
---|
91 | if (rpath == 0) |
---|
92 | throw error(to_find, FIND, strerror(errno)); |
---|
93 | |
---|
94 | to_find = rpath; |
---|
95 | free(rpath); |
---|
96 | rpath = 0; |
---|
97 | } |
---|
98 | |
---|
99 | // Check mounts. |
---|
100 | sbuild::mntstream mounts("/proc/mounts"); |
---|
101 | sbuild::mntstream::mntentry entry; |
---|
102 | sbuild::string_list mountlist; |
---|
103 | |
---|
104 | while (mounts >> entry) |
---|
105 | { |
---|
106 | std::string mount_dir(entry.directory); |
---|
107 | if (to_find == "/" || |
---|
108 | (mount_dir.find(to_find) == 0 && |
---|
109 | (// Names are the same. |
---|
110 | mount_dir.size() == to_find.size() || |
---|
111 | // Must have a following /, or not the same directory. |
---|
112 | (mount_dir.size() > to_find.size() && |
---|
113 | mount_dir[to_find.size()] == '/')))) |
---|
114 | mountlist.push_back(mount_dir); |
---|
115 | } |
---|
116 | |
---|
117 | for (sbuild::string_list::const_reverse_iterator pos = mountlist.rbegin(); |
---|
118 | pos != mountlist.rend(); |
---|
119 | ++pos) |
---|
120 | std::cout << *pos << '\n'; |
---|
121 | std::cout << std::flush; |
---|
122 | } |
---|
123 | |
---|
124 | int |
---|
125 | main::run_impl () |
---|
126 | { |
---|
127 | if (this->opts->action == options::ACTION_HELP) |
---|
128 | action_help(std::cerr); |
---|
129 | else if (this->opts->action == options::ACTION_VERSION) |
---|
130 | action_version(std::cerr); |
---|
131 | else if (this->opts->action == options::ACTION_LISTMOUNTS) |
---|
132 | action_listmounts(); |
---|
133 | else |
---|
134 | assert(0); // Invalid action. |
---|
135 | |
---|
136 | return EXIT_SUCCESS; |
---|
137 | } |
---|