00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <config.h>
00020
00021 #include <sbuild/sbuild-mntstream.h>
00022
00023 #include "schroot-listmounts-main.h"
00024
00025 #include <cerrno>
00026 #include <climits>
00027 #include <cstdio>
00028 #include <cstdlib>
00029 #include <ctime>
00030 #include <iostream>
00031 #include <locale>
00032
00033 #include <sys/types.h>
00034 #include <sys/stat.h>
00035 #include <unistd.h>
00036
00037 #include <boost/format.hpp>
00038
00039 #include <lockdev.h>
00040
00041 using std::endl;
00042 using boost::format;
00043 using sbuild::_;
00044 using sbuild::N_;
00045 using namespace schroot_listmounts;
00046
00047 namespace
00048 {
00049
00050 typedef std::pair<main::error_code,const char *> emap;
00051
00056 emap init_errors[] =
00057 {
00058
00059 emap(main::FIND, N_("Failed to find '%1%'"))
00060 };
00061
00062 }
00063
00064 template<>
00065 sbuild::error<main::error_code>::map_type
00066 sbuild::error<main::error_code>::error_strings
00067 (init_errors,
00068 init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
00069
00070 main::main (options::ptr& options):
00071 schroot_base::main("schroot-listmounts",
00072
00073
00074 _("[OPTION...] - list mount points"),
00075 options,
00076 false),
00077 opts(options)
00078 {
00079 }
00080
00081 main::~main ()
00082 {
00083 }
00084
00085 void
00086 main::action_listmounts ()
00087 {
00088 std::string to_find = sbuild::normalname(this->opts->mountpoint);
00089
00090 {
00091
00092 char *rpath = realpath(to_find.c_str(), NULL);
00093 if (rpath == 0)
00094 throw error(to_find, FIND, strerror(errno));
00095
00096 to_find = rpath;
00097 free(rpath);
00098 rpath = 0;
00099 }
00100
00101
00102 sbuild::mntstream mounts("/proc/mounts");
00103 sbuild::mntstream::mntentry entry;
00104 sbuild::string_list mountlist;
00105
00106 while (mounts >> entry)
00107 {
00108 std::string mount_dir(entry.directory);
00109 if (to_find == "/" ||
00110 (mount_dir.find(to_find) == 0 &&
00111 (
00112 mount_dir.size() == to_find.size() ||
00113
00114 (mount_dir.size() > to_find.size() &&
00115 mount_dir[to_find.size()] == '/'))))
00116 mountlist.push_back(mount_dir);
00117 }
00118
00119 for (sbuild::string_list::const_reverse_iterator pos = mountlist.rbegin();
00120 pos != mountlist.rend();
00121 ++pos)
00122 std::cout << *pos << '\n';
00123 std::cout << std::flush;
00124 }
00125
00126 int
00127 main::run_impl ()
00128 {
00129 if (this->opts->action == options::ACTION_HELP)
00130 action_help(std::cerr);
00131 else if (this->opts->action == options::ACTION_VERSION)
00132 action_version(std::cerr);
00133 else if (this->opts->action == options::ACTION_LISTMOUNTS)
00134 action_listmounts();
00135 else
00136 assert(0);
00137
00138 return EXIT_SUCCESS;
00139 }